Search in sources :

Example 6 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 7 with Metadata

use of com.zimbra.cs.mailbox.Metadata in project zm-mailbox by Zimbra.

the class GetMailboxMetadata method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Mailbox mbox = getRequestedMailbox(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    Element meta = request.getElement(MailConstants.E_METADATA);
    String section = meta.getAttribute(MailConstants.A_SECTION);
    Metadata metadata = mbox.getConfig(octxt, section);
    Element response = zsc.createElement(MailConstants.GET_MAILBOX_METADATA_RESPONSE);
    meta = response.addElement(MailConstants.E_METADATA);
    meta.addAttribute(MailConstants.A_SECTION, section);
    if (metadata != null) {
        for (Map.Entry<String, ?> entry : metadata.asMap().entrySet()) {
            meta.addKeyValuePair(entry.getKey(), entry.getValue().toString());
        }
    }
    return response;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Element(com.zimbra.common.soap.Element) Metadata(com.zimbra.cs.mailbox.Metadata) Map(java.util.Map)

Example 8 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 9 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 10 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)

Aggregations

Metadata (com.zimbra.cs.mailbox.Metadata)60 Mailbox (com.zimbra.cs.mailbox.Mailbox)16 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 OperationContext (com.zimbra.cs.mailbox.OperationContext)7 ICalTimeZone (com.zimbra.common.calendar.ICalTimeZone)6 MailItem (com.zimbra.cs.mailbox.MailItem)5 HashMap (java.util.HashMap)5 ZParameter (com.zimbra.common.calendar.ZCalendar.ZParameter)4 DataSource (com.zimbra.cs.account.DataSource)4 DbDataSource (com.zimbra.cs.db.DbDataSource)4 DataSourceItem (com.zimbra.cs.db.DbDataSource.DataSourceItem)4 Map (java.util.Map)4 Entry (java.util.Map.Entry)4 ParsedDateTime (com.zimbra.common.calendar.ParsedDateTime)3