1   /**
2    * Copyright (c) 2000-2009 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.generic;
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.Query;
29  
30  import java.util.ArrayList;
31  import java.util.Collections;
32  import java.util.List;
33  
34  /**
35   * <a href="BooleanQueryImpl.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Michael C. Han
38   */
39  public class BooleanQueryImpl implements BooleanQuery {
40  
41      public void add(Query query, BooleanClauseOccur occur) {
42          _clauses.add(new BooleanClauseImpl(query, occur));
43      }
44  
45      public void addExactTerm(String field, boolean value) {
46          addExactTerm(field, String.valueOf(value));
47      }
48  
49      public void addExactTerm(String field, Boolean value) {
50          addExactTerm(field, String.valueOf(value));
51      }
52  
53      public void addExactTerm(String field, double value) {
54          addExactTerm(field, String.valueOf(value));
55      }
56  
57      public void addExactTerm(String field, Double value) {
58          addExactTerm(field, String.valueOf(value));
59      }
60  
61      public void addExactTerm(String field, int value) {
62          addExactTerm(field, String.valueOf(value));
63      }
64  
65      public void addExactTerm(String field, Integer value) {
66          addExactTerm(field, String.valueOf(value));
67      }
68  
69      public void addExactTerm(String field, long value) {
70          addExactTerm(field, String.valueOf(value));
71      }
72  
73      public void addExactTerm(String field, Long value) {
74          addExactTerm(field, String.valueOf(value));
75      }
76  
77      public void addExactTerm(String field, short value) {
78          addExactTerm(field, String.valueOf(value));
79      }
80  
81      public void addExactTerm(String field, Short value) {
82          addExactTerm(field, String.valueOf(value));
83      }
84  
85      public void addExactTerm(String field, String value) {
86          TermQueryImpl termQuery = new TermQueryImpl(
87              new QueryTermImpl(field, String.valueOf(value)));
88  
89          add(termQuery, BooleanClauseOccur.SHOULD);
90      }
91  
92      public void addRequiredTerm(String field, boolean value) {
93          addRequiredTerm(field, String.valueOf(value), false);
94      }
95  
96      public void addRequiredTerm(String field, Boolean value) {
97          addRequiredTerm(field, String.valueOf(value), false);
98      }
99  
100     public void addRequiredTerm(String field, double value) {
101         addRequiredTerm(field, String.valueOf(value), false);
102     }
103 
104     public void addRequiredTerm(String field, Double value) {
105         addRequiredTerm(field, String.valueOf(value), false);
106     }
107 
108     public void addRequiredTerm(String field, int value) {
109         addRequiredTerm(field, String.valueOf(value), false);
110     }
111 
112     public void addRequiredTerm(String field, Integer value) {
113         addRequiredTerm(field, String.valueOf(value), false);
114     }
115 
116     public void addRequiredTerm(String field, long value) {
117         addRequiredTerm(field, String.valueOf(value), false);
118     }
119 
120     public void addRequiredTerm(String field, Long value) {
121         addRequiredTerm(field, String.valueOf(value), false);
122     }
123 
124     public void addRequiredTerm(String field, short value) {
125         addRequiredTerm(field, String.valueOf(value), false);
126     }
127 
128     public void addRequiredTerm(String field, Short value) {
129         addRequiredTerm(field, String.valueOf(value), false);
130     }
131 
132     public void addRequiredTerm(String field, String value) {
133         addRequiredTerm(field, value, false);
134     }
135 
136     public void addRequiredTerm(String field, String value, boolean like) {
137         Query query = null;
138 
139         if (like) {
140             query = new WildcardQueryImpl(
141                 new QueryTermImpl(field, String.valueOf(value)));
142         }
143         else {
144             query = new TermQueryImpl(
145                 new QueryTermImpl(field, String.valueOf(value)));
146         }
147 
148         add(query , BooleanClauseOccur.MUST);
149     }
150 
151     public void addTerm(String field, long value) {
152         addTerm(field, String.valueOf(value), false);
153     }
154 
155     public void addTerm(String field, String value) {
156         addTerm(field, value, false);
157     }
158 
159     public void addTerm(String field, String value, boolean like) {
160         Query query = null;
161 
162         if (like) {
163             query = new WildcardQueryImpl(
164                 new QueryTermImpl(field, String.valueOf(value)));
165         }
166         else {
167             query = new TermQueryImpl(
168                 new QueryTermImpl(field, String.valueOf(value)));
169         }
170 
171         add(query , BooleanClauseOccur.SHOULD);
172     }
173 
174     public List<BooleanClause> clauses() {
175         return Collections.unmodifiableList(_clauses);
176     }
177 
178     private List<BooleanClause> _clauses = new ArrayList<BooleanClause>();
179 
180 }