1 package com.ca.directory.jxplorer.search; 2 3 import java.io.*; 4 import java.util.*; 5 import java.util.logging.Logger ; 6 7 import com.ca.commons.cbutil.*; 8 import com.ca.directory.jxplorer.*; 9 10 25 public class SearchModel 26 { 27 public static ReturnAttributesDisplay rat =null; 28 29 protected Properties properties; 30 protected String localDir; protected ArrayList dependantFilters = new ArrayList(); 32 protected ArrayList tempList = new ArrayList(); 33 protected String searchFilterConfig; 35 static final String SEARCH_FILTER_FILENAME = "search_filters.txt"; static final String NAME = "JXFilter."; static final String TEXTNAME = "JXTextFilter."; 39 40 43 public static final int BUILDFILTER = 1; 44 45 48 public static final int ALLFILTERS = 2; 49 50 54 public static final int JOINFILTER = 3; 55 56 60 public static final int TEXTFILTER = 4; 61 62 65 public static final int FULLNAMES = 5; 66 67 70 public static final int BUILDJOIN = 6; 71 72 75 public static final String BASEDN = "baseDN"; 76 77 80 public static final String RETATTRS = "retAttrs"; 81 82 85 public static final String SEARCHLEVEL = "searchLevel"; 86 87 90 public static final String FIND = "find"; 91 92 95 public static final String SEARCH = "search"; 96 97 private static Logger log = Logger.getLogger(SearchModel.class.getName()); 98 99 100 103 public SearchModel() 104 { 105 properties = new Properties(); 106 107 searchFilterConfig = CBUtility.getPropertyConfigPath(SEARCH_FILTER_FILENAME); 108 109 if (new File(searchFilterConfig).exists()==false) { log.info("no search filter config file found at: " + searchFilterConfig); return;} 110 111 properties = CBUtility.readPropertyFile(searchFilterConfig); 112 113 if (properties.size()==0) { log.info("Initialising config file: " + searchFilterConfig); return;} 114 } 115 116 122 public void openRetAttrDisplay(JXplorer jx, String [] attrNames, DataSource ds) 123 { 124 rat = new ReturnAttributesDisplay(jx, attrNames); 125 rat.registerDataSource(ds); 126 } 127 128 132 protected Enumeration getAllFilters() 133 { 134 properties = CBUtility.readPropertyFile(searchFilterConfig); 135 return properties.propertyNames(); 136 } 137 138 144 public ArrayList getFilterNames(int type) 145 { 146 Enumeration list = getAllFilters(); 147 ArrayList loadList = new ArrayList(); 148 149 while (list.hasMoreElements()) 150 { 151 String temp = list.nextElement().toString(); 152 153 switch (type) 154 { 155 case BUILDFILTER : { if (properties.get(temp).toString().startsWith("(") && temp.startsWith(NAME)) loadList.add(temp.substring(temp.indexOf(NAME)+9)); break; } case ALLFILTERS : { if (temp.startsWith(NAME)) loadList.add(temp.substring(temp.indexOf(NAME)+9)); else if (temp.startsWith(TEXTNAME)) loadList.add(temp.substring(temp.indexOf(NAME)+14)); break; } case JOINFILTER : { if (!properties.get(temp).toString().startsWith("(") && temp.startsWith(NAME)) loadList.add(temp.substring(temp.indexOf(NAME)+9)); break; } case TEXTFILTER : { if (temp.startsWith(TEXTNAME)) loadList.add(temp.substring(temp.indexOf(NAME)+14)); break; } 159 case FULLNAMES : { if(temp.startsWith(NAME) || temp.startsWith(TEXTNAME)) loadList.add(temp); break; } case BUILDJOIN : { if (temp.startsWith(NAME)) loadList.add(temp.substring(temp.indexOf(NAME)+9)); break; } default: loadList.add(""); 162 } 163 } 164 return loadList; 165 } 166 167 173 public String getFilter(String name) 174 { 175 if(name.startsWith("JXFilter.")) 176 return properties.getProperty(name); else 178 return properties.getProperty(NAME+name); } 180 181 186 public String getTextFilter(String name) 187 { 188 return properties.getProperty(TEXTNAME+name); } 190 191 197 public String getLDAPFilter(String name) 198 { 199 206 ArrayList list = getFilterNames(BUILDFILTER); 207 208 if (list.contains(name.substring(9))) 209 return properties.getProperty(name); 211 list.clear(); 212 213 list = getFilterNames(JOINFILTER); 214 215 if (list.contains(name.substring(9))) 216 return getJoinFilter(properties.getProperty(name)); 218 list.clear(); 219 220 list = getFilterNames(TEXTFILTER); 221 222 return properties.getProperty(name); } 224 225 231 protected int getOccurences(String string, String substring) 232 { 233 int pos = -1; 234 int count = 0; 235 236 while ((pos = string.indexOf(substring, pos+1))!=-1) 237 count++; 238 239 return count; 240 } 241 242 249 protected ArrayList getJoinFilterNames(String filter) 250 { 251 ArrayList list = new ArrayList(); 252 String names; 253 254 int num = getOccurences(filter, "JXFilter"); 255 256 for(int i=0; i<num; i++) 257 { 258 try 259 { 260 names = filter.substring(filter.indexOf(NAME)+9); names = names.substring(0, names.indexOf(NAME)); } 263 264 catch(Exception e) 265 { 266 names = filter.substring(filter.indexOf(NAME)+9); } 268 filter = filter.substring(filter.indexOf(names)+ names.length()); 270 list.add(names); 271 } 272 273 return list; 274 } 275 276 282 protected String getJoinFilter(String filter) 283 { 284 StringBuffer buffy = new StringBuffer (); 285 286 getOperator(buffy, filter); 287 288 return buffy.toString(); 289 } 290 291 301 public void getOperator(StringBuffer buffy, String filter) 302 { 303 if (filter==null) { log.warning("Unexpected error in processing a search filter: no filter supplied."); return;} 304 305 int count = 0; if (filter.startsWith("!")) 307 { 308 buffy.append("(!"); count++; 310 filter = filter.substring(1); } 312 313 if (filter.startsWith("&")) 314 { 315 buffy.append("(&"); count++; 317 filter = filter.substring(1); } 319 else if (filter.startsWith("|")) 320 { 321 buffy.append("(|"); count++; 323 filter = filter.substring(1); } 325 326 ArrayList list = getJoinFilterNames(filter); 328 String [] names = (String [])list.toArray(new String [list.size()]); 330 for (int i=0; i<names.length; i++) 331 { 332 String name = names[i]; 333 getValue(buffy, name); } 335 336 for(int i=0; i<count; i++) 337 buffy.append(")"); } 339 340 346 protected void getValue(StringBuffer buffy, String filter) 347 { 348 ArrayList list = getFilterNames(BUILDFILTER); 349 350 if (list.contains(filter)) { 352 buffy.append(getFilter(filter)); 353 } 354 else 355 { 356 getOperator(buffy, getFilter(filter)); 357 } 358 } 359 360 367 protected boolean exists(String name) 368 { 369 if(properties.containsKey(NAME+name)) return true; 371 else if(properties.containsKey(TEXTNAME+name)) return true; 373 374 return false; 375 } 376 377 383 protected boolean isTextFilter(String name) 384 { 385 if(properties.containsKey(TEXTNAME+name)) 386 return true; 387 388 return false; 389 } 390 391 399 protected void saveFilter(String name, String filter) 400 { 401 properties.setProperty(NAME+name, filter); 402 CBUtility.writePropertyFile(searchFilterConfig, properties, ""); 403 } 404 405 412 protected void saveTextFilter(String name, String filter) 413 { 414 properties.setProperty(TEXTNAME+name, filter); 415 CBUtility.writePropertyFile(searchFilterConfig, properties, ""); 416 } 417 418 425 protected void saveValue(String name, String type, String value) 426 { 427 properties.setProperty(name+"."+type, value); 428 CBUtility.writePropertyFile(searchFilterConfig, properties, ""); 429 } 430 431 436 protected void saveSearchLevel(String name, int searchLevel) 437 { 438 properties.setProperty(name+"."+SEARCHLEVEL, Integer.toString(searchLevel)); 439 CBUtility.writePropertyFile(searchFilterConfig, properties, ""); 440 } 441 442 447 protected void saveAlias(String name, String aliasType, boolean state) 448 { 449 if(state) 450 properties.setProperty(name+"."+aliasType, "true"); 451 else 452 properties.setProperty(name+"."+aliasType, "false"); 453 454 CBUtility.writePropertyFile(searchFilterConfig, properties, ""); 455 } 456 457 465 public String getValue(String key) 466 { 467 if(properties.containsKey(key)) 468 return properties.getProperty(key); 469 470 return null; 471 } 472 473 479 protected void removeRetAttrs(String value) 480 { 481 if(properties.containsValue(value)) 482 { 483 Enumeration en = properties.propertyNames(); 484 while (en.hasMoreElements()) 485 { 486 String temp = (en.nextElement()).toString(); 487 if (temp.indexOf(RETATTRS)>-1) 488 { 489 if(((properties.get(temp)).toString()).equalsIgnoreCase(value)) 490 properties.remove(temp); 491 } 492 } 493 494 CBUtility.writePropertyFile(searchFilterConfig, properties, ""); 495 } 496 } 497 498 502 protected void removeFilters(String [] filterNames) 503 { 504 for(int i=0;i<filterNames.length;i++) 505 { 506 removeFilter(filterNames[i]); 507 } 508 } 509 510 514 protected void removeFilter(String filterName) 515 { 516 properties.remove(filterName); 517 518 String name = filterName.startsWith(NAME)? filterName.substring(9) : filterName.substring(13); 519 520 Enumeration en = properties.propertyNames(); 521 while (en.hasMoreElements()) 522 { 523 String temp = (en.nextElement()).toString(); 524 if(temp.startsWith(name)) 525 properties.remove(temp); 526 } 527 528 CBUtility.writePropertyFile(searchFilterConfig, properties, ""); 529 } 530 531 537 protected ArrayList getDependants(Object filterName) 538 { 539 dependantFilters.clear(); tempList.clear(); 541 542 if (!dependantFilters.contains(filterName)) dependantFilters.add(NAME+filterName); 544 getDependantFilters(NAME+filterName); 545 546 return dependantFilters; 547 } 548 549 562 protected void getDependantFilters(Object filterName) 563 { Collection col = properties.values(); Object [] allValues = col.toArray(); 567 for(int i=0;i<allValues.length;i++) 568 { 569 if (allValues[i].toString().indexOf(filterName.toString())>-1) 570 { 571 String temp = getKeyForValue(allValues[i].toString()); 573 if (!temp.equalsIgnoreCase("")) 574 { 575 if (!dependantFilters.contains(temp)) { 577 dependantFilters.add(temp); 578 tempList.add(temp); 579 getDependantFilters(temp); } 581 } 582 } 583 } 584 } 585 586 594 protected String getKeyForValue(String value) 595 { 596 String [] allKeys = new String [properties.size()]; 597 Enumeration en = properties.propertyNames(); 598 int counter =0; 599 600 while (en.hasMoreElements()) 601 { 602 allKeys[counter]= (en.nextElement()).toString(); counter++; 604 } 605 606 for(int i=0;i<allKeys.length;i++) 607 { 608 if(properties.getProperty(allKeys[i]).equalsIgnoreCase(value) && !tempList.contains(allKeys[i])) return allKeys[i]; 610 } 611 612 return ""; 613 } 614 615 619 public static class StringComparator implements Comparator 620 { 621 628 public int compare(Object o1, Object o2) 629 { 630 return ((String )o1).compareToIgnoreCase((String )o2); 631 } 632 } 633 } | Popular Tags |