KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.enhydra.snapper.business;
2
3 import java.io.File JavaDoc;
4 import java.io.FileOutputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.PrintStream JavaDoc;
7 import java.io.Reader JavaDoc;
8 import java.io.StringReader JavaDoc;
9 import java.sql.Timestamp JavaDoc;
10 import java.util.Enumeration JavaDoc;
11 import java.util.Properties JavaDoc;
12
13 import org.enhydra.snapper.business.xml.*;
14 import org.enhydra.snapper.Log;
15 import org.enhydra.snapper.Snapper;
16 import org.enhydra.snapper.spec.Converter;
17 import org.enhydra.snapper.spec.ConverterFactory;
18 import org.enhydra.snapper.spec.Path;
19 import org.enhydra.snapper.spec.PathFactory;
20 import org.enhydra.snapper.spec.PathType;
21 import org.enhydra.snapper.spec.PathTypeFactory;
22 import org.enhydra.snapper.spec.SearcherBase;
23 import org.enhydra.snapper.spec.SearcherBaseFactory;
24 import org.enhydra.snapper.spec.Site;
25 import org.enhydra.snapper.spec.SiteList;
26 import org.enhydra.snapper.spec.SiteListFactory;
27 import org.enhydra.snapper.spec.Builder;
28 import org.enhydra.snapper.spec.TransformManager;
29 import org.enhydra.snapper.spec.TransformManagerFactory;
30
31 import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
32 import com.lutris.appserver.server.sql.DBTransaction;
33 import java.io.*;
34
35 /**
36  * @author Milin Radivoj
37  *
38  */

39
40 public class BuilderImpl implements Builder {
41     
42     
43     Properties JavaDoc standardRequestParameters = new Properties JavaDoc();
44     Properties JavaDoc siteRequestParameters = new Properties JavaDoc();
45     DBTransaction dbTransaction;
46     Summary result;
47   
48    
49     public void setStandardRequestParameters(Properties JavaDoc prop)
50     {
51         standardRequestParameters = prop;
52     }
53     
54     public void setSiteRequestParameters(Properties JavaDoc prop)
55     {
56         siteRequestParameters = prop;
57     }
58     
59    
60  
61    public String JavaDoc getResult(DBTransaction db)
62     {
63     dbTransaction=db;
64    
65     result = new SummaryImpl();
66     
67     Configuration one = addConfigurationSection();
68     SearchedParameters two = addSearchParameters();
69     Search free = addSearchResult();
70     
71     result.setConfiguration(one);
72     result.setSearchedParameters(two);
73     result.setSearch(free);
74
75     //File temp = new File("c:/xml.xml");
76

77     try {
78         result.setOutputEncoding("UTF-8");
79     //result.marshal(temp);
80

81     
82     Writer is = new StringWriter();
83     result.marshal(is);
84
85     return is.toString();
86     
87     } catch (IOException JavaDoc ex){
88         Log.logException(ex);
89         
90     }
91     return null;
92     
93     }
94     
95    public void writeResult(DBTransaction db,OutputStream JavaDoc os)
96     {
97     dbTransaction=db;
98   
99     result = new SummaryImpl();
100     
101     Configuration one = addConfigurationSection();
102     SearchedParameters two = addSearchParameters();
103     Search free = addSearchResult();
104     
105     result.setConfiguration(one);
106     result.setSearchedParameters(two);
107     result.setSearch(free);
108
109     //File temp = new File("c:/xml.xml");
110

111     try {
112         result.setOutputEncoding("UTF-8");
113     //result.marshal(temp);
114

115     result.marshal(os);
116
117     
118     
119     } catch (IOException JavaDoc ex){
120     Log.logException(ex);
121     
122     }
123
124     
125     }
126    
127    
128      private Configuration addConfigurationSection()
129      {
130         Configuration result = new ConfigurationImpl();
131         
132         Site[] sites;
133         try {
134             SiteList sl = SiteListFactory.getSiteList("org.enhydra.snapper.business.SiteListImpl");
135             sites = sl.getList(dbTransaction);
136             
137         }catch (Exception JavaDoc e){
138                         Log.logException(e);
139                         sites = null;
140            
141         }
142          
143          for (int i = 0; i < sites.length; i++)
144         {
145             try {
146             String JavaDoc siteName = sites[i].getName();
147             String JavaDoc siteId = sites[i].getID();
148             String JavaDoc indexDir = sites[i].getINDEXDIR();
149             
150             boolean download = sites[i].getDOWNLOAD();
151             int size = sites[i].getSize();
152             boolean isSearched = sites[i].getSEARCH();
153             int age = sites[i].getAge();
154          
155             int fileNR = sites[i].getFILENR();
156             int queryNR = sites[i].getQUERYNR();
157             Timestamp JavaDoc lastQuery = sites[i].getLASTQUERY();
158             Timestamp JavaDoc lastUpdate = sites[i].getLASTUPDATE();
159                         
160             org.enhydra.snapper.business.xml.Site siteSection = org.enhydra.snapper.business.xml.SiteImpl.newInstance();
161             
162             Name n = NameImpl.newInstance();
163             n.setValue(siteName);
164             siteSection.setName(n);
165             
166             ID id = IDImpl.newInstance();
167             id.setValue(siteId);
168             siteSection.setID(id);
169             
170             if(!indexDir.equals(""))
171             {
172                 IndexDir temp = IndexDirImpl.newInstance();
173                 
174              if(Snapper.getFileSeparatorConvention()!=null &&
175                     Snapper.getFileSeparatorConvention().equalsIgnoreCase("unix")) {
176              indexDir = indexDir.replaceAll("\\\\","/");
177                   }
178                 
179                 temp.setValue(filter(indexDir));
180                 siteSection.setIndexDir(temp);
181             }
182             else
183             {
184                 IndexDir temp = IndexDirImpl.newInstance();
185                 String JavaDoc inDir = filter(Snapper.getIndexDir()+File.separator+siteName);
186                 if(Snapper.getFileSeparatorConvention()!=null &&
187                         Snapper.getFileSeparatorConvention().equalsIgnoreCase("unix")) {
188                     inDir = inDir.replaceAll("\\\\","/");
189                       }
190                 
191                 temp.setValue(inDir);
192                 siteSection.setIndexDir(temp);
193             }
194                 
195             if(download)
196             {
197                 org.enhydra.snapper.business.xml.DownloadImpl temp = org.enhydra.snapper.business.xml.DownloadImpl.newInstance();
198                 temp.setValue("true");
199                 siteSection.setDownload(temp);
200             }
201             else
202             {
203                 org.enhydra.snapper.business.xml.DownloadImpl temp = org.enhydra.snapper.business.xml.DownloadImpl.newInstance();
204                 temp.setValue("false");
205                 siteSection.setDownload(temp);
206             }
207             
208             
209             if(isSearched)
210             {
211                 SearchedImpl temp = SearchedImpl.newInstance();
212                 temp.setValue("true");
213                 siteSection.setSearched(temp);
214             }
215             else
216             {
217                 SearchedImpl temp = SearchedImpl.newInstance();
218                 temp.setValue("false");
219                 siteSection.setSearched(temp);
220             }
221             
222             FileNR fileN = FileNRImpl.newInstance();
223             fileN.setValue((new Integer JavaDoc(fileNR)).toString());
224             siteSection.setFileNR(fileN);
225         
226             QueryNR queryN = QueryNRImpl.newInstance();
227             queryN.setValue((new Integer JavaDoc(queryNR)).toString());
228             siteSection.setQueryNR(queryN);
229         
230             
231             
232             if(lastQuery!=null)
233             {
234                 LastQueryImpl temp =LastQueryImpl.newInstance();
235                 temp.setValue(lastQuery.toLocaleString());
236                 siteSection.setLastQuery(temp);
237             }
238             else
239             {
240                 LastQueryImpl temp =LastQueryImpl.newInstance();
241                 temp.setValue("");
242             
243                 siteSection.setLastQuery(temp);
244             }
245             if(lastUpdate!=null)
246             {
247                 LastUpdateImpl temp = LastUpdateImpl.newInstance();
248                 temp.setValue(lastUpdate.toLocaleString());
249                 siteSection.setLastUpdate(temp);
250             }
251             else
252             {
253                 LastUpdateImpl temp = LastUpdateImpl.newInstance();
254                 temp.setValue("");
255              
256                 siteSection.setLastUpdate(temp);
257             }
258             
259             Path pt;
260             Path[] pathArray;
261             
262             pt = PathFactory.getPath("org.enhydra.snapper.business.PathImpl");
263             
264             pathArray = pt.getListForID(dbTransaction, siteId);
265             for (int num = 0; num < pathArray.length; num++)
266             {
267                   Path currentPath = pathArray[num];
268                   org.enhydra.snapper.business.xml.Path pathXML = org.enhydra.snapper.business.xml.PathImpl.newInstance();
269                   
270                   String JavaDoc pathType = currentPath.getType();
271                   
272                   PathType ptype;
273                   ptype = PathTypeFactory.getPathType("org.enhydra.snapper.business.PathTypeImpl");
274                   
275                   PathType pathTypeString = ptype.findPathTypeByID(pathType,dbTransaction);
276                   
277                   TypeImpl temp = TypeImpl.newInstance();
278                   temp.setValue(pathTypeString.getName());
279                   pathXML.setType(temp);
280                 
281                   String JavaDoc pathRoot = currentPath.getRoot();
282                   RootImpl temp1 = RootImpl.newInstance();
283                   
284                  if(Snapper.getFileSeparatorConvention()!=null &&
285                         Snapper.getFileSeparatorConvention().equalsIgnoreCase("unix")) {
286                     pathRoot = pathRoot.replaceAll("\\\\","/");
287                       }
288                   
289                   temp1.setValue(filter(pathRoot));
290                   pathXML.setRoot(temp1);
291                   String JavaDoc mappingRoot = currentPath.getMappingRoot();
292                   
293                   MappingRootImpl temp2 = MappingRootImpl.newInstance();
294                   temp2.setValue(filter(mappingRoot));
295                   pathXML.setMappingRoot(temp2);
296                   
297                   siteSection.addPath(pathXML);
298             }
299             result.addSite(siteSection);
300              
301             
302         }catch (Exception JavaDoc e){
303             Log.logException(e);
304             sites = null;
305
306          }
307             
308           }
309          
310      return result;
311      }
312      
313 private SearchedParameters addSearchParameters()
314      {
315             
316     SearchedParameters result = SearchedParametersImpl.newInstance();
317         for (Enumeration JavaDoc e = standardRequestParameters.propertyNames() ; e.hasMoreElements() ;)
318          {
319             String JavaDoc name = (String JavaDoc)e.nextElement();
320             String JavaDoc value = (String JavaDoc)standardRequestParameters.getProperty(name);
321             
322           Parameter perameter = ParameterImpl.newInstance();
323         
324           KeyImpl temp = KeyImpl.newInstance();
325           temp.setValue(name);
326           perameter.setKey(temp);
327           
328           ValueImpl temp1 = ValueImpl.newInstance();
329           temp1.setValue(filter(value));
330         
331           perameter.setValue(temp1);
332           result.addParameter(perameter);
333          }
334         
335         for (Enumeration JavaDoc e = siteRequestParameters.propertyNames() ; e.hasMoreElements() ;)
336      {
337            String JavaDoc name = (String JavaDoc)e.nextElement();
338            String JavaDoc value = (String JavaDoc)siteRequestParameters.getProperty(name);
339            
340           Parameter perameter = ParameterImpl.newInstance();
341          
342           KeyImpl temp = KeyImpl.newInstance();
343           temp.setValue(name);
344           perameter.setKey(temp);
345         
346           ValueImpl temp1 = ValueImpl.newInstance();
347           temp1.setValue(filter(value));
348     
349           perameter.setValue(temp1);
350           result.addParameter(perameter);
351      }
352         
353         return result;
354      }
355      
356      
357      private Search addSearchResult()
358      {
359     
360         org.enhydra.snapper.business.xml.Search result = org.enhydra.snapper.business.xml.SearchImpl.newInstance();
361         String JavaDoc searceherResult = null;
362         try {
363             SearcherBase searcher = SearcherBaseFactory.createSearcherBase("org.enhydra.snapper.business.SearcherBaseImpl");
364             searcher.setUpSearcher(siteRequestParameters,dbTransaction);
365             searcher.setUpSearcherQuery(standardRequestParameters);
366              
367             searcher.searchDocs();
368             
369             int docsNumber=searcher.getSearchedDocsNumber();
370                          
371             String JavaDoc frS = standardRequestParameters.getProperty("from");
372             String JavaDoc toS = standardRequestParameters.getProperty("to");
373             
374             if((frS==null&&toS==null))
375                 searceherResult = searcher.getXmlSearchedResult(1,10);
376             
377             if((frS!=null&&toS==null))
378              {
379                 int fr = (new Integer JavaDoc(frS)).intValue();
380                 searceherResult = searcher.getXmlSearchedResult(fr,fr+9);
381              }
382             
383             if((frS==null&&toS!=null))
384              {
385                  int to = (new Integer JavaDoc(toS)).intValue();
386                 searceherResult = searcher.getXmlSearchedResult(to-9,to);
387               }
388             
389             if((frS!=null&&toS!=null))
390              {
391                  int to = (new Integer JavaDoc(toS)).intValue();
392                  int fr = (new Integer JavaDoc(frS)).intValue();
393                            
394                  int range = to - fr;
395                  int maxRange = Snapper.getMaxRange();
396                  
397                  
398                     
399                     if(maxRange!=0&&range>maxRange)
400                      {
401                         to = fr+maxRange-1;
402                      }
403                 searceherResult = searcher.getXmlSearchedResult(fr,to);
404               }
405     
406                ////
407
// File temp = new File("c:\\lucinov.xml");
408
// FileOutputStream eee = new FileOutputStream(temp);
409
// PrintStream myOutput = new PrintStream(eee);
410
// myOutput.print(searceherResult);
411
/////////
412

413     
414                
415             ConverterImpl converter = new ConverterImpl();
416             converter.setXml(searceherResult);
417             result = converter.transform();
418             searcher.close();
419     
420         return result;
421             
422          } catch(Exception JavaDoc ex) {
423             Log.logException(ex);
424             return null;
425         }
426     
427      }
428      public static String JavaDoc replaceAll(
429             String JavaDoc input,
430             String JavaDoc forReplace,
431             String JavaDoc replaceWith) {
432             if( input == null )
433               return null;
434             StringBuffer JavaDoc result = new StringBuffer JavaDoc();
435             boolean hasMore = true;
436             while (hasMore) {
437               int start = input.indexOf(forReplace);
438               int end = start + forReplace.length();
439               if (start != -1) {
440                 result.append(input.substring(0, start) + replaceWith);
441                 input = input.substring(end);
442               }
443               else {
444                 hasMore = false;
445                 result.append(input);
446               }
447             }
448             if (result.toString().equals(""))
449               return input; //nothing is changed
450
else
451               return result.toString();
452           }
453
454      private String JavaDoc filter(String JavaDoc stringToFilter)
455       {
456          //potrebno zbog xml-a
457
stringToFilter = replaceAll(stringToFilter,"&","&amp;");
458       return stringToFilter;
459       }
460
461     
462 }
463
Popular Tags