1 /*
2 * PROJECT : DAR Runtime and Tools
3 * COPYRIGHT : Copyright (C) 1999-2004 tim.stephenson@enableit.org
4 * LICENSE : GNU LESSER GENERAL PUBLIC LICENSE
5 * Version 2.1, February 1999
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 package org.enableit.db;
22
23 import java.util.Map;
24 import java.util.TreeMap;
25
26 import org.apache.commons.beanutils.ConversionException;
27 import org.apache.commons.beanutils.ConvertUtils;
28 import org.apache.log4j.Logger;
29
30
31 /***
32 * Provides an encapsulated means to extend a SQL statement's WHERE clause.
33 *
34 */
35 public class DBFilter {
36 /***
37 * Map of operators to use with this class.
38 */
39 private static final Map OPERATORS = new TreeMap();
40
41 /***
42 * A valid match type
43 * @deprecated Use EQ
44 */
45 public static final int EXACT_MATCH = 0;
46
47 /***
48 * A valid match type
49 * @deprecated Use LT
50 */
51 public static final int LESS_THAN_MATCH = 1;
52
53 /***
54 * A valid match type
55 * @deprecated Use GT
56 */
57 public static final int GREATER_THAN_MATCH = 2;
58
59 /*** A valid match type */
60 public static final int IS_NULL = 3;
61
62 /*** A valid match type */
63 public static final int IS_NOT_NULL = 4;
64
65 /*** A valid match type */
66 public static final int EQ = 0;
67
68 /*** A valid match type */
69 public static final int LT = 1;
70
71 /*** A valid match type */
72 public static final int GT = 2;
73
74 /*** A valid match type */
75 public static final int LTEQ = 5;
76
77 /*** A valid match type */
78 public static final int GTEQ = 6;
79
80 /*** A valid match type */
81 public static final int NE = 7;
82
83 /*** A valid match type */
84 public static final int LIKE = 8;
85
86 /***
87 * The Log4J <code>Logger</code> doing the logging.
88 */
89 private static Logger logger = Logger.getLogger(DBFilter.class);
90
91 /***
92 * CVS info about this class and its current version
93 */
94 public static final String ABOUT = "$Revision: 1.12 $";
95
96 /***
97 * The column to filter on
98 */
99 private String name;
100
101 /***
102 * The value of column to filter on
103 */
104 private Object value;
105
106 /***
107 * The type of match required
108 */
109 private int match = EXACT_MATCH;
110
111 /***
112 * Default Constructor
113 */
114 public DBFilter() {
115 }
116
117 /***
118 * Convenience constructor.
119 * @param name Initialise filter name.
120 * @param value Initialise filter value.
121 */
122 public DBFilter(String name, String value) {
123 this(name, value, EQ);
124 }
125
126 /***
127 * Convenience constructor.
128 * @param name Initialise filter name.
129 * @param value Initialise filter value.
130 */
131 public DBFilter(String name, Object value) {
132 this(name, value, EQ);
133 }
134
135 /***
136 * Convenience constructor setting name, value and match as object is created.
137 * @param name Initialise filter name.
138 * @param value Initialise filter value.
139 * @param match Initialise filter match (enumerated as class constants).
140 */
141 public DBFilter(String name, String value, int match) {
142 this();
143 logger.info("constructor(" + name + ", " + value + ", " + match + ")");
144 setName(name);
145 setMatch(match);
146
147 // User may employ '*' as globbing character
148 if (match == LIKE) {
149 value = value.replace('*', '%');
150
151 // If user did not use any glob char, add
152 if (value.indexOf('%') == -1) {
153 value = "%" + value + "%";
154 }
155 }
156
157 setValue(value);
158 }
159
160 /***
161 * Convenience constructor setting name, value and match as object is created.
162 * @param name Initialise filter name.
163 * @param value Initialise filter value.
164 * @param match Initialise filter value type.
165 * @param match Initialise filter match (enumerated as class constants).
166 */
167 public DBFilter(String name, String value, Class type, int match)
168 throws ConversionException {
169 this();
170 setName(name);
171 setMatch(match);
172 setValue(value, type);
173 }
174
175 /***
176 * Convenience constructor setting name, value and match as object is created.
177 * @param name Initialise filter name.
178 * @param value Initialise filter value.
179 * @param type Initialise filter value type (Java class name).
180 * @param match Initialise filter match (enumerated as class constants).
181 */
182 public DBFilter(String name, String value, String type, int match)
183 throws ConversionException {
184 this();
185 setName(name);
186 setMatch(match);
187 setValue(value, type);
188 }
189
190 /***
191 * Convenience constructor setting name, value and match as object is created.
192 * @param name Initialise filter name.
193 * @param value Initialise filter value.
194 * @param match Initialise filter match (enumerated as class constants).
195 */
196 public DBFilter(String name, Object value, int match) {
197 this();
198 setName(name);
199 setValue(value);
200 setMatch(match);
201 }
202
203 /***
204 * Convenience constructor.
205 * @param name Initialise filter name.
206 * @param value Initialise filter value.
207 * @param match Initialise filter match with actual string (e.g. '=').
208 */
209 public DBFilter(String name, Object value, String match) {
210 this(name, value); // will set match to '='
211
212 if ("!=".equals(match)) {
213 setMatch(NE);
214 } else if ("<".equals(match)) {
215 setMatch(LT);
216 } else if ("<=".equals(match)) {
217 setMatch(LTEQ);
218 } else if (">=".equals(match)) {
219 setMatch(GTEQ);
220 } else if (">".equals(match)) {
221 setMatch(GT);
222 } else if ("LIKE".equals(match)) {
223 setMatch(LIKE);
224 }
225 }
226
227 /***
228 * @param name The column to filter on.
229 */
230 public void setName(String name) {
231 this.name = name;
232 }
233
234 /***
235 * @param column The value of filter column.
236 */
237 public void setValue(String value) {
238 this.value = value;
239 }
240
241 /***
242 * @param column The value of filter column.
243 */
244 public void setValue(Object value) {
245 this.value = value.toString();
246 }
247
248 /***
249 * @param column The value of filter column.
250 */
251 public void setValue(String value, String type)
252 throws ConversionException {
253 try {
254 Class cType = DBFilter.class.getClassLoader().loadClass(type);
255 setValue(value, cType);
256 } catch (Exception e) {
257 logger.error(e.getMessage(), e);
258 throw new ConversionException(e.getMessage());
259 }
260 }
261
262 /***
263 * @param column The value of filter column.
264 */
265 public void setValue(String value, Class type)
266 throws ConversionException {
267 try {
268 this.value = ConvertUtils.convert(value, type);
269 } catch (ConversionException e) {
270 logger.error(e.getMessage());
271 throw e;
272 }
273 }
274
275 /***
276 * @param The type of match required (enumerated as class constants).
277 */
278 public void setMatch(int match) {
279 logger.info("METHOD_ENTRY setMatch, match = " + match);
280 this.match = match;
281
282 if (match == IS_NULL) {
283 setValue("");
284 }
285
286 logger.info("METHOD_EXIT setMatch");
287 }
288
289 /***
290 * @return Name of the column to filter on
291 */
292 public String getName() {
293 return name;
294 }
295
296 /***
297 * @return The value of column to filter on.
298 */
299 public Object getValue() {
300 return value;
301 }
302
303 /***
304 * Returns the type of match required as an int.
305 *
306 * @return matchType
307 * One of the matches enumerated in this class as constants.
308 */
309 public int getMatchType() {
310 return match;
311 }
312
313 /***
314 * @return String representaion of the match.
315 */
316 public String getMatch() {
317 // Determine the match as a string
318 String match = " = ";
319
320 switch (this.match) {
321 case DBFilter.EXACT_MATCH:
322 match = " = ";
323
324 break;
325 case DBFilter.LESS_THAN_MATCH:
326 match = " <= ";
327
328 break;
329 case DBFilter.GREATER_THAN_MATCH:
330 match = " >= ";
331
332 break;
333 case DBFilter.IS_NULL:
334 match = " IS NULL";
335
336 break;
337 case DBFilter.LIKE:
338 match = " LIKE ";
339
340 break;
341 default:
342 match = " = ";
343
344 break;
345 }
346
347 return match;
348 }
349
350 /***
351 * @return String representation of the filter.
352 */
353 public String toString() {
354 return name + getMatch() + value;
355 }
356
357 /***
358 * @return Map of the 'match' operators, keyed by a symbolic name.
359 */
360 public static Map getOperators() {
361 if (OPERATORS.size() == 0) {
362 synchronized (DBFilter.class) {
363 OPERATORS.put("=", String.valueOf(EQ));
364 OPERATORS.put("<", String.valueOf(LT));
365 OPERATORS.put(">", String.valueOf(GT));
366 OPERATORS.put("<=", String.valueOf(LTEQ));
367 OPERATORS.put(">=", String.valueOf(GTEQ));
368 OPERATORS.put("LIKE", String.valueOf(LIKE));
369 }
370 }
371
372 return OPERATORS;
373 }
374 }
This page was automatically generated by Maven