1 | /** |
---|
2 | * Search Domain Class |
---|
3 | * |
---|
4 | * Abstract class containing search criteria and search results when querying. |
---|
5 | * Should be subclassed in order to enable searching for different entities. |
---|
6 | * |
---|
7 | * @author Robert Horlings (robert@isdat.nl) |
---|
8 | * @since 20110118 |
---|
9 | * @package dbnp.query |
---|
10 | * |
---|
11 | * Revision information: |
---|
12 | * $Rev: 1430 $ |
---|
13 | * $Author: work@osx.eu $ |
---|
14 | * $Date: 2011-01-21 20:05:36 +0000 (vr, 21 jan 2011) $ |
---|
15 | */ |
---|
16 | package dbnp.query |
---|
17 | |
---|
18 | import nl.grails.plugins.gdt.* |
---|
19 | import java.util.List; |
---|
20 | import java.text.DateFormat; |
---|
21 | import java.text.SimpleDateFormat |
---|
22 | |
---|
23 | class Search { |
---|
24 | public String entity; |
---|
25 | |
---|
26 | protected List criteria; |
---|
27 | protected List results; |
---|
28 | |
---|
29 | public List getCriteria() { return criteria; } |
---|
30 | public void setCriteria( List c ) { criteria = c; } |
---|
31 | |
---|
32 | public List getResults() { return results; } |
---|
33 | public void setResults( List r ) { results = r; } |
---|
34 | |
---|
35 | /** |
---|
36 | * Returns the number of results found by this search |
---|
37 | * @return |
---|
38 | */ |
---|
39 | public int getNumResults() { |
---|
40 | if( results ) |
---|
41 | return results.size(); |
---|
42 | |
---|
43 | return 0; |
---|
44 | } |
---|
45 | |
---|
46 | /** |
---|
47 | * Executes a search based on the given criteria. Should be filled in by |
---|
48 | * subclasses searching for a specific entity |
---|
49 | * |
---|
50 | * @param c List with criteria to search on |
---|
51 | */ |
---|
52 | public void execute( List c ) { |
---|
53 | setCriteria( c ); |
---|
54 | execute(); |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * Executes a search based on the given criteria. Should be filled in by |
---|
59 | * subclasses searching for a specific entity |
---|
60 | */ |
---|
61 | public void execute() {} |
---|
62 | |
---|
63 | /** |
---|
64 | * Returns a list of criteria targeted on the given entity |
---|
65 | * @param entity Entity to search criteria for |
---|
66 | * @return List of criteria |
---|
67 | */ |
---|
68 | protected List getEntityCriteria( String entity ) { |
---|
69 | return criteria?.findAll { it.entity == entity } |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * Filters a list with entities, based on the given criteria and a closure to check whether a criterion is matched |
---|
74 | * |
---|
75 | * @param entities Original list with entities to check for these criteria |
---|
76 | * @param criteria List with criteria to match on |
---|
77 | * @param check Closure to see whether a specific entity matches a criterion. Gets two arguments: |
---|
78 | * element The element to check |
---|
79 | * criterion The criterion to check on. |
---|
80 | * Returns true if the criterion holds, false otherwise |
---|
81 | * @return The filtered list of entities |
---|
82 | */ |
---|
83 | protected List filterEntityList( List entities, List<Criterion> criteria, Closure check ) { |
---|
84 | if( !entities || !criteria || criteria.size() == 0 ) { |
---|
85 | return entities; |
---|
86 | } |
---|
87 | |
---|
88 | return entities.findAll { entity -> |
---|
89 | for( criterion in criteria ) { |
---|
90 | if( !check( entity, criterion ) ) { |
---|
91 | return false; |
---|
92 | } |
---|
93 | } |
---|
94 | return true; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * Prepares a value from a template entity for comparison, by giving it a correct type |
---|
100 | * |
---|
101 | * @param value Value of the field |
---|
102 | * @param type TemplateFieldType Type of the specific field |
---|
103 | * @return The value of the field in the correct entity |
---|
104 | */ |
---|
105 | public static def prepare( def value, TemplateFieldType type ) { |
---|
106 | switch (type) { |
---|
107 | case TemplateFieldType.DATE: |
---|
108 | try { |
---|
109 | return new SimpleDateFormat( "yyyy-MM-dd" ).parse( value ) |
---|
110 | } catch( Exception e ) { |
---|
111 | return value.toString(); |
---|
112 | } |
---|
113 | case TemplateFieldType.RELTIME: |
---|
114 | try { |
---|
115 | if( value instanceof Number ) { |
---|
116 | return new RelTime( value ); |
---|
117 | } else if( value.toString().isNumber() ) { |
---|
118 | return new RelTime( Long.parseLong( value.toString() ) ) |
---|
119 | } else { |
---|
120 | return new RelTime( value ); |
---|
121 | } |
---|
122 | } catch( Exception e ) { |
---|
123 | try { |
---|
124 | return Long.parseLong( value ) |
---|
125 | } catch( Exception e2 ) { |
---|
126 | return value.toString(); |
---|
127 | } |
---|
128 | } |
---|
129 | case TemplateFieldType.DOUBLE: |
---|
130 | try { |
---|
131 | return Double.valueOf( value ) |
---|
132 | } catch( Exception e ) { |
---|
133 | return value.toString(); |
---|
134 | } |
---|
135 | case TemplateFieldType.BOOLEAN: |
---|
136 | try { |
---|
137 | return Boolean.valueOf( value ) |
---|
138 | } catch( Exception e ) { |
---|
139 | println e.getMessage(); |
---|
140 | return value.toString(); |
---|
141 | } |
---|
142 | case TemplateFieldType.LONG: |
---|
143 | try { |
---|
144 | return Long.valueOf( value ) |
---|
145 | } catch( Exception e ) { |
---|
146 | return value.toString(); |
---|
147 | } |
---|
148 | case TemplateFieldType.STRING: |
---|
149 | case TemplateFieldType.TEXT: |
---|
150 | case TemplateFieldType.STRINGLIST: |
---|
151 | case TemplateFieldType.TEMPLATE: |
---|
152 | case TemplateFieldType.MODULE: |
---|
153 | case TemplateFieldType.FILE: |
---|
154 | case TemplateFieldType.ONTOLOGYTERM: |
---|
155 | default: |
---|
156 | return value.toString(); |
---|
157 | } |
---|
158 | |
---|
159 | } |
---|
160 | } |
---|