Search in sources :

Example 1 with Metadata

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;
}
Also used : Metadata(com.zimbra.cs.mailbox.Metadata) Folder(com.zimbra.cs.mailbox.Folder) LinkedHashMap(java.util.LinkedHashMap) Type(com.zimbra.cs.mailbox.MailItem.Type) MailItem(com.zimbra.cs.mailbox.MailItem) Entry(java.util.Map.Entry) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with Metadata

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();
}
Also used : Metadata(com.zimbra.cs.mailbox.Metadata) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Entry(java.util.Map.Entry) MailItem(com.zimbra.cs.mailbox.MailItem)

Example 3 with Metadata

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;
}
Also used : Metadata(com.zimbra.cs.mailbox.Metadata) Folder(com.zimbra.cs.mailbox.Folder) LinkedHashMap(java.util.LinkedHashMap) Entry(java.util.Map.Entry) MailItem(com.zimbra.cs.mailbox.MailItem)

Example 4 with Metadata

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;
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) Metadata(com.zimbra.cs.mailbox.Metadata) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 5 with Metadata

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);
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) SQLException(java.sql.SQLException) Metadata(com.zimbra.cs.mailbox.Metadata) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Aggregations

Metadata (com.zimbra.cs.mailbox.Metadata)68 Mailbox (com.zimbra.cs.mailbox.Mailbox)19 ArrayList (java.util.ArrayList)15 ServiceException (com.zimbra.common.service.ServiceException)13 DbConnection (com.zimbra.cs.db.DbPool.DbConnection)9 SQLException (java.sql.SQLException)9 IOException (java.io.IOException)8 PreparedStatement (java.sql.PreparedStatement)8 ResultSet (java.sql.ResultSet)8 MailItem (com.zimbra.cs.mailbox.MailItem)7 OperationContext (com.zimbra.cs.mailbox.OperationContext)7 ICalTimeZone (com.zimbra.common.calendar.ICalTimeZone)6 MetadataList (com.zimbra.cs.mailbox.MetadataList)6 HashMap (java.util.HashMap)6 Folder (com.zimbra.cs.mailbox.Folder)5 Map (java.util.Map)5 Entry (java.util.Map.Entry)5 ZMailbox (com.zimbra.client.ZMailbox)4 ZParameter (com.zimbra.common.calendar.ZCalendar.ZParameter)4 DataSource (com.zimbra.cs.account.DataSource)4