Search in sources :

Example 51 with Mailbox

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

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

the class DbDataSource method deleteAllMappingsInFolder.

public static Collection<DataSourceItem> deleteAllMappingsInFolder(DataSource ds, int folderId, boolean isBatch) throws ServiceException {
    Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
    ArrayList<DataSourceItem> items = new ArrayList<DataSourceItem>();
    ZimbraLog.datasource.debug("Deleting all mappings for dataSource %s in folder %d", ds.getName(), folderId);
    mbox.lock.lock();
    try {
        DbConnection conn = null;
        PreparedStatement stmt = null;
        try {
            if (isBatch) {
                conn = mbox.getOperationConnection();
            } else {
                conn = DbPool.getConnection(mbox);
            }
            String dataSourceTable = getTableName(mbox);
            String IN_THIS_MAILBOX_AND = DebugConfig.disableMailboxGroups ? "" : dataSourceTable + ".mailbox_id = ? AND ";
            StringBuilder sb = new StringBuilder();
            sb.append("DELETE FROM ");
            sb.append(dataSourceTable);
            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);
            int 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);
            }
        }
    } finally {
        mbox.lock.release();
    }
    return items;
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 53 with Mailbox

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

the class DbDataSource method lookupPurgedConversationsByHash.

public static List<PurgedConversation> lookupPurgedConversationsByHash(DataSource ds, List<String> hashes) throws ServiceException {
    Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
    DbConnection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs;
    Map<Integer, PurgedConversation> convMap = new HashMap<Integer, PurgedConversation>();
    try {
        conn = DbPool.getConnection();
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT pm.item_id, pm.parent_id, pm.remote_id, pm.remote_folder_id FROM");
        sb.append(" (SELECT item_id, parent_id, remote_id, remote_folder_id FROM ").append(getPurgedMessagesTableName(mbox));
        sb.append(" WHERE mailbox_id = ? AND data_source_id = ?)");
        sb.append(" pm ");
        sb.append(" INNER JOIN ");
        sb.append("(SELECT DISTINCT item_id FROM ").append(getPurgedConvsTableName(mbox));
        sb.append(" WHERE mailbox_id = ? AND data_source_id = ? AND ");
        sb.append(DbUtil.whereIn("hash", hashes.size()));
        sb.append(") pc ");
        sb.append("ON COALESCE(pm.parent_id, pm.item_id) = pc.item_id");
        stmt = conn.prepareStatement(sb.toString());
        int pos = 1;
        pos = DbMailItem.setMailboxId(stmt, mbox, pos);
        stmt.setString(pos++, ds.getId());
        pos = DbMailItem.setMailboxId(stmt, mbox, pos);
        stmt.setString(pos++, ds.getId());
        for (String hash : hashes) {
            stmt.setString(pos++, hash);
        }
        rs = stmt.executeQuery();
        while (rs.next()) {
            Integer msgId = rs.getInt(1);
            Integer parentId = rs.getInt(2);
            String remoteId = rs.getString(3);
            String remoteFolderId = rs.getString(4);
            PurgedConversation conv = null;
            if (convMap.containsKey(parentId)) {
                conv = convMap.get(parentId);
            } else {
                conv = new PurgedConversation(ds, parentId);
                convMap.put(parentId, conv);
            }
            conv.addMessage(new PurgedMessage(msgId, parentId, remoteId, remoteFolderId));
        }
        rs.close();
        stmt.close();
        Collection<PurgedConversation> values = convMap.values();
        return new ArrayList<PurgedConversation>(values);
    } catch (SQLException e) {
        throw ServiceException.FAILURE("Unable to get purged conversations", e);
    } finally {
        DbPool.closeStatement(stmt);
        DbPool.quietClose(conn);
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) 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)

Example 54 with Mailbox

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

the class DbMailItem method getCalendarItemMetadata.

/**
     * @param start start time of range, in milliseconds. {@code -1} means to leave the start time unconstrained.
     * @param end end time of range, in milliseconds. {@code -1} means to leave the end time unconstrained.
     */
public static List<CalendarItem.CalendarMetadata> getCalendarItemMetadata(Folder folder, long start, long end) throws ServiceException {
    Mailbox mbox = folder.getMailbox();
    ArrayList<CalendarItem.CalendarMetadata> result = new ArrayList<CalendarItem.CalendarMetadata>();
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        // Note - Negative times are valid.  However, treat -1 as meaning "unconstrained"
        // Want appointments that end after the start time
        String startConstraint = (start != -1) ? " AND ci.end_time > ?" : "";
        // Want appointments that start before the end time
        String endConstraint = (end != -1) ? " AND ci.start_time < ?" : "";
        String folderConstraint = " AND mi.folder_id = ?";
        stmt = conn.prepareStatement("SELECT mi.mailbox_id, mi.id, ci.uid, mi.mod_metadata, mi.mod_content, ci.start_time, ci.end_time" + " FROM " + getMailItemTableName(mbox, "mi") + ", " + getCalendarItemTableName(mbox, "ci") + " WHERE mi.mailbox_id = ci.mailbox_id AND mi.id = ci.item_id" + (DebugConfig.disableMailboxGroups ? "" : " AND mi.mailbox_id = ? ") + startConstraint + endConstraint + folderConstraint);
        int pos = 1;
        pos = setMailboxId(stmt, mbox, pos);
        if (!startConstraint.isEmpty()) {
            stmt.setTimestamp(pos++, new Timestamp(start));
        }
        if (!endConstraint.isEmpty()) {
            stmt.setTimestamp(pos++, new Timestamp(end));
        }
        stmt.setInt(pos++, folder.getId());
        rs = stmt.executeQuery();
        while (rs.next()) {
            result.add(new CalendarItem.CalendarMetadata(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getInt(4), rs.getInt(5), rs.getTimestamp(6).getTime(), rs.getTimestamp(7).getTime()));
        }
    } catch (SQLException e) {
        throw ServiceException.FAILURE("fetching CalendarItem Metadata for mbox " + mbox.getId(), e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) DbConnection(com.zimbra.cs.db.DbPool.DbConnection) CalendarItem(com.zimbra.cs.mailbox.CalendarItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) ResultSet(java.sql.ResultSet)

Example 55 with Mailbox

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

the class DbMailItem method getLeafNodes.

public static PendingDelete getLeafNodes(Folder folder) throws ServiceException {
    Mailbox mbox = folder.getMailbox();
    int folderId = folder.getId();
    QueryParams params = new QueryParams();
    params.setFolderIds(Collections.singletonList(folderId));
    PendingDelete info = getLeafNodes(mbox, params);
    // make sure that the folder is in the list of deleted items
    info.itemIds.add(folder.getType(), folderId, folder.getUuid());
    return info;
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) PendingDelete(com.zimbra.cs.mailbox.MailItem.PendingDelete)

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