use of com.zimbra.cs.mailbox.Mailbox in project zm-mailbox by Zimbra.
the class DbMailItem method loadPop3Folder.
public static List<Pop3Message> loadPop3Folder(Set<Folder> folders, Date popSince) throws ServiceException {
assert !folders.isEmpty() : folders;
Mailbox mbox = Iterables.get(folders, 0).getMailbox();
long popDate = popSince == null ? -1 : Math.max(popSince.getTime(), -1);
List<Pop3Message> result = new ArrayList<Pop3Message>();
DbConnection conn = mbox.getOperationConnection();
PreparedStatement stmt = null;
ResultSet rs = null;
try {
String dateConstraint = popDate < 0 ? "" : " AND date > ?";
stmt = conn.prepareStatement("SELECT mi.id, mi.size, mi.blob_digest FROM " + getMailItemTableName(mbox, " mi") + " WHERE " + IN_THIS_MAILBOX_AND + DbUtil.whereIn("folder_id", folders.size()) + " AND type = " + MailItem.Type.MESSAGE.toByte() + " AND " + Db.getInstance().bitAND("flags", String.valueOf(Flag.BITMASK_DELETED | Flag.BITMASK_POPPED)) + " = 0" + dateConstraint);
if (getTotalFolderSize(folders) > RESULTS_STREAMING_MIN_ROWS) {
//TODO: Because of POPPED flag, the folder size no longer represent the count.
Db.getInstance().enableStreaming(stmt);
}
int pos = 1;
pos = setMailboxId(stmt, mbox, pos);
for (Folder folder : folders) {
stmt.setInt(pos++, folder.getId());
}
if (popDate >= 0) {
stmt.setInt(pos++, (int) (popDate / 1000L));
}
rs = stmt.executeQuery();
while (rs.next()) {
result.add(new Pop3Message(rs.getInt(1), rs.getLong(2), rs.getString(3)));
}
return result;
} catch (SQLException e) {
throw ServiceException.FAILURE("loading POP3 folder data: " + folders, e);
} finally {
DbPool.closeResults(rs);
DbPool.closeStatement(stmt);
}
}
use of com.zimbra.cs.mailbox.Mailbox 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.Mailbox 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.Mailbox in project zm-mailbox by Zimbra.
the class DbMailItem method updateInCalendarItemTable.
public static void updateInCalendarItemTable(CalendarItem calItem) throws ServiceException {
Mailbox mbox = calItem.getMailbox();
long start = calItem.getStartTime();
long end = calItem.getEndTime();
Timestamp startTs = new Timestamp(start);
Timestamp endTs = getEnd(start, end);
DbConnection conn = mbox.getOperationConnection();
PreparedStatement stmt = null;
try {
String command = Db.supports(Db.Capability.REPLACE_INTO) ? "REPLACE" : "INSERT";
String mailbox_id = DebugConfig.disableMailboxGroups ? "" : "mailbox_id, ";
stmt = conn.prepareStatement(command + " INTO " + getCalendarItemTableName(mbox) + " (" + mailbox_id + "uid, item_id, start_time, end_time)" + " VALUES (" + MAILBOX_ID_VALUE + "?, ?, ?, ?)");
int pos = 1;
pos = setMailboxId(stmt, mbox, pos);
stmt.setString(pos++, calItem.getUid());
stmt.setInt(pos++, calItem.getId());
stmt.setTimestamp(pos++, startTs);
stmt.setTimestamp(pos++, endTs);
stmt.executeUpdate();
} catch (SQLException e) {
if (Db.errorMatches(e, Db.Error.DUPLICATE_ROW)) {
try {
DbPool.closeStatement(stmt);
stmt = conn.prepareStatement("UPDATE " + getCalendarItemTableName(mbox) + " SET item_id = ?, start_time = ?, end_time = ? WHERE " + IN_THIS_MAILBOX_AND + "uid = ?");
int pos = 1;
stmt.setInt(pos++, calItem.getId());
stmt.setTimestamp(pos++, startTs);
stmt.setTimestamp(pos++, endTs);
pos = setMailboxId(stmt, mbox, pos);
stmt.setString(pos++, calItem.getUid());
stmt.executeUpdate();
} catch (SQLException nested) {
throw ServiceException.FAILURE("updating data in calendar item table " + calItem.getUid(), nested);
}
} else {
throw ServiceException.FAILURE("writing invite to calendar item table " + calItem.getUid(), e);
}
} finally {
DbPool.closeStatement(stmt);
}
}
use of com.zimbra.cs.mailbox.Mailbox in project zm-mailbox by Zimbra.
the class DbDataSource method updateMapping.
public static void updateMapping(DataSource ds, DataSourceItem item, final boolean isBatch) throws ServiceException {
Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
ZimbraLog.datasource.debug("Updating mapping for dataSource %s: itemId(%d), remoteId(%s)", ds.getName(), item.itemId, item.remoteId);
DbConnection conn = null;
PreparedStatement stmt = null;
try {
if (isBatch) {
conn = mbox.getOperationConnection();
} else {
conn = DbPool.getConnection(mbox);
}
if (!Db.supports(Db.Capability.ON_DUPLICATE_KEY) && !hasMapping(ds, item.itemId)) {
// if we need to update due to unique remoteId then itemid
// isn't going to be the same
StringBuilder sb = new StringBuilder();
sb.append("UPDATE ");
sb.append(getTableName(mbox));
sb.append(" SET folder_id = ?, item_id = ?, metadata = ? WHERE ");
sb.append(DbMailItem.IN_THIS_MAILBOX_AND);
sb.append(" remote_id = ?");
stmt = conn.prepareStatement(sb.toString());
int i = 1;
stmt.setInt(i++, item.folderId);
stmt.setInt(i++, item.itemId);
stmt.setString(i++, DbMailItem.checkMetadataLength((item.md == null) ? null : item.md.toString()));
i = DbMailItem.setMailboxId(stmt, mbox, i);
stmt.setString(i++, item.remoteId);
} else {
StringBuilder sb = new StringBuilder();
sb.append("UPDATE ");
sb.append(getTableName(mbox));
sb.append(" SET folder_id = ?, remote_id = ?, metadata = ? WHERE ");
sb.append(DbMailItem.IN_THIS_MAILBOX_AND);
sb.append(" item_id = ?");
stmt = conn.prepareStatement(sb.toString());
int i = 1;
stmt.setInt(i++, item.folderId);
stmt.setString(i++, item.remoteId);
stmt.setString(i++, DbMailItem.checkMetadataLength((item.md == null) ? null : item.md.toString()));
i = DbMailItem.setMailboxId(stmt, mbox, i);
stmt.setInt(i++, item.itemId);
}
stmt.executeUpdate();
if (!isBatch) {
conn.commit();
}
} catch (SQLException e) {
throw ServiceException.FAILURE("Unable to update mapping for dataSource " + ds.getName(), e);
} finally {
DbPool.closeStatement(stmt);
if (!isBatch) {
DbPool.quietClose(conn);
}
}
}
Aggregations