1 package org.enhydra.snapper.business; 2 3 import java.io.File ; 4 import java.util.Enumeration ; 5 import java.util.Vector ; 6 import java.util.Properties ; 7 8 import org.enhydra.snapper.Log; 9 import org.enhydra.snapper.SnapperManager; 10 import org.enhydra.snapper.Snapper; 11 12 import org.enhydra.snapper.spec.*; 13 import org.enhydra.snapper.api.*; 14 import com.lutris.appserver.server.sql.DBTransaction; 15 16 20 21 public class SearcherBaseImpl implements SearcherBase{ 22 23 Searcher searcher; 24 String defaultIndexDir = Snapper.getIndexDir(); 25 SiteListImpl siteList = new SiteListImpl(); 26 Properties luceneParameters = new Properties (); 27 Properties highlightParameters = new Properties (); 28 Properties requestParameters = new Properties (); 29 30 public void setUpSearcher(Properties sitesIDs,DBTransaction dbt) 31 { 32 try 33 { 34 Properties prop = new Properties (); 35 36 37 for (Enumeration e = sitesIDs.propertyNames() ; e.hasMoreElements() ;) 38 { 39 String siteId = (String )e.nextElement(); 40 String siteName = null; 41 String indexDir = null; 42 try { 43 SiteList sl = SiteListFactory.getSiteList("org.enhydra.snapper.business.SiteListImpl"); 44 Site site = sl.findSiteByID(siteId,dbt); 45 indexDir = site.getINDEXDIR(); 46 siteName = site.getName(); 47 48 }catch (Exception exx){ 49 try{ 50 SnapperManager.getInstance().getLoggingManager().warn("Could not get index directory for site ID : "+siteId+" \n caught a " + e.getClass() + 51 "\n with message: " + exx.getMessage()); 52 } catch (Exception ex) { 53 System.out.println(" caught a " + e.getClass() + 54 "\n with message: " + exx.getMessage()); 55 } 56 } 57 58 59 File temp; 60 61 if(indexDir!=null&&!indexDir.equals("")) 62 { 63 temp=new File (indexDir); 64 } 65 else 66 { 67 if(defaultIndexDir.endsWith("\\")||defaultIndexDir.endsWith("/")) 68 temp=new File (defaultIndexDir+siteName); 69 else 70 temp=new File (defaultIndexDir+File.separator+siteName); 71 } 72 73 74 if(temp.isDirectory()) 75 { 76 77 78 prop.setProperty(siteName,temp.getAbsolutePath()); 79 IncrementQueryNumber.increment(siteId); 80 } 81 else 82 { 83 try{ 84 SnapperManager.getInstance().getLoggingManager().warn("Site : "+siteName+" with path "+temp.getAbsolutePath()+" is not directory , will not be searched "); 85 } catch (Exception ex) { 86 System.out.println(" caught a " + ex.getClass() + 87 "\n with message: " + ex.getMessage()); 88 } 89 } 90 } 91 92 93 searcher = SnapperManager.getInstance().getSearcherFactory().newSearcher(); 94 searcher.setUpSearcher(prop); 95 96 } 97 catch (Exception e) { 98 try{ 99 SnapperManager.getInstance().getLoggingManager().error("Could not setup Searcher caught a " + e.getClass() + 100 "\n with message: " + e.getMessage()); 101 } catch (Exception ex) { 102 System.out.println(" caught a " + e.getClass() + 103 "\n with message: " + e.getMessage()); 104 } 105 } 106 } 107 108 public void setUpSearcherQuery(Properties requestParameters) 109 { 110 this.requestParameters =requestParameters; 111 String modfrom = requestParameters.getProperty("modifiedfrom"); 112 String modto = requestParameters.getProperty("modifiedto"); 113 114 String wordKew = requestParameters.getProperty("propertiesKey"); 115 String wordValue = requestParameters.getProperty("propertiesValue"); 116 117 String metaDataKey = requestParameters.getProperty("metaDataKey"); 118 String metaDataValue = requestParameters.getProperty("metaDataValue"); 119 120 121 String properties = null; 122 String modified = null; 123 String metaData = null; 124 125 if(wordKew!=null&&!wordKew.equals("")&&wordValue!=null&&!wordValue.equals("")) 126 { 127 properties = "\""+wordKew+" = "+wordValue+"\""; 128 } 129 else 130 { 131 if(wordKew!=null&&!wordKew.equals("")) 132 { 133 properties = wordKew; 134 } 135 else if(wordValue!=null&&!wordValue.equals("")) 136 { 137 properties = wordValue; 138 } 139 } 140 141 if(metaDataKey!=null&&!metaDataKey.equals("")&&metaDataValue!=null&&!metaDataValue.equals("")) 142 { 143 metaData = "\""+metaDataKey+" = "+metaDataValue+"\""; 144 } 145 else 146 { 147 if(metaDataKey!=null&&!metaDataKey.equals("")) 148 { 149 metaData = metaDataKey; 150 } 151 else if(metaDataValue!=null&&!metaDataValue.equals("")) 152 { 153 metaData = metaDataValue; 154 } 155 } 156 157 158 if(modfrom!=null&&!modfrom.equals("")&&modto!=null&&!modto.equals("")) 159 { 160 modified ="["+modfrom+" TO "+modto+"]"; 161 } 162 163 164 if(requestParameters.containsKey("type")) 165 { 166 if(!requestParameters.getProperty("type").equals("")) 167 luceneParameters.setProperty("type",requestParameters.getProperty("type")); 168 } 169 170 if(requestParameters.containsKey("contents")) 171 { 172 if(!requestParameters.getProperty("contents").equals("")) 173 luceneParameters.setProperty("contents",requestParameters.getProperty("contents")); 174 } 175 176 if(requestParameters.containsKey("title")) 177 { 178 if(!requestParameters.getProperty("title").equals("")) 179 luceneParameters.setProperty("title",requestParameters.getProperty("title")); 180 } 181 182 if(properties!=null) 183 { 184 luceneParameters.setProperty("properties",properties); 185 } 186 187 if(metaData!=null) 188 { 189 luceneParameters.setProperty("metadata",metaData); 190 } 191 192 if(modified!=null) 193 { 194 luceneParameters.setProperty("modified",modified); 195 } 196 197 String contentLength = requestParameters.getProperty("contentLength"); 198 String wordLength = requestParameters.getProperty("wordLength"); 199 String metaDataLength = requestParameters.getProperty("metaDataLength"); 200 201 if(contentLength!=null) 202 highlightParameters.setProperty("contentLength",contentLength); 203 204 if(wordLength!=null) 205 highlightParameters.setProperty("wordLength",wordLength); 206 207 if(metaDataLength!=null) 208 highlightParameters.setProperty("metaDataLength",metaDataLength); 209 210 211 } 212 213 public void setUpHighLighter(Properties prop) 214 { 215 216 searcher.setUpHighLighter(prop); 217 } 218 219 220 public void searchDocs() 221 { 222 try{ 223 if(requestParameters.containsKey("typeOfSearch")&&requestParameters.getProperty("typeOfSearch").equals("simple")) 224 { 225 searcher.searchDocs(simpleLuceneQuery(), luceneParameters.getProperty("contents"), luceneParameters.getProperty("properties"), luceneParameters.getProperty("metadata"), requestParameters.getProperty("sortType") , "EN"); 226 } 227 else 228 { 229 searcher.searchDocs(luceneQuery(), luceneParameters.getProperty("contents"), luceneParameters.getProperty("properties"), luceneParameters.getProperty("metadata"), requestParameters.getProperty("sortType") , "EN"); 230 } 231 searcher.setUpHighLighter(highlightParameters); 232 }catch(Exception e){ 233 241 } 242 } 243 244 245 public void searchDocs(String term,String highlighterContextQuery,String language) 246 { 247 try{ 248 searcher.searchDocs(term,highlighterContextQuery,language); 249 }catch(Exception e){ 250 } 258 } 259 260 public void searchDocs(String term,String highlighterContextQuery,String highlighterPropertiesQuery,String sortType, String language) 261 { 262 263 try{ 264 searcher.searchDocs(term,highlighterContextQuery,highlighterPropertiesQuery,sortType,language); 265 }catch(Exception e){ 266 } 274 } 275 276 public int getSearchedDocsNumber() 277 { 278 return searcher.getSearchedDocsNumber(); 279 } 280 281 public String getXmlSearchedResult(int begin,int end) 282 { 283 return searcher.getXmlSearchedResult(begin,end); 284 } 285 286 public String getXmlSearchedResult() 287 { 288 return searcher.getXmlSearchedResult(); 289 } 290 291 public void close() 292 { 293 try{ 294 searcher.close(); 295 }catch(Exception e){ 296 try{ 297 SnapperManager.getInstance().getLoggingManager().error("Could not close searcher \n caught a " + e.getClass() + 298 "\n with message: " + e.getMessage()); 299 } catch (Exception ex) { 300 System.out.println(" caught a " + e.getClass() + 301 "\n with message: " + e.getMessage()); 302 } 303 } 304 searcher = null; 305 SnapperManager.getInstance().getSearcherFactory().removeSearcher(); 306 } 307 308 309 310 public String luceneQuery() 311 { 312 313 StringBuffer luceneQuery = new StringBuffer (); 314 315 int i = 1; 316 317 for (Enumeration e = luceneParameters.propertyNames() ; e.hasMoreElements() ;) 318 { 319 String field = (String )e.nextElement(); 320 String query = (String )luceneParameters.getProperty(field); 321 if(e.hasMoreElements()) 322 { 323 if(field.equals("contents")) 324 luceneQuery.append(query+" AND "); 325 else 326 luceneQuery.append(field+":"+query+" AND "); 327 328 } 329 else 330 { 331 if(field.equals("contents")) 332 luceneQuery.append(query); 333 else 334 luceneQuery.append(field+":"+query); 335 i++; 336 } 337 } 338 return luceneQuery.toString(); 339 } 340 341 342 public String simpleLuceneQuery() 343 { 344 345 StringBuffer luceneQuery = new StringBuffer (); 346 347 String contents = (String )luceneParameters.getProperty("contents"); 348 349 if(contents!=null) 350 { 351 String typeFromXML = Snapper.getSimpleSearchType(); 352 353 if(typeFromXML==null||typeFromXML.equals("Default")) 354 { luceneQuery.append(contents); 356 } 357 else if(typeFromXML!=null&&typeFromXML.equals("ContentsAndTitle")) 358 { 359 if(contents.startsWith("(")&&contents.endsWith(")")) 360 luceneQuery.append(contents+" OR title:"+contents); 361 else 362 luceneQuery.append("("+contents+")"+" OR title:("+contents+")"); 363 } 364 else 365 { 366 luceneQuery.append(contents); 367 } 368 369 } 370 return luceneQuery.toString(); 371 } 372 373 } | Popular Tags |