001: package org.mdarad.framework.index;
002:
003: import java.io.IOException;
004: import java.util.Collection;
005: import java.util.HashMap;
006: import java.util.Iterator;
007: import java.util.Locale;
008: import java.util.Map;
009:
010: import org.apache.lucene.analysis.Analyzer;
011: import org.apache.lucene.analysis.standard.StandardAnalyzer;
012: import org.apache.lucene.document.Document;
013: import org.apache.lucene.index.IndexWriter;
014: import org.apache.lucene.queryParser.ParseException;
015: import org.apache.lucene.queryParser.QueryParser;
016: import org.apache.lucene.search.Hits;
017: import org.apache.lucene.search.IndexSearcher;
018: import org.apache.lucene.search.Query;
019: import org.apache.lucene.search.Searcher;
020: import org.apache.lucene.store.Directory;
021: import org.apache.lucene.store.RAMDirectory;
022: import org.mdarad.framework.delegates.BusinessDelegate;
023: import org.mdarad.framework.exception.SystemException;
024:
025: abstract public class AbstractIndexDelegate implements BusinessDelegate {
026: public final static String DEFAULT_FIELD_NAME = "org.mdarad.framework.index.AbstractIndexFacade.DEFAULT_FIELD_NAME";
027:
028: abstract public Map getEntityDocuments();
029:
030: Map directories = null;
031:
032: public void buildIndex() throws SystemException {
033: try {
034: Map documents = getEntityDocuments();
035:
036: Map newDirectories = new HashMap();
037: Iterator documentLocaleIterator = documents.keySet()
038: .iterator();
039: while (documentLocaleIterator.hasNext()) {
040: Locale locale = (Locale) documentLocaleIterator.next();
041: Directory directory = new RAMDirectory();
042: newDirectories.put(locale, directory);
043: Collection documentsForLocale = (Collection) documents
044: .get(locale);
045:
046: IndexWriter indexWriter = new IndexWriter(directory,
047: getAnalyzerer(locale), true);
048:
049: Iterator documentIterator = documentsForLocale
050: .iterator();
051: while (documentIterator.hasNext()) {
052: indexWriter.addDocument((Document) documentIterator
053: .next());
054: }
055:
056: indexWriter.optimize();
057: indexWriter.close();
058: }
059:
060: directories = newDirectories;
061: } catch (IOException ioe) {
062: throw new SystemException(ioe);
063: }
064: }
065:
066: public Hits getSearchResults(String queryString, Locale locale)
067: throws IndexNotFoundException {
068: Hits hits = null;
069: Directory directory = null;
070:
071: if (directories == null)
072: throw new IndexNotFoundException(
073: "Directory map has not been initialized.");
074:
075: Map directories = this .directories;
076: if (directories != null) {
077: directory = (Directory) directories.get(locale);
078: }
079:
080: if (directory == null)
081: throw new IndexNotFoundException(
082: "Directory map does not contain locale.");
083:
084: Searcher searcher = null;
085:
086: try {
087: searcher = new IndexSearcher(directory);
088: } catch (IOException ioe) {
089: throw new IndexNotFoundException(ioe);
090: }
091:
092: Query query = null;
093:
094: try {
095:
096: query = QueryParser.parse(queryString, DEFAULT_FIELD_NAME,
097: getAnalyzerer(locale));
098:
099: } catch (ParseException e) {
100: // TODO Auto-generated catch block
101: e.printStackTrace();
102: }
103:
104: try {
105: hits = searcher.search(query);
106: } catch (IOException e) {
107: // TODO Auto-generated catch block
108: e.printStackTrace();
109: }
110:
111: return hits;
112: }
113:
114: static Analyzer defaultAnalyzer = new StandardAnalyzer();
115:
116: public Analyzer getAnalyzerer(Locale locale) {
117: // TODO : get right Analyzer
118: return defaultAnalyzer;
119: }
120: }
|