use of com.zimbra.cs.mailbox.Metadata in project zm-mailbox by Zimbra.
the class PendingModifications method deserialize.
@SuppressWarnings("unchecked")
public static PendingModifications deserialize(Mailbox mbox, byte[] data) throws IOException, ClassNotFoundException, ServiceException {
ByteArrayInputStream bis = new ByteArrayInputStream(data);
ObjectInputStream ois = new ObjectInputStream(bis);
PendingModifications pms = new PendingModifications();
pms.changedTypes = (Set<Type>) ois.readObject();
LinkedHashMap<ModificationKeyMeta, String> metaCreated = (LinkedHashMap<ModificationKeyMeta, String>) ois.readObject();
if (metaCreated != null) {
pms.created = new LinkedHashMap<ModificationKey, MailItem>();
Iterator<Entry<ModificationKeyMeta, String>> iter = metaCreated.entrySet().iterator();
while (iter.hasNext()) {
Entry<ModificationKeyMeta, String> entry = iter.next();
Metadata meta = new Metadata(entry.getValue());
MailItem.UnderlyingData ud = new MailItem.UnderlyingData();
ud.deserialize(meta);
MailItem item = MailItem.constructItem(mbox, ud, true);
if (item instanceof Folder) {
Folder folder = ((Folder) item);
folder.setParent(mbox.getFolderById(null, folder.getFolderId()));
}
ModificationKey key = new ModificationKey(entry.getKey().accountId, entry.getKey().itemId);
pms.created.put(key, item);
}
}
Map<ModificationKeyMeta, ChangeMeta> metaModified = (Map<ModificationKeyMeta, ChangeMeta>) ois.readObject();
pms.modified = getOriginal(mbox, metaModified);
Map<ModificationKeyMeta, ChangeMeta> metaDeleted = (Map<ModificationKeyMeta, ChangeMeta>) ois.readObject();
pms.deleted = getOriginal(mbox, metaDeleted);
return pms;
}
use of com.zimbra.cs.mailbox.Metadata in project zm-mailbox by Zimbra.
the class PendingModifications method getSerializedBytes.
public byte[] getSerializedBytes() throws IOException {
// assemble temporary created, modified, deleted with Metadata
LinkedHashMap<ModificationKeyMeta, String> metaCreated = null;
Map<ModificationKeyMeta, ChangeMeta> metaModified = null;
Map<ModificationKeyMeta, ChangeMeta> metaDeleted = null;
if (created != null) {
metaCreated = new LinkedHashMap<ModificationKeyMeta, String>();
Iterator<Entry<ModificationKey, MailItem>> iter = created.entrySet().iterator();
while (iter.hasNext()) {
Entry<ModificationKey, MailItem> entry = iter.next();
ModificationKeyMeta keyMeta = new ModificationKeyMeta(entry.getKey().getAccountId(), entry.getKey().getItemId());
MailItem item = entry.getValue();
Metadata meta = item.serializeUnderlyingData();
metaCreated.put(keyMeta, meta.toString());
}
}
metaModified = getSerializable(modified);
metaDeleted = getSerializable(deleted);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(changedTypes);
oos.writeObject(metaCreated);
oos.writeObject(metaModified);
oos.writeObject(metaDeleted);
oos.flush();
oos.close();
return bos.toByteArray();
}
use of com.zimbra.cs.mailbox.Metadata in project zm-mailbox by Zimbra.
the class PendingModifications method getOriginal.
private static Map<ModificationKey, Change> getOriginal(Mailbox mbox, Map<ModificationKeyMeta, ChangeMeta> map) throws ServiceException {
if (map == null) {
return null;
}
Map<ModificationKey, Change> ret = new LinkedHashMap<ModificationKey, Change>();
Iterator<Entry<ModificationKeyMeta, ChangeMeta>> iter = map.entrySet().iterator();
while (iter.hasNext()) {
Entry<ModificationKeyMeta, ChangeMeta> entry = iter.next();
ModificationKey key = new ModificationKey(entry.getKey().accountId, entry.getKey().itemId);
ChangeMeta changeMeta = entry.getValue();
Object what = null;
Object preModifyObj = null;
if (changeMeta.whatType == ChangeMeta.ObjectType.MAILITEM) {
Metadata meta = new Metadata(changeMeta.metaWhat);
MailItem.UnderlyingData ud = new MailItem.UnderlyingData();
ud.deserialize(meta);
what = MailItem.constructItem(mbox, ud, true);
if (what instanceof Folder) {
Folder folder = ((Folder) what);
folder.setParent(mbox.getFolderById(null, folder.getFolderId()));
}
} else if (changeMeta.whatType == ChangeMeta.ObjectType.MAILITEMTYPE) {
what = MailItem.Type.of(changeMeta.metaWhat);
} else if (changeMeta.whatType == ChangeMeta.ObjectType.MAILBOX) {
mbox.refreshMailbox(null);
what = mbox;
} else {
ZimbraLog.session.warn("Unexpected mailbox change type received : " + changeMeta.whatType);
continue;
}
if (changeMeta.preModifyObjType == ChangeMeta.ObjectType.MAILITEM) {
Metadata meta = new Metadata(changeMeta.metaPreModifyObj);
MailItem.UnderlyingData ud = new MailItem.UnderlyingData();
ud.deserialize(meta);
preModifyObj = MailItem.constructItem(mbox, ud, true);
if (preModifyObj instanceof Folder) {
Folder folder = ((Folder) preModifyObj);
folder.setParent(mbox.getFolderById(null, folder.getFolderId()));
}
} else if (changeMeta.preModifyObjType == ChangeMeta.ObjectType.MAILITEMTYPE) {
preModifyObj = MailItem.Type.of(changeMeta.metaPreModifyObj);
} else if (changeMeta.whatType == ChangeMeta.ObjectType.MAILBOX) {
what = mbox;
} else {
ZimbraLog.session.warn("Unexpected mailbox change type received : " + changeMeta.whatType);
continue;
}
Change change = new Change(what, changeMeta.metaWhy, preModifyObj);
ret.put(key, change);
}
return ret;
}
use of com.zimbra.cs.mailbox.Metadata in project zm-mailbox by Zimbra.
the class DbDataSource method getAllMappingsAndFlagsInFolder.
public static Collection<DataSourceItem> getAllMappingsAndFlagsInFolder(DataSource ds, int folderId) throws ServiceException {
Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
ArrayList<DataSourceItem> items = new ArrayList<DataSourceItem>();
ZimbraLog.datasource.debug("Get all mappings for %s in folder %d", ds.getName(), folderId);
DbConnection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
String thisTable = getTableName(mbox);
String IN_THIS_MAILBOX_AND = DebugConfig.disableMailboxGroups ? "" : thisTable + ".mailbox_id = ? AND ";
String MBOX_JOIN = DebugConfig.disableMailboxGroups ? " " : thisTable + ".mailbox_id = mi.mailbox_id AND ";
conn = DbPool.getConnection(mbox);
StringBuilder sb = new StringBuilder();
sb.append("SELECT item_id, remote_id, ").append(thisTable).append(".metadata, mi.unread, mi.flags FROM ");
sb.append(thisTable);
sb.append(" LEFT OUTER JOIN " + DbMailItem.getMailItemTableName(mbox)).append(" mi ");
sb.append(" ON ").append(MBOX_JOIN).append(thisTable).append(".item_id = mi.id ");
sb.append(" WHERE ");
sb.append(IN_THIS_MAILBOX_AND);
sb.append(" data_source_id = ? AND ").append(thisTable).append(".folder_id = ?");
stmt = conn.prepareStatement(sb.toString());
int pos = 1;
pos = DbMailItem.setMailboxId(stmt, mbox, pos);
stmt.setString(pos++, ds.getId());
stmt.setInt(pos++, folderId);
rs = stmt.executeQuery();
while (rs.next()) {
Metadata md = null;
String buf = DbMailItem.decodeMetadata(rs.getString(3));
int unread = rs.getInt(4);
int flags = rs.getInt(5);
if (buf != null)
md = new Metadata(buf);
flags = unread > 0 ? (flags | Flag.BITMASK_UNREAD) : (flags & ~Flag.BITMASK_UNREAD);
items.add(new DataSourceItem(folderId, rs.getInt(1), rs.getString(2), md, flags));
}
rs.close();
stmt.close();
} catch (SQLException e) {
throw ServiceException.FAILURE("Unable to get mapping for dataSource " + ds.getName(), e);
} finally {
DbPool.closeResults(rs);
DbPool.closeStatement(stmt);
DbPool.quietClose(conn);
}
return items;
}
use of com.zimbra.cs.mailbox.Metadata in project zm-mailbox by Zimbra.
the class DbDataSource method getMapping.
public static DataSourceItem getMapping(DataSource ds, int itemId, boolean isBatch) throws ServiceException {
Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
int folderId = 0;
String remoteId = null;
Metadata md = null;
ZimbraLog.datasource.debug("Get mapping for %s, itemId=%d", ds.getName(), itemId);
DbConnection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
if (isBatch) {
conn = mbox.getOperationConnection();
} else {
conn = DbPool.getConnection(mbox);
}
StringBuilder sb = new StringBuilder();
sb.append("SELECT folder_id, remote_id, metadata FROM ");
sb.append(getTableName(mbox));
sb.append(" WHERE ");
sb.append(DbMailItem.IN_THIS_MAILBOX_AND);
sb.append(" data_source_id = ? AND item_id = ?");
stmt = conn.prepareStatement(sb.toString());
int i = 1;
i = DbMailItem.setMailboxId(stmt, mbox, i);
stmt.setString(i++, ds.getId());
stmt.setInt(i++, itemId);
rs = stmt.executeQuery();
if (rs.next()) {
folderId = rs.getInt(1);
remoteId = rs.getString(2);
String buf = DbMailItem.decodeMetadata(rs.getString(3));
if (buf != null)
md = new Metadata(buf);
}
rs.close();
stmt.close();
} catch (SQLException e) {
throw ServiceException.FAILURE("Unable to get mapping for dataSource " + ds.getName(), e);
} finally {
DbPool.closeResults(rs);
DbPool.closeStatement(stmt);
if (!isBatch) {
DbPool.quietClose(conn);
}
}
return new DataSourceItem(folderId, itemId, remoteId, md);
}
Aggregations