001: package org.mdarad.framework.index;
002:
003: import java.io.IOException;
004: import java.util.Collection;
005: import java.util.Date;
006: import java.util.HashMap;
007: import java.util.Iterator;
008: import java.util.Locale;
009: import java.util.Map;
010: import java.util.Vector;
011:
012: import org.apache.lucene.analysis.Analyzer;
013: import org.apache.lucene.analysis.standard.StandardAnalyzer;
014: import org.apache.lucene.document.Document;
015: import org.apache.lucene.index.IndexWriter;
016: import org.apache.lucene.queryParser.ParseException;
017: import org.apache.lucene.queryParser.QueryParser;
018: import org.apache.lucene.search.Hits;
019: import org.apache.lucene.search.IndexSearcher;
020: import org.apache.lucene.search.Query;
021: import org.apache.lucene.search.Searcher;
022: import org.apache.lucene.store.Directory;
023: import org.apache.lucene.store.RAMDirectory;
024: import org.dataisland.primitives.bean.LocalizationContext;
025: import org.dataisland.primitives.exception.LocalizationException;
026:
027: abstract public class AbstractModelIndexFacade {
028: abstract public Map getEntityDocuments(
029: LocalizationContext localizationContext)
030: throws IndexationException, LocalizationException;
031:
032: Map directories = null;
033: Date lastIndexed = null;
034:
035: public void buildIndex(LocalizationContext localizationContext)
036: throws IndexationException, LocalizationException {
037: try {
038: Map documents = getEntityDocuments(localizationContext);
039:
040: Map newDirectories = new HashMap();
041: Iterator documentLocaleIterator = documents.keySet()
042: .iterator();
043: while (documentLocaleIterator.hasNext()) {
044: Locale locale = (Locale) documentLocaleIterator.next();
045: Directory directory = new RAMDirectory();
046: newDirectories.put(locale, directory);
047: Collection documentsForLocale = (Collection) documents
048: .get(locale);
049:
050: IndexWriter indexWriter = new IndexWriter(directory,
051: getAnalyzer(locale), true);
052:
053: Iterator documentIterator = documentsForLocale
054: .iterator();
055: while (documentIterator.hasNext()) {
056: indexWriter.addDocument((Document) documentIterator
057: .next());
058: }
059:
060: indexWriter.optimize();
061: indexWriter.close();
062: }
063:
064: directories = newDirectories;
065: lastIndexed = new Date();
066: } catch (IOException ioe) {
067: throw new IndexationException(ioe);
068: }
069: }
070:
071: public Hits getSearchResults(String queryString, Locale locale)
072: throws IndexNotFoundException, SearchException {
073: Hits hits = null;
074: Directory directory = null;
075:
076: if (directories == null)
077: throw new IndexNotFoundException(
078: "Directory map has not been initialized.");
079:
080: Map directories = this .directories;
081: if (directories != null) {
082: directory = (Directory) directories.get(locale);
083: }
084:
085: if (directory == null)
086: throw new IndexNotFoundException(
087: "Directory map does not contain locale.");
088:
089: Searcher searcher = null;
090:
091: try {
092: searcher = new IndexSearcher(directory);
093: } catch (IOException ioe) {
094: throw new IndexNotFoundException(ioe);
095: }
096:
097: Query query = null;
098:
099: try {
100: query = QueryParser
101: .parse(
102: queryString,
103: AbstractEntityIndexFacade.DOCUMENT_FIELD_KEY_CONTENTS,
104: getAnalyzer(locale));
105: hits = searcher.search(query);
106: } catch (ParseException pe) {
107: throw new SearchException(pe);
108: } catch (IOException ioe) {
109: throw new SearchException(ioe);
110: }
111:
112: return hits;
113: }
114:
115: static Analyzer defaultAnalyzer = new StandardAnalyzer();
116:
117: public Analyzer getAnalyzer(Locale locale) {
118: // TODO : get right Analyzer
119: return defaultAnalyzer;
120: }
121:
122: static public void mergeDocuments(Map target, Map source) {
123: Iterator sourceMapLocaleIterator = source.keySet().iterator();
124: while (sourceMapLocaleIterator.hasNext()) {
125: Locale locale = (Locale) sourceMapLocaleIterator.next();
126: Collection sourceForLocale = (Collection) source
127: .get(locale);
128: Collection targetForLocale = (Collection) target
129: .get(locale);
130: if (targetForLocale == null) {
131: targetForLocale = new Vector();
132: target.put(locale, targetForLocale);
133: }
134: targetForLocale.addAll(sourceForLocale);
135: }
136: }
137:
138: public Date getLastIndexed() {
139: return lastIndexed;
140: }
141: }
|