Search in sources :

Example 1 with ContactHit

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

the class ContactAutoComplete method queryFolders.

private void queryFolders(String str, String generatedQuery, Map<ItemId, Mountpoint> mountpoints, int limit, AutoCompleteResult result) throws ServiceException {
    ZimbraQueryResults qres = null;
    try {
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(getRequestedAcctId());
        SearchParams params = new SearchParams();
        params.setQueryString(generatedQuery);
        params.setDefaultField("contact:");
        params.setTypes(CONTACT_TYPES);
        params.setSortBy(SortBy.NONE);
        params.setLimit(limit + 1);
        params.setPrefetch(true);
        params.setFetchMode(SearchParams.Fetch.NORMAL);
        ZimbraLog.gal.debug("querying contact folders: %s", params.getQueryString());
        qres = mbox.index.search(SoapProtocol.Soap12, octxt, params);
        while (qres.hasNext()) {
            ZimbraHit hit = qres.getNext();
            Map<String, String> fields = null;
            ItemId id = null;
            int fid = 0;
            if (hit instanceof ContactHit) {
                Contact c = ((ContactHit) hit).getContact();
                ZimbraLog.gal.debug("hit: %d", c.getId());
                fields = c.getFields();
                id = new ItemId(c);
                fid = c.getFolderId();
                if (returnFullContactData) {
                    List<Attachment> contactAttachments = c.getAttachments();
                    if (contactAttachments != null && contactAttachments.size() != 0) {
                        fields.put("image", c.getId() + "_" + contactAttachments.get(0).getName());
                    }
                }
            } else if (hit instanceof ProxiedHit) {
                fields = new HashMap<String, String>();
                Element top = ((ProxiedHit) hit).getElement();
                id = new ItemId(top.getAttribute(MailConstants.A_ID), (String) null);
                ZimbraLog.gal.debug("hit: %s", id);
                ItemId fiid = new ItemId(top.getAttribute(MailConstants.A_FOLDER), (String) null);
                Mountpoint mp = mountpoints.get(fiid);
                if (mp != null) {
                    // if the hit came from a descendant folder of
                    // the mountpoint, we don't have a peer folder ID.
                    fid = mp.getId();
                } else {
                    fid = FOLDER_ID_MOUNTPOINT_SUBFOLDER;
                }
                for (Element elt : top.listElements(MailConstants.E_ATTRIBUTE)) {
                    try {
                        String name = elt.getAttribute(MailConstants.A_ATTRIBUTE_NAME);
                        fields.put(name, elt.getText());
                    } catch (ServiceException se) {
                        ZimbraLog.gal.warn("error handling proxied query result " + hit);
                    }
                }
                if (returnFullContactData) {
                    if (fields.containsKey("image")) {
                        fields.put("image", id.getAccountId() + "_" + id.getId() + "_image");
                    }
                }
            } else {
                continue;
            }
            addMatchedContacts(str, fields, fid, id, result);
            if (!result.canBeCached) {
                return;
            }
        }
    } finally {
        Closeables.closeQuietly(qres);
    }
}
Also used : ZimbraHit(com.zimbra.cs.index.ZimbraHit) GalSearchParams(com.zimbra.cs.gal.GalSearchParams) SearchParams(com.zimbra.cs.index.SearchParams) HashMap(java.util.HashMap) Element(com.zimbra.common.soap.Element) Attachment(com.zimbra.cs.mailbox.Contact.Attachment) ItemId(com.zimbra.cs.service.util.ItemId) GalContact(com.zimbra.cs.account.GalContact) ProxiedHit(com.zimbra.cs.index.ProxiedHit) ContactHit(com.zimbra.cs.index.ContactHit) ServiceException(com.zimbra.common.service.ServiceException) ZimbraQueryResults(com.zimbra.cs.index.ZimbraQueryResults)

Example 2 with ContactHit

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

the class SearchResponse method add.

/* We need to pass in a boolean signifying whether to expand the message or not (bug 75990)
    */
void add(ZimbraHit hit, boolean expandMsg) throws ServiceException {
    Element el = null;
    if (params.getFetchMode() == SearchParams.Fetch.IDS) {
        if (hit instanceof ConversationHit) {
            // need to expand the contained messages
            el = element.addElement("hit");
            el.addAttribute(MailConstants.A_ID, ifmt.formatItemId(hit.getParsedItemID()));
        } else {
            el = element.addElement("hit");
            el.addAttribute(MailConstants.A_ID, ifmt.formatItemId(hit.getParsedItemID()));
        }
    } else if (hit instanceof ProxiedHit) {
        element.addElement(((ProxiedHit) hit).getElement().detach());
        size++;
        return;
    } else {
        if (hit instanceof ConversationHit) {
            el = add((ConversationHit) hit);
        } else if (hit instanceof MessageHit) {
            el = add((MessageHit) hit, expandMsg);
        } else if (hit instanceof MessagePartHit) {
            el = add((MessagePartHit) hit);
        } else if (hit instanceof ContactHit) {
            el = add((ContactHit) hit);
        } else if (hit instanceof NoteHit) {
            el = add((NoteHit) hit);
        } else if (hit instanceof CalendarItemHit) {
            // el could be null
            el = add((CalendarItemHit) hit);
        } else if (hit instanceof DocumentHit) {
            el = add((DocumentHit) hit);
        } else {
            LOG.error("Got an unknown hit type putting search hits: " + hit);
            return;
        }
    }
    if (el != null) {
        size++;
        el.addAttribute(MailConstants.A_SORT_FIELD, hit.getSortField(sortOrder).toString());
        if (includeMailbox) {
            el.addAttribute(MailConstants.A_ID, new ItemId(hit.getAcctIdStr(), hit.getItemId()).toString());
        }
    }
}
Also used : NoteHit(com.zimbra.cs.index.NoteHit) MessagePartHit(com.zimbra.cs.index.MessagePartHit) ProxiedHit(com.zimbra.cs.index.ProxiedHit) ConversationHit(com.zimbra.cs.index.ConversationHit) ContactHit(com.zimbra.cs.index.ContactHit) DocumentHit(com.zimbra.cs.index.DocumentHit) CalendarItemHit(com.zimbra.cs.index.CalendarItemHit) Element(com.zimbra.common.soap.Element) MessageHit(com.zimbra.cs.index.MessageHit) ItemId(com.zimbra.cs.service.util.ItemId)

Example 3 with ContactHit

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

the class GalSearchControl method doLocalGalAccountSearch.

private boolean doLocalGalAccountSearch(Account galAcct) {
    ZimbraQueryResults zqr = null;
    try {
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(galAcct);
        SearchParams searchParams = mParams.getSearchParams();
        zqr = mbox.index.search(SoapProtocol.Soap12, new OperationContext(mbox), searchParams);
        ResultsPager pager = ResultsPager.create(zqr, searchParams);
        GalSearchResultCallback callback = mParams.getResultCallback();
        int num = 0;
        while (pager.hasNext()) {
            ZimbraHit hit = pager.getNextHit();
            if (hit instanceof ContactHit) {
                Element contactElem = callback.handleContact(((ContactHit) hit).getContact());
                if (contactElem != null)
                    contactElem.addAttribute(MailConstants.A_SORT_FIELD, hit.getSortField(pager.getSortOrder()).toString());
            }
            num++;
            if (num == mParams.getLimit())
                break;
        }
        callback.setSortBy(zqr.getSortBy().toString());
        callback.setQueryOffset(searchParams.getOffset());
        callback.setHasMoreResult(pager.hasNext());
    } catch (Exception e) {
        ZimbraLog.gal.warn("search on GalSync account failed for %s", galAcct.getId(), e);
        return false;
    } finally {
        Closeables.closeQuietly(zqr);
    }
    return true;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ZimbraHit(com.zimbra.cs.index.ZimbraHit) SearchParams(com.zimbra.cs.index.SearchParams) Mailbox(com.zimbra.cs.mailbox.Mailbox) ContactHit(com.zimbra.cs.index.ContactHit) ResultsPager(com.zimbra.cs.index.ResultsPager) Element(com.zimbra.common.soap.Element) ZimbraQueryResults(com.zimbra.cs.index.ZimbraQueryResults) SoapFaultException(com.zimbra.common.soap.SoapFaultException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException)

Example 4 with ContactHit

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

the class AddressObject method getMatchingHit.

/**
     * There could be multiple contacts with the same UID from different collections.
     * Due to an old CardDAV bug, it is also possible there may be more than one contact with the same UID in
     * the same collection.  Using "preferredBaseName" ensures that CardDAV clients can still do an
     * update in that case.
     *
     * @param zqr
     * @param folderId
     * @param preferredBaseName If more than one item matches, prefer one matching this name - can be null
     *        Ignored if folderId < 0
     * @return
     * @throws ServiceException
     */
private static Contact getMatchingHit(ZimbraQueryResults zqr, int folderId, String preferredBaseName) throws ServiceException {
    Contact item = null;
    Contact firstMatchingItem = null;
    while (zqr.hasNext()) {
        ZimbraHit hit = zqr.getNext();
        if (hit instanceof ContactHit) {
            item = ((ContactHit) hit).getContact();
            if (folderId < 0) {
                break;
            }
            if (item.getFolderId() == folderId) {
                if (firstMatchingItem == null) {
                    firstMatchingItem = item;
                }
                if (preferredBaseName != null) {
                    String contactBaseName = VCard.getUrl(item) + AddressObject.VCARD_EXTENSION;
                    if (!preferredBaseName.equals(contactBaseName)) {
                        item = null;
                        continue;
                    }
                }
                break;
            }
            item = null;
        }
    }
    return item != null ? item : firstMatchingItem;
}
Also used : ZimbraHit(com.zimbra.cs.index.ZimbraHit) ContactHit(com.zimbra.cs.index.ContactHit) Contact(com.zimbra.cs.mailbox.Contact)

Aggregations

ContactHit (com.zimbra.cs.index.ContactHit)4 Element (com.zimbra.common.soap.Element)3 ZimbraHit (com.zimbra.cs.index.ZimbraHit)3 ServiceException (com.zimbra.common.service.ServiceException)2 ProxiedHit (com.zimbra.cs.index.ProxiedHit)2 SearchParams (com.zimbra.cs.index.SearchParams)2 ZimbraQueryResults (com.zimbra.cs.index.ZimbraQueryResults)2 ItemId (com.zimbra.cs.service.util.ItemId)2 SoapFaultException (com.zimbra.common.soap.SoapFaultException)1 GalContact (com.zimbra.cs.account.GalContact)1 GalSearchParams (com.zimbra.cs.gal.GalSearchParams)1 CalendarItemHit (com.zimbra.cs.index.CalendarItemHit)1 ConversationHit (com.zimbra.cs.index.ConversationHit)1 DocumentHit (com.zimbra.cs.index.DocumentHit)1 MessageHit (com.zimbra.cs.index.MessageHit)1 MessagePartHit (com.zimbra.cs.index.MessagePartHit)1 NoteHit (com.zimbra.cs.index.NoteHit)1 ResultsPager (com.zimbra.cs.index.ResultsPager)1 Contact (com.zimbra.cs.mailbox.Contact)1 Attachment (com.zimbra.cs.mailbox.Contact.Attachment)1