1 | package dbnp.query |
---|
2 | |
---|
3 | import java.util.Date; |
---|
4 | import java.util.List; |
---|
5 | import java.text.SimpleDateFormat; |
---|
6 | |
---|
7 | import nl.grails.plugins.gdt.* |
---|
8 | |
---|
9 | /** |
---|
10 | * Available operators for criteria |
---|
11 | * @author robert |
---|
12 | * |
---|
13 | */ |
---|
14 | enum Operator { |
---|
15 | equals, contains, gte, gt, lte, lt |
---|
16 | } |
---|
17 | |
---|
18 | /** |
---|
19 | * Represents a criterion to search on |
---|
20 | * @author robert |
---|
21 | * |
---|
22 | */ |
---|
23 | class Criterion { |
---|
24 | public String entity |
---|
25 | public String field |
---|
26 | public Operator operator |
---|
27 | public def value |
---|
28 | |
---|
29 | /** |
---|
30 | * Checks if the given object (with template) that satisfies the given criterion. |
---|
31 | * |
---|
32 | * @param entity Entity to check for criterion satisfaction. Should be a child of template entity |
---|
33 | * @param criterion Criterion to match on |
---|
34 | * @return True iff there the entity satisfies the given criterion. |
---|
35 | */ |
---|
36 | public boolean matchOne( TemplateEntity entity ) { |
---|
37 | try { |
---|
38 | def fieldValue |
---|
39 | if( field == "Template" ) { |
---|
40 | fieldValue = entity.template?.name |
---|
41 | } else { |
---|
42 | fieldValue = Search.prepare( entity.getFieldValue( field ), entity.giveFieldType( field ) ) |
---|
43 | } |
---|
44 | |
---|
45 | return this.match( fieldValue ); |
---|
46 | } catch( Exception e ) { |
---|
47 | // An exception occurs if the given field doesn't exist. In that case, this criterion will fail. |
---|
48 | // TODO: Maybe give the user a choice whether he want's to include these studies or not |
---|
49 | return false; |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * Checks for all entities in the given entityList, if there is any object that satisfies the given criterion. |
---|
55 | * |
---|
56 | * @param entityList List with entities. The entities should be child classes of TemplateEntity |
---|
57 | * @param criterion Criterion to match on |
---|
58 | * @return True iff there is any entity in the list that satisfies the given criterion. |
---|
59 | */ |
---|
60 | public boolean matchAny( List entityList ) { |
---|
61 | for( entity in entityList ) { |
---|
62 | if( matchOne( entity ) ) |
---|
63 | return true; |
---|
64 | } |
---|
65 | return false; |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * Checks for all entities in the given entityList, if all objects satisfy the given criterion. |
---|
70 | * |
---|
71 | * @param entityList List with entities. The entities should be child classes of TemplateEntity |
---|
72 | * @param criterion Criterion to match on |
---|
73 | * @return True iff all entities satisfy the given criterion. |
---|
74 | */ |
---|
75 | public boolean matchAll( List entityList ) { |
---|
76 | for( entity in entityList ) { |
---|
77 | if( !matchOne( entity ) ) |
---|
78 | return false; |
---|
79 | } |
---|
80 | return true; |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | * Tries to match a value against a criterion and returns true if it matches |
---|
85 | * |
---|
86 | * @param value Value of the field to match |
---|
87 | * @return True iff the value matches this criterion, false otherwise |
---|
88 | */ |
---|
89 | public boolean match( def fieldValue ) { |
---|
90 | if( fieldValue == null ) |
---|
91 | return false; |
---|
92 | |
---|
93 | def classname = fieldValue.class.getName(); |
---|
94 | classname = classname[classname.lastIndexOf( '.' ) + 1..-1].toLowerCase(); |
---|
95 | |
---|
96 | try { |
---|
97 | switch( classname ) { |
---|
98 | case "integer": return longCompare( new Long( fieldValue.longValue() ) ); |
---|
99 | case "long": return longCompare( fieldValue ); |
---|
100 | case "float": return doubleCompare( new Long( fieldValue.doubleValue() ) ); |
---|
101 | case "double": return doubleCompare( fieldValue ); |
---|
102 | case "boolean": return booleanCompare( fieldValue ); |
---|
103 | case "date": return dateCompare( fieldValue); |
---|
104 | case "reltime": return relTimeCompare( fieldValue ); |
---|
105 | case "assaymodule": |
---|
106 | case "template": |
---|
107 | case "term": |
---|
108 | case "templatefieldlistitem": |
---|
109 | case "string": |
---|
110 | default: return compareValues( fieldValue.toString().trim().toLowerCase(), this.operator, value.toString().toLowerCase().trim() ); |
---|
111 | } |
---|
112 | } catch( Exception e ) { |
---|
113 | log.error e.class.getName() + ": " + e.getMessage(); |
---|
114 | return false; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * Tries to match a value against a criterion and returns true if it matches |
---|
120 | * |
---|
121 | * @param fieldValue Value of the field to match |
---|
122 | * @param operator Operator to apply |
---|
123 | * @param criterionValue Value of the criterion |
---|
124 | * @return True iff the value matches this criterion value, false otherwise |
---|
125 | */ |
---|
126 | protected boolean compareValues( def fieldValue, Operator operator, def criterionValue ) { |
---|
127 | switch( operator ) { |
---|
128 | case Operator.gte: |
---|
129 | return fieldValue >= criterionValue; |
---|
130 | case Operator.gt: |
---|
131 | return fieldValue > criterionValue; |
---|
132 | case Operator.lt: |
---|
133 | return fieldValue < criterionValue; |
---|
134 | case Operator.lte: |
---|
135 | return fieldValue <= criterionValue; |
---|
136 | case Operator.contains: |
---|
137 | return fieldValue.contains( criterionValue ); |
---|
138 | case Operator.equals: |
---|
139 | default: |
---|
140 | return fieldValue.equals( criterionValue ); |
---|
141 | } |
---|
142 | |
---|
143 | } |
---|
144 | |
---|
145 | /** |
---|
146 | * Tries to match a date value against a criterion and returns true if it matches |
---|
147 | * |
---|
148 | * @param value Date value of the field to match |
---|
149 | * @return True iff the value matches this criterion, false otherwise |
---|
150 | */ |
---|
151 | protected boolean dateCompare( Date fieldValue ) { |
---|
152 | try { |
---|
153 | Date dateCriterion = new SimpleDateFormat( "yyyy-MM-dd" ).parse( value ); |
---|
154 | Date fieldDate = new Date( fieldValue.getTime() ); |
---|
155 | |
---|
156 | // Clear time in order to just compare dates |
---|
157 | dateCriterion.clearTime(); |
---|
158 | fieldDate.clearTime(); |
---|
159 | |
---|
160 | return compareValues( fieldDate, this.operator, dateCriterion ) |
---|
161 | } catch( Exception e ) { |
---|
162 | log.error e.class.getName() + ": " + e.getMessage(); |
---|
163 | return false; |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * Tries to match a long value against a criterion and returns true if it matches |
---|
169 | * |
---|
170 | * @param value Long value of the field to match |
---|
171 | * @param criterion Criterion to match on. Should be a map with entries 'operator' and 'value' |
---|
172 | * @return True iff the value matches this criterion, false otherwise |
---|
173 | */ |
---|
174 | protected boolean longCompare( Long fieldValue ) { |
---|
175 | Long longCriterion; |
---|
176 | try { |
---|
177 | longCriterion = Long.parseLong( value ); |
---|
178 | } catch( Exception e ) { |
---|
179 | try { |
---|
180 | // If converting to long doesn't work, try converting to double and rounding it |
---|
181 | Double doubleCriterion = Double.parseDouble(value); |
---|
182 | longCriterion = new Long( doubleCriterion.longValue() ); |
---|
183 | } catch( Exception e2 ) { |
---|
184 | log.error e2.class.getName() + ": " + e2.getMessage(); |
---|
185 | return false; |
---|
186 | } |
---|
187 | } |
---|
188 | return compareValues( fieldValue, this.operator, longCriterion ); |
---|
189 | } |
---|
190 | |
---|
191 | /** |
---|
192 | * Tries to match a double value against a criterion and returns true if it matches |
---|
193 | * |
---|
194 | * @param value Double value of the field to match |
---|
195 | * @return True iff the value matches this criterion, false otherwise |
---|
196 | */ |
---|
197 | protected boolean doubleCompare( Double fieldValue ) { |
---|
198 | try { |
---|
199 | Double doubleCriterion = Double.parseDouble( value ); |
---|
200 | return compareValues( fieldValue, this.operator, doubleCriterion ); |
---|
201 | } catch( Exception e ) { |
---|
202 | log.error e.class.getName() + ": " + e.getMessage(); |
---|
203 | return false; |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | |
---|
208 | /** |
---|
209 | * Tries to match a boolean value against a criterion and returns true if it matches |
---|
210 | * |
---|
211 | * @param value Boolean value of the field to match |
---|
212 | * @return True iff the value matches this criterion, false otherwise |
---|
213 | */ |
---|
214 | protected boolean booleanCompare( Boolean fieldValue ) { |
---|
215 | try { |
---|
216 | Boolean booleanCriterion = Boolean.parseBoolean( value ); |
---|
217 | return compareValues( fieldValue, this.operator, booleanCriterion ); |
---|
218 | } catch( Exception e ) { |
---|
219 | log.error e.class.getName() + ": " + e.getMessage(); |
---|
220 | return false; |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | /** |
---|
225 | * Tries to match a relTime value against a criterion and returns true if it matches |
---|
226 | * |
---|
227 | * @param value relTime value of the field to match |
---|
228 | * @return True iff the value matches this criterion, false otherwise |
---|
229 | */ |
---|
230 | protected boolean relTimeCompare( RelTime fieldValue ) { |
---|
231 | try { |
---|
232 | RelTime rt |
---|
233 | |
---|
234 | // Numbers are taken to be seconds, if a non-numeric value is given, try to parse it |
---|
235 | if( value.toString().isNumber() ) { |
---|
236 | rt = new RelTime( Long.parseLong( value.toString() ) ); |
---|
237 | } else { |
---|
238 | rt = new RelTime( value.toString() ); |
---|
239 | } |
---|
240 | |
---|
241 | return compareValues( fieldValue, this.operator, rt ); |
---|
242 | } catch( Exception e ) { |
---|
243 | log.error e.class.getName() + ": " + e.getMessage(); |
---|
244 | return false; |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | public String toString() { |
---|
249 | return "[Criterion " + entity + "." + field + " " + operator + " " + value + "]"; |
---|
250 | } |
---|
251 | } |
---|