1 /***
2 * The contents of this file are subject to the Mozilla Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/MPL/
6 *
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
11 *
12 * The Original Code is pow2toolkit library.
13 *
14 * The Initial Owner of the Original Code is
15 * Power Of Two S.R.L. (www.pow2.com)
16 *
17 * Portions created by Power Of Two S.R.L. are
18 * Copyright (C) Power Of Two S.R.L.
19 * All Rights Reserved.
20 *
21 * Contributor(s):
22 */
23
24 package com.pow2.dao;
25
26
27 import org.apache.log4j.Category;
28
29 import com.pow2.util.*;
30
31
32 /***
33 * Utility class for build query conditions.
34 *
35 * @version $Id: Condition.java,v 1.1.1.1 2004/08/31 20:22:46 foxat Exp $
36 * @author Luca Fossato
37 */
38 public class Condition
39 {
40 /*** Log4J category */
41 protected Category cat = Category.getInstance(this.getClass());
42
43 /*** equal operator */
44 public static final String EQUAL = "=";
45
46 /*** not equal operator */
47 public static final String NOT_EQUAL = "!=";
48
49 /*** like operator */
50 public static final String LIKE = "like";
51
52 /*** not like operator */
53 public static final String NOT_LIKE = "not like";
54
55 /*** and boolean operator */
56 public static final String AND = "and";
57
58 /*** or boolean operator */
59 public static final String OR = "or";
60
61
62 /*** empty string */
63 public static final String EMPTY = "";
64
65 /*** null */
66 public static final String NULL = "null";
67
68
69 /*** the WHERE condition clause */
70 private String condition;
71
72 /*** the size of the first boolean operator */
73 private int firstBooleanOperatorSize;
74
75
76 /***
77 * Default constructor.
78 */
79 public Condition()
80 {
81 clear();
82 }
83
84
85 /***
86 * Constructor.
87 * <br>
88 * Add the condition builded usong the input attributeName, operator, attributeValue
89 * to the global WHERE clause.
90 *
91 * @param attributeName the name of the element used by the binary operator <code>op</code>
92 * @param op the binary operator that binds the attributeValue to the attributeName.
93 * @param attributeValue the value bond to the input attribute
94 * @param encloseInBrackets true if the resulting condition statements must be enclosed
95 * in brackets; false otherwise
96 *
97 */
98 public Condition(String attributeName,
99 String op,
100 String attributeValue,
101 boolean encloseInBrackets)
102 {
103 this();
104 add(attributeName, op, attributeValue, encloseInBrackets);
105 }
106
107
108 /***
109 * Add the input Condition object to the current condition.
110 *
111 * @param booleanOp the boolean operator used to bind the input condition object
112 * with the current condition
113 * @param cnd the input Condition object to add to the current condition
114 * @param encloseInBrackets true if the resulting condition statements must be enclosed
115 * in brackets; false otherwise
116 * @return the current Condition object
117 */
118 public Condition add(String booleanOp, Condition cnd, boolean encloseInBrackets)
119 {
120
121 if (cnd.isEmpty())
122 {
123 cat.warn("::add - the input condition is null; returning");
124 return this;
125 }
126
127
128 if (Util.isNull(condition))
129 {
130 cat.warn("::add - the current condition is null; set the current condition using the input one");
131 condition = (cnd.toString() + " ");
132 return this;
133 }
134
135 if (!Util.isNull(cnd.toString()))
136 {
137 StringBuffer sb = new StringBuffer();
138 String brackets = "";
139
140 sb.append((encloseInBrackets) ? " (" : " ")
141 .append(condition)
142 .append(" ")
143 .append(booleanOp)
144 .append(" (")
145 .append(cnd.toString())
146 .append(")")
147 .append((encloseInBrackets) ? ") " : " ");
148
149 condition = sb.toString();
150 }
151
152 return this;
153 }
154
155
156 /***
157 * Add a condition to the global WHERE clause.
158 *
159 * @param attributeName the name of the element used by the binary operator <code>op</code>
160 * @param op the binary operator that binds the attributeValue to the attributeName.
161 * <br>
162 * Supported binary operators are:
163 * <ul>
164 * <li>EQUAL</li>
165 * <li>NOT_EQUAL</li>
166 * <li>LIKE</li>
167 * </ul>
168 *
169 * @param attributeValue the value bond to the input attribute
170 * @param encloseInBrackets true if the resulting condition statements must be enclosed
171 * in brackets; false otherwise
172 *
173 * @return the current Condition object
174 */
175 public Condition add(String attributeName,
176 String op,
177 String attributeValue,
178 boolean encloseInBrackets)
179 {
180 return add(EMPTY, attributeName, op, attributeValue, encloseInBrackets);
181 }
182
183
184 /***
185 * Add a condition to the global WHERE clause.
186 *
187 * @param booleanOp the boolean operator that adds this condition statement
188 * to the previous condition statement (if any) of the
189 * the global WHERE clause
190 * <br>
191 * Supported boolean operators are:
192 * <ul>
193 * <li>AND</li>
194 * <li>OR</li>
195 * </ul>
196 *
197 * @param attributeName the name of the element used by the binary operator <code>op</code>
198 *
199 * @param op the binary operator that binds the attributeValue to the attributeName.
200 * <br>
201 * Supported binary operators are:
202 * <ul>
203 * <li>EQUAL</li>
204 * <li>NOT_EQUAL</li>
205 * <li>LIKE</li>
206 * </ul>
207 *
208 * @param attributeValue the value bond to the input attribute
209 *
210 *
211 * @return the current Condition object
212 */
213 public Condition add(String booleanOp,
214 String attributeName,
215 String op,
216 String attributeValue,
217 boolean encloseInBrackets)
218 {
219 StringBuffer sb = new StringBuffer();
220 String bracket = "";
221
222
223
224
225
226 if ((booleanOp == null) ||
227 Util.isNull(attributeName) ||
228 Util.isNull(op) ||
229 Util.isNull(attributeValue, "''"))
230 {
231 cat.error(new StringBuffer("::add - cannot build a valid condition statement:\n")
232 .append(" boolean op = ").append(booleanOp)
233 .append(" attributeName = ").append(attributeName)
234 .append(" operator = ").append(op)
235 .append(" attributeValue = ").append(attributeValue)
236 .toString());
237
238 return this;
239 }
240
241
242 op.toLowerCase();
243 booleanOp.toLowerCase();
244
245
246 sb.append(booleanOp)
247 .append((encloseInBrackets) ? " (" : " ")
248 .append(attributeName)
249 .append(" ")
250 .append(op)
251 .append(" ")
252 .append(attributeValue)
253 .append((encloseInBrackets) ? ") " : " ");
254
255
256
257
258 if (Util.isNull(condition)) firstBooleanOperatorSize = (booleanOp.length() + 1);
259
260 condition += sb.toString();
261
262 return this;
263 }
264
265
266 /***
267 * Clear the internal condition string.
268 */
269 public void clear()
270 {
271 condition = "";
272 firstBooleanOperatorSize = 0;
273 }
274
275
276 /***
277 * Test if the condition is empty
278 *
279 * @return true if this condition is empty; false otherwise
280 */
281 public boolean isEmpty()
282 {
283 return (Util.isNull(condition));
284 }
285
286
287 /***
288 * Get the string representation of the current condition.
289 *
290 * @return the string representation of the current condition
291 */
292 public String toString()
293 {
294 String s = removeFirstBooleanOperator(condition).trim();
295 return s;
296 }
297
298
299 /***
300 * Enclose the input condition in brackets
301 *
302 * @return Description of the Return Value
303 */
304 public Condition encloseInBrackets()
305 {
306 if (Util.isNull(condition))
307 return this;
308
309 condition = new StringBuffer(" (").append(condition).append(") ").toString();
310 return this;
311 }
312
313
314
315
316 /***
317 * PRIVATE METHODS here
318 */
319
320
321 /***
322 * Remove the first boolean operator from the condition.
323 *
324 * @param cond the condition string.
325 * @return the condition string without the first boolean operator
326 */
327 private String removeFirstBooleanOperator(String cond)
328 {
329 return cond.substring(firstBooleanOperatorSize, cond.length());
330 }
331 }