KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/search/company/CompanySearchQuery.java,v 1.8 2006/04/14 17:05:27 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.8 $
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 import java.sql.Timestamp JavaDoc;
45 import java.util.ArrayList JavaDoc;
46 import java.util.Collection JavaDoc;
47
48 import net.myvietnam.mvncore.exception.DatabaseException;
49 import net.myvietnam.mvncore.exception.ObjectNotFoundException;
50
51 import org.apache.commons.logging.Log;
52 import org.apache.commons.logging.LogFactory;
53 import org.apache.lucene.analysis.Analyzer;
54 import org.apache.lucene.document.Document;
55 import org.apache.lucene.queryParser.ParseException;
56 import org.apache.lucene.queryParser.QueryParser;
57 import org.apache.lucene.search.*;
58
59 import com.mvnforum.MVNForumConfig;
60 import com.mvnforum.db.CompanyBean;
61 import com.mvnforum.db.DAOFactory;
62
63 /**
64  * This class is used for specifying query that should be searched for. Query
65  * can contain keywords and further it can be filtered by specifying member
66  * name of the company, address as well as date interval for searching.
67  *
68  * searchString contains one or more keywords. Each keyword can use wildcards.
69  * ? for single character and * for multiple characters.
70  * For specifying boolean operators AND and OR operators can be used.....
71  *
72  * For all available options consult Lucene documentation http://jakarta.apache.org/lucene
73  *
74  * @author Dejan Krsmanovic dejan_krsmanovic@yahoo.com
75  */

76 public class CompanySearchQuery
77 {
78     public static final int SEARCH_ANY_DATE = 0;
79
80     public static final int SEARCH_NEWER = 1;
81     public static final int SEARCH_OLDER = 2;
82
83     private static Log log = LogFactory.getLog(CompanySearchQuery.class);
84
85     private int companyID = -1;
86     private String JavaDoc companyNameKey = null;
87     private String JavaDoc companyAddressKey = null;
88     private Timestamp JavaDoc companyDateKey = null;
89     private Timestamp JavaDoc fromCompanyDateKey = null;
90     private Timestamp JavaDoc toCompanyDateKey = null;
91
92     private int hitCount = 0;
93     private Collection JavaDoc searchResult = null;
94
95     public CompanySearchQuery() {
96     }
97
98     /**
99      * Set name of the author that should be searched for
100      * @param companyId
101      */

102     public void setCompanyId(int companyId) {
103         this.companyID = companyId;
104     }
105
106     /**
107      * Set string that should be searched for.
108      * @param companyNameKey
109      */

110     public void setCompanyAddressKey(String JavaDoc companyAddressKey) {
111         this.companyAddressKey = companyAddressKey;
112     }
113
114     /**
115      * Set string that should be searched for.
116      * @param companyNameKey
117      */

118     public void setCompanyNameKey(String JavaDoc companyNameKey) {
119         this.companyNameKey = companyNameKey;
120     }
121
122     public void setCompanyDateKey(Timestamp JavaDoc companyDateKey) {
123         this.companyDateKey = companyDateKey;
124     }
125
126     public void setFromCompanyDateKey(Timestamp JavaDoc fromCompanyDateKey) {
127         this.fromCompanyDateKey = fromCompanyDateKey;
128     }
129
130     public void setToCompanyDateKey(Timestamp JavaDoc toCompanyDateKey) {
131         this.toCompanyDateKey = toCompanyDateKey;
132     }
133
134     protected IndexSearcher getSearcher() throws IOException JavaDoc {
135         try {
136             IndexSearcher searcher = new IndexSearcher(MVNForumConfig.getSearchCompanyIndexDir());
137             return searcher;
138         } catch (IOException JavaDoc ex) {
139             // we throw new IOException because the original exception
140
// contain sensitive directory information
141
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);
142             throw new IOException JavaDoc("Cannot access the lucene search index. Please report this error to web site Administrator (check mvnForumHome or rebuild Lucene index).");
143         }
144     }
145
146     public void searchDocuments(int offset, int rowsToReturn)
147         throws IOException JavaDoc, DatabaseException, ObjectNotFoundException {
148         //Build the query
149
//Analyzer analyzer = CompanyIndexer.getAnalyzer();
150
BooleanQuery query = new BooleanQuery();//query.
151
try {
152             Query companyNameQuery = getCompanyNameQuery();
153             if (companyNameQuery != null) {
154                 query.add(companyNameQuery, true, false);
155                 log.debug("companyNameQuery = " + companyNameQuery);
156             }
157             Query companyAddressQuery = getCompanyAddressQuery();
158             if (companyAddressQuery != null) {
159                 query.add(companyAddressQuery, true, false);
160                 log.debug("companyAddressQuery = " + companyAddressQuery);
161             }
162
163             /*if (companyDateQuery != null) {
164                 query.add(companyDateQuery, true, false);
165                 log.debug("companyDateQuery = " + companyDateQuery);
166             }*/

167         } catch (ParseException pe) {
168             log.error("Cannot parse the search query", pe);
169         }
170         log.debug("[OK ] booleanQuery = " + query);
171
172         DateFilter dateFilter = null;
173         if (fromCompanyDateKey != null && toCompanyDateKey != null) {
174             dateFilter = new DateFilter(CompanyIndexer.FIELD_CREATION_DATE, fromCompanyDateKey, toCompanyDateKey );
175         } else if (fromCompanyDateKey != null) {
176             dateFilter = DateFilter.After(CompanyIndexer.FIELD_CREATION_DATE, fromCompanyDateKey);
177         } else if (toCompanyDateKey != null) {
178             dateFilter = DateFilter.Before(CompanyIndexer.FIELD_CREATION_DATE, toCompanyDateKey );
179         }
180
181         //Now search the documents
182
IndexSearcher searcher = null;
183         try {
184             searcher = getSearcher();
185
186             //If dateFilter set then use it
187
Hits companyHits = null;
188             //dateFilter = null;
189
if (dateFilter != null) {
190                 companyHits = searcher.search(query, dateFilter);
191             } else {
192                 companyHits = searcher.search(query);
193             }
194             hitCount = companyHits.length();
195             log.debug("[ HIT COUNT ]" + hitCount);
196             searchResult = getCompanies(companyHits, offset, rowsToReturn);
197         } catch (IOException JavaDoc ex) {
198             throw ex;
199         } finally {
200             try {
201                 if (searcher != null) {
202                     searcher.close();
203                 }
204             } catch (Exception JavaDoc ex) {}
205         }
206     }
207
208     public int getHitCount() {
209         return hitCount;
210     }
211
212     public Collection JavaDoc getCompanyResult() {
213         return searchResult;
214     }
215
216     private Collection JavaDoc getCompanies(Hits companyHits, int offset, int rowsToReturn)
217         throws IOException JavaDoc, ObjectNotFoundException, DatabaseException {
218
219         if (offset < 0) throw new IllegalArgumentException JavaDoc("The offset < 0 is not allowed.");
220         if (rowsToReturn <= 0) throw new IllegalArgumentException JavaDoc("The rowsToReturn <= 0 is not allowed.");
221
222         //int hitCount = getHitCount();
223
ArrayList JavaDoc retValue = new ArrayList JavaDoc(hitCount);
224
225         for (int i = offset; (i < offset + rowsToReturn) && (i < hitCount); i++) {
226             Document companyDocument = companyHits.doc(i);
227             int currentCompanyID = Integer.parseInt(companyDocument.get(CompanyIndexer.FIELD_COMPANY_ID));
228             CompanyBean companyBean = DAOFactory.getCompanyDAO().getCompany(currentCompanyID);
229             retValue.add(companyBean);
230         }
231         return retValue;
232     }
233
234     private Query getCompanyNameQuery() throws ParseException {
235         if (companyNameKey == null) {
236             return null;
237         }
238         Analyzer analyzer = CompanyIndexer.getAnalyzer();
239         Query companyNameQuery = QueryParser.parse(companyNameKey,
240                                                    CompanyIndexer.FIELD_COMPANY_NAME,
241                                                    analyzer);
242         return companyNameQuery;
243     }
244
245     private Query getCompanyAddressQuery() throws ParseException {
246         if (companyAddressKey == null || companyAddressKey.equals("")) {
247             return null;
248         }
249         Analyzer analyzer = CompanyIndexer.getAnalyzer();
250         Query companyAddressQuery = QueryParser.parse(companyAddressKey,
251                                                    CompanyIndexer.FIELD_COMPANY_ADDRESS,
252                                                    analyzer);
253         return companyAddressQuery;
254     }
255     /*
256     private Query getCompanyDateQuery() throws ParseException {
257         if (companyDateKey == null || companyDateKey.equals("") || companyDateKey.equals("dd/MM/yyyy")) {
258             return null;
259         }
260         Analyzer analyzer = CompanyIndexer.getAnalyzer();
261         Query companyDateQuery = QueryParser.parse(DateField.dateToString(companyDateKey),
262                                                    CompanyIndexer.FIELD_CREATION_DATE,
263                                                    analyzer);
264         return companyDateQuery;
265     }
266     */

267 }
268
Popular Tags