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