Search in sources :

Example 6 with UnderlyingData

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

the class DbMailItem method getByType.

public static List<UnderlyingData> getByType(Mailbox mbox, MailItem.Type type, SortBy sort) throws ServiceException {
    if (Mailbox.isCachedType(type)) {
        throw ServiceException.INVALID_REQUEST("folders and tags must be retrieved from cache", null);
    }
    ArrayList<UnderlyingData> result = new ArrayList<UnderlyingData>();
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        stmt = conn.prepareStatement("SELECT " + DB_FIELDS + " FROM " + getMailItemTableName(mbox, " mi") + " WHERE " + IN_THIS_MAILBOX_AND + typeIn(type) + DbSearch.orderBy(sort, false));
        if (type == MailItem.Type.MESSAGE) {
            Db.getInstance().enableStreaming(stmt);
        }
        setMailboxId(stmt, mbox, 1);
        rs = stmt.executeQuery();
        while (rs.next()) {
            result.add(constructItem(rs));
        }
        rs.close();
        rs = null;
        stmt.close();
        stmt = null;
        if (type == MailItem.Type.CONVERSATION) {
            completeConversations(mbox, conn, result);
        }
        return result;
    } catch (SQLException e) {
        throw ServiceException.FAILURE("fetching items of type " + type, e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
    }
}
Also used : SQLException(java.sql.SQLException) UnderlyingData(com.zimbra.cs.mailbox.MailItem.UnderlyingData) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 7 with UnderlyingData

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

the class DbMailItem method getUnreadMessages.

public static List<UnderlyingData> getUnreadMessages(MailItem relativeTo) throws ServiceException {
    if (relativeTo instanceof Tag) {
        return DbTag.getUnreadMessages((Tag) relativeTo);
    }
    Mailbox mbox = relativeTo.getMailbox();
    ArrayList<UnderlyingData> result = new ArrayList<UnderlyingData>();
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        String relation;
        if (relativeTo instanceof VirtualConversation) {
            relation = "id = ?";
        } else if (relativeTo instanceof Conversation) {
            relation = "parent_id = ?";
        } else if (relativeTo instanceof Folder) {
            relation = "folder_id = ?";
        } else {
            relation = "id = ?";
        }
        stmt = conn.prepareStatement("SELECT " + DB_FIELDS + " FROM " + getMailItemTableName(relativeTo.getMailbox(), " mi") + " WHERE " + IN_THIS_MAILBOX_AND + "unread > 0 AND " + relation + " AND type NOT IN " + NON_SEARCHABLE_TYPES);
        if (relativeTo.getUnreadCount() > RESULTS_STREAMING_MIN_ROWS) {
            Db.getInstance().enableStreaming(stmt);
        }
        int pos = 1;
        pos = setMailboxId(stmt, mbox, pos);
        if (relativeTo instanceof VirtualConversation) {
            stmt.setInt(pos++, ((VirtualConversation) relativeTo).getMessageId());
        } else {
            stmt.setInt(pos++, relativeTo.getId());
        }
        rs = stmt.executeQuery();
        while (rs.next()) {
            UnderlyingData data = constructItem(rs);
            if (Mailbox.isCachedType(MailItem.Type.of(data.type))) {
                throw ServiceException.INVALID_REQUEST("folders and tags must be retrieved from cache", null);
            }
            result.add(data);
        }
        return result;
    } catch (SQLException e) {
        throw ServiceException.FAILURE("fetching unread messages for item " + relativeTo.getId(), e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
    }
}
Also used : SQLException(java.sql.SQLException) UnderlyingData(com.zimbra.cs.mailbox.MailItem.UnderlyingData) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) VirtualConversation(com.zimbra.cs.mailbox.VirtualConversation) Conversation(com.zimbra.cs.mailbox.Conversation) Folder(com.zimbra.cs.mailbox.Folder) DbConnection(com.zimbra.cs.db.DbPool.DbConnection) Mailbox(com.zimbra.cs.mailbox.Mailbox) ResultSet(java.sql.ResultSet) Tag(com.zimbra.cs.mailbox.Tag) VirtualConversation(com.zimbra.cs.mailbox.VirtualConversation)

Example 8 with UnderlyingData

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

the class DbMailItem method getByParent.

public static List<UnderlyingData> getByParent(MailItem parent, SortBy sort, int limit, boolean fromDumpster) throws ServiceException {
    Mailbox mbox = parent.getMailbox();
    List<UnderlyingData> result = new ArrayList<UnderlyingData>();
    StringBuilder sql = new StringBuilder("SELECT ").append(DB_FIELDS).append(" FROM ").append(getMailItemTableName(parent.getMailbox(), " mi", fromDumpster)).append(" WHERE ").append(IN_THIS_MAILBOX_AND).append("parent_id = ? ").append(DbSearch.orderBy(sort, false));
    if (limit > 0) {
        sql.append(" LIMIT ?");
    }
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        stmt = conn.prepareStatement(sql.toString());
        if (parent.getSize() > RESULTS_STREAMING_MIN_ROWS) {
            Db.getInstance().enableStreaming(stmt);
        }
        int pos = 1;
        pos = setMailboxId(stmt, mbox, pos);
        stmt.setInt(pos++, parent.getId());
        if (limit > 0) {
            stmt.setInt(pos++, limit);
        }
        rs = stmt.executeQuery();
        while (rs.next()) {
            UnderlyingData data = constructItem(rs, fromDumpster);
            if (Mailbox.isCachedType(MailItem.Type.of(data.type))) {
                throw ServiceException.INVALID_REQUEST("folders and tags must be retrieved from cache", null);
            }
            result.add(data);
        }
        return result;
    } catch (SQLException e) {
        throw ServiceException.FAILURE("fetching children of item " + parent.getId(), e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
    }
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) SQLException(java.sql.SQLException) UnderlyingData(com.zimbra.cs.mailbox.MailItem.UnderlyingData) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 9 with UnderlyingData

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

the class DbMailItem method getByHashes.

public static List<UnderlyingData> getByHashes(Mailbox mbox, List<String> hashes) throws ServiceException {
    if (ListUtil.isEmpty(hashes)) {
        return null;
    }
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        stmt = conn.prepareStatement("SELECT " + DB_FIELDS + " FROM " + getMailItemTableName(mbox, "mi") + ", " + getConversationTableName(mbox, "oc") + " WHERE mi.id = oc.conv_id AND " + DbUtil.whereIn("oc.hash", hashes.size()) + (DebugConfig.disableMailboxGroups ? "" : " AND oc.mailbox_id = ? AND mi.mailbox_id = oc.mailbox_id"));
        int pos = 1;
        for (String hash : hashes) {
            stmt.setString(pos++, hash);
        }
        pos = setMailboxId(stmt, mbox, pos);
        rs = stmt.executeQuery();
        List<UnderlyingData> dlist = new ArrayList<UnderlyingData>(3);
        Set<Integer> convIds = Sets.newHashSetWithExpectedSize(3);
        while (rs.next()) {
            int id = rs.getInt(CI_ID);
            if (convIds.contains(id)) {
                continue;
            }
            UnderlyingData data = constructItem(rs);
            if (data.type == MailItem.Type.CONVERSATION.toByte()) {
                completeConversation(mbox, conn, data);
            }
            dlist.add(data);
            convIds.add(data.id);
        }
        return dlist.isEmpty() ? null : dlist;
    } catch (SQLException e) {
        throw ServiceException.FAILURE("fetching conversation for hash " + hashes, e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
    }
}
Also used : SQLException(java.sql.SQLException) UnderlyingData(com.zimbra.cs.mailbox.MailItem.UnderlyingData) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 10 with UnderlyingData

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

the class DbMailItem method completeConversations.

private static void completeConversations(Mailbox mbox, DbConnection conn, List<UnderlyingData> convData) throws ServiceException {
    if (convData == null || convData.isEmpty()) {
        return;
    }
    for (UnderlyingData data : convData) {
        if (data.type != MailItem.Type.CONVERSATION.toByte()) {
            throw ServiceException.FAILURE("attempting to complete a non-conversation", null);
        }
    }
    Map<Integer, UnderlyingData> conversations = new HashMap<Integer, UnderlyingData>(Db.getINClauseBatchSize() * 3 / 2);
    PreparedStatement stmt = null;
    ResultSet rs = null;
    for (int i = 0; i < convData.size(); i += Db.getINClauseBatchSize()) {
        try {
            int count = Math.min(Db.getINClauseBatchSize(), convData.size() - i);
            stmt = conn.prepareStatement("SELECT parent_id, unread, flags, tag_names" + " FROM " + getMailItemTableName(mbox) + " WHERE " + IN_THIS_MAILBOX_AND + DbUtil.whereIn("parent_id", count));
            int pos = 1;
            pos = setMailboxId(stmt, mbox, pos);
            for (int index = i; index < i + count; index++) {
                UnderlyingData data = convData.get(index);
                stmt.setInt(pos++, data.id);
                conversations.put(data.id, data);
                // don't assume that the UnderlyingData structure was new...
                data.unreadCount = 0;
                data.setFlags(0);
                data.setTags(null);
            }
            rs = stmt.executeQuery();
            HashMultimap<Integer, String> convTags = HashMultimap.create();
            while (rs.next()) {
                Integer convId = rs.getInt(1);
                UnderlyingData data = conversations.get(convId);
                assert data != null;
                data.unreadCount += rs.getInt(2);
                data.setFlags(data.getFlags() | rs.getInt(3));
                String[] tags = DbTag.deserializeTags(rs.getString(4));
                if (tags != null) {
                    convTags.putAll(convId, Arrays.asList(tags));
                }
            }
            for (Map.Entry<Integer, Collection<String>> entry : convTags.asMap().entrySet()) {
                UnderlyingData data = conversations.get(entry.getKey());
                if (data != null) {
                    data.setTags(new Tag.NormalizedTags(entry.getValue()));
                }
            }
        } catch (SQLException e) {
            throw ServiceException.FAILURE("completing conversation data", e);
        } finally {
            DbPool.closeResults(rs);
            DbPool.closeStatement(stmt);
        }
        conversations.clear();
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) UnderlyingData(com.zimbra.cs.mailbox.MailItem.UnderlyingData) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet) Collection(java.util.Collection) Tag(com.zimbra.cs.mailbox.Tag) Map(java.util.Map) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap)

Aggregations

UnderlyingData (com.zimbra.cs.mailbox.MailItem.UnderlyingData)30 PreparedStatement (java.sql.PreparedStatement)21 SQLException (java.sql.SQLException)21 ResultSet (java.sql.ResultSet)20 DbConnection (com.zimbra.cs.db.DbPool.DbConnection)18 ArrayList (java.util.ArrayList)13 Mailbox (com.zimbra.cs.mailbox.Mailbox)8 Tag (com.zimbra.cs.mailbox.Tag)4 ServiceException (com.zimbra.common.service.ServiceException)3 MailItem (com.zimbra.cs.mailbox.MailItem)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Color (com.zimbra.common.mailbox.Color)2 BufferStream (com.zimbra.common.util.BufferStream)2 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)2 ZSharedFileInputStream (com.zimbra.common.zmime.ZSharedFileInputStream)2 DbMailItem (com.zimbra.cs.db.DbMailItem)2 Folder (com.zimbra.cs.mailbox.Folder)2 ArchiveInputEntry (com.zimbra.cs.service.formatter.ArchiveFormatter.ArchiveInputEntry)2 ArchiveInputStream (com.zimbra.cs.service.formatter.ArchiveFormatter.ArchiveInputStream)2