001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.mail.filter.plugins;
019:
020: import java.util.Date;
021: import java.util.logging.Logger;
022:
023: import org.columba.core.filter.AbstractFilter;
024: import org.columba.core.filter.FilterCriteria;
025: import org.columba.core.filter.IFilterCriteria;
026: import org.columba.core.folder.api.IFolder;
027: import org.columba.mail.folder.IMailbox;
028: import org.columba.mail.gui.config.filter.plugins.DateCriteriaRow;
029:
030: /**
031: *
032: * Search for a certain absolute Date
033: *
034: * @author fdietz
035: */
036: public class DateFilter extends AbstractFilter {
037:
038: /** JDK 1.4+ logging framework logger, used for logging. */
039: private static final Logger LOG = Logger
040: .getLogger("org.columba.mail.filter.plugins");
041:
042: private String pattern;
043:
044: private int condition;
045:
046: protected Date transformDate(String pattern) {
047: Date searchPattern = null;
048:
049: try {
050: searchPattern = DateCriteriaRow.dateFormat.parse(pattern);
051: } catch (java.text.ParseException ex) {
052: // should never happen
053: LOG.severe("Date unparsable: " + pattern);
054: searchPattern = new Date();
055: }
056:
057: return searchPattern;
058: }
059:
060: /**
061: * @see org.columba.core.filter.AbstractFilter#process(java.lang.Object,
062: * org.columba.mail.folder.Folder, java.lang.Object,
063: * org.columba.api.command.IWorkerStatusController)
064: */
065: public boolean process(IFolder folder, Object uid) throws Exception {
066:
067: // transform string to Date representation
068: Date date = transformDate(pattern);
069: if (date == null)
070: return false;
071:
072: boolean result = false;
073:
074: // get date
075: Date d = (Date) ((IMailbox) folder).getAttribute(uid,
076: "columba.date");
077:
078: if (d == null) {
079: LOG.fine("field date not found");
080:
081: return false;
082: }
083:
084: switch (condition) {
085: case FilterCriteria.DATE_BEFORE:
086:
087: if (d.before(date)) {
088: result = true;
089: }
090:
091: break;
092:
093: case FilterCriteria.DATE_AFTER:
094:
095: if (d.after(date)) {
096: result = true;
097: }
098:
099: break;
100: }
101:
102: return result;
103: }
104:
105: /**
106: * @see org.columba.core.filter.AbstractFilter#setUp(org.columba.mail.filter.FilterCriteria)
107: */
108: public void setUp(IFilterCriteria f) {
109:
110: // string to search
111: pattern = f.getPatternString();
112:
113: // convert criteria into int-value
114: condition = f.getCriteria();
115: }
116: }
|