1 package net.suberic.pooka; 2 3 import javax.mail.search.*; 4 import javax.mail.*; 5 import java.util.HashMap ; 6 import java.util.Vector ; 7 import java.text.DateFormat ; 8 9 17 public class SearchTermManager { 18 19 HashMap labelToPropertyMap; 20 Vector termLabels; 21 HashMap labelToOperationMap; 22 Vector operationLabels; 23 HashMap typeToLabelMap; 24 25 DateFormat dateFormat; 26 27 Class stringTermClass; 28 Class flagTermClass; 29 Class dateTermClass; 30 31 String sourceProperty; 32 33 public static String STRING_MATCH = "String"; 34 public static String BOOLEAN_MATCH = "Boolean"; 35 public static String DATE_MATCH = "Date"; 36 public static String HEADER_MATCH = "Header"; 37 38 40 Vector displayFilterLabels; 41 Vector backendFilterLabels; 42 HashMap filterLabelToPropertyMap; 43 HashMap filterClassToPropertyMap; 44 45 49 public SearchTermManager(String 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 e) { } 56 createTermMaps(propertyName + ".searchTerms"); 57 createOperationMaps(propertyName + ".operations"); 58 createOperationTypeMaps(propertyName); 59 60 createFilterMaps(); 61 62 dateFormat = new java.text.SimpleDateFormat (Pooka.getProperty(propertyName + ".dateFormat", "MM/dd/yyyy")); 63 } 64 65 69 private void createTermMaps(String propName) { 70 Vector keys = Pooka.getResources().getPropertyAsVector(propName, ""); 71 termLabels = new Vector (); 72 if (keys != null) { 73 labelToPropertyMap = new HashMap (); 74 for (int i = 0; i < keys.size(); i++) { 75 String thisValue = propName + "." + (String ) keys.elementAt(i); 76 String thisLabel = Pooka.getProperty(thisValue + ".label", (String )keys.elementAt(i)); 77 labelToPropertyMap.put(thisLabel, thisValue); 78 termLabels.add(thisLabel); 79 } 80 } 81 } 82 83 87 private void createOperationMaps(String propName) { 88 Vector keys = Pooka.getResources().getPropertyAsVector(propName, ""); 89 operationLabels = new Vector (); 90 if (keys != null) { 91 labelToOperationMap = new HashMap (); 92 for (int i = 0; i < keys.size(); i++) { 93 String thisValue = propName + "." + (String ) keys.elementAt(i); 94 String thisLabel = Pooka.getProperty(thisValue + ".label", (String )keys.elementAt(i)); 95 labelToOperationMap.put(thisLabel, thisValue); 96 operationLabels.add(thisLabel); 97 } 98 } 99 } 100 101 104 private void createOperationTypeMaps(String propName) { 105 typeToLabelMap = new HashMap (); 106 Vector types = Pooka.getResources().getPropertyAsVector(propName + ".operationTypes", ""); 107 for (int i = 0; i < types.size(); i++) { 108 String currentType = (String ) types.elementAt(i); 109 Vector currentList = Pooka.getResources().getPropertyAsVector(propName + ".operationTypes." + currentType, ""); 110 Vector labelList = new Vector (); 111 112 for (int j = 0; j < currentList.size(); j++) { 113 labelList.add(Pooka.getProperty(propName + ".operations." + (String ) currentList.elementAt(j) + ".label")); 114 } 115 116 typeToLabelMap.put(currentType, labelList); 117 } 118 } 119 120 123 public void createFilterMaps() { 124 displayFilterLabels=new Vector (); 125 backendFilterLabels=new Vector (); 126 filterLabelToPropertyMap = new HashMap (); 127 filterClassToPropertyMap = new HashMap (); 128 129 Vector filterProperties = Pooka.getResources().getPropertyAsVector("FolderFilters.display", ""); 130 for (int i = 0; i < filterProperties.size(); i++) { 131 String currentProperty = "FolderFilters.display." + (String ) filterProperties.elementAt(i); 132 String label = Pooka.getProperty(currentProperty + ".label", (String ) filterProperties.elementAt(i)); 133 String 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 currentProperty = "FolderFilters.backend." + (String ) filterProperties.elementAt(i); 142 String label = Pooka.getProperty(currentProperty + ".label", (String ) filterProperties.elementAt(i)); 143 String className = Pooka.getProperty(currentProperty + ".class", ""); 144 backendFilterLabels.add(label); 145 filterLabelToPropertyMap.put(label, currentProperty); 146 filterClassToPropertyMap.put(className, currentProperty); 147 } 148 } 149 150 153 public SearchTerm generateCompoundSearchTerm(String [] properties, String operation) throws java.text.ParseException { 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 182 public SearchTerm generateSearchTermFromProperty(String property) throws java.text.ParseException { 183 String type = Pooka.getProperty(property + ".type", "single"); 185 if (type.equalsIgnoreCase("single")) { 186 String searchProperty = Pooka.getProperty(property + ".searchTerm", ""); 187 String operationProperty = Pooka.getProperty(property + ".operation", ""); 188 String pattern = Pooka.getProperty(property + ".pattern", ""); 189 String header = Pooka.getProperty(property + ".header", ""); 190 return generateSearchTerm(searchProperty, operationProperty, pattern, header); 191 } else if (type.equalsIgnoreCase("compound")) { 192 Vector subTermList = Pooka.getResources().getPropertyAsVector(property + ".subTerms", ""); 193 String [] subTerms = new String [subTermList.size()]; 194 for (int i = 0; i < subTerms.length; i++) 195 subTerms[i] = (String ) subTermList.elementAt(i); 196 String operation = Pooka.getProperty(property + ".operation", ""); 197 198 return generateCompoundSearchTerm(subTerms, operation); 199 } else 200 return null; 201 } 202 203 218 public SearchTerm generateSearchTerm(String searchProperty, String operationProperty, String pattern) throws java.text.ParseException { 219 return generateSearchTerm(searchProperty, operationProperty, pattern, ""); 220 } 221 222 223 238 public SearchTerm generateSearchTerm(String searchProperty, String operationProperty, String pattern, String header) throws java.text.ParseException { 239 SearchTerm term = null; 240 try { 241 String className = Pooka.getProperty(searchProperty + ".class", ""); 242 Class stClass = Class.forName(className); 243 244 if (stringTermClass.isAssignableFrom(stClass)) { 246 boolean ignoreCase = Pooka.getProperty(searchProperty + ".ignoreCase", "false").equals("true"); 247 248 if (className.equals("javax.mail.search.RecipientStringTerm")) { 250 String 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 263 java.lang.reflect.Constructor termConst = stClass.getConstructor(new Class [] {Class.forName("java.lang.String")}); 264 term = (SearchTerm) termConst.newInstance(new Object [] { pattern}); 265 266 } 267 } 268 269 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 277 else if (dateTermClass.isAssignableFrom(stClass)) { 278 279 java.util.Date compareDate = dateFormat.parse(pattern); 280 281 int comparison = 0; 282 283 String 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 termConst = stClass.getConstructor(new Class [] {Integer.TYPE , Class.forName("java.util.Date")}); 292 term = (SearchTerm) termConst.newInstance(new Object [] { new Integer (comparison), compareDate }); 293 } 294 295 297 else { 298 term = (SearchTerm) stClass.newInstance(); 300 } 301 302 304 String operationPropertyValue = Pooka.getProperty(operationProperty, ""); 305 if (operationPropertyValue.equalsIgnoreCase("not") || operationPropertyValue.equalsIgnoreCase("notEquals")) 306 term = new NotTerm(term); 307 } catch (ClassNotFoundException cnfe) { 308 showError(Pooka.getProperty("error.search.generatingSearchTerm", "Error generating SearchTerm: "), cnfe); 309 } catch (NoSuchMethodException nsme) { 310 showError(Pooka.getProperty("error.search.generatingSearchTerm", "Error generating SearchTerm: "), nsme); 311 } catch (InstantiationException ie) { 312 showError(Pooka.getProperty("error.search.generatingSearchTerm", "Error generating SearchTerm: "), ie); 313 } catch (IllegalAccessException iae) { 314 showError(Pooka.getProperty("error.search.generatingSearchTerm", "Error generating SearchTerm: "), iae); 315 } catch (java.lang.reflect.InvocationTargetException ite) { 316 showError(Pooka.getProperty("error.search.generatingSearchTerm", "Error generating SearchTerm: "), ite); 317 } 318 319 return term; 320 } 321 322 326 public Flags getFlags(String 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 346 public Vector getFlagLabels() { 347 Vector v = new Vector (); 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 361 public Vector getDisplayFilterLabels() { 362 return displayFilterLabels; 363 } 364 365 368 public Vector getBackendFilterLabels() { 369 return backendFilterLabels; 370 } 371 372 375 public net.suberic.pooka.gui.filter.FilterEditor getEditorForFilterLabel(String label) { 376 377 String property = (String ) filterLabelToPropertyMap.get(label); 378 String className = Pooka.getProperty(property + ".editorClass", ""); 379 if (className.equals("")) 380 return null; 381 else { 382 try { 383 Class 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 e) { 387 e.printStackTrace(); 388 return null; 389 } 390 } 391 } 392 393 396 public String getLabelForFilterClass(String className) { 397 String property = (String ) filterClassToPropertyMap.get(className); 398 String label = Pooka.getProperty(property + ".label", ""); 399 return label; 400 } 401 402 405 public void showError ( String message, Exception 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 415 public HashMap getLabelToPropertyMap() { 416 return labelToPropertyMap; 417 } 418 419 public Vector getTermLabels() { 420 return termLabels; 421 } 422 423 public HashMap getLabelToOperationMap() { 424 return labelToOperationMap; 425 } 426 427 public Vector getOperationLabels() { 428 return operationLabels; 429 } 430 431 public Vector getOperationLabels(String operationType) { 432 return (Vector ) typeToLabelMap.get(operationType); 433 } 434 } 435 436 | Popular Tags |