TagsetCache tagsets = getTagsetCache(conn, mbox.getId()); tagsets.addTagset(item.getTagBitmask());
if (tag instanceof Flag) { TagsetCache flagsets = getFlagsetCache(conn, mbox.getId()); flagsets.addTagset(item.getInternalFlagBitmask()); } else { TagsetCache tagsets = getTagsetCache(conn, mbox.getId()); tagsets.addTagset(item.getTagBitmask()); }
    public static void alterTag(MailItem item, Tag tag, boolean add) throws ServiceException {        Mailbox mbox = item.getMailbox();        if (mbox != tag.getMailbox())            throw MailServiceException.WRONG_MAILBOX();        if (tag.getId() == Flag.ID_FLAG_UNREAD)            throw ServiceException.FAILURE("unread state must be updated with alterUnread()", null);        Connection conn = mbox.getOperationConnection();        PreparedStatement stmt = null;        try {            String relation, column = (tag instanceof Flag ? "flags" : "tags");            if (item instanceof VirtualConversation)  relation = "id = ?";            else if (item instanceof Conversation)    relation = "parent_id = ?";            else if (item instanceof Folder)          relation = "folder_id = ?";            else if (item instanceof Flag)            relation = "flags & ?";            else if (item instanceof Tag)             relation = "tags & ?";            else                                      relation = "id = ?";            stmt = conn.prepareStatement("UPDATE " + getMailItemTableName(item) +                    " SET " + column + " = " + column + (add ? " | ?" : " & ?") + ", mod_metadata = ?" +                    " WHERE " + relation);            stmt.setLong(1, add ? tag.getBitmask() : ~tag.getBitmask());            stmt.setInt(2, mbox.getOperationChangeID());            if (item instanceof Tag)                stmt.setLong(3, ((Tag) item).getBitmask());            else if (item instanceof VirtualConversation)                stmt.setInt(3, ((VirtualConversation) item).getMessageId());            else                stmt.setInt(3, item.getId());            stmt.executeUpdate();                        TagsetCache tagsets = getTagsetCache(conn, mbox.getId());            tagsets.addTagset(item.getTagBitmask());        } catch (SQLException e) {            throw ServiceException.FAILURE("updating tag data for item " + item.getId(), e);        } finally {            DbPool.closeStatement(stmt);        }    }