use of com.zimbra.cs.mailbox.MailItem.UnderlyingData in project zm-mailbox by Zimbra.
the class DbMailItem method getByType.
public static List<UnderlyingData> getByType(Mailbox mbox, MailItem.Type type, SortBy sort) throws ServiceException {
if (Mailbox.isCachedType(type)) {
throw ServiceException.INVALID_REQUEST("folders and tags must be retrieved from cache", null);
}
ArrayList<UnderlyingData> result = new ArrayList<UnderlyingData>();
DbConnection conn = mbox.getOperationConnection();
PreparedStatement stmt = null;
ResultSet rs = null;
try {
stmt = conn.prepareStatement("SELECT " + DB_FIELDS + " FROM " + getMailItemTableName(mbox, " mi") + " WHERE " + IN_THIS_MAILBOX_AND + typeIn(type) + DbSearch.orderBy(sort, false));
if (type == MailItem.Type.MESSAGE) {
Db.getInstance().enableStreaming(stmt);
}
setMailboxId(stmt, mbox, 1);
rs = stmt.executeQuery();
while (rs.next()) {
result.add(constructItem(rs));
}
rs.close();
rs = null;
stmt.close();
stmt = null;
if (type == MailItem.Type.CONVERSATION) {
completeConversations(mbox, conn, result);
}
return result;
} catch (SQLException e) {
throw ServiceException.FAILURE("fetching items of type " + type, e);
} finally {
DbPool.closeResults(rs);
DbPool.closeStatement(stmt);
}
}
use of com.zimbra.cs.mailbox.MailItem.UnderlyingData in project zm-mailbox by Zimbra.
the class DbMailItem method getUnreadMessages.
public static List<UnderlyingData> getUnreadMessages(MailItem relativeTo) throws ServiceException {
if (relativeTo instanceof Tag) {
return DbTag.getUnreadMessages((Tag) relativeTo);
}
Mailbox mbox = relativeTo.getMailbox();
ArrayList<UnderlyingData> result = new ArrayList<UnderlyingData>();
DbConnection conn = mbox.getOperationConnection();
PreparedStatement stmt = null;
ResultSet rs = null;
try {
String relation;
if (relativeTo instanceof VirtualConversation) {
relation = "id = ?";
} else if (relativeTo instanceof Conversation) {
relation = "parent_id = ?";
} else if (relativeTo instanceof Folder) {
relation = "folder_id = ?";
} else {
relation = "id = ?";
}
stmt = conn.prepareStatement("SELECT " + DB_FIELDS + " FROM " + getMailItemTableName(relativeTo.getMailbox(), " mi") + " WHERE " + IN_THIS_MAILBOX_AND + "unread > 0 AND " + relation + " AND type NOT IN " + NON_SEARCHABLE_TYPES);
if (relativeTo.getUnreadCount() > RESULTS_STREAMING_MIN_ROWS) {
Db.getInstance().enableStreaming(stmt);
}
int pos = 1;
pos = setMailboxId(stmt, mbox, pos);
if (relativeTo instanceof VirtualConversation) {
stmt.setInt(pos++, ((VirtualConversation) relativeTo).getMessageId());
} else {
stmt.setInt(pos++, relativeTo.getId());
}
rs = stmt.executeQuery();
while (rs.next()) {
UnderlyingData data = constructItem(rs);
if (Mailbox.isCachedType(MailItem.Type.of(data.type))) {
throw ServiceException.INVALID_REQUEST("folders and tags must be retrieved from cache", null);
}
result.add(data);
}
return result;
} catch (SQLException e) {
throw ServiceException.FAILURE("fetching unread messages for item " + relativeTo.getId(), e);
} finally {
DbPool.closeResults(rs);
DbPool.closeStatement(stmt);
}
}
use of com.zimbra.cs.mailbox.MailItem.UnderlyingData in project zm-mailbox by Zimbra.
the class DbMailItem method getByParent.
public static List<UnderlyingData> getByParent(MailItem parent, SortBy sort, int limit, boolean fromDumpster) throws ServiceException {
Mailbox mbox = parent.getMailbox();
List<UnderlyingData> result = new ArrayList<UnderlyingData>();
StringBuilder sql = new StringBuilder("SELECT ").append(DB_FIELDS).append(" FROM ").append(getMailItemTableName(parent.getMailbox(), " mi", fromDumpster)).append(" WHERE ").append(IN_THIS_MAILBOX_AND).append("parent_id = ? ").append(DbSearch.orderBy(sort, false));
if (limit > 0) {
sql.append(" LIMIT ?");
}
DbConnection conn = mbox.getOperationConnection();
PreparedStatement stmt = null;
ResultSet rs = null;
try {
stmt = conn.prepareStatement(sql.toString());
if (parent.getSize() > RESULTS_STREAMING_MIN_ROWS) {
Db.getInstance().enableStreaming(stmt);
}
int pos = 1;
pos = setMailboxId(stmt, mbox, pos);
stmt.setInt(pos++, parent.getId());
if (limit > 0) {
stmt.setInt(pos++, limit);
}
rs = stmt.executeQuery();
while (rs.next()) {
UnderlyingData data = constructItem(rs, fromDumpster);
if (Mailbox.isCachedType(MailItem.Type.of(data.type))) {
throw ServiceException.INVALID_REQUEST("folders and tags must be retrieved from cache", null);
}
result.add(data);
}
return result;
} catch (SQLException e) {
throw ServiceException.FAILURE("fetching children of item " + parent.getId(), e);
} finally {
DbPool.closeResults(rs);
DbPool.closeStatement(stmt);
}
}
use of com.zimbra.cs.mailbox.MailItem.UnderlyingData in project zm-mailbox by Zimbra.
the class DbMailItem method getByHashes.
public static List<UnderlyingData> getByHashes(Mailbox mbox, List<String> hashes) throws ServiceException {
if (ListUtil.isEmpty(hashes)) {
return null;
}
DbConnection conn = mbox.getOperationConnection();
PreparedStatement stmt = null;
ResultSet rs = null;
try {
stmt = conn.prepareStatement("SELECT " + DB_FIELDS + " FROM " + getMailItemTableName(mbox, "mi") + ", " + getConversationTableName(mbox, "oc") + " WHERE mi.id = oc.conv_id AND " + DbUtil.whereIn("oc.hash", hashes.size()) + (DebugConfig.disableMailboxGroups ? "" : " AND oc.mailbox_id = ? AND mi.mailbox_id = oc.mailbox_id"));
int pos = 1;
for (String hash : hashes) {
stmt.setString(pos++, hash);
}
pos = setMailboxId(stmt, mbox, pos);
rs = stmt.executeQuery();
List<UnderlyingData> dlist = new ArrayList<UnderlyingData>(3);
Set<Integer> convIds = Sets.newHashSetWithExpectedSize(3);
while (rs.next()) {
int id = rs.getInt(CI_ID);
if (convIds.contains(id)) {
continue;
}
UnderlyingData data = constructItem(rs);
if (data.type == MailItem.Type.CONVERSATION.toByte()) {
completeConversation(mbox, conn, data);
}
dlist.add(data);
convIds.add(data.id);
}
return dlist.isEmpty() ? null : dlist;
} catch (SQLException e) {
throw ServiceException.FAILURE("fetching conversation for hash " + hashes, e);
} finally {
DbPool.closeResults(rs);
DbPool.closeStatement(stmt);
}
}
use of com.zimbra.cs.mailbox.MailItem.UnderlyingData in project zm-mailbox by Zimbra.
the class DbMailItem method completeConversations.
private static void completeConversations(Mailbox mbox, DbConnection conn, List<UnderlyingData> convData) throws ServiceException {
if (convData == null || convData.isEmpty()) {
return;
}
for (UnderlyingData data : convData) {
if (data.type != MailItem.Type.CONVERSATION.toByte()) {
throw ServiceException.FAILURE("attempting to complete a non-conversation", null);
}
}
Map<Integer, UnderlyingData> conversations = new HashMap<Integer, UnderlyingData>(Db.getINClauseBatchSize() * 3 / 2);
PreparedStatement stmt = null;
ResultSet rs = null;
for (int i = 0; i < convData.size(); i += Db.getINClauseBatchSize()) {
try {
int count = Math.min(Db.getINClauseBatchSize(), convData.size() - i);
stmt = conn.prepareStatement("SELECT parent_id, unread, flags, tag_names" + " FROM " + getMailItemTableName(mbox) + " WHERE " + IN_THIS_MAILBOX_AND + DbUtil.whereIn("parent_id", count));
int pos = 1;
pos = setMailboxId(stmt, mbox, pos);
for (int index = i; index < i + count; index++) {
UnderlyingData data = convData.get(index);
stmt.setInt(pos++, data.id);
conversations.put(data.id, data);
// don't assume that the UnderlyingData structure was new...
data.unreadCount = 0;
data.setFlags(0);
data.setTags(null);
}
rs = stmt.executeQuery();
HashMultimap<Integer, String> convTags = HashMultimap.create();
while (rs.next()) {
Integer convId = rs.getInt(1);
UnderlyingData data = conversations.get(convId);
assert data != null;
data.unreadCount += rs.getInt(2);
data.setFlags(data.getFlags() | rs.getInt(3));
String[] tags = DbTag.deserializeTags(rs.getString(4));
if (tags != null) {
convTags.putAll(convId, Arrays.asList(tags));
}
}
for (Map.Entry<Integer, Collection<String>> entry : convTags.asMap().entrySet()) {
UnderlyingData data = conversations.get(entry.getKey());
if (data != null) {
data.setTags(new Tag.NormalizedTags(entry.getValue()));
}
}
} catch (SQLException e) {
throw ServiceException.FAILURE("completing conversation data", e);
} finally {
DbPool.closeResults(rs);
DbPool.closeStatement(stmt);
}
conversations.clear();
}
}
Aggregations