KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > filter > plugins > DateFilter


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.mail.filter.plugins;
19
20 import java.util.Date JavaDoc;
21 import java.util.logging.Logger JavaDoc;
22
23 import org.columba.core.filter.AbstractFilter;
24 import org.columba.core.filter.FilterCriteria;
25 import org.columba.core.filter.IFilterCriteria;
26 import org.columba.core.folder.api.IFolder;
27 import org.columba.mail.folder.IMailbox;
28 import org.columba.mail.gui.config.filter.plugins.DateCriteriaRow;
29
30 /**
31  *
32  * Search for a certain absolute Date
33  *
34  * @author fdietz
35  */

36 public class DateFilter extends AbstractFilter {
37
38     /** JDK 1.4+ logging framework logger, used for logging. */
39     private static final Logger JavaDoc LOG = Logger
40             .getLogger("org.columba.mail.filter.plugins");
41
42     private String JavaDoc pattern;
43
44     private int condition;
45
46     protected Date JavaDoc transformDate(String JavaDoc pattern) {
47         Date JavaDoc searchPattern = null;
48
49         try {
50             searchPattern = DateCriteriaRow.dateFormat.parse(pattern);
51         } catch (java.text.ParseException JavaDoc ex) {
52             // should never happen
53
LOG.severe("Date unparsable: "+pattern);
54             searchPattern = new Date JavaDoc();
55         }
56
57         return searchPattern;
58     }
59
60     /**
61      * @see org.columba.core.filter.AbstractFilter#process(java.lang.Object,
62      * org.columba.mail.folder.Folder, java.lang.Object,
63      * org.columba.api.command.IWorkerStatusController)
64      */

65     public boolean process(IFolder folder, Object JavaDoc uid)
66             throws Exception JavaDoc {
67
68         // transform string to Date representation
69
Date JavaDoc date = transformDate(pattern);
70         if (date == null)
71             return false;
72
73         boolean result = false;
74
75         // get date
76
Date JavaDoc d = (Date JavaDoc) ((IMailbox)folder).getAttribute(uid, "columba.date");
77
78         if (d == null) {
79             LOG.fine("field date not found");
80
81             return false;
82         }
83
84         switch (condition) {
85         case FilterCriteria.DATE_BEFORE:
86
87             if (d.before(date)) {
88                 result = true;
89             }
90
91             break;
92
93         case FilterCriteria.DATE_AFTER:
94
95             if (d.after(date)) {
96                 result = true;
97             }
98
99             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 }
Popular Tags