Example usage for javax.mail.search SentDateTerm SentDateTerm

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

Introduction

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

Prototype

public SentDateTerm(int comparison, Date date) 

Source Link

Document

Constructor.

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) {/* www.  ja  va 2  s .  c  o 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.googlecode.gmail4j.javamail.ImapGmailClient.java

@Override
public GmailMessageList getMessagesBy(EmailSearchStrategy strategy, String value) {
    SearchTerm seekStrategy = null;//from   ww  w  .j  a v a  2  s  .  c o  m
    switch (strategy) {
    case SUBJECT:
        seekStrategy = new SubjectTerm(value);
        LOG.debug("Fetching emails with a subject of \"" + value + "\"");
        break;
    case TO:
        seekStrategy = new RecipientStringTerm(Message.RecipientType.TO, value);
        LOG.debug("Fetching emails sent to \"" + value + "\"");
        break;
    case FROM:
        seekStrategy = new FromStringTerm(value);
        LOG.debug("Fetching emails sent from \"" + value + "\"");
        break;
    case CC:
        seekStrategy = new RecipientStringTerm(Message.RecipientType.CC, value);
        LOG.debug("Fetching emails CC'd to \"" + value + "\"");
        break;
    case DATE_GT:
        seekStrategy = new SentDateTerm(SentDateTerm.GT, parseDate(value));
        LOG.debug("Fetching emails with a send date newer than \"" + value + "\"");
        break;
    case DATE_LT:
        seekStrategy = new SentDateTerm(SentDateTerm.LT, parseDate(value));
        LOG.debug("Fetching emails with a send date newer than \"" + value + "\"");
        break;
    case DATE_EQ:
        seekStrategy = new SentDateTerm(SentDateTerm.EQ, parseDate(value));
        LOG.debug("Fetching emails with a send date newer than \"" + value + "\"");
        break;
    case KEYWORD:
        seekStrategy = new BodyTerm(value);
        LOG.debug("Fetching emails containing the keyword \"" + value + "\"");
        break;
    case UNREAD:
        seekStrategy = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
        LOG.debug("Fetching all unread emails");
        break;
    }
    try {
        final GmailMessageList found = new GmailMessageList();
        final Store store = openGmailStore();
        final Folder folder = getFolder(this.srcFolder, store);
        folder.open(Folder.READ_ONLY);
        for (final Message msg : folder.search(seekStrategy)) {
            found.add(new JavaMailGmailMessage(msg));
        }
        LOG.debug("Found " + found.size() + " emails");
        return found;
    } catch (final Exception e) {
        throw new GmailException("Failed getting unread messages", e);
    }
}

From source file:edu.stanford.muse.email.Filter.java

public SearchTerm convertToSearchTerm(boolean useReceivedDateTerms) {
    // FLAG DEBUG: end date = null
    //endDate = null;
    SearchTerm sentOnlyTerm = null;//from ww  w  .j  a  v  a 2 s  . c o m
    if (sentMessagesOnly) {
        List<SearchTerm> fromAddrTerms = new ArrayList<SearchTerm>();
        if (ownCI != null) {
            for (String e : ownCI.emails)
                try {
                    fromAddrTerms.add(new FromTerm(new InternetAddress(e, ""))); // the name doesn't matter for equality of InternetAddress            
                } catch (Exception ex) {
                    Util.print_exception(ex, log);
                }

            // or all the from terms (not and)
            if (fromAddrTerms.size() >= 1) {
                sentOnlyTerm = fromAddrTerms.get(0);
                for (int i = 1; i < fromAddrTerms.size(); i++)
                    sentOnlyTerm = new OrTerm(sentOnlyTerm, fromAddrTerms.get(i));
            }
        }
    }

    SearchTerm result = sentOnlyTerm;

    if (startDate != null) {
        SearchTerm startTerm = useReceivedDateTerms ? new ReceivedDateTerm(ComparisonTerm.GT, startDate)
                : new SentDateTerm(ComparisonTerm.GT, startDate);
        if (result != null)
            result = new AndTerm(result, startTerm);
        else
            result = startTerm;
    }

    if (endDate != null) {
        SearchTerm endTerm = useReceivedDateTerms ? new ReceivedDateTerm(ComparisonTerm.LT, endDate)
                : new SentDateTerm(ComparisonTerm.LT, endDate);
        if (result != null)
            result = new AndTerm(result, endTerm);
        else
            result = endTerm;
    }

    if (keywords != null)
        for (String s : keywords) {
            if (Util.nullOrEmpty(s))
                continue;
            // look for this keyword in both subject and body
            SearchTerm sTerm = new OrTerm(new BodyTerm(s), new SubjectTerm(s));
            if (result != null)
                result = new AndTerm(result, sTerm);
            else
                result = sTerm;
        }
    return result;
}

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

private SearchTerm parseSentDateTerm(Node terms) throws XPathException {
    SearchTerm st = null;/* ww w.  j  a va 2  s  .com*/

    String value = ((Element) terms).getAttribute("date");
    String format = ((Element) terms).getAttribute("format");

    if (StringUtils.isEmpty(value)) {
        throw (new XPathException(this,
                "value not specified for term with type: " + ((Element) terms).getAttribute("type")));
    }

    if (StringUtils.isEmpty(format)) {
        throw (new XPathException(this,
                "format not specified for term with type: " + ((Element) terms).getAttribute("type")));
    }

    int cp = parseComparisonAttribute(terms);

    try {
        SimpleDateFormat sdf = new SimpleDateFormat(format);

        Date date = sdf.parse(value);

        st = new SentDateTerm(cp, date);
    } catch (ParseException pe) {
        throw (new XPathException(this, "Cannot parse date value: " + value + ", using format: " + format
                + ", for term with type: " + ((Element) terms).getAttribute("type")));
    }

    return (st);
}