KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > snapper > business > SearcherBaseImpl


1 package org.enhydra.snapper.business;
2
3 import java.io.File JavaDoc;
4 import java.util.Enumeration JavaDoc;
5 import java.util.Vector JavaDoc;
6 import java.util.Properties JavaDoc;
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 /**
17  * @author Milin Radivoj
18  *
19  */

20
21 public class SearcherBaseImpl implements SearcherBase{
22     
23     Searcher searcher;
24     String JavaDoc defaultIndexDir = Snapper.getIndexDir();
25     SiteListImpl siteList = new SiteListImpl();
26     Properties JavaDoc luceneParameters = new Properties JavaDoc();
27     Properties JavaDoc highlightParameters = new Properties JavaDoc();
28     Properties JavaDoc requestParameters = new Properties JavaDoc();
29     
30     public void setUpSearcher(Properties JavaDoc sitesIDs,DBTransaction dbt)
31      {
32         try
33          {
34           Properties JavaDoc prop = new Properties JavaDoc();
35          
36         
37       for (Enumeration JavaDoc e = sitesIDs.propertyNames() ; e.hasMoreElements() ;)
38          {
39           String JavaDoc siteId = (String JavaDoc)e.nextElement();
40           String JavaDoc siteName = null;
41           String JavaDoc 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 JavaDoc 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 JavaDoc ex) {
53                   System.out.println(" caught a " + e.getClass() +
54                    "\n with message: " + exx.getMessage());
55             }
56         }
57           
58      
59           File JavaDoc temp;
60               
61          if(indexDir!=null&&!indexDir.equals(""))
62          {
63                 temp=new File JavaDoc(indexDir);
64          }
65          else
66          {
67             if(defaultIndexDir.endsWith("\\")||defaultIndexDir.endsWith("/"))
68                   temp=new File JavaDoc(defaultIndexDir+siteName);
69             else
70                 temp=new File JavaDoc(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 JavaDoc 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 JavaDoc 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 JavaDoc ex) {
102                   System.out.println(" caught a " + e.getClass() +
103                    "\n with message: " + e.getMessage());
104             }
105         }
106      }
107     
108     public void setUpSearcherQuery(Properties JavaDoc requestParameters)
109      {
110          this.requestParameters =requestParameters;
111          String JavaDoc modfrom = requestParameters.getProperty("modifiedfrom");
112          String JavaDoc modto = requestParameters.getProperty("modifiedto");
113         
114         String JavaDoc wordKew = requestParameters.getProperty("propertiesKey");
115         String JavaDoc wordValue = requestParameters.getProperty("propertiesValue");
116         
117         String JavaDoc metaDataKey = requestParameters.getProperty("metaDataKey");
118         String JavaDoc metaDataValue = requestParameters.getProperty("metaDataValue");
119      
120             
121         String JavaDoc properties = null;
122         String JavaDoc modified = null;
123         String JavaDoc 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 JavaDoc contentLength = requestParameters.getProperty("contentLength");
198          String JavaDoc wordLength = requestParameters.getProperty("wordLength");
199          String JavaDoc 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 JavaDoc 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 JavaDoc e){
233         /* try{
234                 SnapperManager.getInstance().getLoggingManager().error("Could not search Docs for Query : "+term+" \n caught a " + e.getClass() +
235                    "\n with message: " + e.getMessage());
236             } catch (Exception ex) {
237                   System.out.println(" caught a " + e.getClass() +
238                    "\n with message: " + e.getMessage());
239             }
240           */

241           }
242         }
243     
244     
245   public void searchDocs(String JavaDoc term,String JavaDoc highlighterContextQuery,String JavaDoc language)
246     {
247         try{
248         searcher.searchDocs(term,highlighterContextQuery,language);
249       }catch(Exception JavaDoc e){
250     // try{
251
// SnapperManager.getInstance().getLoggingManager().error("Could not search Docs for Query : "+term+" \n caught a " + e.getClass() +
252
// "\n with message: " + e.getMessage());
253
// } catch (Exception ex) {
254
// System.out.println(" caught a " + e.getClass() +
255
// "\n with message: " + e.getMessage());
256
// }
257
}
258     }
259     
260     public void searchDocs(String JavaDoc term,String JavaDoc highlighterContextQuery,String JavaDoc highlighterPropertiesQuery,String JavaDoc sortType, String JavaDoc language)
261     {
262         
263         try{
264         searcher.searchDocs(term,highlighterContextQuery,highlighterPropertiesQuery,sortType,language);
265     }catch(Exception JavaDoc e){
266     // try{
267
// SnapperManager.getInstance().getLoggingManager().error("Could not search Docs for Query : "+term+" \n caught a " + e.getClass() +
268
// "\n with message: " + e.getMessage());
269
// } catch (Exception ex) {
270
// System.out.println(" caught a " + e.getClass() +
271
// "\n with message: " + e.getMessage());
272
// }
273
}
274     }
275     
276     public int getSearchedDocsNumber()
277     {
278      return searcher.getSearchedDocsNumber();
279     }
280     
281     public String JavaDoc getXmlSearchedResult(int begin,int end)
282     {
283      return searcher.getXmlSearchedResult(begin,end);
284     }
285     
286     public String JavaDoc getXmlSearchedResult()
287     {
288      return searcher.getXmlSearchedResult();
289     }
290     
291     public void close()
292     {
293         try{
294             searcher.close();
295         }catch(Exception JavaDoc 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 JavaDoc 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 JavaDoc luceneQuery()
311      {
312        
313         StringBuffer JavaDoc luceneQuery = new StringBuffer JavaDoc();
314                 
315         int i = 1;
316         
317         for (Enumeration JavaDoc e = luceneParameters.propertyNames() ; e.hasMoreElements() ;)
318          {
319           String JavaDoc field = (String JavaDoc)e.nextElement();
320           String JavaDoc query = (String JavaDoc)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 JavaDoc simpleLuceneQuery()
343      {
344         
345         StringBuffer JavaDoc luceneQuery = new StringBuffer JavaDoc();
346  
347           String JavaDoc contents = (String JavaDoc)luceneParameters.getProperty("contents");
348         
349          if(contents!=null)
350           {
351                 String JavaDoc typeFromXML = Snapper.getSimpleSearchType();
352     
353                 if(typeFromXML==null||typeFromXML.equals("Default"))
354                 {//default
355
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