KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > bean > query > SimpleLuceneQueryBean


1 /*
2   Copyright 1999-2004 The Apache Software Foundation
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15 */

16
17 package org.apache.cocoon.bean.query;
18
19 import java.io.IOException JavaDoc;
20 import java.io.Serializable JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Enumeration JavaDoc;
27
28 import org.apache.lucene.document.Document;
29 import org.apache.lucene.document.Field;
30 import org.apache.lucene.search.Hits;
31 import org.apache.lucene.search.Query;
32 import org.apache.lucene.search.BooleanQuery;
33 import org.apache.cocoon.components.search.LuceneCocoonSearcher;
34 import org.apache.cocoon.ProcessingException;
35
36
37 /**
38  * The query bean.
39  * <p>
40  * This object defines a <code>Bean</code> for searching.<br/>
41  * The idea is to abstract the process of searching into a Bean to be manipulated by CForms.<br/>
42  * This Bean is designed to be persistable.
43  * </p>
44  *
45  * @version CVS $Id: SimpleLuceneQueryBean.java 202255 2005-06-28 17:45:25Z vgritsenko $
46  */

47 public class SimpleLuceneQueryBean implements SimpleLuceneQuery, Cloneable JavaDoc, Serializable JavaDoc {
48
49     /**
50      * The DEFAULT_PAGE_SIZE of this bean.
51      * ie. <code>20</code>
52      */

53     public static Long JavaDoc DEFAULT_PAGE_SIZE = new Long JavaDoc (20);
54
55     /**
56      * The DEFAULT_PAGE of this bean.
57      * ie. <code>0</code>
58      */

59     public static Long JavaDoc DEFAULT_PAGE = new Long JavaDoc (0);
60
61     /**
62      * The SCORE_FIELD of this bean.
63      * This is the key of the Lucene Score as output by this Bean in each hit.
64      * ie. <code>_lucene-score_</code>
65      */

66     public static String JavaDoc SCORE_FIELD = "_lucene-score_";
67
68     /**
69      * The INDEX_FIELD of this bean.
70      * This is the key of the hit index as output by this Bean in each hit.
71      * ie. <code>_lucene-index_</code>
72      */

73     public static String JavaDoc INDEX_FIELD = "_lucene-index_";
74
75     /**
76      * The date this Query was created.
77      */

78     private Date JavaDoc date;
79
80     /**
81      * The Bean's list of Criteria.
82      */

83     private List JavaDoc criteria;
84
85     /**
86      * The Bean's ID.
87      */

88     private Long JavaDoc id;
89
90     /**
91      * The Bean's current page.
92      */

93     private Long JavaDoc page;
94
95     /**
96      * The Bean's page isze.
97      */

98     private Long JavaDoc size;
99
100     /**
101      * The Bean's hit count.
102      */

103     private Long JavaDoc total;
104
105     /**
106      * The Bean's query boolean.
107      */

108     private String JavaDoc bool;
109
110     /**
111      * The Bean's query name.
112      */

113     private String JavaDoc name;
114
115     /**
116      * The Bean's query type.
117      */

118     private String JavaDoc type;
119
120     /**
121      * The Bean's owner.
122      */

123     private String JavaDoc user;
124
125     /**
126      * Default constructor.
127      */

128     public SimpleLuceneQueryBean() {
129     }
130
131     /**
132      * Utility constructor.
133      *
134      * @param type the type of this query
135      * @param bool the kind of boolean opperation to apply to each of it's Criteria
136      * @param match the kind of match to use for the generated <code>Criterion</code>
137      * @param field the field to search for the generated <code>Criterion</code>
138      * @param query the terms to search for the generated <code>Criterion</code>
139      */

140     public SimpleLuceneQueryBean(String JavaDoc type, String JavaDoc bool, String JavaDoc match, String JavaDoc field, String JavaDoc query) {
141         this.name = "My Query";
142         this.type = type;
143         this.bool = bool;
144         this.size = DEFAULT_PAGE_SIZE;
145         this.page = DEFAULT_PAGE;
146         this.total = null;
147         this.user = null;
148         this.id = null;
149         this.addCriterion(new SimpleLuceneCriterionBean(field, match, query));
150     }
151     
152     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
153         SimpleLuceneQueryBean query = (SimpleLuceneQueryBean)super.clone();
154         query.setCriteria(new ArrayList JavaDoc(this.criteria.size()));
155         Iterator JavaDoc it = this.getCriteria().iterator();
156         while (it.hasNext()) query.addCriterion((SimpleLuceneCriterionBean)((SimpleLuceneCriterionBean)it.next()).clone());
157         return query;
158     }
159         
160     /**
161      * Gets the Bean to perform it's query
162      * <p>
163      * The searcher specifies which LuceneCocoonSearcher to use for this search.<br/>
164      * It needs to have been initialised properly before use.<br/>
165      * Each <code>Map</code> in the <code>List</code> returned by this method contains:
166      * <ul>
167      * <li>Each stored field from the Index</li>
168      * <li><code>SCORE_FIELD</code> the Lucene score</li>
169      * <li><code>INDEX_FIELD</code> the index of the hit</li>
170      * </ul>
171      * </p>
172      *
173      * @param searcher The <code>LuceneCocoonSearcher</code> to use for this search
174      * @return a List of Maps, each representing a Hit.
175      * @exception ProcessingException thrown by the searcher
176      * @exception IOException thrown when the searcher's directory cannot be found
177      */

178     public List JavaDoc search (LuceneCocoonSearcher searcher) throws IOException JavaDoc, ProcessingException {
179         BooleanQuery query = new BooleanQuery();
180         Iterator JavaDoc it = criteria.iterator();
181         boolean required = false;
182         if (AND_BOOL.equals(this.bool)) required = true;
183         while (it.hasNext()) {
184             SimpleLuceneCriterion criterion = (SimpleLuceneCriterion)it.next();
185             Query subquery = criterion.getQuery (searcher.getAnalyzer());
186             query.add(subquery, required, criterion.isProhibited());
187         }
188         Hits hits = searcher.search(query);
189         this.total = new Long JavaDoc (hits.length());
190         this.date = new Date JavaDoc();
191         return page(hits);
192     }
193
194     /**
195      * Outputs part of a Hit List according to the Bean's paging properties.
196      *
197      * @param hits The Lucene Hits you want to page
198      * @return a List of Maps, each representing a Hit.
199      * @exception IOException thrown when the searcher's directory cannot be found
200      */

201     private List JavaDoc page (Hits hits) throws java.io.IOException JavaDoc {
202         ArrayList JavaDoc results = new ArrayList JavaDoc();
203         int start = getPage().intValue() * getSize().intValue();
204         if (start > this.total.intValue()) start = this.total.intValue();
205         int end = start + getSize().intValue();
206         if (end > this.total.intValue()) end = this.total.intValue();
207         for (int i = start; i < end; i++) {
208             HashMap JavaDoc hit = new HashMap JavaDoc();
209             hit.put(SCORE_FIELD, new Float JavaDoc(hits.score (i)));
210             hit.put(INDEX_FIELD, new Long JavaDoc(i));
211             Document doc = hits.doc(i);
212             for (Enumeration JavaDoc e = doc.fields(); e.hasMoreElements(); ) {
213                 Field field = (Field)e.nextElement();
214                 if (field.name().equals(SCORE_FIELD)) continue;
215                 if (field.name().equals(INDEX_FIELD)) continue;
216                 hit.put(field.name(), field.stringValue());
217       }
218             results.add(hit);
219         }
220         return (results);
221     }
222     
223     /**
224      * Gets the Bean's ID.
225      *
226      * @return the <code>Long</code> ID of the Bean.
227      */

228     public Long JavaDoc getId() {
229         return this.id;
230     }
231     
232     /**
233      * Sets the Bean's ID.
234      *
235      * @param id the <code>Long</code> ID of the Bean.
236      */

237     public void setId(Long JavaDoc id) {
238         this.id = id;
239     }
240     
241     /**
242      * Gets the Bean's name.
243      *
244      * @return the <code>String</code> name of the Bean.
245      */

246     public String JavaDoc getName() {
247         return this.name;
248     }
249     
250     /**
251      * Sets the Bean's Name.
252      *
253      * @param name the <code>String</code> name of the Bean.
254      */

255     public void setName(String JavaDoc name) {
256         this.name = name;
257     }
258
259     /**
260      * Gets the Bean's type.
261      *
262      * @return the <code>String</code> type of the Bean.
263      */

264     public String JavaDoc getType() {
265         return this.type;
266     }
267     
268     /**
269      * Sets the Bean's type.
270      *
271      * @param type the <code>String</code> type of the Bean.
272      */

273     public void setType(String JavaDoc type) {
274         this.type = type;
275     }
276
277     /**
278      * Gets the Bean's boolean operator.
279      *
280      * @return the <code>String</code> boolean of the Bean.
281      */

282     public String JavaDoc getBool() {
283         return this.bool;
284     }
285     
286     /**
287      * Sets the Bean's boolean operator.
288      * ie. which kind of boolean operation do you want performed on each <code>Criterion</code>.
289      *
290      * @param bool the <code>String</code> boolean of the Bean.
291      */

292     public void setBool(String JavaDoc bool) {
293         this.bool = bool;
294     }
295
296     /**
297      * Gets the Bean's owner.
298      *
299      * @return the <code>String</code> owner of the Bean.
300      */

301     public String JavaDoc getUser() {
302         return this.user;
303     }
304     
305     /**
306      * Sets the Bean's owner.
307      *
308      * @param user the <code>String</code> owner of the Bean.
309      */

310     public void setUser(String JavaDoc user) {
311         this.user = user;
312     }
313     
314     /**
315      * Gets the Bean's page size
316      *
317      * @return the <code>Long</code> page size of the Bean.
318      */

319     public Long JavaDoc getSize() {
320         if (this.size == null) {
321             return DEFAULT_PAGE_SIZE;
322         } else {
323             return this.size;
324         }
325     }
326     
327     /**
328      * Sets the Bean's page size.
329      * ie. how many hits do you want this Bean to show on in page.
330      *
331      * @param size the <code>Long</code> page size of the Bean.
332      */

333     public void setSize(Long JavaDoc size) {
334         this.size = size;
335     }
336     
337     /**
338      * Gets the Bean's page index
339      *
340      * @return the <code>Long</code> page index of the Bean.
341      */

342     public Long JavaDoc getPage() {
343         if (this.page == null) {
344             return DEFAULT_PAGE;
345         } else {
346             return this.page;
347         }
348     }
349     
350     /**
351      * Sets the Bean's page index.
352      * ie. which page do you want this Bean to show.
353      *
354      * @param page the <code>Long</code> page index of the Bean.
355      */

356     public void setPage(Long JavaDoc page) {
357         this.page = page;
358     }
359
360     /**
361      * Gets the Bean's hit count.
362      *
363      * @return the <code>Long</code> hit count of the Bean.
364      */

365     public Long JavaDoc getTotal() {
366         return this.total;
367     }
368     
369     /**
370      * Sets the Bean's hit count.
371      *
372      * @param total the <code>Long</code> hit count of the Bean.
373      */

374     public void setTotal(Long JavaDoc total) {
375         this.total = total;
376     }
377
378     /**
379      * Gets the Bean's inception date.
380      *
381      * @return the <code>Date</code> of the Bean.
382      */

383     public Date JavaDoc getDate() {
384         return this.date;
385     }
386     
387     /**
388      * Sets the Bean's inception date.
389      *
390      * @param date the <code>Date</code> inception date of the Bean.
391      */

392     public void setDate(Date JavaDoc date) {
393         this.date = date;
394     }
395
396     /**
397      * Gets the Bean's criteria.
398      *
399      * @return the <code>List</code> of Bean Query criteria.
400      */

401     public List JavaDoc getCriteria() {
402         return this.criteria;
403     }
404     
405     /**
406      * Sets the Bean's criteria.
407      *
408      * @param criteria the <code>List</code> of Bean Query criteria.
409      */

410     public void setCriteria(List JavaDoc criteria) {
411         this.criteria = criteria;
412     }
413
414     /**
415      * Adds a <code>Criterion</code> the Bean.
416      *
417      * @param criterion the <code>SimpleLuceneCriterionBean</code> to add to the Bean.
418      */

419     public void addCriterion(SimpleLuceneCriterionBean criterion) {
420         if (this.criteria == null) this.criteria = new ArrayList JavaDoc();
421         this.criteria.add(criterion);
422     }
423
424     /**
425      * Removes a <code>Criterion</code> from the Bean.
426      *
427      * @param criterion the <code>SimpleLuceneCriterionBean</code> to remove from the Bean.
428      */

429     public void removeCriterion(SimpleLuceneCriterionBean criterion) {
430         if (this.criteria != null) this.criteria.remove(criterion);
431     }
432     
433 }
434
Popular Tags