Example usage for javax.mail.search AndTerm AndTerm

List of usage examples for javax.mail.search AndTerm AndTerm

Introduction

In this page you can find the example usage for javax.mail.search AndTerm AndTerm.

Prototype

public AndTerm(SearchTerm[] t) 

Source Link

Document

Constructor that takes an array of SearchTerms.

Usage

From source file:com.sonicle.webtop.mail.bol.model.ImapQuery.java

public static SearchTerm toSearchTerm(String allFlagStrings[], List<Tag> atags, QueryObj query,
        DateTimeZone timezone) {//from w  w  w  .j  a va 2 s . co  m
    SearchTerm searchTerm = null;
    ArrayList<SearchTerm> terms = new ArrayList<SearchTerm>();

    if (query != null) {
        ArrayList<Condition> conditionsList = query.conditions;
        String allText = query.allText;

        if (allText != null && allText.trim().length() > 0) {
            SearchTerm defaultterms[] = toDefaultSearchTerm(allText);
            terms.add(new OrTerm(defaultterms));
        }

        conditionsList.forEach(condition -> {
            String key = condition.keyword;
            String value = condition.value;

            if (key.equals(FROM)) {
                terms.add(new FromStringTerm(value));
            } else if (key.equals(TO)) {
                terms.add(new RecipientStringTerm(Message.RecipientType.TO, value));
            } else if (key.equals(SUBJECT)) {
                terms.add(new SubjectTerm(value));
            } else if (key.equals(MESSAGE)) {
                terms.add(new BodyTerm(value));
            } else if (key.equals(EVERYWHERE)) {
                SearchTerm anyterms[] = toAnySearchTerm(value);
                terms.add(new OrTerm(anyterms));
            } else if (key.equals(AFTER)) {
                Date afterDate = ImapQuery.parseDate(value, timezone);
                terms.add(new ReceivedDateTerm(DateTerm.GE, afterDate));
                terms.add(new SentDateTerm(DateTerm.GE, afterDate));
            } else if (key.equals(BEFORE)) {
                Date beforeDate = ImapQuery.parseDate(value, timezone);
                terms.add(new ReceivedDateTerm(DateTerm.LE, beforeDate));
                terms.add(new SentDateTerm(DateTerm.LE, beforeDate));

            } else if (value.equals(ATTACHMENT)) {

            } else if (value.equals(UNREAD)) {
                terms.add(new FlagTerm(new Flags(Flag.SEEN), false));
            } else if (value.equals(FLAGGED)) {
                FlagTerm fts[] = new FlagTerm[allFlagStrings.length + 1];
                fts[0] = new FlagTerm(new Flags(Flag.FLAGGED), true);
                for (int i = 0; i < allFlagStrings.length; ++i)
                    fts[i + 1] = new FlagTerm(new Flags(allFlagStrings[i]), true);
                terms.add(new OrTerm(fts));
            } else if (value.equals(TAGGED)) {
                FlagTerm fts[] = new FlagTerm[atags.size()];
                int i = 0;
                for (Tag tag : atags)
                    fts[i++] = new FlagTerm(new Flags(tag.getTagId()), true);
                terms.add(new OrTerm(fts));
            } else if (value.equals(UNANSWERED)) {
                terms.add(new FlagTerm(new Flags(Flag.ANSWERED), false));
            } else if (value.equals(PRIORITY)) {
                HeaderTerm p1 = new HeaderTerm("X-Priority", "1");
                HeaderTerm p2 = new HeaderTerm("X-Priority", "2");
                terms.add(new OrTerm(p1, p2));
            }

        });
    }

    int n = terms.size();
    if (n == 1) {
        searchTerm = terms.get(0);
    } else if (n > 1) {
        SearchTerm vterms[] = new SearchTerm[n];
        terms.toArray(vterms);
        searchTerm = new AndTerm(vterms);
    }
    return searchTerm;
}

From source file:com.cubusmail.server.mail.util.MessageUtils.java

private static SearchTerm createSearchTerm(MessageListFields searchField, String[] searchValues) {

    SearchTerm[] terms = new SearchTerm[searchValues.length];
    for (int i = 0; i < searchValues.length; i++) {
        if (searchField == MessageListFields.SUBJECT) {
            terms[i] = new SubjectTerm(searchValues[i]);
        } else if (searchField == MessageListFields.FROM) {
            terms[i] = new FromStringTerm(searchValues[i]);
        } else if (searchField == MessageListFields.TO) {
            terms[i] = new RecipientStringTerm(RecipientType.TO, searchValues[i]);
        } else if (searchField == MessageListFields.CC) {
            terms[i] = new RecipientStringTerm(RecipientType.CC, searchValues[i]);
        } else if (searchField == MessageListFields.CONTENT) {
            terms[i] = new BodyTerm(searchValues[i]);
        } else {//from   w  w w .ja  v a2s  .com
            throw new IllegalArgumentException("Search field now allowed: " + searchField.name());
        }
    }

    if (searchValues.length > 1) {
        return new AndTerm(terms);
    } else {
        return terms[0];
    }
}

From source file:com.sonicle.webtop.mail.Service.java

public void processRunSmartSearch(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
    try {/*from   w  ww  .  j  a va 2 s . c o  m*/
        if (sst != null && sst.isRunning())
            sst.cancel();

        UserProfile profile = environment.getProfile();
        MailAccount account = getAccount(request);
        String pattern = ServletUtils.getStringParameter(request, "pattern", true);
        String folder = ServletUtils.getStringParameter(request, "folder", false);
        boolean trashspam = ServletUtils.getBooleanParameter(request, "trashspam", false);
        boolean fromme = ServletUtils.getBooleanParameter(request, "fromme", false);
        boolean tome = ServletUtils.getBooleanParameter(request, "tome", false);
        boolean attachments = ServletUtils.getBooleanParameter(request, "attachments", false);
        int year = ServletUtils.getIntParameter(request, "year", 0);
        int month = ServletUtils.getIntParameter(request, "month", 0);
        int day = ServletUtils.getIntParameter(request, "day", 0);
        ArrayList<String> ispersonfilters = ServletUtils.getStringParameters(request, "ispersonfilters");
        ArrayList<String> isnotpersonfilters = ServletUtils.getStringParameters(request, "isnotpersonfilters");
        ArrayList<String> isfolderfilters = ServletUtils.getStringParameters(request, "isfolderfilters");
        ArrayList<String> isnotfolderfilters = ServletUtils.getStringParameters(request, "isnotfolderfilters");
        Set<String> _folderIds = account.getFolderCacheKeys();

        ArrayList<SearchTerm> terms = new ArrayList<>();
        SearchTerm searchTerm = null;

        //sort folders, placing first interesting ones
        ArrayList<String> folderIds = new ArrayList<>();
        Collections.sort(folderIds);
        String firstFolders[] = { account.getInboxFolderFullName(), account.getFolderSent() };
        for (String folderId : firstFolders)
            folderIds.add(folderId);
        for (String folderId : _folderIds) {

            //if folder selected, look only under that folder
            if (folder != null && folder.trim().length() > 0) {
                if (!folder.equals(folderId) && !account.isUnderFolder(folder, folderId))
                    continue;
            } else {
                //else skip shared
                if (account.isUnderSharedFolder(folderId))
                    continue;
            }

            //skip trash & spam unless selected
            if (!trashspam && (account.isTrashFolder(folderId) || account.isSpamFolder(folderId)))
                continue;

            boolean skip = false;
            for (String skipfolder : firstFolders) {
                if (skipfolder.equals(folderId)) {
                    skip = true;
                    break;
                }
            }
            if (!skip)
                folderIds.add(folderId);
        }

        terms.add(new OrTerm(ImapQuery.toAnySearchTerm(pattern)));
        int n = terms.size();

        if (n == 1)
            searchTerm = terms.get(0);
        else if (n > 1) {
            SearchTerm vterms[] = new SearchTerm[n];
            terms.toArray(vterms);
            searchTerm = new AndTerm(vterms);
        }

        sst = new SmartSearchThread(this, account, folderIds, fromme, tome, attachments, ispersonfilters,
                isnotpersonfilters, isfolderfilters, isnotfolderfilters, year, month, day, searchTerm, false);
        sst.start();
        new JsonResult().printTo(out);
    } catch (Exception exc) {
        Service.logger.error("Exception", exc);
        new JsonResult(false, exc.getMessage()).printTo(out);
    }
}

From source file:com.sonicle.webtop.mail.Service.java

public void processPortletRunSearch(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
    try {/*from  w w w  .j  a  va  2s.c  o m*/
        UserProfile profile = environment.getProfile();
        boolean trash = false;
        boolean spam = false;
        ArrayList<SearchTerm> terms = new ArrayList<SearchTerm>();
        SearchTerm searchTerm = null;

        if (pst != null && pst.isRunning())
            pst.cancel();

        MailAccount account = getAccount(request);

        String pattern = ServletUtils.getStringParameter(request, "pattern", true);

        Set<String> _folderIds = account.getFolderCacheKeys();

        //sort folders, placing first interesting ones
        ArrayList<String> folderIds = new ArrayList<>();
        Collections.sort(folderIds);
        String firstFolders[] = { account.getInboxFolderFullName(), account.getFolderSent() };
        for (String folderId : firstFolders)
            folderIds.add(folderId);
        for (String folderId : _folderIds) {

            if (account.isUnderSharedFolder(folderId))
                continue;
            //skip trash & spam unless selected
            if (!trash && account.isTrashFolder(folderId))
                continue;
            if (!spam && account.isSpamFolder(folderId))
                continue;

            folderIds.add(folderId);
        }

        terms.add(new OrTerm(ImapQuery.toAnySearchTerm(pattern)));
        int n = terms.size();

        if (n == 1)
            searchTerm = terms.get(0);
        else if (n > 1) {
            SearchTerm vterms[] = new SearchTerm[n];
            terms.toArray(vterms);
            searchTerm = new AndTerm(vterms);
        }
        pst = new PortletSearchThread(this, account, folderIds, searchTerm, false);
        pst.start();
        new JsonResult().printTo(out);
    } catch (Exception exc) {
        Service.logger.error("Exception", exc);
        new JsonResult(false, exc.getMessage()).printTo(out);
    }
}

From source file:org.exist.xquery.modules.mail.MessageListFunctions.java

private SearchTerm parseSearchTerms(Node terms) throws XPathException {
    SearchTerm st = null;//ww w  .jav  a2s . co m

    if (terms.getNodeType() == Node.ELEMENT_NODE && terms.getLocalName().equalsIgnoreCase("searchTerm")) {
        String type = ((Element) terms).getAttribute("type");

        if (type != null) {
            if (type.equalsIgnoreCase("not")) {
                st = new NotTerm(parseChildSearchTerm(terms));
            } else if (type.equalsIgnoreCase("and")) {
                st = new AndTerm(parseChildSearchTerms(terms));
            } else if (type.equalsIgnoreCase("or")) {
                st = new OrTerm(parseChildSearchTerms(terms));
            } else if (type.equalsIgnoreCase("from")) {
                st = parseFromTerm(terms);
            } else if (type.equalsIgnoreCase("subject")) {
                st = parseSubjectTerm(terms);
            } else if (type.equalsIgnoreCase("body")) {
                st = parseBodyTerm(terms);
            } else if (type.equalsIgnoreCase("to") || type.equalsIgnoreCase("recipient")) {
                st = parseRecipientTerm(terms);
            } else if (type.equalsIgnoreCase("header")) {
                st = parseHeaderTerm(terms);
            } else if (type.equalsIgnoreCase("flag")) {
                st = parseFlagTerm(terms);
            } else if (type.equalsIgnoreCase("sent")) {
                st = parseSentDateTerm(terms);
            } else if (type.equalsIgnoreCase("received")) {
                st = parseReceivedDateTerm(terms);
            } else {
                throw (new XPathException(this, "Invalid Search Term type specified: " + type));
            }
        } else {
            throw (new XPathException(this, "Invalid Search Term type specified: null"));
        }
    }

    if (st == null) {
        throw (new XPathException(this, "Invalid Search Terms specified"));
    }

    return (st);
}