Clover Coverage Report - SmartWeb
Coverage timestamp: Sun Jun 8 2008 21:20:12 CEST
../../../img/srcFileCovDistChart0.png 29% of files have more coverage
57   421   34   3,8
22   137   0,6   5
15     2,27  
3    
 
  DataAccessObject       Line # 37 0 0 - -1.0
  DataAccessObject.SearchInfo       Line # 94 46 28 0% 0.0
  DataAccessObject.SearchInfo.Filter       Line # 335 11 6 0% 0.0
 
No Tests
 
1    /*
2    * The SmartWeb Framework
3    * Copyright (C) 2004-2006
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10    * This library is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this library; if not, write to the Free Software
17    * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18    *
19    * For further informations on the SmartWeb Framework please visit
20    *
21    * http://smartweb.sourceforge.net
22    */
23    package net.smartlab.web;
24   
25    import java.io.Serializable;
26    import java.util.ArrayList;
27    import java.util.Collection;
28    import java.util.List;
29   
30    /**
31    * This interface should be implemented by classes which provide some form of
32    * persistence.
33    *
34    * @author rlogiacco,gperrone
35    * @uml.dependency supplier="net.smartlab.web.DAOException"
36    */
 
37    public interface DataAccessObject {
38   
39    /**
40    * Retrieves from the persistence tier the object which primary key equals
41    * the one specified.
42    *
43    * @param key the primary key used to search the instance into the
44    * persistence tier.
45    * @return an object representing the datas stored in the persistence tier
46    * associated with the specified key.
47    * @throws DAOException if an error occur while accessing the persistence
48    * tier.
49    * @throws UndefinedKeyException if the specified primary key is not present
50    * on the persistence tier.
51    */
52    public Object findByKey(Serializable key) throws DAOException;
53   
54    /**
55    * Permanently deletes an instance from the persistence tier.
56    *
57    * @param object the instance representing the informations to be deleted
58    * from the store.
59    * @throws DAOException if an error occur while accessing the persistence
60    * tier.
61    */
62    public void remove(Object object) throws DAOException;
63   
64    /**
65    * Ensures the persistence tier representation of the object is consistent
66    * with the in memory representation. If the object doesn't exist yet into
67    * the persistence tier it must be added and a new primary key assigned to
68    * the object.
69    *
70    * @param object the object to be persisted.
71    * @throws DAOException if an error occur while accessing the persistence
72    * tier
73    */
74    public void update(Object object) throws DAOException;
75   
76    /**
77    * Returns a collection of objects representing all the persistence tier
78    * informations matching the specified search criterias.
79    *
80    * @param info the criterias to be used to search the persistence tier.
81    * @return a collection of matching entities.
82    * @throws DAOException if an error occur while accessing the persistence
83    * tier
84    */
85    public Collection list(SearchInfo info) throws DAOException;
86   
87   
88    /**
89    * Instances of this class represents a set of criterias to be used in
90    * persistence tier searches.
91    *
92    * @author rlogiacco
93    */
 
94    public static class SearchInfo {
95   
96    /**
97    * Entity properties to be filtered.
98    *
99    * @uml.property name="filters"
100    */
101    private Collection filters = new ArrayList();
102   
103    /**
104    * Property to be used for ordering.
105    *
106    * @uml.property name="order"
107    */
108    private String order = null;
109   
110    /**
111    * The ordering direction.
112    *
113    * @uml.property name="descendant"
114    */
115    private boolean descendant = false;
116   
117    /**
118    * The filtering style.
119    *
120    * @uml.property name="union"
121    */
122    private boolean union = false;
123   
124    /**
125    * Identifies a <code>greater</code> expression condition.
126    */
127    public final static int EQUALS = 0;
128   
129    /**
130    * Identifies an <code>equals</code> expression condition.
131    */
132    public final static int GREATER = 1;
133   
134    /**
135    * Identifies a <code>greater</code> expression condition.
136    */
137    public final static int GREATER_EQUALS = 2;
138   
139    /**
140    * Identifies a <code>greater equals</code> expression condition.
141    */
142    public final static int LESSER = 3;
143   
144    /**
145    * Identifies a <code>lesser</code> expression condition.
146    */
147    public final static int LESSER_EQUALS = 4;
148   
149    /**
150    * Identifies a <code>lesser equal</code> expression condition.
151    */
152    public final static int NOT_EQUALS = 5;
153   
154    /**
155    * Identifies a <code>like</code> expression condition.
156    */
157    public final static int LIKE = 6;
158   
159    /**
160    * Identifies a <code>like</code> expression condition.
161    */
162    public final static int ILIKE = 7;
163   
164    /**
165    * Identifies a <code>between</code> expression condition.
166    */
167    public final static int BETWEEN = 8;
168   
169   
170    /**
171    * @return returns the ordering direction.
172    * @uml.property name="descendant"
173    */
 
174  0 toggle public boolean isDescendant() {
175  0 return descendant;
176    }
177   
178    /**
179    * @return returns the filters.
180    * @uml.property name="filters"
181    */
 
182  0 toggle public Collection getFilters() {
183  0 return filters;
184    }
185   
186    /**
187    * @param filters the filters to set.
188    * @uml.property name="filters"
189    */
 
190  0 toggle public void setFilters(Collection filters) {
191  0 this.filters = filters;
192    }
193   
194    /**
195    * @param filters the filters to set.
196    */
 
197  0 toggle public void setFilters(String[] filters) {
198  0 if (filters != null) {
199  0 for (int i = 0; i < filters.length; i++) {
200  0 this.addFilter(filters[i]);
201    }
202    }
203    }
204   
205    /**
206    * Adds a filter.
207    *
208    * @param filter the filter to add.
209    */
 
210  0 toggle public void addFilter(String filter) {
211  0 for (int i = 0; i < filter.length(); i++) {
212  0 switch (filter.charAt(i)) {
213    // Equal
214  0 case '=':
215  0 filters.add(new Filter(filter.substring(0, i), EQUALS, filter.substring(i + 1)));
216  0 return;
217    // Greater
218  0 case '>':
219    // Greater equal
220  0 if (filter.charAt(i + 1) == '=') {
221  0 filters
222    .add(new Filter(filter.substring(0, i + 1), GREATER_EQUALS, filter.substring(i + 2)));
223    } else {
224  0 filters.add(new Filter(filter.substring(0, i), GREATER, filter.substring(i + 1)));
225    }
226  0 return;
227    // Lesser
228  0 case '<':
229    // Lesser equal
230  0 if (filter.charAt(i + 1) == '=') {
231  0 filters.add(new Filter(filter.substring(0, i + 1), LESSER_EQUALS, filter.substring(i + 2)));
232    } else {
233  0 filters.add(new Filter(filter.substring(0, i), LESSER, filter.substring(i + 1)));
234    }
235  0 return;
236    // Not equals
237  0 case '!':
238  0 filters.add(new Filter(filter.substring(0, i), NOT_EQUALS, filter.substring(i + 1)));
239  0 return;
240    // Between
241  0 case '|':
242  0 filters.add(new Filter(filter.substring(0, i), BETWEEN, filter.substring(i + 1)));
243  0 return;
244    // Like / ILike
245  0 case '%':
246  0 if (filter.charAt(i + 1) == '%') {
247    // ILike
248  0 filters.add(new Filter(filter.substring(0, i), ILIKE, filter.substring(i + 1)));
249  0 return;
250    } else {
251    // Like
252  0 filters.add(new Filter(filter.substring(0, i), ILIKE, filter.substring(i + 1)));
253  0 return;
254    }
255    }
256    }
257    }
258   
259    /**
260    * Adds a filter.
261    *
262    * @param column the column to filter on.
263    * @param expression the expression condition to apply on the column.
264    * @param values a comma separated list of values to evaluate against
265    * the condition.
266    */
 
267  0 toggle public void addFilter(String column, int expression, String values) {
268  0 filters.add(new Filter(column, expression, values));
269    }
270   
271    /**
272    * @return returns the order.
273    * @uml.property name="order"
274    */
 
275  0 toggle public String getOrder() {
276  0 return order;
277    }
278   
279    /**
280    * Sets the property used to order the collection. By default the
281    * ordering is set to <code>descendant</code> unless an <b>! </b>
282    * <code>(exclamation mark)</code> is prefixed indicating an
283    * <code>ascendant</code> order must be used.
284    *
285    * @param order the property used to order the collection, optionally
286    * prefixed by <b>! </b> <code>(exclamation mark)</code> to
287    * invert the sorting direction.
288    * @uml.property name="order"
289    */
 
290  0 toggle public void setOrder(String order) {
291  0 if (order != null && order.length() > 0) {
292  0 if (order.charAt(0) == '!') {
293  0 this.descendant = true;
294  0 this.order = order.substring(1);
295    } else {
296  0 this.order = order;
297    }
298    } else {
299  0 order = null;
300    }
301    }
302   
303    /**
304    * TODO documentation
305    *
306    * @param style
307    */
 
308  0 toggle public void setUnion(String style) {
309  0 if (style != null && style.length() > 0) {
310  0 if (style.equalsIgnoreCase("OR")) {
311  0 this.union = true;
312    } else {
313  0 this.union = false;
314    }
315    }
316    }
317   
318    /**
319    * TODO documentation
320    *
321    * @return
322    * @uml.property name="union"
323    */
 
324  0 toggle public boolean isUnion() {
325  0 return union;
326    }
327   
328   
329    /**
330    * Represents a filtering condition to be applied while performing
331    * searches.
332    *
333    * @author rlogiacco
334    */
 
335    protected class Filter {
336   
337    /**
338    * The column.
339    *
340    * @uml.property name="column"
341    */
342    private String column;
343   
344    /**
345    * The condition.
346    *
347    * @uml.property name="condition"
348    */
349    private int condition;
350   
351    /**
352    * The values array.
353    *
354    * @uml.property name="values"
355    */
356    private String[] values;
357   
358   
359    /**
360    * Creates a filter with a column, a condition and a comma separated
361    * list of values to check against.
362    *
363    * @param column the column to filter on.
364    * @param condition the condition to apply.
365    * @param values a comma separated list of values to check against.
366    */
 
367  0 toggle private Filter(String column, int condition, String values) {
368  0 this.column = column;
369  0 this.condition = condition;
370  0 List list = new ArrayList();
371  0 for (int start = 0, end = values.indexOf(','); end > -1; start = end + 1, end = values.indexOf(start,
372    ',')) {
373  0 list.add(values.substring(start, end));
374    }
375  0 list.add(values.substring(values.lastIndexOf(',') + 1));
376  0 this.values = (String[])list.toArray(new String[0]);
377    }
378   
379    /**
380    * Returns the column.
381    *
382    * @return the column.
383    * @uml.property name="column"
384    */
 
385  0 toggle protected String getColumn() {
386  0 return column;
387    }
388   
389    /**
390    * Returns the condition.
391    *
392    * @return the condition.
393    * @uml.property name="condition"
394    */
 
395  0 toggle protected int getCondition() {
396  0 return condition;
397    }
398   
399    /**
400    * Returns the values.
401    *
402    * @return the values array.
403    * @uml.property name="values"
404    */
 
405  0 toggle protected String[] getValues() {
406  0 return values;
407    }
408   
409    /**
410    * Returns the n-th value in the list where the specified index is 0
411    * based.
412    *
413    * @param index the 0 based index of the value to retrieve.
414    * @return the n-th value in the values list.
415    */
 
416  0 toggle protected String getValue(int index) {
417  0 return values[index];
418    }
419    }
420    }
421    }