View Javadoc

1   /***
2   Copyright (C) 2005 The Java Community
3   
4   This program is free software; you can redistribute it and/or modify  it under
5   the terms of the GNU General Public License as published by  the Free Software
6   Foundation; either version 2 of the License, or  (at your option) any later
7   version.
8   
9   This program is distributed in the hope that it will be useful,  but WITHOUT
10  ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS
11  FOR A PARTICULAR PURPOSE. See the  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License  along with
14  this program; if not, write to the Free Software  Foundation, Inc., 59 Temple
15  Place, Suite 330, Boston, MA 02111-1307 USA.
16  
17  */
18  package org.bejug.javacareers.common.search;
19  
20  import java.io.Serializable;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  /***
25   * Generic implemetation of the SearchCriteria-class.
26   * We implement the Criteria-class as an inner-class.
27   * It should not be accessible outside the SearchCriteria-class...
28   * @author Bart Meyers (last modified by $Author: shally $)
29   * @version $Revision: 1.2 $ $Date: 2005/12/20 15:36:43 $
30   *
31   */
32  public class SearchCriteria implements Serializable {
33  
34      /***
35       * the class-type we want this searchcriteria to apply on.
36       */
37      private Class clazz = null;
38      
39      /***
40       * the list containing the Criterias.
41       */
42      private List criterias = null;
43      
44      /***
45       * the list containing the Orders. 
46       */
47      private List orders = null;
48      
49      /***
50       * the number of results to fetch
51       */
52      private int resultsPerPage = -1;
53      
54      /***
55       * indicates the pageNumber we are on.
56       */
57      private int pageNumber = -1;
58      
59      
60      /***
61       * default constructor.
62       * @param clazz The Class the query has to be performed on.
63       */
64      SearchCriteria(Class clazz) {
65      	this.clazz = clazz;
66          this.criterias = new ArrayList();
67          this.orders = new ArrayList();
68      }
69      
70      
71      /***
72       * default constructor.
73       * @param clazz The Class the query has to be performed on.
74       * @param resultsPerPage the maximum number of results on a page.
75       */
76      SearchCriteria(Class clazz, int resultsPerPage) {
77         this.clazz = clazz;
78         this.resultsPerPage = resultsPerPage;
79         this.criterias = new ArrayList();
80         this.orders = new ArrayList();
81      }
82      
83      
84      /***
85       * get the wanted orders.
86       * @return an ArrayList of Order-objects.
87       */
88      public List getOrders() {
89  		return orders;
90  	}
91  
92  
93  	/***
94       * gets the wanted criterias.
95       * @return the list of criterias.
96       */
97      public List getCriterias() {
98          return criterias;
99      }
100     
101     /***
102      * @return the class this criteria are defined for.
103      */
104     public Class getClazz() {
105         return clazz;
106     }
107     
108     /***
109      * @param claz set the clazz this criteria are defined for.
110      */
111     public void setClazz(Class claz) {
112         this.clazz = claz;
113     }
114     
115     
116     /***
117      * @return the pageNumber.
118      */
119     public int getPageNumber() {
120 		return pageNumber;
121 	}
122 
123     
124     /***
125      * @param pageNumber the pageNumber to set.
126      */
127 	public void setPageNumber(int pageNumber) {
128 		this.pageNumber = pageNumber;
129 	}
130 
131 	
132 	/***
133 	 * @return the number of results on a page.
134 	 */
135 	public int getResultsPerPage() {
136 		return resultsPerPage;
137 	}
138 
139 	
140 	/***
141 	 * @param resultsPerPage the resultsPerPage to set.
142 	 */
143 	public void setResultsPerPage(int resultsPerPage) {
144 		this.resultsPerPage = resultsPerPage;
145 	}
146 
147 
148 	/***
149      * method for adding an equals-criterium to the criteria-set.
150      * Default behavior -- logical AND.
151      * @param name the name of the column to perform the search on.
152      * @param value the value to match.
153      */
154     public void addEqualsCriterium(String name, Object value) {
155         criterias.add(new Criterium(name, value, Criterium.EQUALS));
156     }
157     
158     /***
159      * method for adding an equals-criterium to the criteria-set.
160      * @param name the name of the column to perform the search on.
161      * @param value the value to match.
162      * @param logicalOperator the logical operator to use.
163      */
164     public void addEqualsCriterium(String name, Object value, 
165             int logicalOperator) {
166         criterias.add(new Criterium(name, value, Criterium.EQUALS, 
167                 logicalOperator));
168     }
169     
170     /***
171      * method for adding an notequals-criterium to the criteria-set.
172      * Default behavior -- logical AND.
173      * @param name the name of the column to perform the search on.
174      * @param value the value to match.
175      */
176     public void addNotEqualsCriterium(String name, Object value) {
177         criterias.add(new Criterium(name, value, Criterium.NOT_EQUALS));
178     }
179     
180     /***
181      * method for adding an notequals-criterium to the criteria-set.
182      * @param name the name of the column to perform the search on.
183      * @param value the value to match.
184      * @param logicalOperator the logical operator to use.
185      */
186     public void addNotEqualsCriterium(String name, Object value, 
187             int logicalOperator) {
188         criterias.add(new Criterium(name, value, Criterium.NOT_EQUALS, 
189                 logicalOperator));
190     }
191     
192     /***
193      * method for adding a greaterThan-criterium to the criteria-set.
194      * @param name the name of the column to perform the search on.
195      * @param value the value to match.
196      */
197     public void addGreaterThanCriterium(String name, Object value) {
198         criterias.add(new Criterium(name, value, Criterium.GREATER_THAN));
199     }
200     
201     /***
202      * method for adding a greaterThan-criterium to the criteria-set.
203      * @param name the name of the column to perform the search on.
204      * @param value the value to match.
205      */
206     public void addGreaterThanEqualsCriterium(String name, Object value) {
207         criterias.add(new Criterium(name, value, Criterium.GREATER_THAN_EQUALS));
208     }
209     
210     /***
211      * method for adding an greaterthanequals-criterium to the criteria-set.
212      * @param name the name of the column to perform the search on.
213      * @param value the value to match.
214      * @param logicalOperator the logical operator to use.
215      */
216     public void addGreaterThanEqualsCriterium(String name, Object value, 
217             int logicalOperator) {
218         criterias.add(new Criterium(name, value, 
219                 Criterium.GREATER_THAN_EQUALS, logicalOperator));
220     }
221     
222     /***
223      * method for adding a lowerthan-criterium to the criteria-set.
224      * @param name the name of the column to perform the search on.
225      * @param value the value to match.
226      */
227     public void addLowerThenCriterium(String name, Object value) {
228         criterias.add(new Criterium(name, value, Criterium.LOWER_THAN));
229     }
230     
231     /***
232      * method for adding a lowerthan-criterium to the criteria-set.
233      * @param name the name of the column to perform the search on.
234      * @param value the value to match.
235      * @param logicalOperator the logical operator to use.
236      */
237     public void addLowerThenCriterium(String name, Object value, 
238             int logicalOperator) {
239         criterias.add(new Criterium(name, value, Criterium.LOWER_THAN, 
240                 logicalOperator));
241     }
242     
243     /***
244      * method for adding a lowerthanequals-criterium to the criteria-set.
245      * @param name the name of the column to perform the search on.
246      * @param value the value to match.
247      * @param logicalOperator the logical operator to use.
248      */
249     public void addLowerThanEqualsCriterium(String name, Object value, 
250            int logicalOperator) {
251         criterias.add(new Criterium(name, value, 
252                 Criterium.LOWER_THAN_EQUALS, logicalOperator));
253     }
254     
255     /***
256      * method for adding a addDifferent-criterium to the criteria-set.
257      * @param name the name of the column to perform the search on.
258      * @param value the value to match.
259      */
260     public void addNotCriterium(String name, Object value) {
261         criterias.add(new Criterium(name, value, Criterium.NOT));
262     }
263     
264     /***
265      * method for adding a addDifferent-criterium to the criteria-set.
266      * @param name the name of the column to perform the search on.
267      * @param value the value to match.
268      * @param logicalOperator the logical operator to use.
269      */
270     public void addNotCriterium(String name, Object value, 
271             int logicalOperator) {
272         criterias.add(new Criterium(name, value, Criterium.NOT, 
273                 logicalOperator));
274     }
275     
276     /***
277      * method for adding a like-criterium to the criteria-set.
278      * @param name the name of the column to perform the search on.
279      * @param value the value to match.
280      */
281     public void addLikeCriterium(String name, Object value) {
282         criterias.add(new Criterium(name, value, Criterium.LIKE));
283     }
284     
285     /***
286      * method for adding a like-criterium to the criteria-set.
287      * @param name the name of the column to perform the search on.
288      * @param value the value to match.
289      * @param logicalOperator the logical operator to use.
290      */
291     public void addLikeCriterium(String name, Object value, 
292             int logicalOperator) {
293         criterias.add(new Criterium(name, value, Criterium.LIKE, 
294                 logicalOperator));
295     }
296     
297     /***
298      * method for adding a notlike-criterium to the criteria-set.
299      * @param name the name of the column to perform the search on.
300      * @param value the value to match.
301      */
302     public void addNotLikeCriterium(String name, Object value) {
303         criterias.add(new Criterium(name, value, Criterium.NOT_LIKE));
304     }
305     
306     /***
307      * method for adding a notlike-criterium to the criteria-set.
308      * @param name the name of the column to perform the search on.
309      * @param value the value to match.
310      * @param logicalOperator the logical operator to use.
311      */
312     public void addNotLikeCriterium(String name, Object value, 
313             int logicalOperator) {
314         criterias.add(new Criterium(name, value, Criterium.NOT_LIKE, 
315                 logicalOperator));
316     }
317     
318     /***
319      * method for adding a between-criterium to the criteria-set.
320      * @param name the name of the column to perform the search on.
321      * @param value1 the first value to match.
322      * @param value2 the second value to match.
323      */
324     public void addBetweenCriterium(String name, Object value1, 
325             Object value2) {
326         criterias.add(new Criterium(name, value1, value2, 
327                 Criterium.BETWEEN));
328     } 
329     
330     /***
331      * method for adding a between-criterium to the criteria-set.
332      * @param name the name of the column to perform the search on.
333      * @param value1 the first value to match.
334      * @param value2 the second value to match.
335      * @param logicalOperator the logical operator to use.
336      */
337     public void addBetweenCriterium(String name, Object value1, 
338             Object value2, int logicalOperator) {
339         criterias.add(new Criterium(name, value1, value2, 
340                 Criterium.BETWEEN, logicalOperator));
341     } 
342     
343     /***
344      * method for adding an in-criterium to the criteria-set.
345      * @param name the name of the column to perform the search on.
346      * @param values the values to match.
347      */
348     public void addInCriterium(String name, Object[] values) {
349         criterias.add(new Criterium(name, values, 
350                 Criterium.IN));
351     } 
352     
353     /***
354      * method for adding an in-criterium to the criteria-set.
355      * @param name the name of the column to perform the search on.
356      * @param values the values to match.
357      * @param logicalOperator the logical operator to use.
358      */
359     public void addInCriterium(String name, Object[] values, int logicalOperator) {
360         criterias.add(new Criterium(name, values, 
361                 Criterium.IN, logicalOperator));
362     } 
363     
364     /***
365      * method for adding a descending ordering to the query.
366      * @param field a String indicating the field to order on.
367      */
368     public void addDescendingOrder(String field) {
369     	orders.add(new Order(field));
370     }
371     
372     /***
373      * method for adding an ascending ordering to the query.
374      * @param field a String indicating the field to order on.
375      */
376     public void addAscendingOrder(String field) {
377     	orders.add(new Order(field, Order.ASC));
378     }
379     
380 }
381 /***
382  * $Log: SearchCriteria.java,v $
383  * Revision 1.2  2005/12/20 15:36:43  shally
384  * CheckStyle and PMD changes.
385  *
386  * Revision 1.1  2005/08/26 07:58:25  ge0ffrey
387  * split up the sources in service, serviceimpl and webclient
388  *
389  * Revision 1.6  2005/08/16 09:10:16  bme_jcs
390  * checkstyle errors resolved
391  *
392  * Revision 1.5  2005/08/11 13:04:30  bme_jcs
393  * update searchCriteria to support ordering and paging
394  *
395  * Revision 1.4  2005/06/17 12:20:01  schauwvliege
396  * CheckStyle/ PMD
397  *
398  * Revision 1.3  2005/06/14 12:05:53  schauwvliege
399  * CheckStyle and fixing tests
400  *
401  * Revision 1.2  2005/06/09 08:18:41  bejug_cc
402  * Fix initial import
403  *
404  * Revision 1.7  2005/06/01 09:12:25  bme
405  * introduction of a factory for the searchCriteria
406  *
407  * Revision 1.6  2005/05/31 07:36:27  bme
408  * introduced count-functionality
409  *
410  * Revision 1.5  2005/05/26 17:55:16  bme
411  * *** empty log message ***
412  *
413  * Revision 1.3  2005/05/13 14:08:48  sja
414  * Removed unused Logger.
415  *
416  * Revision 1.2  2005/05/13 10:28:48  bme
417  * added fucntionality
418  *
419  * Revision 1.1  2005/05/12 14:26:46  bme
420  * First Draft
421  *
422  */