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;
}
Aggregations