KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > SearchTermManager


1 package net.suberic.pooka;
2
3 import javax.mail.search.*;
4 import javax.mail.*;
5 import java.util.HashMap JavaDoc;
6 import java.util.Vector JavaDoc;
7 import java.text.DateFormat JavaDoc;
8
9 /**
10  * This class generates SearchTerms from properties in the Pooka
11  * VariableBundle. It also will give out a list of property labels and
12  * then will translate those labels and/or properties into SearchTerms.
13  *
14  * This class also handles the Filter properties and editors. This might
15  * get moved out of this class eventually.
16  */

17 public class SearchTermManager {
18
19   HashMap JavaDoc labelToPropertyMap;
20   Vector JavaDoc termLabels;
21   HashMap JavaDoc labelToOperationMap;
22   Vector JavaDoc operationLabels;
23   HashMap JavaDoc typeToLabelMap;
24   
25   DateFormat JavaDoc dateFormat;
26   
27   Class JavaDoc stringTermClass;
28   Class JavaDoc flagTermClass;
29   Class JavaDoc dateTermClass;
30   
31   String JavaDoc sourceProperty;
32   
33   public static String JavaDoc STRING_MATCH = "String";
34   public static String JavaDoc BOOLEAN_MATCH = "Boolean";
35   public static String JavaDoc DATE_MATCH = "Date";
36   public static String JavaDoc HEADER_MATCH = "Header";
37   
38   // filter properties
39

40   Vector JavaDoc displayFilterLabels;
41   Vector JavaDoc backendFilterLabels;
42   HashMap JavaDoc filterLabelToPropertyMap;
43   HashMap JavaDoc filterClassToPropertyMap;
44   
45   /**
46    * Default constructor. Initializes the labelToPropertyMap and the
47    * termLabels Vector from the Pooka property.
48    */

49   public SearchTermManager(String JavaDoc propertyName) {
50     sourceProperty = propertyName;
51     try {
52       flagTermClass = Class.forName("javax.mail.search.FlagTerm");
53       stringTermClass = Class.forName("javax.mail.search.StringTerm");
54       dateTermClass = Class.forName("javax.mail.search.DateTerm");
55     } catch (Exception JavaDoc e) { }
56     createTermMaps(propertyName + ".searchTerms");
57     createOperationMaps(propertyName + ".operations");
58     createOperationTypeMaps(propertyName);
59     
60     createFilterMaps();
61     
62     dateFormat = new java.text.SimpleDateFormat JavaDoc(Pooka.getProperty(propertyName + ".dateFormat", "MM/dd/yyyy"));
63   }
64   
65   /**
66    * Creates the labelToOperationMap and operationLabels from the given
67    * property, as well as the termLabels Vector.
68    */

69   private void createTermMaps(String JavaDoc propName) {
70     Vector JavaDoc keys = Pooka.getResources().getPropertyAsVector(propName, "");
71     termLabels = new Vector JavaDoc();
72     if (keys != null) {
73       labelToPropertyMap = new HashMap JavaDoc();
74       for (int i = 0; i < keys.size(); i++) {
75     String JavaDoc thisValue = propName + "." + (String JavaDoc) keys.elementAt(i);
76     String JavaDoc thisLabel = Pooka.getProperty(thisValue + ".label", (String JavaDoc)keys.elementAt(i));
77     labelToPropertyMap.put(thisLabel, thisValue);
78     termLabels.add(thisLabel);
79       }
80     }
81   }
82
83     /**
84      * Creates the labelToOperationMap and operationLabels from the given
85      * propery.
86      */

87     private void createOperationMaps(String JavaDoc propName) {
88     Vector JavaDoc keys = Pooka.getResources().getPropertyAsVector(propName, "");
89     operationLabels = new Vector JavaDoc();
90     if (keys != null) {
91         labelToOperationMap = new HashMap JavaDoc();
92         for (int i = 0; i < keys.size(); i++) {
93         String JavaDoc thisValue = propName + "." + (String JavaDoc) keys.elementAt(i);
94         String JavaDoc thisLabel = Pooka.getProperty(thisValue + ".label", (String JavaDoc)keys.elementAt(i));
95         labelToOperationMap.put(thisLabel, thisValue);
96         operationLabels.add(thisLabel);
97         }
98     }
99     }
100
101     /**
102      * Creates the typeToLabelMap for the given property.
103      */

104     private void createOperationTypeMaps(String JavaDoc propName) {
105     typeToLabelMap = new HashMap JavaDoc();
106     Vector JavaDoc types = Pooka.getResources().getPropertyAsVector(propName + ".operationTypes", "");
107     for (int i = 0; i < types.size(); i++) {
108         String JavaDoc currentType = (String JavaDoc) types.elementAt(i);
109         Vector JavaDoc currentList = Pooka.getResources().getPropertyAsVector(propName + ".operationTypes." + currentType, "");
110         Vector JavaDoc labelList = new Vector JavaDoc();
111
112         for (int j = 0; j < currentList.size(); j++) {
113         labelList.add(Pooka.getProperty(propName + ".operations." + (String JavaDoc) currentList.elementAt(j) + ".label"));
114         }
115
116         typeToLabelMap.put(currentType, labelList);
117     }
118     }
119
120     /**
121      * Creates the filter properties.
122      */

123     public void createFilterMaps() {
124     displayFilterLabels=new Vector JavaDoc();
125     backendFilterLabels=new Vector JavaDoc();
126     filterLabelToPropertyMap = new HashMap JavaDoc();
127     filterClassToPropertyMap = new HashMap JavaDoc();
128
129     Vector JavaDoc filterProperties = Pooka.getResources().getPropertyAsVector("FolderFilters.display", "");
130     for (int i = 0; i < filterProperties.size(); i++) {
131         String JavaDoc currentProperty = "FolderFilters.display." + (String JavaDoc) filterProperties.elementAt(i);
132         String JavaDoc label = Pooka.getProperty(currentProperty + ".label", (String JavaDoc) filterProperties.elementAt(i));
133         String JavaDoc className = Pooka.getProperty(currentProperty + ".class", "");
134         displayFilterLabels.add(label);
135         filterLabelToPropertyMap.put(label, currentProperty);
136         filterClassToPropertyMap.put(className, currentProperty);
137     }
138
139     filterProperties = Pooka.getResources().getPropertyAsVector("FolderFilters.backend", "");
140     for (int i = 0; i < filterProperties.size(); i++) {
141         String JavaDoc currentProperty = "FolderFilters.backend." + (String JavaDoc) filterProperties.elementAt(i);
142         String JavaDoc label = Pooka.getProperty(currentProperty + ".label", (String JavaDoc) filterProperties.elementAt(i));
143         String JavaDoc className = Pooka.getProperty(currentProperty + ".class", "");
144         backendFilterLabels.add(label);
145         filterLabelToPropertyMap.put(label, currentProperty);
146         filterClassToPropertyMap.put(className, currentProperty);
147     }
148     }
149
150     /**
151      * Generates a compound SearchTerm.
152      */

153     public SearchTerm generateCompoundSearchTerm(String JavaDoc[] properties, String JavaDoc operation) throws java.text.ParseException JavaDoc {
154     SearchTerm[] terms = new SearchTerm[properties.length];
155     for (int i = 0; i < properties.length; i++)
156         terms[i] = generateSearchTermFromProperty(properties[i]);
157
158     if (operation.equalsIgnoreCase("and"))
159         return new AndTerm(terms);
160     else if (operation.equalsIgnoreCase("or"))
161         return new OrTerm(terms);
162     else
163         return null;
164     }
165
166     /**
167      * Generates a SearchTerm from a single property root. This method
168      * expects the following sub-properties to be set on the given
169      * property:
170      *
171      * property.type should be set either to 'compound' or 'single'
172      *
173      * for 'single' types:
174      * property.searchTerm
175      * property.operation (optional)
176      * property.pattern (optional)
177      *
178      * for 'compound' types:
179      * property.subTerms
180      * property.operation (should be 'or' or 'and')
181      */

182     public SearchTerm generateSearchTermFromProperty(String JavaDoc property) throws java.text.ParseException JavaDoc {
183       //System.out.println("generating search term for " + property);
184
String JavaDoc type = Pooka.getProperty(property + ".type", "single");
185     if (type.equalsIgnoreCase("single")) {
186       String JavaDoc searchProperty = Pooka.getProperty(property + ".searchTerm", "");
187       String JavaDoc operationProperty = Pooka.getProperty(property + ".operation", "");
188       String JavaDoc pattern = Pooka.getProperty(property + ".pattern", "");
189       String JavaDoc header = Pooka.getProperty(property + ".header", "");
190       return generateSearchTerm(searchProperty, operationProperty, pattern, header);
191     } else if (type.equalsIgnoreCase("compound")) {
192       Vector JavaDoc subTermList = Pooka.getResources().getPropertyAsVector(property + ".subTerms", "");
193       String JavaDoc[] subTerms = new String JavaDoc[subTermList.size()];
194       for (int i = 0; i < subTerms.length; i++)
195         subTerms[i] = (String JavaDoc) subTermList.elementAt(i);
196       String JavaDoc operation = Pooka.getProperty(property + ".operation", "");
197       
198       return generateCompoundSearchTerm(subTerms, operation);
199     } else
200       return null;
201     }
202   
203   /**
204    * Generates a SearchTerm from the given property and pattern.
205    *
206    * This method used the .class subproperty of the given searchProperty
207    * String to determine what type of SearchTerm to create. If the
208    * .class is an instance of FlagTerm, the .flag subproperty is used
209    * to determine which flag to test. If the .class is an instance
210    * of StringTerm, then .ignoreCase is checked to see whether or not
211    * to ignore case (default to false).
212    *
213    * This also uses the operationProperty to determine whether to make
214    * this a positive or negative search (is or is not), or, in the case
215    * of comparison searches, a greater than or less than search.
216    *
217    */

218   public SearchTerm generateSearchTerm(String JavaDoc searchProperty, String JavaDoc operationProperty, String JavaDoc pattern) throws java.text.ParseException JavaDoc {
219     return generateSearchTerm(searchProperty, operationProperty, pattern, "");
220   }
221
222
223   /**
224    * Generates a SearchTerm from the given property and pattern.
225    *
226    * This method used the .class subproperty of the given searchProperty
227    * String to determine what type of SearchTerm to create. If the
228    * .class is an instance of FlagTerm, the .flag subproperty is used
229    * to determine which flag to test. If the .class is an instance
230    * of StringTerm, then .ignoreCase is checked to see whether or not
231    * to ignore case (default to false).
232    *
233    * This also uses the operationProperty to determine whether to make
234    * this a positive or negative search (is or is not), or, in the case
235    * of comparison searches, a greater than or less than search.
236    *
237    */

238   public SearchTerm generateSearchTerm(String JavaDoc searchProperty, String JavaDoc operationProperty, String JavaDoc pattern, String JavaDoc header) throws java.text.ParseException JavaDoc {
239     SearchTerm term = null;
240     try {
241       String JavaDoc className = Pooka.getProperty(searchProperty + ".class", "");
242       Class JavaDoc stClass = Class.forName(className);
243       
244       // ****** Create a StringTerm.
245
if (stringTermClass.isAssignableFrom(stClass)) {
246     boolean ignoreCase = Pooka.getProperty(searchProperty + ".ignoreCase", "false").equals("true");
247     
248     // check for the special cases.
249
if (className.equals("javax.mail.search.RecipientStringTerm")) {
250       String JavaDoc recipientType = Pooka.getProperty(searchProperty + ".recipientType", "to");
251       if (recipientType.equalsIgnoreCase("to"))
252         term = new RecipientStringTerm(javax.mail.Message.RecipientType.TO, pattern);
253       else if (recipientType.equalsIgnoreCase("cc"))
254         term = new RecipientStringTerm(javax.mail.Message.RecipientType.CC, pattern);
255         else if (recipientType.equalsIgnoreCase("toorcc"))
256           term = new OrTerm(new RecipientStringTerm(javax.mail.Message.RecipientType.CC, pattern), new RecipientStringTerm(javax.mail.Message.RecipientType.TO, pattern));
257       
258     } else if (className.equals("javax.mail.search.HeaderTerm")) {
259       term = new HeaderTerm(header, pattern);
260     } else {
261       // default case for StringTerms
262

263       java.lang.reflect.Constructor JavaDoc termConst = stClass.getConstructor(new Class JavaDoc[] {Class.forName("java.lang.String")});
264       term = (SearchTerm) termConst.newInstance(new Object JavaDoc[] { pattern});
265         
266     }
267       }
268       
269       // ********** Create a FlagTerm
270

271       else if (flagTermClass.isAssignableFrom(stClass)) {
272     term = new FlagTerm(getFlags(Pooka.getProperty(searchProperty + ".flag", "")), Pooka.getProperty(searchProperty + ".value", "true").equalsIgnoreCase("true"));
273       }
274       
275       // ********** Create a DateTerm
276

277       else if (dateTermClass.isAssignableFrom(stClass)) {
278     
279     java.util.Date JavaDoc compareDate = dateFormat.parse(pattern);
280     
281     int comparison = 0;
282     
283     String JavaDoc operationPropertyType = Pooka.getProperty(operationProperty, "");
284     if (operationPropertyType.equalsIgnoreCase("equals") || operationPropertyType.equalsIgnoreCase("notEquals"))
285       comparison = DateTerm.EQ;
286     else if (operationPropertyType.equalsIgnoreCase("before"))
287       comparison = DateTerm.LT;
288     else if (operationPropertyType.equalsIgnoreCase("after"))
289       comparison = DateTerm.GT;
290     
291     java.lang.reflect.Constructor JavaDoc termConst = stClass.getConstructor(new Class JavaDoc[] {Integer.TYPE , Class.forName("java.util.Date")});
292     term = (SearchTerm) termConst.newInstance(new Object JavaDoc[] { new Integer JavaDoc(comparison), compareDate });
293       }
294       
295       // ********** Default Case, no term known.
296

297       else {
298     // default case for any term.
299
term = (SearchTerm) stClass.newInstance();
300       }
301       
302       // *********** Handles not cases.
303

304       String JavaDoc operationPropertyValue = Pooka.getProperty(operationProperty, "");
305       if (operationPropertyValue.equalsIgnoreCase("not") || operationPropertyValue.equalsIgnoreCase("notEquals"))
306     term = new NotTerm(term);
307     } catch (ClassNotFoundException JavaDoc cnfe) {
308       showError(Pooka.getProperty("error.search.generatingSearchTerm", "Error generating SearchTerm: "), cnfe);
309     } catch (NoSuchMethodException JavaDoc nsme) {
310       showError(Pooka.getProperty("error.search.generatingSearchTerm", "Error generating SearchTerm: "), nsme);
311     } catch (InstantiationException JavaDoc ie) {
312       showError(Pooka.getProperty("error.search.generatingSearchTerm", "Error generating SearchTerm: "), ie);
313     } catch (IllegalAccessException JavaDoc iae) {
314       showError(Pooka.getProperty("error.search.generatingSearchTerm", "Error generating SearchTerm: "), iae);
315     } catch (java.lang.reflect.InvocationTargetException JavaDoc ite) {
316       showError(Pooka.getProperty("error.search.generatingSearchTerm", "Error generating SearchTerm: "), ite);
317     }
318     
319     return term;
320   }
321   
322   /**
323    * This creates a javax.mail.Flags object containing the flag indicated
324    * by flagName.
325    */

326   public Flags getFlags(String JavaDoc flagName) {
327     if (flagName.equalsIgnoreCase("answered"))
328       return new Flags(Flags.Flag.ANSWERED);
329     else if (flagName.equalsIgnoreCase("deleted"))
330       return new Flags(Flags.Flag.DELETED);
331     else if (flagName.equalsIgnoreCase("draft"))
332       return new Flags(Flags.Flag.DRAFT);
333     else if (flagName.equalsIgnoreCase("flagged"))
334       return new Flags(Flags.Flag.FLAGGED);
335     else if (flagName.equalsIgnoreCase("recent"))
336       return new Flags(Flags.Flag.RECENT);
337     else if (flagName.equalsIgnoreCase("seen"))
338       return new Flags(Flags.Flag.SEEN);
339     
340     return new Flags(flagName);
341   }
342
343     /**
344      * Returns the available flag labels.
345      */

346     public Vector JavaDoc getFlagLabels() {
347     // FIXME this isn't customizable or internationalized at all.
348
Vector JavaDoc v = new Vector JavaDoc();
349     v.add("flagged");
350     v.add("seen");
351     v.add("answered");
352     v.add("deleted");
353     v.add("draft");
354     v.add("recent");
355     return v;
356     }
357
358     /**
359      * Returns the display filter labels.
360      */

361     public Vector JavaDoc getDisplayFilterLabels() {
362     return displayFilterLabels;
363     }
364
365     /**
366      * Returns the backend filter labels.
367      */

368     public Vector JavaDoc getBackendFilterLabels() {
369     return backendFilterLabels;
370     }
371
372     /**
373      * creates an editor for a given filter label.
374      */

375     public net.suberic.pooka.gui.filter.FilterEditor getEditorForFilterLabel(String JavaDoc label) {
376     
377     String JavaDoc property = (String JavaDoc) filterLabelToPropertyMap.get(label);
378     String JavaDoc className = Pooka.getProperty(property + ".editorClass", "");
379     if (className.equals(""))
380         return null;
381     else {
382         try {
383         Class JavaDoc editorClass = Class.forName(className);
384         net.suberic.pooka.gui.filter.FilterEditor editor = (net.suberic.pooka.gui.filter.FilterEditor) editorClass.newInstance();
385         return editor;
386         } catch (Exception JavaDoc e) {
387         e.printStackTrace();
388         return null;
389         }
390     }
391     }
392
393     /**
394      * Returns the appropriate label for the given filter class.
395      */

396     public String JavaDoc getLabelForFilterClass(String JavaDoc className) {
397     String JavaDoc property = (String JavaDoc) filterClassToPropertyMap.get(className);
398     String JavaDoc label = Pooka.getProperty(property + ".label", "");
399     return label;
400     }
401
402     /**
403      * Shows an error message.
404      */

405     public void showError ( String JavaDoc message, Exception JavaDoc e ) {
406     if (Pooka.getUIFactory() != null)
407         Pooka.getUIFactory().showError(message, e);
408     else {
409         System.err.println(message + e.getMessage());
410         e.printStackTrace();
411     }
412     }
413     // accessor methods
414

415     public HashMap JavaDoc getLabelToPropertyMap() {
416     return labelToPropertyMap;
417     }
418
419     public Vector JavaDoc getTermLabels() {
420     return termLabels;
421     }
422
423     public HashMap JavaDoc getLabelToOperationMap() {
424     return labelToOperationMap;
425     }
426
427     public Vector JavaDoc getOperationLabels() {
428     return operationLabels;
429     }
430
431     public Vector JavaDoc getOperationLabels(String JavaDoc operationType) {
432     return (Vector JavaDoc) typeToLabelMap.get(operationType);
433     }
434 }
435
436
Popular Tags