Search in sources :

Example 56 with Mailbox

use of com.zimbra.cs.mailbox.Mailbox in project zm-mailbox by Zimbra.

the class DbMailItem method changeType.

public static void changeType(MailItem item, byte type) throws ServiceException {
    Mailbox mbox = item.getMailbox();
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    try {
        stmt = conn.prepareStatement("UPDATE " + getMailItemTableName(item) + " SET type = ? WHERE " + IN_THIS_MAILBOX_AND + "id = ?");
        int pos = 1;
        stmt.setInt(pos++, type);
        pos = setMailboxId(stmt, mbox, pos);
        stmt.setInt(pos++, item.getId());
        stmt.executeUpdate();
    } catch (SQLException e) {
        throw ServiceException.FAILURE("writing new type for item " + item.getId(), e);
    } finally {
        DbPool.closeStatement(stmt);
    }
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 57 with Mailbox

use of com.zimbra.cs.mailbox.Mailbox in project zm-mailbox by Zimbra.

the class DbMailItem method listByFolder.

public static List<Integer> listByFolder(Folder folder, MailItem.Type type, boolean descending) throws ServiceException {
    Mailbox mbox = folder.getMailbox();
    boolean allTypes = type == MailItem.Type.UNKNOWN;
    List<Integer> result = new ArrayList<Integer>();
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        String typeConstraint = allTypes ? "" : "type = ? AND ";
        stmt = conn.prepareStatement("SELECT id FROM " + getMailItemTableName(folder) + " WHERE " + IN_THIS_MAILBOX_AND + typeConstraint + "folder_id = ?" + " ORDER BY date" + (descending ? " DESC" : ""));
        if (type == MailItem.Type.MESSAGE && folder.getSize() > RESULTS_STREAMING_MIN_ROWS) {
            Db.getInstance().enableStreaming(stmt);
        }
        int pos = 1;
        pos = setMailboxId(stmt, mbox, pos);
        if (!allTypes) {
            stmt.setByte(pos++, type.toByte());
        }
        stmt.setInt(pos++, folder.getId());
        rs = stmt.executeQuery();
        while (rs.next()) {
            result.add(rs.getInt(1));
        }
        return result;
    } catch (SQLException e) {
        throw ServiceException.FAILURE("fetching item list for folder " + folder.getId(), e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
    }
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 58 with Mailbox

use of com.zimbra.cs.mailbox.Mailbox in project zm-mailbox by Zimbra.

the class DbMailItem method getDigestsForItems.

public static Map<String, Integer> getDigestsForItems(Folder folder, MailItem.Type type) throws ServiceException {
    if (Mailbox.isCachedType(type)) {
        throw ServiceException.INVALID_REQUEST("folders and tags must be retrieved from cache", null);
    }
    Map<String, Integer> digestMap = Maps.newHashMap();
    if (null == folder) {
        return digestMap;
    }
    Mailbox mbox = folder.getMailbox();
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        stmt = conn.prepareStatement(String.format("SELECT mi.id, mi.blob_digest FROM %s WHERE %s folder_id = ? AND %s", getMailItemTableName(mbox, "mi"), IN_THIS_MAILBOX_AND, typeIn(type)));
        int pos = 1;
        pos = setMailboxId(stmt, mbox, pos);
        stmt.setInt(pos++, folder.getId());
        rs = stmt.executeQuery();
        while (rs.next()) {
            Integer id = rs.getInt(1);
            String digest = rs.getString(2);
            digestMap.put(digest, id);
        }
    } catch (SQLException e) {
        throw ServiceException.FAILURE(String.format("Getting digests for items from folder id=%s name='%s'", folder.getId(), folder.getName()), e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
    }
    return digestMap;
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 59 with Mailbox

use of com.zimbra.cs.mailbox.Mailbox in project zm-mailbox by Zimbra.

the class DbMailItem method addToCalendarItemTable.

public static void addToCalendarItemTable(CalendarItem calItem) throws ServiceException {
    Mailbox mbox = calItem.getMailbox();
    long start = calItem.getStartTime();
    long end = calItem.getEndTime();
    Timestamp startTs = new Timestamp(start);
    Timestamp endTs = getEnd(start, end);
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    try {
        String mailbox_id = DebugConfig.disableMailboxGroups ? "" : "mailbox_id, ";
        stmt = conn.prepareStatement("INSERT INTO " + getCalendarItemTableName(mbox) + " (" + mailbox_id + "uid, item_id, start_time, end_time)" + " VALUES (" + (DebugConfig.disableMailboxGroups ? "" : "?, ") + "?, ?, ?, ?)");
        int pos = 1;
        pos = setMailboxId(stmt, mbox, pos);
        stmt.setString(pos++, calItem.getUid());
        stmt.setInt(pos++, calItem.getId());
        stmt.setTimestamp(pos++, startTs);
        stmt.setTimestamp(pos++, endTs);
        stmt.executeUpdate();
    } catch (SQLException e) {
        throw ServiceException.FAILURE("writing invite to calendar item table: UID=" + calItem.getUid(), e);
    } finally {
        DbPool.closeStatement(stmt);
    }
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 60 with Mailbox

use of com.zimbra.cs.mailbox.Mailbox in project zm-mailbox by Zimbra.

the class DbMailItem method markDeletionTargets.

/**
     * Updates all conversations affected by a folder deletion.  For all conversations
     * that have messages in the given folder, updates their message count and nulls out
     * metadata so that the sender list is recalculated the next time the conversation
     * is instantiated.
     *
     * @param folder the folder that is being deleted
     * @return the ids of any conversation that were purged as a result of this operation
     */
public static List<Integer> markDeletionTargets(Folder folder, Set<Integer> candidates) throws ServiceException {
    Mailbox mbox = folder.getMailbox();
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        if (Db.supports(Db.Capability.MULTITABLE_UPDATE)) {
            stmt = conn.prepareStatement("UPDATE " + getMailItemTableName(folder) + ", " + "(SELECT parent_id pid, COUNT(*) count FROM " + getMailItemTableName(folder) + " WHERE " + IN_THIS_MAILBOX_AND + "folder_id = ? AND parent_id IS NOT NULL GROUP BY parent_id) AS x" + " SET size = size - count, metadata = NULL, mod_metadata = ?, change_date = ?" + " WHERE " + IN_THIS_MAILBOX_AND + "id = pid AND type = " + MailItem.Type.CONVERSATION.toByte());
            int pos = 1;
            pos = setMailboxId(stmt, mbox, pos);
            stmt.setInt(pos++, folder.getId());
            stmt.setInt(pos++, mbox.getOperationChangeID());
            stmt.setInt(pos++, mbox.getOperationTimestamp());
            pos = setMailboxId(stmt, mbox, pos);
            stmt.executeUpdate();
            stmt.close();
        } else {
            stmt = conn.prepareStatement("SELECT parent_id, COUNT(*) FROM " + getMailItemTableName(folder) + " WHERE " + IN_THIS_MAILBOX_AND + "folder_id = ? AND parent_id IS NOT NULL" + " GROUP BY parent_id");
            int pos = 1;
            pos = setMailboxId(stmt, mbox, pos);
            stmt.setInt(pos++, folder.getId());
            rs = stmt.executeQuery();
            Map<Integer, List<Integer>> counts = new HashMap<Integer, List<Integer>>();
            while (rs.next()) {
                int convId = rs.getInt(1), count = rs.getInt(2);
                List<Integer> targets = counts.get(count);
                if (targets == null) {
                    counts.put(count, targets = new ArrayList<Integer>());
                }
                targets.add(convId);
            }
            rs.close();
            stmt.close();
            for (Map.Entry<Integer, List<Integer>> update : counts.entrySet()) {
                List<Integer> convIDs = update.getValue();
                for (int i = 0; i < convIDs.size(); i += Db.getINClauseBatchSize()) {
                    int count = Math.min(Db.getINClauseBatchSize(), convIDs.size() - i);
                    stmt = conn.prepareStatement("UPDATE " + getMailItemTableName(folder) + " SET size = size - ?, metadata = NULL, mod_metadata = ?, change_date = ?" + " WHERE " + IN_THIS_MAILBOX_AND + DbUtil.whereIn("id", count) + "  AND type = " + MailItem.Type.CONVERSATION.toByte());
                    pos = 1;
                    stmt.setInt(pos++, update.getKey());
                    stmt.setInt(pos++, mbox.getOperationChangeID());
                    stmt.setInt(pos++, mbox.getOperationTimestamp());
                    pos = setMailboxId(stmt, mbox, pos);
                    for (int index = i; index < i + count; index++) {
                        stmt.setInt(pos++, convIDs.get(index));
                    }
                    stmt.executeUpdate();
                    stmt.close();
                }
            }
        }
        return getPurgedConversations(mbox, candidates);
    } catch (SQLException e) {
        throw ServiceException.FAILURE("marking deletions for conversations crossing folder " + folder.getId(), e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection) Mailbox(com.zimbra.cs.mailbox.Mailbox) ResultSet(java.sql.ResultSet) List(java.util.List) ArrayList(java.util.ArrayList) TypedIdList(com.zimbra.cs.mailbox.util.TypedIdList) Map(java.util.Map) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap)

Aggregations

Mailbox (com.zimbra.cs.mailbox.Mailbox)817 Account (com.zimbra.cs.account.Account)389 Test (org.junit.Test)376 OperationContext (com.zimbra.cs.mailbox.OperationContext)365 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)306 Message (com.zimbra.cs.mailbox.Message)303 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)243 ItemId (com.zimbra.cs.service.util.ItemId)243 Element (com.zimbra.common.soap.Element)138 ServiceException (com.zimbra.common.service.ServiceException)127 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)127 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)106 MimeMessage (javax.mail.internet.MimeMessage)104 Folder (com.zimbra.cs.mailbox.Folder)82 ArrayList (java.util.ArrayList)81 ZMailbox (com.zimbra.client.ZMailbox)73 DbConnection (com.zimbra.cs.db.DbPool.DbConnection)71 PreparedStatement (java.sql.PreparedStatement)67 SQLException (java.sql.SQLException)67 Header (javax.mail.Header)66