Search in sources :

Example 61 with MailItem

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

the class ItemAction method handleTrashOperation.

protected String handleTrashOperation(OperationContext octxt, Element request, Mailbox mbox, SoapProtocol responseProto, List<Integer> local, MailItem.Type type, TargetConstraint tcon) throws ServiceException {
    String localResults;
    // determine if any of the items should be moved to an IMAP trash folder
    Map<String, LinkedList<Integer>> remoteTrashIds = new HashMap<String, LinkedList<Integer>>();
    LinkedList<Integer> localTrashIds = new LinkedList<Integer>();
    Map<String, String> msgToConvId = new HashMap<String, String>();
    MailItem[] items = mbox.getItemById(octxt, local, MailItem.Type.UNKNOWN);
    for (MailItem item : items) {
        String dsId = null;
        boolean doneDistributingTrash = false;
        if (item instanceof Message) {
            dsId = getDataSourceOfItem(octxt, mbox, item);
        } else if (item instanceof VirtualConversation) {
            VirtualConversation vConv = (VirtualConversation) item;
            Message msg = mbox.getMessageById(octxt, vConv.getMessageId());
            dsId = getDataSourceOfItem(octxt, mbox, msg);
        } else if (item instanceof Conversation) {
            Set<String> dsIds = new HashSet<String>();
            boolean hasLocal = false;
            List<Message> messages = mbox.getMessagesByConversation(octxt, item.getId());
            for (Message msg : messages) {
                String msgDsId = getDataSourceOfItem(octxt, mbox, msg);
                if (msgDsId == null) {
                    hasLocal = true;
                } else {
                    dsIds.add(msgDsId);
                }
            }
            // If it spans multiple accounts, delete it message-by-message
            if (dsIds.isEmpty() && hasLocal) {
                dsId = null;
            } else if (!hasLocal && dsIds.size() == 1) {
                dsId = dsIds.iterator().next();
            } else {
                type = MailItem.Type.MESSAGE;
                for (Message msg : messages) {
                    String msgDsId = getDataSourceOfItem(octxt, mbox, msg);
                    msgToConvId.put(String.valueOf(msg.getId()), String.valueOf(item.getId()));
                    if (msgDsId == null) {
                        localTrashIds.add(msg.getId());
                    } else {
                        LinkedList<Integer> idsInDs = remoteTrashIds.get(dsId);
                        if (idsInDs == null) {
                            idsInDs = new LinkedList<Integer>();
                            remoteTrashIds.put(msgDsId, idsInDs);
                        }
                        idsInDs.add(msg.getId());
                    }
                }
                doneDistributingTrash = true;
            }
        }
        if (!doneDistributingTrash) {
            if (dsId != null) {
                LinkedList<Integer> idsInDs = remoteTrashIds.get(dsId);
                if (idsInDs == null) {
                    idsInDs = new LinkedList<Integer>();
                    remoteTrashIds.put(dsId, idsInDs);
                }
                idsInDs.add(item.getId());
            } else {
                localTrashIds.add(item.getId());
            }
        }
    }
    // move non-IMAP items to local trash
    ItemId iidTrash = new ItemId(mbox, Mailbox.ID_FOLDER_TRASH);
    List<String> trashResults = new LinkedList<String>();
    String localTrashResults = ItemActionHelper.MOVE(octxt, mbox, responseProto, localTrashIds, type, tcon, iidTrash).getResult();
    if (!Strings.isNullOrEmpty(localTrashResults)) {
        trashResults.add(localTrashResults);
    }
    for (String dataSourceId : remoteTrashIds.keySet()) {
        List<Integer> imapTrashIds = remoteTrashIds.get(dataSourceId);
        Integer imapTrashId = getImapTrashFolder(mbox, dataSourceId);
        if (null == imapTrashId) {
            // e.g. for non-IMAP data sources like RSS feeds
            imapTrashId = Mailbox.ID_FOLDER_TRASH;
        }
        ItemId iidImapTrash = new ItemId(mbox, imapTrashId);
        String imapTrashResults = ItemActionHelper.MOVE(octxt, mbox, responseProto, imapTrashIds, type, tcon, iidImapTrash).getResult();
        if (!Strings.isNullOrEmpty(imapTrashResults)) {
            trashResults.add(imapTrashResults);
        }
    }
    localResults = Joiner.on(",").join(trashResults);
    if (!msgToConvId.isEmpty()) {
        String[] ids = localResults.split(",");
        Set<String> reconstructedConvIds = new HashSet<String>();
        for (String id : ids) {
            String convId = msgToConvId.get(id);
            if (convId != null) {
                reconstructedConvIds.add(convId);
            } else {
                reconstructedConvIds.add(id);
            }
        }
        localResults = Joiner.on(",").join(reconstructedConvIds);
    }
    return localResults;
}
Also used : Message(com.zimbra.cs.mailbox.Message) HashMap(java.util.HashMap) VirtualConversation(com.zimbra.cs.mailbox.VirtualConversation) Conversation(com.zimbra.cs.mailbox.Conversation) ItemId(com.zimbra.cs.service.util.ItemId) LinkedList(java.util.LinkedList) MailItem(com.zimbra.cs.mailbox.MailItem) VirtualConversation(com.zimbra.cs.mailbox.VirtualConversation) HashSet(java.util.HashSet)

Example 62 with MailItem

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

the class ParseMimeMessage method attachDocument.

private static void attachDocument(MimeMultipart mmp, String path, String contentID, ParseMessageContext ctxt) throws MessagingException, ServiceException {
    MailItem item = null;
    try {
        // first, just blindly try to fetch the item
        item = ctxt.mbox.getItemByPath(ctxt.octxt, path);
    } catch (NoSuchItemException nsie) {
    }
    if (item == null) {
        // on a miss, check for a mountpoint and, if so, fetch via UserServlet
        Pair<Folder, String> match = ctxt.mbox.getFolderByPathLongestMatch(ctxt.octxt, Mailbox.ID_FOLDER_USER_ROOT, path);
        if (!(match.getFirst() instanceof Mountpoint)) {
            throw MailServiceException.NO_SUCH_DOC(path);
        }
        Map<String, String> params = new HashMap<String, String>(3);
        params.put(UserServlet.QP_NAME, match.getSecond());
        attachRemoteItem(mmp, ((Mountpoint) match.getFirst()).getTarget(), contentID, ctxt, params, null);
        return;
    }
    // on a hit, attach it directly
    if (!(item instanceof Document))
        throw MailServiceException.NO_SUCH_DOC(path);
    attachDocument(mmp, (Document) item, contentID, ctxt);
}
Also used : MailItem(com.zimbra.cs.mailbox.MailItem) HashMap(java.util.HashMap) Folder(com.zimbra.cs.mailbox.Folder) Document(com.zimbra.cs.mailbox.Document) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) Mountpoint(com.zimbra.cs.mailbox.Mountpoint)

Example 63 with MailItem

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

the class TestConcurrency method tearDown.

@After
public void tearDown() throws Exception {
    // Delete tags - kept for testing purposes
    List<Tag> tagList = mMbox.getTagList(null);
    if (tagList != null) {
        Iterator<Tag> i = tagList.iterator();
        while (i.hasNext()) {
            Tag tag = i.next();
            if (tag.getName().startsWith(TAG_PREFIX)) {
                mMbox.delete(null, tag.getId(), tag.getType());
            }
        }
    }
    // Move items from temp folder back to inbox
    Folder folder = TestUtil.getFolderByPath(mMbox, FOLDER_NAME);
    if (folder != null) {
        List<MailItem> ids = mMbox.getItemList(null, MailItem.Type.MESSAGE, folder.getId());
        Iterator<MailItem> i = ids.iterator();
        while (i.hasNext()) {
            Message message = (Message) i.next();
            mMbox.move(null, message.getId(), MailItem.Type.MESSAGE, Mailbox.ID_FOLDER_INBOX);
        }
        mMbox.delete(null, folder.getId(), folder.getType());
    }
    cleanUp();
}
Also used : MailItem(com.zimbra.cs.mailbox.MailItem) Message(com.zimbra.cs.mailbox.Message) Tag(com.zimbra.cs.mailbox.Tag) Folder(com.zimbra.cs.mailbox.Folder) After(org.junit.After)

Example 64 with MailItem

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

the class GalSearchControl method doLocalGalAccountSync.

private void doLocalGalAccountSync(GalSearchResultCallback callback, Mailbox mbox, OperationContext octxt, int changeId, Set<Integer> folderIds, String syncToken, int limit, String filterAttr, String filterValue) throws ServiceException {
    ZimbraLog.gal.info("Using limit %d for gal account sync", limit);
    Pair<List<Integer>, TypedIdList> changed = mbox.getModifiedItems(octxt, changeId, 0, MailItem.Type.CONTACT, folderIds, -1, limit);
    int count = 0;
    boolean hasMore = false;
    for (int itemId : changed.getFirst()) {
        try {
            MailItem item = mbox.getItemById(octxt, itemId, MailItem.Type.CONTACT);
            if (item instanceof Contact) {
                Contact c = (Contact) item;
                if (filterAttr != null && !filterValue.equals(c.get(filterAttr))) {
                    continue;
                }
                callback.handleContact(c);
                count++;
                if (count % 100 == 0) {
                    ZimbraLog.gal.trace("processing #%s", count);
                }
                changeId = item.getModifiedSequence();
                if (count == limit) {
                    hasMore = true;
                    break;
                }
            }
        } catch (MailServiceException mse) {
            if (MailServiceException.NO_SUCH_ITEM.equals(mse.getId())) {
                ZimbraLog.gal.warn("skipping item %d due to no such item; probably deleted during sync", itemId, mse);
            } else {
                throw mse;
            }
        }
    }
    GalSyncToken newToken = new GalSyncToken(syncToken, mbox.getAccountId(), changeId);
    ZimbraLog.gal.debug("computing new sync token for %s:%s", mbox.getAccountId(), newToken);
    callback.setNewToken(newToken);
    callback.setHasMoreResult(hasMore);
}
Also used : MailItem(com.zimbra.cs.mailbox.MailItem) List(java.util.List) TypedIdList(com.zimbra.cs.mailbox.util.TypedIdList) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) TypedIdList(com.zimbra.cs.mailbox.util.TypedIdList) Contact(com.zimbra.cs.mailbox.Contact)

Example 65 with MailItem

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

the class ItemActionTest method deleteAllTagKeepsStatusOfFlags.

@Test
public void deleteAllTagKeepsStatusOfFlags() throws Exception {
    //Bug 76781
    Account acct = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
    acct.setMailThreadingAlgorithm(MailThreadingAlgorithm.subject);
    // setup: add the root message
    ParsedMessage pm = MailboxTestUtil.generateMessage("test subject");
    DeliveryOptions dopt = new DeliveryOptions().setFolderId(Mailbox.ID_FOLDER_INBOX);
    mbox.addMessage(null, pm, dopt, null);
    // add additional messages for conversation
    pm = MailboxTestUtil.generateMessage("Re: test subject");
    int msgId = mbox.addMessage(null, pm, dopt, null).getId();
    // set flag to unread for  this message
    MailboxTestUtil.setFlag(mbox, msgId, Flag.FlagInfo.UNREAD);
    MailItem item = mbox.getItemById(null, msgId, MailItem.Type.UNKNOWN);
    // verify message unread flag is set
    Assert.assertEquals("Verifying Unread flag is set.", Flag.BITMASK_UNREAD, item.getFlagBitmask());
    // add 2 tags
    mbox.alterTag(null, msgId, MailItem.Type.MESSAGE, tag1, true, null);
    mbox.alterTag(null, msgId, MailItem.Type.MESSAGE, tag2, true, null);
    Element request = new Element.XMLElement(MailConstants.ITEM_ACTION_REQUEST);
    Element action = request.addElement(MailConstants.E_ACTION);
    action.addAttribute(MailConstants.A_OPERATION, ItemAction.OP_UPDATE);
    action.addAttribute(MailConstants.A_ITEM_TYPE, "");
    action.addAttribute(MailConstants.A_ID, msgId);
    new ItemAction().handle(request, ServiceTestUtil.getRequestContext(acct));
    Assert.assertEquals("Verifying unread flag is set after tag deletion", Flag.BITMASK_UNREAD, item.getFlagBitmask());
    Tag tag = mbox.getTagByName(null, tag1);
    Assert.assertEquals(tag1 + " (tag messages)", 0, tag.getSize());
    tag = mbox.getTagByName(null, tag2);
    Assert.assertEquals(tag1 + " (tag messages)", 0, tag.getSize());
}
Also used : Account(com.zimbra.cs.account.Account) MailItem(com.zimbra.cs.mailbox.MailItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Element(com.zimbra.common.soap.Element) Tag(com.zimbra.cs.mailbox.Tag) DeliveryOptions(com.zimbra.cs.mailbox.DeliveryOptions) Test(org.junit.Test)

Aggregations

MailItem (com.zimbra.cs.mailbox.MailItem)74 Mailbox (com.zimbra.cs.mailbox.Mailbox)36 ServiceException (com.zimbra.common.service.ServiceException)30 Folder (com.zimbra.cs.mailbox.Folder)23 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)23 Message (com.zimbra.cs.mailbox.Message)19 Mountpoint (com.zimbra.cs.mailbox.Mountpoint)17 ArrayList (java.util.ArrayList)17 IOException (java.io.IOException)16 OperationContext (com.zimbra.cs.mailbox.OperationContext)15 NoSuchItemException (com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)14 Element (com.zimbra.common.soap.Element)13 CalendarItem (com.zimbra.cs.mailbox.CalendarItem)13 Account (com.zimbra.cs.account.Account)12 Document (com.zimbra.cs.mailbox.Document)12 ItemId (com.zimbra.cs.service.util.ItemId)11 HashMap (java.util.HashMap)11 ZMailbox (com.zimbra.client.ZMailbox)9 Contact (com.zimbra.cs.mailbox.Contact)9 HashSet (java.util.HashSet)9