KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mdarad > framework > index > AbstractModelIndexFacade


1 package org.mdarad.framework.index;
2
3 import java.io.IOException JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.Date JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.Locale JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Vector JavaDoc;
11
12 import org.apache.lucene.analysis.Analyzer;
13 import org.apache.lucene.analysis.standard.StandardAnalyzer;
14 import org.apache.lucene.document.Document;
15 import org.apache.lucene.index.IndexWriter;
16 import org.apache.lucene.queryParser.ParseException;
17 import org.apache.lucene.queryParser.QueryParser;
18 import org.apache.lucene.search.Hits;
19 import org.apache.lucene.search.IndexSearcher;
20 import org.apache.lucene.search.Query;
21 import org.apache.lucene.search.Searcher;
22 import org.apache.lucene.store.Directory;
23 import org.apache.lucene.store.RAMDirectory;
24 import org.dataisland.primitives.bean.LocalizationContext;
25 import org.dataisland.primitives.exception.LocalizationException;
26
27 abstract public class AbstractModelIndexFacade {
28     abstract public Map JavaDoc getEntityDocuments(LocalizationContext localizationContext) throws IndexationException, LocalizationException;
29     
30     Map JavaDoc directories = null;
31     Date JavaDoc lastIndexed = null;
32
33     public void buildIndex(LocalizationContext localizationContext) throws IndexationException, LocalizationException {
34         try {
35             Map JavaDoc documents = getEntityDocuments(localizationContext);
36
37             Map JavaDoc newDirectories = new HashMap JavaDoc();
38             Iterator JavaDoc documentLocaleIterator = documents.keySet().iterator();
39             while(documentLocaleIterator.hasNext()) {
40                 Locale JavaDoc locale = (Locale JavaDoc) documentLocaleIterator.next();
41                 Directory directory = new RAMDirectory();
42                 newDirectories.put(locale, directory);
43                 Collection JavaDoc documentsForLocale = (Collection JavaDoc) documents.get(locale);
44
45                 IndexWriter indexWriter = new IndexWriter(directory, getAnalyzer(locale), true);
46
47                 Iterator JavaDoc documentIterator = documentsForLocale.iterator();
48                 while(documentIterator.hasNext()) {
49                     indexWriter.addDocument((Document) documentIterator.next());
50                 }
51
52                 indexWriter.optimize();
53                 indexWriter.close();
54             }
55
56             directories = newDirectories;
57             lastIndexed = new Date JavaDoc();
58         } catch (IOException JavaDoc ioe) {
59             throw new IndexationException(ioe);
60         }
61     }
62     
63     public Hits getSearchResults(String JavaDoc queryString, Locale JavaDoc locale) throws IndexNotFoundException, SearchException {
64         Hits hits = null;
65         Directory directory = null;
66         
67         if(directories == null) throw new IndexNotFoundException("Directory map has not been initialized.");
68         
69         Map JavaDoc directories = this.directories;
70         if(directories != null) {
71             directory = (Directory) directories.get(locale);
72         }
73         
74         if(directory == null) throw new IndexNotFoundException("Directory map does not contain locale.");
75
76         Searcher searcher = null;
77         
78         try {
79             searcher = new IndexSearcher(directory);
80         } catch (IOException JavaDoc ioe) {
81             throw new IndexNotFoundException(ioe);
82         }
83         
84         Query query = null;
85         
86         try {
87             query = QueryParser.parse(queryString, AbstractEntityIndexFacade.DOCUMENT_FIELD_KEY_CONTENTS, getAnalyzer(locale));
88             hits = searcher.search(query);
89         } catch (ParseException pe) {
90             throw new SearchException(pe);
91         } catch (IOException JavaDoc ioe) {
92             throw new SearchException(ioe);
93         }
94         
95         return hits;
96     }
97     
98     static Analyzer defaultAnalyzer = new StandardAnalyzer();
99     public Analyzer getAnalyzer(Locale JavaDoc locale) {
100         // TODO : get right Analyzer
101
return defaultAnalyzer;
102     }
103
104     static public void mergeDocuments(Map JavaDoc target, Map JavaDoc source) {
105         Iterator JavaDoc sourceMapLocaleIterator = source.keySet().iterator();
106         while(sourceMapLocaleIterator.hasNext()) {
107             Locale JavaDoc locale = (Locale JavaDoc) sourceMapLocaleIterator.next();
108             Collection JavaDoc sourceForLocale = (Collection JavaDoc) source.get(locale);
109             Collection JavaDoc targetForLocale = (Collection JavaDoc) target.get(locale);
110             if(targetForLocale == null) {
111                 targetForLocale = new Vector JavaDoc();
112                 target.put(locale, targetForLocale);
113             }
114             targetForLocale.addAll(sourceForLocale);
115         }
116     }
117
118     public Date JavaDoc getLastIndexed() {
119         return lastIndexed;
120     }
121 }
122
Popular Tags