use of com.zimbra.cs.mailbox.VirtualConversation 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.VirtualConversation in project zm-mailbox by Zimbra.
the class DbMailItem method reparentChildren.
public static void reparentChildren(MailItem oldParent, MailItem newParent) throws ServiceException {
if (oldParent == newParent) {
return;
}
Mailbox mbox = oldParent.getMailbox();
if (mbox != newParent.getMailbox()) {
throw MailServiceException.WRONG_MAILBOX();
}
DbConnection conn = mbox.getOperationConnection();
PreparedStatement stmt = null;
try {
String relation = (oldParent instanceof VirtualConversation ? "id = ?" : "parent_id = ?");
stmt = conn.prepareStatement("UPDATE " + getMailItemTableName(oldParent) + " SET parent_id = ?, mod_metadata = ?, change_date = ?" + " WHERE " + IN_THIS_MAILBOX_AND + relation);
int pos = 1;
if (newParent instanceof VirtualConversation) {
stmt.setNull(pos++, Types.INTEGER);
} else {
stmt.setInt(pos++, newParent.getId());
}
stmt.setInt(pos++, mbox.getOperationChangeID());
stmt.setInt(pos++, mbox.getOperationTimestamp());
pos = setMailboxId(stmt, mbox, pos);
stmt.setInt(pos++, oldParent instanceof VirtualConversation ? ((VirtualConversation) oldParent).getMessageId() : oldParent.getId());
stmt.executeUpdate();
} catch (SQLException e) {
throw ServiceException.FAILURE("writing new parent for children of item " + oldParent.getId(), e);
} finally {
DbPool.closeStatement(stmt);
}
}
use of com.zimbra.cs.mailbox.VirtualConversation in project zm-mailbox by Zimbra.
the class DbMailItem method setParent.
public static void setParent(MailItem[] children, MailItem parent) throws ServiceException {
if (children == null || children.length == 0) {
return;
}
Mailbox mbox = children[0].getMailbox();
if (parent != null && mbox != parent.getMailbox()) {
throw MailServiceException.WRONG_MAILBOX();
}
DbConnection conn = mbox.getOperationConnection();
PreparedStatement stmt = null;
try {
for (int i = 0; i < children.length; i += Db.getINClauseBatchSize()) {
int count = Math.min(Db.getINClauseBatchSize(), children.length - i);
stmt = conn.prepareStatement("UPDATE " + getMailItemTableName(mbox) + " SET parent_id = ?, mod_metadata = ?, change_date = ?" + " WHERE " + IN_THIS_MAILBOX_AND + DbUtil.whereIn("id", count));
int pos = 1;
if (parent == null || parent instanceof VirtualConversation) {
stmt.setNull(pos++, Types.INTEGER);
} else {
stmt.setInt(pos++, parent.getId());
}
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++, children[index].getId());
}
stmt.executeUpdate();
stmt.close();
stmt = null;
}
} catch (SQLException e) {
throw ServiceException.FAILURE("adding children to parent " + (parent == null ? "NULL" : parent.getId() + ""), e);
} finally {
DbPool.closeStatement(stmt);
}
}
use of com.zimbra.cs.mailbox.VirtualConversation in project zm-mailbox by Zimbra.
the class DbMailItem method setFolder.
public static void setFolder(MailItem item, Folder folder) throws ServiceException {
Mailbox mbox = item.getMailbox();
if (mbox != folder.getMailbox()) {
throw MailServiceException.WRONG_MAILBOX();
}
checkNamingConstraint(mbox, folder.getId(), item.getName(), item.getId());
DbConnection conn = mbox.getOperationConnection();
PreparedStatement stmt = null;
try {
String imapRenumber = mbox.isTrackingImap() ? ", imap_id = CASE WHEN imap_id IS NULL THEN NULL ELSE 0 END" : "";
int pos = 1;
boolean hasIndexId = false;
if (item instanceof Folder) {
stmt = conn.prepareStatement("UPDATE " + getMailItemTableName(item) + " SET parent_id = ?, folder_id = ?, prev_folders = ?, mod_metadata = ?, change_date = ?" + " WHERE " + IN_THIS_MAILBOX_AND + "id = ?");
stmt.setInt(pos++, folder.getId());
} else if (item instanceof Conversation && !(item instanceof VirtualConversation)) {
stmt = conn.prepareStatement("UPDATE " + getMailItemTableName(item) + " SET folder_id = ?, prev_folders = ?, mod_metadata = ?, change_date = ?" + imapRenumber + " WHERE " + IN_THIS_MAILBOX_AND + "parent_id = ?");
} else {
// set the indexId, in case it changed (moving items out of junk can trigger an index ID change)
hasIndexId = true;
stmt = conn.prepareStatement("UPDATE " + getMailItemTableName(item) + " SET folder_id = ?, prev_folders = ?, index_id = ?, mod_metadata = ?, change_date = ? " + imapRenumber + " WHERE " + IN_THIS_MAILBOX_AND + "id = ?");
}
stmt.setInt(pos++, folder.getId());
int modseq = mbox.getOperationChangeID();
String prevFolders = findPrevFolders(item, modseq);
stmt.setString(pos++, prevFolders);
item.getUnderlyingData().setPrevFolders(prevFolders);
if (hasIndexId) {
if (item.getIndexStatus() == MailItem.IndexStatus.NO) {
stmt.setNull(pos++, Types.INTEGER);
} else {
stmt.setInt(pos++, item.getIndexId());
}
}
stmt.setInt(pos++, modseq);
stmt.setInt(pos++, mbox.getOperationTimestamp());
pos = setMailboxId(stmt, mbox, pos);
stmt.setInt(pos++, item instanceof VirtualConversation ? ((VirtualConversation) item).getMessageId() : item.getId());
stmt.executeUpdate();
} catch (SQLException e) {
// catch item_id uniqueness constraint violation and return failure
if (Db.errorMatches(e, Db.Error.DUPLICATE_ROW)) {
throw MailServiceException.ALREADY_EXISTS(item.getName(), e);
} else {
throw ServiceException.FAILURE("writing new folder data for item " + item.getId(), e);
}
} finally {
DbPool.closeStatement(stmt);
}
}
use of com.zimbra.cs.mailbox.VirtualConversation in project zm-mailbox by Zimbra.
the class DbMailItem method changeOpenTargets.
public static void changeOpenTargets(MailItem oldTarget, int newTargetId) throws ServiceException {
Mailbox mbox = oldTarget.getMailbox();
int oldTargetId = oldTarget instanceof VirtualConversation ? ((VirtualConversation) oldTarget).getMessageId() : oldTarget.getId();
DbConnection conn = mbox.getOperationConnection();
PreparedStatement stmt = null;
try {
stmt = conn.prepareStatement("UPDATE " + getConversationTableName(oldTarget) + " SET conv_id = ? WHERE " + IN_THIS_MAILBOX_AND + "conv_id = ?");
int pos = 1;
stmt.setInt(pos++, newTargetId);
pos = setMailboxId(stmt, mbox, pos);
stmt.setInt(pos++, oldTargetId);
stmt.executeUpdate();
} catch (SQLException e) {
throw ServiceException.FAILURE("switching open conversation association for item " + oldTarget.getId(), e);
} finally {
DbPool.closeStatement(stmt);
}
}
Aggregations