Search in sources :

Example 1 with ImapMessageCollection

use of com.zimbra.cs.datasource.imap.ImapMessageCollection in project zm-mailbox by Zimbra.

the class DbImapMessage method getImapMessages.

/**
     * Returns a collection of tracked IMAP messages for the given data source.
     */
public static ImapMessageCollection getImapMessages(Mailbox mbox, DataSource ds, ImapFolder imapFolder) throws ServiceException {
    ImapMessageCollection imapMessages = new ImapMessageCollection();
    DbConnection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        String MBOX_JOIN = DebugConfig.disableMailboxGroups ? "" : "imap.mailbox_id = mi.mailbox_id AND";
        String IN_IMAP_MAILBOX_AND = DebugConfig.disableMailboxGroups ? "" : "imap.mailbox_id = ? AND";
        conn = DbPool.getConnection(mbox);
        stmt = conn.prepareStatement("SELECT imap.uid, imap.item_id, imap.flags as tflags, mi.unread, mi.flags " + "FROM " + getTableName(mbox) + " imap " + "  LEFT OUTER JOIN " + DbMailItem.getMailItemTableName(mbox) + " mi " + "  ON " + MBOX_JOIN + " imap.item_id = mi.id " + "WHERE " + IN_IMAP_MAILBOX_AND + " imap.imap_folder_id = ?");
        int pos = 1;
        pos = DbMailItem.setMailboxId(stmt, mbox, pos);
        stmt.setInt(pos++, imapFolder.getItemId());
        rs = stmt.executeQuery();
        while (rs.next()) {
            long uid = rs.getLong("uid");
            int itemId = rs.getInt("item_id");
            int flags = rs.getInt("flags");
            int unread = rs.getInt("unread");
            int tflags = rs.getInt("tflags");
            flags = unread > 0 ? (flags | Flag.BITMASK_UNREAD) : (flags & ~Flag.BITMASK_UNREAD);
            imapMessages.add(new ImapMessage(ds, imapFolder.getItemId(), itemId, tflags, uid, flags));
        }
    } catch (SQLException e) {
        throw ServiceException.FAILURE("Unable to get IMAP message data", e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
        DbPool.quietClose(conn);
    }
    ZimbraLog.datasource.debug("Found %d tracked IMAP messages for %s", imapMessages.size(), imapFolder.getRemoteId());
    return imapMessages;
}
Also used : ImapMessage(com.zimbra.cs.datasource.imap.ImapMessage) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ImapMessageCollection(com.zimbra.cs.datasource.imap.ImapMessageCollection) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Aggregations

ImapMessage (com.zimbra.cs.datasource.imap.ImapMessage)1 ImapMessageCollection (com.zimbra.cs.datasource.imap.ImapMessageCollection)1 DbConnection (com.zimbra.cs.db.DbPool.DbConnection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1