Search in sources :

Example 61 with Mailbox

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

the class DbMailItem method copy.

public static String copy(MailItem item, int id, String uuid, Folder folder, int indexId, int parentId, String locator, String metadata, boolean fromDumpster) throws ServiceException {
    Mailbox mbox = item.getMailbox();
    String prevFolders = null;
    if (id <= 0 || folder == null || parentId == 0) {
        throw ServiceException.FAILURE("invalid data for DB item copy", null);
    }
    checkNamingConstraint(mbox, folder.getId(), item.getName(), id);
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    try {
        String srcTable = getMailItemTableName(mbox, fromDumpster);
        String destTable = getMailItemTableName(mbox);
        String mailbox_id = DebugConfig.disableMailboxGroups ? "" : "mailbox_id, ";
        stmt = conn.prepareStatement("INSERT INTO " + destTable + "(" + mailbox_id + " id, type, parent_id, folder_id, prev_folders, index_id, imap_id, date, size, locator, blob_digest," + " unread, flags, tag_names, sender, subject, name, metadata, mod_metadata, change_date, mod_content, uuid) " + "SELECT " + MAILBOX_ID_VALUE + " ?, type, ?, ?, ?, ?, ?, date, size, ?, blob_digest, unread," + " flags, tag_names, sender, subject, name, ?, ?, ?, ?, ? FROM " + srcTable + " WHERE " + IN_THIS_MAILBOX_AND + "id = ?");
        int pos = 1;
        pos = setMailboxId(stmt, mbox, pos);
        // ID
        stmt.setInt(pos++, id);
        if (parentId <= 0) {
            // PARENT_ID null for messages in virtual convs
            stmt.setNull(pos++, Types.INTEGER);
        } else {
            //   or, PARENT_ID specified by caller
            stmt.setInt(pos++, parentId);
        }
        // FOLDER_ID
        stmt.setInt(pos++, folder.getId());
        int modseq = mbox.getOperationChangeID();
        prevFolders = findPrevFolders(item, modseq);
        stmt.setString(pos++, prevFolders);
        if (indexId == MailItem.IndexStatus.NO.id()) {
            stmt.setNull(pos++, Types.INTEGER);
        } else {
            stmt.setInt(pos++, indexId);
        }
        // IMAP_ID is initially the same as ID
        stmt.setInt(pos++, id);
        stmt.setString(pos++, locator);
        // METADATA
        stmt.setString(pos++, checkMetadataLength(metadata));
        // MOD_METADATA
        stmt.setInt(pos++, modseq);
        // CHANGE_DATE
        stmt.setInt(pos++, mbox.getOperationTimestamp());
        // MOD_CONTENT
        stmt.setInt(pos++, mbox.getOperationChangeID());
        // UUID
        stmt.setString(pos++, uuid);
        pos = setMailboxId(stmt, mbox, pos);
        stmt.setInt(pos++, item.getId());
        int num = stmt.executeUpdate();
        if (num != 1) {
            throw ServiceException.FAILURE("failed to create object", null);
        }
        DbTag.storeTagReferences(mbox, id, item.getType(), item.getInternalFlagBitmask(), item.isUnread());
        DbTag.storeTagReferences(mbox, id, item.getType(), item.getTags());
    } catch (SQLException e) {
        // catch item_id uniqueness constraint violation and return failure
        if (Db.errorMatches(e, Db.Error.DUPLICATE_ROW)) {
            throw MailServiceException.ALREADY_EXISTS(id, e);
        } else {
            throw ServiceException.FAILURE("copying " + item.getType() + ": " + item.getId(), e);
        }
    } finally {
        DbPool.closeStatement(stmt);
    }
    return prevFolders;
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 62 with Mailbox

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

the class DbMailbox method getMailboxRawData.

public static List<Mailbox.MailboxData> getMailboxRawData(DbConnection conn) throws ServiceException {
    List<Mailbox.MailboxData> results = new ArrayList<Mailbox.MailboxData>();
    if (DebugConfig.externalMailboxDirectory) {
        return results;
    }
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        if (!DebugConfig.disableMailboxGroups) {
            stmt = conn.prepareStatement("SELECT id, group_id, account_id, index_volume_id, item_id_checkpoint," + " contact_count, size_checkpoint, change_checkpoint, tracking_sync," + " tracking_imap, last_backup_at, last_soap_access, new_messages FROM mailbox");
            rs = stmt.executeQuery();
            readMailboxRawData(results, rs);
        } else {
            // FIXME: need an (impossible) assert for synchronization purposes
            int[] mailboxIds = MailboxManager.getInstance().getMailboxIds();
            for (int mailboxId : mailboxIds) {
                Db.getInstance().registerDatabaseInterest(conn, getDatabaseName(mailboxId));
                stmt = conn.prepareStatement("SELECT id, id, account_id, index_volume_id, item_id_checkpoint, contact_count, size_checkpoint," + " change_checkpoint, tracking_sync, tracking_imap, -1, last_soap_access, new_messages" + " FROM " + qualifyZimbraTableName(mailboxId, TABLE_MAILBOX));
                rs = stmt.executeQuery();
                readMailboxRawData(results, rs);
                rs.close();
                rs = null;
                stmt.close();
                stmt = null;
                // XXX: have to avoid having too many attached databases referenced in the same transaction?
                conn.commit();
            }
        }
    } catch (SQLException e) {
        throw ServiceException.FAILURE("getting distinct account id's", e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
    }
    return results;
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 63 with Mailbox

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

the class DbMailItem method saveSubject.

// need to kill the Note class sooner rather than later
public static void saveSubject(Note note) throws ServiceException {
    Mailbox mbox = note.getMailbox();
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    try {
        stmt = conn.prepareStatement("UPDATE " + getMailItemTableName(note) + " SET date = ?, size = ?, subject = ?, mod_metadata = ?, change_date = ?, mod_content = ?" + " WHERE " + IN_THIS_MAILBOX_AND + "id = ?");
        int pos = 1;
        stmt.setInt(pos++, (int) (note.getDate() / 1000));
        stmt.setLong(pos++, note.getSize());
        stmt.setString(pos++, note.getSubject());
        stmt.setInt(pos++, mbox.getOperationChangeID());
        stmt.setInt(pos++, mbox.getOperationTimestamp());
        stmt.setInt(pos++, mbox.getOperationChangeID());
        pos = setMailboxId(stmt, mbox, pos);
        stmt.setInt(pos++, note.getId());
        stmt.executeUpdate();
    } catch (SQLException e) {
        throw ServiceException.FAILURE("writing subject for mailbox " + note.getMailboxId() + ", note " + note.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 64 with Mailbox

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

the class DbMailItem method saveDate.

public static void saveDate(MailItem item) throws ServiceException {
    Mailbox mbox = item.getMailbox();
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    try {
        stmt = conn.prepareStatement("UPDATE " + getMailItemTableName(mbox) + " SET date = ?, mod_metadata = ?, change_date = ? WHERE " + IN_THIS_MAILBOX_AND + "id = ?");
        int pos = 1;
        stmt.setInt(pos++, (int) (item.getDate() / 1000));
        stmt.setInt(pos++, mbox.getOperationChangeID());
        stmt.setInt(pos++, mbox.getOperationTimestamp());
        pos = setMailboxId(stmt, mbox, pos);
        stmt.setInt(pos++, item.getId());
        stmt.executeUpdate();
    } catch (SQLException e) {
        throw ServiceException.FAILURE("setting IMAP UID 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 65 with Mailbox

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

the class DbMailItem method icopy.

public static void icopy(MailItem source, UnderlyingData data, boolean shared) throws ServiceException {
    Mailbox mbox = source.getMailbox();
    if (data == null || data.id <= 0 || data.folderId <= 0 || data.parentId == 0) {
        throw ServiceException.FAILURE("invalid data for DB item i-copy", null);
    }
    checkNamingConstraint(mbox, data.folderId, source.getName(), data.id);
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    try {
        String table = getMailItemTableName(mbox);
        String mailbox_id = DebugConfig.disableMailboxGroups ? "" : "mailbox_id, ";
        String flags;
        if (!shared) {
            flags = "flags";
        } else if (Db.supports(Db.Capability.BITWISE_OPERATIONS)) {
            flags = "flags | " + Flag.BITMASK_COPIED;
        } else {
            flags = "CASE WHEN " + Db.getInstance().bitAND("flags", String.valueOf(Flag.BITMASK_COPIED)) + " <> 0 THEN flags ELSE flags + " + Flag.BITMASK_COPIED + " END";
        }
        stmt = conn.prepareStatement("INSERT INTO " + table + "(" + mailbox_id + " id, type, parent_id, folder_id, prev_folders, index_id, imap_id, date, size, locator, blob_digest," + " unread, flags, tag_names, sender, subject, name, metadata, mod_metadata, change_date, mod_content) " + "SELECT " + mailbox_id + " ?, type, parent_id, ?, ?, ?, ?, date, size, ?, blob_digest," + " unread, " + flags + ", tag_names, sender, subject, name, metadata, ?, ?, ? FROM " + table + " WHERE " + IN_THIS_MAILBOX_AND + "id = ?");
        int pos = 1;
        // ID
        stmt.setInt(pos++, data.id);
        // FOLDER_ID
        stmt.setInt(pos++, data.folderId);
        stmt.setString(pos++, data.getPrevFolders());
        if (data.indexId == MailItem.IndexStatus.NO.id()) {
            stmt.setNull(pos++, Types.INTEGER);
        } else {
            stmt.setInt(pos++, data.indexId);
        }
        // IMAP_ID
        stmt.setInt(pos++, data.imapId);
        stmt.setString(pos++, data.locator);
        // MOD_METADATA
        stmt.setInt(pos++, mbox.getOperationChangeID());
        // CHANGE_DATE
        stmt.setInt(pos++, mbox.getOperationTimestamp());
        // MOD_CONTENT
        stmt.setInt(pos++, mbox.getOperationChangeID());
        pos = setMailboxId(stmt, mbox, pos);
        stmt.setInt(pos++, source.getId());
        stmt.executeUpdate();
        stmt.close();
        boolean needsTag = shared && !source.isTagged(Flag.FlagInfo.COPIED);
        if (needsTag || source.getParentId() > 0) {
            boolean altersMODSEQ = source.getParentId() > 0;
            String updateChangeID = (altersMODSEQ ? ", mod_metadata = ?, change_date = ?" : "");
            stmt = conn.prepareStatement("UPDATE " + table + " SET parent_id = NULL, flags = " + flags + updateChangeID + " WHERE " + IN_THIS_MAILBOX_AND + "id = ?");
            pos = 1;
            if (altersMODSEQ) {
                stmt.setInt(pos++, mbox.getOperationChangeID());
                stmt.setInt(pos++, mbox.getOperationTimestamp());
            }
            pos = setMailboxId(stmt, mbox, pos);
            stmt.setInt(pos++, source.getId());
            stmt.executeUpdate();
            stmt.close();
        }
        if (source instanceof Message && source.getParentId() <= 0) {
            changeOpenTargets(source, data.id);
        }
        MailItem.Type type = MailItem.Type.of(data.type);
        DbTag.storeTagReferences(mbox, data.id, type, data.getFlags() | (shared ? Flag.BITMASK_COPIED : 0), data.unreadCount > 0);
        DbTag.storeTagReferences(mbox, data.id, type, data.getTags());
    } catch (SQLException e) {
        // catch item_id uniqueness constraint violation and return failure
        if (Db.errorMatches(e, Db.Error.DUPLICATE_ROW)) {
            throw MailServiceException.ALREADY_EXISTS(data.id, e);
        } else {
            throw ServiceException.FAILURE("i-copying " + source.getType() + ": " + source.getId(), e);
        }
    } finally {
        DbPool.closeStatement(stmt);
    }
}
Also used : MailItem(com.zimbra.cs.mailbox.MailItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) ImapMessage(com.zimbra.cs.imap.ImapMessage) Message(com.zimbra.cs.mailbox.Message) Pop3Message(com.zimbra.cs.pop3.Pop3Message) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

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