KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mvnforum > search > company > CompanyIndexer


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/search/company/CompanyIndexer.java,v 1.18 2006/04/14 17:05:27 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.18 $
5  * $Date: 2006/04/14 17:05:27 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding mvnForum MUST remain
12  * intact in the scripts and in the outputted HTML.
13  * The "powered by" text/logo with a link back to
14  * http://www.mvnForum.com and http://www.MyVietnam.net in
15  * the footer of the pages MUST remain visible when the pages
16  * are viewed on the internet or intranet.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  *
32  * Support can be obtained from support forums at:
33  * http://www.mvnForum.com/mvnforum/index
34  *
35  * Correspondence and Marketing Questions can be sent to:
36  * info at MyVietnam net
37  *
38  * @author: Minh Nguyen
39  * @author: Dejan Krsmanovic dejan_krsmanovic@yahoo.com
40  */

41 package com.mvnforum.search.company;
42
43 import java.io.IOException JavaDoc;
44
45 import net.myvietnam.mvncore.exception.SearchException;
46 import net.myvietnam.mvncore.util.DateUtil;
47 import net.myvietnam.mvncore.util.TimerUtil;
48
49 import org.apache.commons.logging.Log;
50 import org.apache.commons.logging.LogFactory;
51 import org.apache.lucene.analysis.Analyzer;
52 import org.apache.lucene.analysis.standard.StandardAnalyzer;
53 import org.apache.lucene.document.*;
54 import org.apache.lucene.index.*;
55 import org.apache.lucene.search.IndexSearcher;
56
57 import com.mvnforum.*;
58 import com.mvnforum.db.CompanyBean;
59
60 public class CompanyIndexer
61 {
62     private static Log log = LogFactory.getLog(CompanyIndexer.class);
63
64     //Field names (used for indexing)
65
public static final String JavaDoc FIELD_COMPANY_ID = "CompanyID";
66     public static final String JavaDoc FIELD_COMPANY_NAME = "CompanyName";
67     public static final String JavaDoc FIELD_COMPANY_ADDRESS = "companyAddress";
68     public static final String JavaDoc FIELD_CREATION_DATE = "CompanyCreationDate";
69     public static final String JavaDoc FIELD_MODIFIED_DATE = "CompanyModifiedDate";
70
71     //public static final String PROPERTY_SEARCH_PATH = "search.path";
72
//public static final String PROPERTY_SEARCH_AUTOINDEX = "search.autoindex";
73

74     //Timer is used for scheduling jobs
75
private static Analyzer analyzer;
76
77     private static long lastOptimizeTime = 0;
78
79     static {
80         initializeAnalyzer();
81     }
82
83     public static void scheduleAddCompanyTask(CompanyBean companyBean) {
84         AddUpdateCompanyIndexTask task = new AddUpdateCompanyIndexTask(companyBean, AddUpdateCompanyIndexTask.OPERATION_ADD);
85         TimerUtil.getInstance().schedule(task, 0);
86     }
87
88     public static void scheduleUpdateCompanyTask(CompanyBean companyBean) {
89         AddUpdateCompanyIndexTask task = new AddUpdateCompanyIndexTask(companyBean, AddUpdateCompanyIndexTask.OPERATION_UPDATE);
90         TimerUtil.getInstance().schedule(task, 0);
91     }
92
93     public static void scheduleDeleteCompanyTask(int objectID) {
94         DeleteCompanyIndexTask task = new DeleteCompanyIndexTask(objectID);
95         TimerUtil.getInstance().schedule(task, 0);
96     }
97
98     public static void scheduleRebuildIndexTask() {
99         RebuildCompanyIndexTask task = new RebuildCompanyIndexTask();
100         TimerUtil.getInstance().schedule(task, 0);
101     }
102
103     static Analyzer getAnalyzer() {
104         return analyzer;
105     }
106
107     /**
108      * This class will load analyzer when starting. If specified analyzer class
109      * cannot be loaded then default analyzer will be used.
110      */

111     private static void initializeAnalyzer() {
112         String JavaDoc analyzerClassName = MVNForumFactoryConfig.getLuceneAnalyzerClassName();
113         if ( (analyzerClassName == null) || (analyzerClassName.equals("")) ) {
114             //create standard analyzer
115
//String[] stopWords = this.loadStopWords();
116
analyzer = new StandardAnalyzer();
117             log.debug("Using StandardAnalyzer for indexing");
118         } else {
119             //try to create specified analyzer
120
try {
121                 log.debug("About to load Analyzer [" + analyzerClassName + "] for indexing");
122                 analyzer = (Analyzer) Class.forName(analyzerClassName).newInstance();
123             } catch (Exception JavaDoc e) {
124                 log.warn("Cannot load " + analyzerClassName + ". Loading StandardAnalyzer");
125                 analyzer = new StandardAnalyzer();
126             }
127         }
128     }
129
130     /**
131      * This method is used for getting new IndexWriter. It can create new index
132      * or add company to existing index. Creating new index will delete previous so it
133      * should be used for rebuilding index.
134      * @param create - true if new index should be created.
135      * - false for adding companies to existing index
136      * @return IndexWriter object that is used for adding companies to index
137      */

138     static IndexWriter getIndexWriter(boolean create) throws SearchException {
139         IndexWriter writer = null;
140
141         //If create = false, we will create IndexWriter with false argument
142
if (create == false) {
143             try {
144                 writer = new IndexWriter(MVNForumConfig.getSearchCompanyIndexDir(), analyzer, false);
145                 if (MVNForumConfig.getSearchCompanyIndexType() == MVNForumGlobal.SEARCH_INDEX_TYPE_DISK) {
146                     writer.setUseCompoundFile(true);
147                 }
148                 return writer;
149             } catch (IOException JavaDoc e) {
150                 log.warn("Cannot open existed index. New index will be created.", e);
151                 //Ignore Exception. We will try to create index with true parameter
152
}
153         }
154         // We are here in two cases: We wanted to create new index or because
155
// index doesn't existed
156
try {
157             //This will create new index and delete existing
158
writer = new IndexWriter(MVNForumConfig.getSearchCompanyIndexDir(true), analyzer, true);
159             if (MVNForumConfig.getSearchCompanyIndexType() == MVNForumGlobal.SEARCH_INDEX_TYPE_DISK) {
160                 writer.setUseCompoundFile(true);
161             }
162             return writer;
163         } catch (IOException JavaDoc e) {
164             throw new SearchException("Error while creating index writer");
165         }
166     }
167
168     /**
169      * This method is used for adding single company to index
170      * Note: this method doesnt close the writer
171      * @param companyBean A company that should be indexed
172      * @param writer IndexWriter that is used for storing
173      * @throws SearchException
174      */

175     static void doIndexCompany(CompanyBean companyBean, IndexWriter writer) throws SearchException {
176         //log.debug("WHAT COMPANY ?" + companyBean.getCompanyName());
177
//Note that:: user can add a company without having the such fields
178
//However, search engine trict this problem more seriously.
179
if (companyBean == null) return;
180         if ( (companyBean.getCompanyID() == 0)||
181              (companyBean.getCompanyName() == null || companyBean.getCompanyName().equals("")) ||
182              (companyBean.getCompanyAddress() == null) ||
183              (companyBean.getCompanyCreationDate() == null) ||
184              (companyBean.getCompanyModifiedDate() == null)) {
185             return;
186         }
187
188         //Each company will be represented as a document
189
Document companyDocument = new Document();
190         //Document has following fields that could be queried on
191
companyDocument.add(Field.Keyword(FIELD_COMPANY_ID, Integer.toString(companyBean.getCompanyID())));
192         //document companyname and company address is not stored since we can retrieve them from database
193
companyDocument.add(Field.UnStored(FIELD_COMPANY_NAME, companyBean.getCompanyName()));
194         companyDocument.add(Field.UnStored(FIELD_COMPANY_ADDRESS, companyBean.getCompanyAddress()));
195         //add date field
196
companyDocument.add(Field.Keyword(FIELD_CREATION_DATE, DateField.dateToString(companyBean.getCompanyCreationDate())));//companyBean.getCompanyCreationDate())); // DateField.dateToString(companyBean.getCompanyCreationDate())));
197
companyDocument.add(Field.Keyword(FIELD_MODIFIED_DATE, DateField.dateToString(companyBean.getCompanyModifiedDate()))); // DateField.dateToString(companyBean.getCompanyModifiedDate())));
198
//now we have created document with fields so we can store it
199
try {
200             writer.addDocument(companyDocument);
201         } catch (IOException JavaDoc e) {
202             log.error("CompanyIndexer.doIndexCompany failed", e);
203             throw new SearchException("Error writing new company to index");
204         }
205     }
206
207     /**
208      * Add single company to index
209      * @param companyBean
210      * @throws SearchException
211      */

212     static void addCompanyToIndex(CompanyBean companyBean) throws SearchException, IOException JavaDoc {
213         IndexWriter writer = null;
214         try {
215             writer = getIndexWriter(false);
216             if (writer == null) {
217                 log.warn("Cannot get the IndexWriter");
218                 return;
219             }
220             doIndexCompany(companyBean, writer);
221
222             // now check if we should optimize index (each hour)
223
long now = System.currentTimeMillis();
224             long timeFromLastOptimize = now - lastOptimizeTime;
225             if (timeFromLastOptimize > DateUtil.HOUR) {
226                 log.debug("writer.optimize() called in addCompanyToIndex");
227                 writer.optimize();
228                 lastOptimizeTime = now;
229             }
230         } catch (SearchException ex) {
231             throw ex;
232         } finally {
233             try {
234                 if (writer != null) {
235                     writer.close();
236                 }
237             } catch (IOException JavaDoc e) {
238             }
239         }
240     }
241
242     /**
243      * This method is used for deleting company from index.
244      * @param companyID is id of the company that should be deleted
245      * @throws SearchException
246      */

247     static void deleteCompanyFromIndex(int companyID) throws SearchException {
248         IndexReader reader = null;
249         try {
250             reader = IndexReader.open(MVNForumConfig.getSearchCompanyIndexDir());
251             if (reader == null) {
252                 log.warn("Cannot get the IndexReader");
253                 return;
254             }
255
256             Term term = new Term(FIELD_COMPANY_ID, String.valueOf(companyID));
257             int deletedCount = reader.delete(term);
258             log.debug("deleteCompanyFromIndex: deleted companies = " + deletedCount);
259         } catch (IOException JavaDoc e) {
260             throw new SearchException("Error trying to delete company with companyID = " + companyID);
261         } finally {
262             try {
263                 if (reader != null) {
264                     reader.close();
265                 }
266             } catch (IOException JavaDoc e) {
267             }
268         }
269     }
270
271     public static IndexSearcher getSearcher() throws IOException JavaDoc {
272         try {
273             IndexSearcher searcher = new IndexSearcher(MVNForumConfig.getSearchCompanyIndexDir());
274             return searcher;
275         } catch (IOException JavaDoc ex) {
276             // we throw new IOException because the original exception
277
// contain sensitive directory information
278
log.error("Cannot access the lucene search index for query. Please check if you have configed mvnForumHome properly. You can also go to Admin Zone to rebuild the Lucene index files.", ex);
279             //@todo : localize me
280
throw new IOException JavaDoc("Cannot access the lucene search index. Please report this error to web site Administrator (check mvnForumHome or rebuild Lucene index).");
281         }
282     }
283
284     public static int getNumDocs() {
285         int numDocs = -1;
286         IndexReader reader = null;
287         try {
288             reader = IndexReader.open(MVNForumConfig.getSearchCompanyIndexDir());
289             if (reader == null) {
290                 log.warn("Cannot get the IndexReader");
291                 return -1;
292             }
293             numDocs = reader.numDocs();
294         } catch ( IOException JavaDoc ioe) {
295             //ignore
296
} finally {
297             try {
298                 if (reader != null) reader.close();
299             } catch (IOException JavaDoc e) {
300                 log.debug("Error closing Lucene IndexReader", e);
301             }
302         }
303         return numDocs;
304     }
305
306 }
307
Popular Tags