Search in sources :

Example 46 with Metadata

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

the class DbTag method asUnderlyingData.

private static UnderlyingData asUnderlyingData(ResultSet rs) throws SQLException, ServiceException {
    UnderlyingData data = new UnderlyingData();
    data.id = rs.getInt(1);
    data.type = MailItem.Type.TAG.toByte();
    data.folderId = Mailbox.ID_FOLDER_TAGS;
    data.name = rs.getString(2);
    data.size = rs.getInt(4);
    data.unreadCount = rs.getInt(5);
    boolean listed = rs.getBoolean(6);
    String policy = rs.getString(8);
    RetentionPolicy rp = policy == null ? null : RetentionPolicyManager.retentionPolicyFromMetadata(new Metadata(policy), true);
    long c = rs.getLong(3);
    Color color = rs.wasNull() ? null : Color.fromMetadata(c);
    // FIXME: if Tag weren't a subclass of MailItem, this step wouldn't be necessary
    data.metadata = Tag.encodeMetadata(color, 1, 1, rp, listed);
    data.modMetadata = rs.getInt(7);
    data.modContent = data.modMetadata;
    return data;
}
Also used : UnderlyingData(com.zimbra.cs.mailbox.MailItem.UnderlyingData) Color(com.zimbra.common.mailbox.Color) Metadata(com.zimbra.cs.mailbox.Metadata) RetentionPolicy(com.zimbra.soap.mail.type.RetentionPolicy)

Example 47 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 48 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 49 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 50 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)

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