Search in sources :

Example 6 with SortBy

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

the class SearchConv method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Mailbox mbox = getRequestedMailbox(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
    SearchConvRequest req = JaxbUtil.elementToJaxb(request);
    boolean nest = ZmBoolean.toBool(req.getNestMessages(), false);
    Account acct = getRequestedAccount(zsc);
    SearchParams params = SearchParams.parse(req, zsc, acct.getPrefMailInitialSearch());
    // append (conv:(convid)) onto the beginning of the queryStr
    ItemId cid = new ItemId(req.getConversationId(), zsc);
    params.setQueryString("conv:\"" + cid.toString(ifmt) + "\" (" + params.getQueryString() + ')');
    // force to group-by-message
    params.setTypes(EnumSet.of(MailItem.Type.MESSAGE));
    Element response = null;
    if (cid.belongsTo(mbox)) {
        // local
        ZimbraQueryResults results = mbox.index.search(zsc.getResponseProtocol(), octxt, params);
        try {
            response = zsc.createElement(MailConstants.SEARCH_CONV_RESPONSE);
            response.addAttribute(MailConstants.A_QUERY_OFFSET, Integer.toString(params.getOffset()));
            SortBy sort = results.getSortBy();
            response.addAttribute(MailConstants.A_SORTBY, sort.toString());
            List<Message> msgs = mbox.getMessagesByConversation(octxt, cid.getId(), sort, -1);
            if (msgs.isEmpty() && zsc.isDelegatedRequest()) {
                throw ServiceException.PERM_DENIED("you do not have sufficient permissions");
            }
            // filter out IMAP \Deleted messages from the message lists
            Conversation conv = mbox.getConversationById(octxt, cid.getId());
            if (conv.isTagged(Flag.FlagInfo.DELETED)) {
                List<Message> raw = msgs;
                msgs = new ArrayList<Message>();
                for (Message msg : raw) {
                    if (!msg.isTagged(Flag.FlagInfo.DELETED)) {
                        msgs.add(msg);
                    }
                }
            }
            Element container = nest ? ToXML.encodeConversationSummary(response, ifmt, octxt, conv, CONVERSATION_FIELD_MASK) : response;
            SearchResponse builder = new SearchResponse(zsc, octxt, container, params);
            builder.setAllRead(conv.getUnreadCount() == 0);
            boolean more = putHits(octxt, ifmt, builder, msgs, results, params, conv);
            response.addAttribute(MailConstants.A_QUERY_MORE, more);
            // call me AFTER putHits since some of the <info> is generated by the getting of the hits!
            builder.add(results.getResultInfo());
        } finally {
            Closeables.closeQuietly(results);
        }
        return response;
    } else {
        // remote
        try {
            Element proxyRequest = zsc.createElement(MailConstants.SEARCH_CONV_REQUEST);
            Account target = Provisioning.getInstance().get(AccountBy.id, cid.getAccountId(), zsc.getAuthToken());
            if (target != null) {
                params.setInlineRule(params.getInlineRule().toLegacyExpandResults(target.getServer()));
            }
            params.encodeParams(proxyRequest);
            proxyRequest.addAttribute(MailConstants.A_NEST_MESSAGES, nest);
            proxyRequest.addAttribute(MailConstants.A_CONV_ID, cid.toString());
            // okay, lets run the search through the query parser -- this has the side-effect of
            // re-writing the query in a format that is OK to proxy to the other server -- since the
            // query has an "AND conv:remote-conv-id" part, the query parser will figure out the right
            // format for us.  TODO somehow make this functionality a bit more exposed in the
            // ZimbraQuery APIs...
            String rewrittenQueryString = mbox.getRewrittenQueryString(octxt, params);
            proxyRequest.addAttribute(MailConstants.E_QUERY, rewrittenQueryString, Element.Disposition.CONTENT);
            // proxy to remote account
            response = proxyRequest(proxyRequest, context, target.getId());
            return response.detach();
        } catch (SoapFaultException e) {
            throw ServiceException.FAILURE("SoapFaultException: ", e);
        }
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) SearchParams(com.zimbra.cs.index.SearchParams) Message(com.zimbra.cs.mailbox.Message) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) SortBy(com.zimbra.cs.index.SortBy) Element(com.zimbra.common.soap.Element) Conversation(com.zimbra.cs.mailbox.Conversation) ItemId(com.zimbra.cs.service.util.ItemId) SoapFaultException(com.zimbra.common.soap.SoapFaultException) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) ZimbraQueryResults(com.zimbra.cs.index.ZimbraQueryResults) SearchConvRequest(com.zimbra.soap.mail.message.SearchConvRequest)

Aggregations

SortBy (com.zimbra.cs.index.SortBy)6 ArrayList (java.util.ArrayList)4 ZimbraQueryResults (com.zimbra.cs.index.ZimbraQueryResults)3 Mailbox (com.zimbra.cs.mailbox.Mailbox)3 Element (com.zimbra.common.soap.Element)2 SearchParams (com.zimbra.cs.index.SearchParams)2 Mountpoint (com.zimbra.cs.mailbox.Mountpoint)2 OperationContext (com.zimbra.cs.mailbox.OperationContext)2 ItemId (com.zimbra.cs.service.util.ItemId)2 ItemIdFormatter (com.zimbra.cs.service.util.ItemIdFormatter)2 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)2 HashMap (java.util.HashMap)2 ZMailbox (com.zimbra.client.ZMailbox)1 ServiceException (com.zimbra.common.service.ServiceException)1 SoapFaultException (com.zimbra.common.soap.SoapFaultException)1 Account (com.zimbra.cs.account.Account)1 AccountServiceException (com.zimbra.cs.account.AccountServiceException)1 ImapMessageSet (com.zimbra.cs.imap.ImapMessage.ImapMessageSet)1 ReSortingQueryResults (com.zimbra.cs.index.ReSortingQueryResults)1 ZimbraHit (com.zimbra.cs.index.ZimbraHit)1