Search in sources :

Example 46 with Mailbox

use of com.zimbra.cs.mailbox.Mailbox 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)

Example 47 with Mailbox

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

the class DbDataSource method getReverseMapping.

public static DataSourceItem getReverseMapping(DataSource ds, String remoteId) throws ServiceException {
    Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
    int folderId = 0;
    int itemId = 0;
    Metadata md = null;
    ZimbraLog.datasource.debug("Get reverse mapping for %s, remoteId=%s", ds.getName(), remoteId);
    DbConnection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        conn = DbPool.getConnection(mbox);
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT item_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 remote_id = ?");
        stmt = conn.prepareStatement(sb.toString());
        int i = 1;
        i = DbMailItem.setMailboxId(stmt, mbox, i);
        stmt.setString(i++, ds.getId());
        stmt.setString(i++, remoteId);
        rs = stmt.executeQuery();
        if (rs.next()) {
            itemId = rs.getInt(1);
            folderId = rs.getInt(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 reverse mapping for dataSource " + ds.getName(), e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
        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)

Example 48 with Mailbox

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

the class DbDataSource method getOldestConversationsUpToSize.

public static List<PurgeableConv> getOldestConversationsUpToSize(List<DataSource> dataSources, long targetSize, long startDate) throws ServiceException {
    Mailbox mbox = DataSourceManager.getInstance().getMailbox(dataSources.get(0));
    DbConnection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    long totalSize = 0;
    try {
        conn = DbPool.getConnection();
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT DISTINCT COALESCE(msgs.parent_id, msgs.msg_id) AS item_id," + " msgs.data_source_id AS data_source," + " COALESCE(convs.latest_date, msgs.mdate) AS newest_msg_date," + " COALESCE(convs.num_msgs, 1) AS num_msgs," + " COALESCE(convs.conv_size, msgs.msize) AS size");
        sb.append(" FROM ");
        sb.append("(SELECT di.data_source_id AS data_source_id, " + " mi.id AS msg_id," + " mi.parent_id AS parent_id," + " mi.size AS msize," + " mi.date AS mdate FROM ");
        sb.append(getTableName(mbox)).append(" di ");
        sb.append(" INNER JOIN ");
        sb.append(DbMailItem.getMailItemTableName(mbox)).append(" mi ");
        sb.append(" ON di.mailbox_id = mi.mailbox_id AND di.item_id = mi.id");
        sb.append(" WHERE di.mailbox_id = ? AND mi.type = ? AND ");
        sb.append(DbUtil.whereIn("di.data_source_id", dataSources.size()));
        sb.append(") AS msgs");
        sb.append(" LEFT JOIN ");
        sb.append("(SELECT parent_id, max(date) AS latest_date, count(date) AS num_msgs, sum(size) AS conv_size");
        sb.append(" FROM ").append(DbMailItem.getMailItemTableName(mbox));
        sb.append(" WHERE mailbox_id = ? AND type = ? GROUP BY parent_id) AS convs");
        sb.append(" ON msgs.parent_id = convs.parent_id");
        if (startDate > 0L) {
            sb.append(" WHERE COALESCE(convs.latest_date, msgs.mdate) > ?");
        }
        sb.append(" ORDER BY COALESCE(convs.latest_date, msgs.mdate) ASC");
        stmt = conn.prepareStatement(sb.toString());
        int pos = 1;
        pos = DbMailItem.setMailboxId(stmt, mbox, pos);
        stmt.setByte(pos++, MailItem.Type.MESSAGE.toByte());
        for (DataSource ds : dataSources) {
            stmt.setString(pos++, ds.getId());
        }
        pos = DbMailItem.setMailboxId(stmt, mbox, pos);
        stmt.setByte(pos++, MailItem.Type.MESSAGE.toByte());
        if (startDate > 0L) {
            stmt.setLong(pos++, startDate);
        }
        rs = stmt.executeQuery();
        List<PurgeableConv> convs = new LinkedList<PurgeableConv>();
        while (rs.next() && totalSize < targetSize) {
            long convSize = rs.getLong("size");
            int itemId = rs.getInt("item_id");
            int numMsgs = rs.getInt("num_msgs");
            long convDate = rs.getLong("newest_msg_date");
            String dataSourceId = rs.getString("data_source");
            convs.add(new PurgeableConv(itemId, convSize, convDate, dataSourceId, numMsgs));
            totalSize += convSize;
        }
        rs.close();
        stmt.close();
        return convs;
    } catch (SQLException e) {
        throw ServiceException.FAILURE("Unable to get oldest conversations for data sources", e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
        DbPool.quietClose(conn);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection) LinkedList(java.util.LinkedList) DataSource(com.zimbra.cs.account.DataSource) PurgeableConv(com.zimbra.cs.purge.DataSourcePurge.PurgeableConv) Mailbox(com.zimbra.cs.mailbox.Mailbox) ResultSet(java.sql.ResultSet)

Example 49 with Mailbox

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

the class DbDataSource method deleteMappings.

public static void deleteMappings(DataSource ds, Collection<Integer> itemIds, boolean isBatch) throws ServiceException {
    Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
    ZimbraLog.datasource.debug("Deleting %d mappings for dataSource %s", itemIds.size(), ds.getName());
    List<List<Integer>> splitIds = ListUtil.split(itemIds, Db.getINClauseBatchSize());
    DbConnection conn = null;
    PreparedStatement stmt = null;
    try {
        if (isBatch) {
            conn = mbox.getOperationConnection();
        } else {
            conn = DbPool.getConnection(mbox);
        }
        int numRows = 0;
        for (List<Integer> curIds : splitIds) {
            StringBuilder sb = new StringBuilder();
            sb.append("DELETE 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 itemId : curIds) stmt.setInt(i++, itemId);
            numRows += stmt.executeUpdate();
            if (!isBatch) {
                conn.commit();
            }
            stmt.close();
        }
        ZimbraLog.datasource.debug("Deleted %d mappings for %s", numRows, ds.getName());
    } catch (SQLException e) {
        throw ServiceException.FAILURE("Unable to delete mapping for dataSource " + ds.getName(), e);
    } finally {
        DbPool.closeStatement(stmt);
        if (!isBatch) {
            DbPool.quietClose(conn);
        }
    }
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 50 with Mailbox

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

the class DbDataSource method getAllMappings.

public static Collection<DataSourceItem> getAllMappings(DataSource ds) throws ServiceException {
    Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
    ArrayList<DataSourceItem> items = new ArrayList<DataSourceItem>();
    ZimbraLog.datasource.debug("Get all mappings for %s", ds.getName());
    DbConnection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        conn = DbPool.getConnection(mbox);
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT item_id, 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 = ?");
        stmt = conn.prepareStatement(sb.toString());
        int i = 1;
        i = DbMailItem.setMailboxId(stmt, mbox, i);
        stmt.setString(i++, ds.getId());
        rs = stmt.executeQuery();
        while (rs.next()) {
            Metadata md = null;
            String buf = DbMailItem.decodeMetadata(rs.getString(4));
            if (buf != null)
                md = new Metadata(buf);
            items.add(new DataSourceItem(rs.getInt(2), rs.getInt(1), rs.getString(3), 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)

Aggregations

Mailbox (com.zimbra.cs.mailbox.Mailbox)817 Account (com.zimbra.cs.account.Account)389 Test (org.junit.Test)376 OperationContext (com.zimbra.cs.mailbox.OperationContext)365 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)306 Message (com.zimbra.cs.mailbox.Message)303 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)243 ItemId (com.zimbra.cs.service.util.ItemId)243 Element (com.zimbra.common.soap.Element)138 ServiceException (com.zimbra.common.service.ServiceException)127 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)127 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)106 MimeMessage (javax.mail.internet.MimeMessage)104 Folder (com.zimbra.cs.mailbox.Folder)82 ArrayList (java.util.ArrayList)81 ZMailbox (com.zimbra.client.ZMailbox)73 DbConnection (com.zimbra.cs.db.DbPool.DbConnection)71 PreparedStatement (java.sql.PreparedStatement)67 SQLException (java.sql.SQLException)67 Header (javax.mail.Header)66