use of com.zimbra.soap.admin.type.ExportAndDeleteItemSpec in project zm-mailbox by Zimbra.
the class ExportAndDeleteItems method handle.
@Override
public Element handle(Element requst, Map<String, Object> context) throws ServiceException {
ZimbraSoapContext zsc = getZimbraSoapContext(context);
checkRight(zsc, context, null, AdminRight.PR_SYSTEM_ADMIN_ONLY);
// Parse request.
ExportAndDeleteItemsRequest req = JaxbUtil.elementToJaxb(requst);
ExportAndDeleteMailboxSpec mailbox = req.getMailbox();
if (mailbox == null) {
throw ServiceException.INVALID_REQUEST("empty mbox id", null);
}
Mailbox mbox = MailboxManager.getInstance().getMailboxById(mailbox.getId());
Multimap<Integer, Integer> idRevs = HashMultimap.create();
for (ExportAndDeleteItemSpec item : mailbox.getItems()) {
idRevs.put(item.getId(), item.getVersion());
}
String dirPath = req.getExportDir();
String prefix = req.getExportFilenamePrefix();
// Lock the mailbox, to make sure that another thread doesn't modify the items we're exporting/deleting.
mbox.lock.lock();
try {
DbConnection conn = null;
try {
conn = DbPool.getConnection();
if (dirPath != null) {
File exportDir = new File(dirPath);
if (!exportDir.isDirectory()) {
DbPool.quietClose(conn);
throw ServiceException.INVALID_REQUEST(dirPath + " is not a directory", null);
}
String filePath = makePath(dirPath, DbMailItem.TABLE_MAIL_ITEM, prefix);
export(conn, mbox, DbMailItem.TABLE_MAIL_ITEM, "id", idRevs, filePath);
filePath = makePath(dirPath, DbMailItem.TABLE_MAIL_ITEM_DUMPSTER, prefix);
export(conn, mbox, DbMailItem.TABLE_MAIL_ITEM_DUMPSTER, "id", idRevs, filePath);
filePath = makePath(dirPath, DbMailItem.TABLE_REVISION, prefix);
export(conn, mbox, DbMailItem.TABLE_REVISION, "item_id", idRevs, filePath);
filePath = makePath(dirPath, DbMailItem.TABLE_REVISION_DUMPSTER, prefix);
export(conn, mbox, DbMailItem.TABLE_REVISION_DUMPSTER, "item_id", idRevs, filePath);
filePath = makePath(dirPath, DbMailItem.TABLE_APPOINTMENT, prefix);
export(conn, mbox, DbMailItem.TABLE_APPOINTMENT, "item_id", idRevs, filePath);
filePath = makePath(dirPath, DbMailItem.TABLE_APPOINTMENT_DUMPSTER, prefix);
export(conn, mbox, DbMailItem.TABLE_APPOINTMENT_DUMPSTER, "item_id", idRevs, filePath);
}
// delete item from mail_item and revision table
for (Integer itemId : idRevs.keySet()) {
Collection<Integer> revs = idRevs.get(itemId);
for (int rev : revs) {
if (rev == 0) {
// delete all revisions to make sure we delete all blobs
List<MailItem> list = null;
try {
list = mbox.getAllRevisions(null, itemId, MailItem.Type.UNKNOWN);
} catch (NoSuchItemException ex) {
// exception happens when we try to delete a mail_item which is already in mail_item_dumpster
continue;
}
for (MailItem item : list) {
if (item.getType() == MailItem.Type.DOCUMENT) {
mbox.purgeRevision(null, itemId, item.getVersion(), false);
}
}
mbox.delete(null, itemId, MailItem.Type.UNKNOWN, null);
break;
} else if (!revs.contains(0)) {
try {
mbox.purgeRevision(null, itemId, rev, false);
} catch (NoSuchItemException ex) {
// exception happens when we try to delete a revision which is already in revision_dumpster
continue;
}
}
}
}
// Delete items from mail_item_dumpster & revision_dumpster tables just
// incase moved to dumpster tables
DbBlobConsistency.delete(conn, mbox, idRevs);
} finally {
conn.commit();
DbPool.quietClose(conn);
}
} finally {
mbox.lock.release();
}
return zsc.createElement(AdminConstants.EXPORT_AND_DELETE_ITEMS_RESPONSE);
}
Aggregations