1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.search.lucene;
24  
25  import com.liferay.portal.kernel.search.BooleanClause;
26  import com.liferay.portal.kernel.search.BooleanClauseOccur;
27  import com.liferay.portal.kernel.search.BooleanQuery;
28  import com.liferay.portal.kernel.search.ParseException;
29  import com.liferay.portal.kernel.search.Query;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  /**
35   * <a href="BooleanQueryImpl.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class BooleanQueryImpl implements BooleanQuery {
41  
42      public BooleanQueryImpl() {
43          _booleanQuery = new org.apache.lucene.search.BooleanQuery();
44      }
45  
46      public void add(Query query, BooleanClauseOccur occur) {
47          _booleanQuery.add(
48              QueryTranslator.translate(query),
49              BooleanClauseOccurTranslator.translate(occur));
50      }
51  
52      public void addExactTerm(String field, long value) {
53          LuceneUtil.addExactTerm(_booleanQuery, field, value);
54      }
55  
56      public void addExactTerm(String field, String value) {
57          LuceneUtil.addExactTerm(_booleanQuery, field, value);
58      }
59  
60      public void addRequiredTerm(String field, long value) {
61          LuceneUtil.addRequiredTerm(_booleanQuery, field, value);
62      }
63  
64      public void addRequiredTerm(String field, String value) {
65          LuceneUtil.addRequiredTerm(_booleanQuery, field, value);
66      }
67  
68      public void addTerm(String field, long value) throws ParseException {
69          try {
70              LuceneUtil.addTerm(_booleanQuery, field, value);
71          }
72          catch (org.apache.lucene.queryParser.ParseException pe) {
73              throw new ParseException(pe.getMessage());
74          }
75      }
76  
77      public void addTerm(String field, String value) throws ParseException {
78          try {
79              LuceneUtil.addTerm(_booleanQuery, field, value);
80          }
81          catch (org.apache.lucene.queryParser.ParseException pe) {
82              throw new ParseException(pe.getMessage());
83          }
84      }
85  
86      public List<BooleanClause> clauses() {
87          List<org.apache.lucene.search.BooleanClause> luceneClauses =
88              _booleanQuery.clauses();
89  
90          List<BooleanClause> clauses = new ArrayList<BooleanClause>(
91              luceneClauses.size());
92  
93          for (int i = 0; i < luceneClauses.size(); i++) {
94              clauses.add(new BooleanClauseImpl(luceneClauses.get(i)));
95          }
96  
97          return clauses;
98      }
99  
100     public org.apache.lucene.search.BooleanQuery getBooleanQuery() {
101         return _booleanQuery;
102     }
103 
104     public String toString() {
105         return _booleanQuery.toString();
106     }
107 
108     private org.apache.lucene.search.BooleanQuery _booleanQuery;
109 
110 }