Search in sources :

Example 31 with Metadata

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

the class DbDataSource method getAllMappingsInFolder.

public static Collection<DataSourceItem> getAllMappingsInFolder(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 {
        conn = DbPool.getConnection(mbox);
        String thisTable = getTableName(mbox);
        String IN_THIS_MAILBOX_AND = DebugConfig.disableMailboxGroups ? "" : thisTable + ".mailbox_id = ? AND ";
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT item_id, remote_id, ").append(thisTable).append(".metadata FROM ");
        sb.append(thisTable);
        sb.append(" WHERE ");
        sb.append(IN_THIS_MAILBOX_AND);
        sb.append("  data_source_id = ? AND 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));
            if (buf != null)
                md = new Metadata(buf);
            items.add(new DataSourceItem(folderId, rs.getInt(1), rs.getString(2), md));
        }
        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 32 with Metadata

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

the class DbDataSource method getMappings.

public static Collection<DataSourceItem> getMappings(DataSource ds, Collection<Integer> ids) throws ServiceException {
    Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
    int folderId = 0;
    int itemId = 0;
    String remoteId = null;
    Metadata md = null;
    List<List<Integer>> splitIds = ListUtil.split(ids, Db.getINClauseBatchSize());
    ArrayList<DataSourceItem> items = new ArrayList<DataSourceItem>();
    ZimbraLog.datasource.debug("Get mappings for %s", ds.getName());
    DbConnection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        conn = DbPool.getConnection(mbox);
        for (List<Integer> curIds : splitIds) {
            StringBuilder sb = new StringBuilder();
            sb.append("SELECT item_id, remote_id, folder_id, metadata FROM ");
            sb.append(getTableName(mbox));
            sb.append(" WHERE ");
            sb.append(DbMailItem.IN_THIS_MAILBOX_AND);
            sb.append(" data_source_id = ? AND ");
            sb.append(DbUtil.whereIn("item_id", curIds.size()));
            stmt = conn.prepareStatement(sb.toString());
            int i = 1;
            i = DbMailItem.setMailboxId(stmt, mbox, i);
            stmt.setString(i++, ds.getId());
            for (int uid : curIds) stmt.setInt(i++, uid);
            rs = stmt.executeQuery();
            while (rs.next()) {
                itemId = rs.getInt(1);
                remoteId = rs.getString(2);
                folderId = rs.getInt(3);
                String buf = DbMailItem.decodeMetadata(rs.getString(4));
                if (buf != null)
                    md = new Metadata(buf);
                items.add(new DataSourceItem(folderId, itemId, remoteId, md));
            }
            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 : SQLException(java.sql.SQLException) Metadata(com.zimbra.cs.mailbox.Metadata) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection) Mailbox(com.zimbra.cs.mailbox.Mailbox) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Example 33 with Metadata

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

the class DbDataSource method getReverseMappings.

public static Collection<DataSourceItem> getReverseMappings(DataSource ds, Collection<String> remoteIds) throws ServiceException {
    Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
    int folderId = 0;
    int itemId = 0;
    String remoteId;
    Metadata md = null;
    List<List<String>> splitIds = ListUtil.split(remoteIds, Db.getINClauseBatchSize());
    ArrayList<DataSourceItem> items = new ArrayList<DataSourceItem>();
    ZimbraLog.datasource.debug("Get reverse mappings for %s", ds.getName());
    DbConnection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        conn = DbPool.getConnection(mbox);
        for (List<String> curIds : splitIds) {
            StringBuilder sb = new StringBuilder();
            sb.append("SELECT item_id, remote_id, folder_id, metadata FROM ");
            sb.append(getTableName(mbox));
            sb.append(" WHERE ");
            sb.append(DbMailItem.IN_THIS_MAILBOX_AND);
            sb.append(" data_source_id = ? AND ");
            sb.append(DbUtil.whereIn("remote_id", curIds.size()));
            stmt = conn.prepareStatement(sb.toString());
            int i = 1;
            i = DbMailItem.setMailboxId(stmt, mbox, i);
            stmt.setString(i++, ds.getId());
            for (String uid : curIds) stmt.setString(i++, uid);
            rs = stmt.executeQuery();
            while (rs.next()) {
                itemId = rs.getInt(1);
                remoteId = rs.getString(2);
                folderId = rs.getInt(3);
                String buf = DbMailItem.decodeMetadata(rs.getString(4));
                if (buf != null)
                    md = new Metadata(buf);
                items.add(new DataSourceItem(folderId, itemId, remoteId, md));
            }
            rs.close();
            stmt.close();
        }
    } catch (SQLException e) {
        throw ServiceException.FAILURE("Unable to get reverse mapping for dataSource " + ds.getName(), e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
        DbPool.quietClose(conn);
    }
    return items;
}
Also used : SQLException(java.sql.SQLException) Metadata(com.zimbra.cs.mailbox.Metadata) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection) Mailbox(com.zimbra.cs.mailbox.Mailbox) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Example 34 with Metadata

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

the class DbDataSource method getAllMappingsForRemoteIdPrefix.

public static Collection<DataSourceItem> getAllMappingsForRemoteIdPrefix(DataSource ds, int folderId, String prefix) throws ServiceException {
    Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
    List<DataSourceItem> items = new ArrayList<DataSourceItem>();
    DbConnection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        conn = DbPool.getConnection(mbox);
        String db = DbMailbox.getDatabaseName(mbox);
        String dst = db + ".data_source_item";
        String mit = db + ".mail_item";
        Formatter fmt = new Formatter();
        fmt.format("SELECT item_id, remote_id, %s.metadata FROM %s", dst, dst);
        fmt.format(" INNER JOIN %s ON %s.item_id = %s.id", mit, dst, mit);
        fmt.format(" WHERE %s.mailbox_id = ?", dst);
        fmt.format(" AND data_source_id = ? AND folder_id = ?");
        if (prefix != null) {
            fmt.format(" AND remote_id LIKE '%s%%'", prefix);
        }
        stmt = conn.prepareStatement(fmt.toString());
        stmt.setInt(1, mbox.getId());
        stmt.setString(2, ds.getId());
        stmt.setInt(3, folderId);
        rs = stmt.executeQuery();
        while (rs.next()) {
            String s = DbMailItem.decodeMetadata(rs.getString(3));
            Metadata md = s != null ? new Metadata(s) : null;
            items.add(new DataSourceItem(folderId, rs.getInt(1), rs.getString(2), md));
        }
    } catch (SQLException e) {
        throw ServiceException.FAILURE("Unable to get mapping for data source " + 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) Formatter(java.util.Formatter) 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 35 with Metadata

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

the class CreateInvite method deserializeData.

@Override
protected void deserializeData(RedoLogInput in) throws IOException {
    mCalendarItemId = in.readInt();
    if (getVersion().atLeast(1, 1))
        mCalendarItemPartStat = in.readUTF();
    mFolderId = in.readInt();
    if (getVersion().atLeast(1, 0))
        in.readShort();
    // keep this for backward compatibility when there was mForce field
    in.readBoolean();
    // in this class
    int dataLen = in.readInt();
    if (dataLen > 0) {
        mData = new byte[dataLen];
        in.readFully(mData);
    }
    try {
        ICalTimeZone localTz = Util.decodeTimeZoneFromMetadata(new Metadata(in.readUTF()));
        mInvite = Invite.decodeMetadata(getMailboxId(), new Metadata(in.readUTF()), null, localTz);
    } catch (ServiceException ex) {
        ex.printStackTrace();
        throw new IOException("Cannot read serialized entry for CreateInvite " + ex.toString());
    }
    if (getVersion().atLeast(1, 22)) {
        mPreserveExistingAlarms = in.readBoolean();
        mDiscardExistingInvites = in.readBoolean();
    } else {
        mPreserveExistingAlarms = false;
        mDiscardExistingInvites = false;
    }
    if (getVersion().atLeast(1, 23))
        mAddRevision = in.readBoolean();
    else
        mAddRevision = false;
}
Also used : ServiceException(com.zimbra.common.service.ServiceException) Metadata(com.zimbra.cs.mailbox.Metadata) IOException(java.io.IOException) ICalTimeZone(com.zimbra.common.calendar.ICalTimeZone)

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