Search in sources :

Example 1 with Cursor

use of com.zimbra.client.ZSearchParams.Cursor in project zm-mailbox by Zimbra.

the class ZMailbox method internalSearch.

// ------------------------
private synchronized ZSearchResult internalSearch(String convId, ZSearchParams params, boolean nest) throws ServiceException {
    QName name;
    if (convId != null) {
        name = MailConstants.SEARCH_CONV_REQUEST;
    } else if (params.getTypes().equals(ZSearchParams.TYPE_VOICE_MAIL) || params.getTypes().equals(ZSearchParams.TYPE_CALL)) {
        name = VoiceConstants.SEARCH_VOICE_REQUEST;
    } else if (params.getTypes().equals(ZSearchParams.TYPE_GAL)) {
        name = AccountConstants.SEARCH_GAL_REQUEST;
    } else {
        name = MailConstants.SEARCH_REQUEST;
    }
    Element req = newRequestElement(name);
    if (params.getTypes().equals(ZSearchParams.TYPE_GAL)) {
        req.addAttribute(AccountConstants.A_TYPE, GalEntryType.account.name());
        req.addElement(AccountConstants.E_NAME).setText(params.getQuery());
    //req.addAttribute(MailConstants.A_SORTBY, SearchSortBy.nameAsc.name());
    }
    req.addAttribute(MailConstants.A_CONV_ID, convId);
    if (nest) {
        req.addAttribute(MailConstants.A_NEST_MESSAGES, true);
    }
    if (params.getLimit() != 0) {
        req.addAttribute(MailConstants.A_QUERY_LIMIT, params.getLimit());
    }
    if (params.getOffset() != 0) {
        req.addAttribute(MailConstants.A_QUERY_OFFSET, params.getOffset());
    }
    if (params.getSortBy() != null) {
        req.addAttribute(MailConstants.A_SORTBY, params.getSortBy().name());
    }
    if (params.getTypes() != null) {
        req.addAttribute(MailConstants.A_SEARCH_TYPES, params.getTypes());
    }
    if (params.getFetch() != null && params.getFetch() != Fetch.none) {
        // use "1" for "first" for backward compat until DF is updated
        req.addAttribute(MailConstants.A_FETCH, params.getFetch() == Fetch.first ? "1" : params.getFetch().name());
    }
    if (params.getCalExpandInstStart() != 0) {
        req.addAttribute(MailConstants.A_CAL_EXPAND_INST_START, params.getCalExpandInstStart());
    }
    if (params.getCalExpandInstEnd() != 0) {
        req.addAttribute(MailConstants.A_CAL_EXPAND_INST_END, params.getCalExpandInstEnd());
    }
    if (params.isPreferHtml()) {
        req.addAttribute(MailConstants.A_WANT_HTML, params.isPreferHtml());
    }
    if (params.isMarkAsRead()) {
        req.addAttribute(MailConstants.A_MARK_READ, params.isMarkAsRead());
    }
    if (params.isRecipientMode()) {
        req.addAttribute(MailConstants.A_RECIPIENTS, params.isRecipientMode());
    }
    if (params.getField() != null) {
        req.addAttribute(MailConstants.A_FIELD, params.getField());
    }
    if (params.getInDumpster()) {
        req.addAttribute(MailConstants.A_IN_DUMPSTER, true);
    }
    req.addAttribute(MailConstants.E_QUERY, params.getQuery(), Element.Disposition.CONTENT);
    if (params.getCursor() != null) {
        Cursor cursor = params.getCursor();
        Element cursorEl = req.addElement(MailConstants.E_CURSOR);
        if (cursor.getPreviousId() != null) {
            cursorEl.addAttribute(MailConstants.A_ID, cursor.getPreviousId());
        }
        if (cursor.getPreviousSortValue() != null) {
            cursorEl.addAttribute(MailConstants.A_SORTVAL, cursor.getPreviousSortValue());
        }
    }
    if (params.getTypes().equals(ZSearchParams.TYPE_VOICE_MAIL) || params.getTypes().equals(ZSearchParams.TYPE_CALL)) {
        getAllPhoneAccounts();
        setVoiceStorePrincipal(req);
    }
    Element resp = invoke(req);
    if (params.getTypes().equals(ZSearchParams.TYPE_GAL)) {
        try {
            resp.getAttribute(MailConstants.A_SORTBY);
        } catch (Exception e) {
            resp.addAttribute(MailConstants.A_SORTBY, params.getSortBy().name());
        }
        try {
            resp.getAttribute(MailConstants.A_QUERY_OFFSET);
        } catch (Exception e) {
            resp.addAttribute(MailConstants.A_QUERY_OFFSET, params.getOffset());
        }
    }
    return new ZSearchResult(resp, nest, params.getTimeZone() != null ? params.getTimeZone() : getPrefs().getTimeZone());
}
Also used : QName(org.dom4j.QName) JSONElement(com.zimbra.common.soap.Element.JSONElement) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) Cursor(com.zimbra.client.ZSearchParams.Cursor) JSONException(org.json.JSONException) SoapFaultException(com.zimbra.common.soap.SoapFaultException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException) ZClientException(com.zimbra.common.zclient.ZClientException) URISyntaxException(java.net.URISyntaxException) RemoteServiceException(com.zimbra.common.service.RemoteServiceException)

Example 2 with Cursor

use of com.zimbra.client.ZSearchParams.Cursor in project zm-mailbox by Zimbra.

the class ZSearchContext method getNextHit.

/**
     *
     * @return next search hit in the results, or null.
     * @throws com.zimbra.common.service.ServiceException
     */
public ZSearchHit getNextHit() throws ServiceException {
    if (mCount < mMaxHits) {
        if (mResult == null || (mIndex + 1 >= mResult.getHits().size() && mResult.hasMore())) {
            if (mResult != null) {
                // get next page
                if (mByOffset) {
                    mParams.setOffset(mParams.getOffset() + mResult.getHits().size());
                } else {
                    List<ZSearchHit> hits = mResult.getHits();
                    ZSearchHit lastHit = hits.get(hits.size() - 1);
                    mParams.setCursor(new Cursor(lastHit.getId(), lastHit.getSortField()));
                }
            }
            // search it
            if (mParams.getConvId() != null)
                mResult = mMailbox.searchConversation(mParams.getConvId(), mParams);
            else
                mResult = mMailbox.search(mParams);
            mIndex = 0;
        } else {
            ++mIndex;
        }
    }
    if (mCount >= mMaxHits || mIndex >= mResult.getHits().size()) {
        mHasMore = false;
        return null;
    } else {
        ++mCount;
        mHasMore = mCount < mMaxHits && ((mIndex + 1 < mResult.getHits().size() || mResult.hasMore()));
        return mResult.getHits().get(mIndex);
    }
}
Also used : Cursor(com.zimbra.client.ZSearchParams.Cursor)

Example 3 with Cursor

use of com.zimbra.client.ZSearchParams.Cursor in project zm-mailbox by Zimbra.

the class ZSearchPager method search.

ZSearchResult search(ZMailbox mailbox, int page, boolean useCursor) throws ServiceException {
    while (mResults.size() <= page) {
        if (mResults.size() == 0) {
            mParams.setCursor(null);
            mParams.setOffset(0);
        } else {
            ZSearchResult lastResult = mResults.get(mResults.size() - 1);
            if (!lastResult.hasMore())
                break;
            if (useCursor) {
                List<ZSearchHit> lastHits = lastResult.getHits();
                ZSearchHit lastHit = lastHits.get(lastHits.size() - 1);
                mParams.setCursor(new Cursor(lastHit.getId(), lastHit.getSortField()));
            } else {
                mParams.setCursor(null);
                mParams.setOffset(page * mParams.getLimit());
            }
        }
        ZSearchResult result = mParams.getConvId() == null ? mailbox.search(mParams) : mailbox.searchConversation(mParams.getConvId(), mParams);
        mResults.add(result);
        if (result.getConversationSummary() != null) {
            mConvId = result.getConversationSummary().getId();
        }
        for (ZSearchHit hit : result.getHits()) mHitMap.put(hit.getId(), hit);
        if (!result.hasMore())
            break;
    }
    if (page < mResults.size())
        return mResults.get(page);
    else
        return mResults.get(mResults.size() - 1);
}
Also used : Cursor(com.zimbra.client.ZSearchParams.Cursor)

Example 4 with Cursor

use of com.zimbra.client.ZSearchParams.Cursor in project zm-mailbox by Zimbra.

the class ZSearchCache method fetchMoreHits.

private void fetchMoreHits(ZMailbox mailbox) throws ServiceException {
    if (!mHasMore)
        return;
    if (mLastHit == null) {
        mParams.setCursor(null);
        mParams.setOffset(0);
    } else {
        mParams.setCursor(new Cursor(mLastHit.getId(), mLastHit.getSortField()));
    }
    ZSearchResult result = mParams.getConvId() == null ? mailbox.search(mParams) : mailbox.searchConversation(mParams.getConvId(), mParams);
    int i = mHits.size();
    for (ZSearchHit hit : result.getHits()) {
        ZSearchCacheHit ch = new ZSearchCacheHit();
        ch.hit = hit;
        ch.index = i++;
        mHits.add(ch);
        mHitMap.put(hit.getId(), ch);
        mLastHit = hit;
    }
    mConvSummary = result.getConversationSummary();
    if (mConvSummary != null)
        mConvId = mConvSummary.getId();
    mHasMore = result.hasMore();
}
Also used : Cursor(com.zimbra.client.ZSearchParams.Cursor)

Example 5 with Cursor

use of com.zimbra.client.ZSearchParams.Cursor in project zm-mailbox by Zimbra.

the class TestSearchSortByDate method testMessagesCloseTimestamps.

@Test
public void testMessagesCloseTimestamps() {
    try {
        Mailbox recieverMbox = TestUtil.getMailbox(RECIPIENT);
        // - 3600*60*1000;
        long timestamp = System.currentTimeMillis();
        int numMessages = 2207;
        int firstLimit = 100;
        int incLimit = 50;
        int offset = 0;
        ArrayList<String> expectedIds = new ArrayList<String>();
        for (int i = 0; i < numMessages; i++) {
            Message msg = TestUtil.addMessage(recieverMbox, TestUtil.getAddress(RECIPIENT), TestUtil.getAddress(SENDER), NAME_PREFIX + " testing bug " + i, String.format("this message contains a search string %s which we are searching for and a number %d and timestamp %d", SEARCH_STRING, i, timestamp), timestamp);
            expectedIds.add(Integer.toString(msg.getId()));
        }
        for (int i = 0; i < numMessages; i++) {
            Message msg = TestUtil.addMessage(recieverMbox, TestUtil.getAddress(RECIPIENT), TestUtil.getAddress(SENDER), NAME_PREFIX + " testing bug " + i, "this message does not contains a search string which we are searching for", timestamp);
        }
        Collections.reverse(expectedIds);
        Thread.sleep(100);
        ZMailbox zmbx = TestUtil.getZMailbox(RECIPIENT);
        ZSearchParams searchParams = new ZSearchParams("in:inbox " + SEARCH_STRING);
        searchParams.setSortBy(SearchSortBy.dateDesc);
        searchParams.setLimit(firstLimit);
        searchParams.setTypes(ZSearchParams.TYPE_MESSAGE);
        List<ZMessage> resultSet = TestUtil.search(zmbx, searchParams);
        assertEquals(firstLimit, resultSet.size());
        int gotMessages = resultSet.size();
        List<String> seenIds = new ArrayList<String>();
        ZMessage lastHit = null;
        for (ZMessage m : resultSet) {
            seenIds.add(m.getId());
            lastHit = m;
        }
        int recCount = 1;
        offset += firstLimit;
        while (gotMessages > 0) {
            searchParams.setCursor((new Cursor(lastHit.getId(), Long.toString(lastHit.getReceivedDate()))));
            searchParams = new ZSearchParams("in:inbox " + SEARCH_STRING);
            searchParams.setSortBy(SearchSortBy.dateDesc);
            searchParams.setLimit(incLimit);
            searchParams.setTypes(ZSearchParams.TYPE_MESSAGE);
            searchParams.setOffset(offset);
            resultSet = TestUtil.search(zmbx, searchParams);
            recCount++;
            gotMessages = resultSet.size();
            int resCount = 0;
            for (ZMessage msg : resultSet) {
                assertFalse(String.format("Request %d, result %d, encountered duplicate ID %s. Previously seen at %d", recCount, resCount, msg.getId(), seenIds.indexOf(msg.getId())), seenIds.contains(msg.getId()));
                seenIds.add(msg.getId());
                resCount++;
                lastHit = msg;
            }
            // Thread.sleep(100); //jetty sometimes crashes on Mac when bombarded with request without a timeout
            offset += incLimit;
        }
        assertEquals("Returned incorrect number of messages", numMessages, seenIds.size());
    } catch (ServiceException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZMessage(com.zimbra.client.ZMessage) Message(com.zimbra.cs.mailbox.Message) ArrayList(java.util.ArrayList) Cursor(com.zimbra.client.ZSearchParams.Cursor) ServiceException(com.zimbra.common.service.ServiceException) ZMailbox(com.zimbra.client.ZMailbox) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) ServiceException(com.zimbra.common.service.ServiceException) ZSearchParams(com.zimbra.client.ZSearchParams) Test(org.junit.Test)

Aggregations

Cursor (com.zimbra.client.ZSearchParams.Cursor)8 ServiceException (com.zimbra.common.service.ServiceException)5 ZMailbox (com.zimbra.client.ZMailbox)4 ZMessage (com.zimbra.client.ZMessage)4 ZSearchParams (com.zimbra.client.ZSearchParams)4 Mailbox (com.zimbra.cs.mailbox.Mailbox)4 Message (com.zimbra.cs.mailbox.Message)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)2 RemoteServiceException (com.zimbra.common.service.RemoteServiceException)1 Element (com.zimbra.common.soap.Element)1 JSONElement (com.zimbra.common.soap.Element.JSONElement)1 XMLElement (com.zimbra.common.soap.Element.XMLElement)1 SoapFaultException (com.zimbra.common.soap.SoapFaultException)1 ZClientException (com.zimbra.common.zclient.ZClientException)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 Date (java.util.Date)1 QName (org.dom4j.QName)1