1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.portals.graffito.jcr.query.impl; |
19 |
|
|
20 |
|
|
21 |
|
import java.util.ArrayList; |
22 |
|
import java.util.Iterator; |
23 |
|
import java.util.Map; |
24 |
|
import java.util.List; |
25 |
|
|
26 |
|
import org.apache.commons.logging.Log; |
27 |
|
import org.apache.commons.logging.LogFactory; |
28 |
|
import org.apache.portals.graffito.jcr.mapper.model.ClassDescriptor; |
29 |
|
import org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverter; |
30 |
|
import org.apache.portals.graffito.jcr.query.Filter; |
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
public class FilterImpl implements Filter { |
39 |
0 |
private final static Log log = LogFactory.getLog(FilterImpl.class); |
40 |
|
|
41 |
|
private Class claszz; |
42 |
0 |
private String scope = ""; |
43 |
0 |
private List jcrExpressions = new ArrayList(); |
44 |
|
|
45 |
|
private ClassDescriptor classDescriptor; |
46 |
|
private Map atomicTypeConverters; |
47 |
|
|
48 |
|
private String orJcrExpression; |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
0 |
public FilterImpl(ClassDescriptor classDescriptor, Map atomicTypeConverters, Class clazz) { |
58 |
0 |
this.claszz = clazz; |
59 |
0 |
this.atomicTypeConverters = atomicTypeConverters; |
60 |
0 |
this.classDescriptor = classDescriptor; |
61 |
0 |
} |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
public Class getFilterClass() { |
68 |
0 |
return claszz; |
69 |
|
} |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
public void setScope(String scope) { |
75 |
0 |
this.scope = scope; |
76 |
0 |
} |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
public String getScope() { |
82 |
0 |
return this.scope; |
83 |
|
} |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
public Filter addContains(String scope, String fullTextSearch) { |
89 |
0 |
String jcrExpression = null; |
90 |
0 |
if (scope.equals(".")) { |
91 |
0 |
jcrExpression = "jcr:contains(., '" + fullTextSearch + "')"; |
92 |
|
} |
93 |
|
else { |
94 |
0 |
jcrExpression = "jcr:contains(@" + this.getJcrFieldName(scope) + ", '" + fullTextSearch |
95 |
|
+ "')"; |
96 |
|
} |
97 |
|
|
98 |
0 |
jcrExpressions.add(jcrExpression); |
99 |
|
|
100 |
0 |
return this; |
101 |
|
} |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
public Filter addBetween(String fieldAttributeName, Object value1, Object value2) { |
107 |
0 |
String jcrExpression = "( @" + this.getJcrFieldName(fieldAttributeName) + " >= " |
108 |
|
+ this.getStringValue(value1) |
109 |
|
+ " and @" + this.getJcrFieldName(fieldAttributeName) + " <= " |
110 |
|
+ this.getStringValue(value2) + ")"; |
111 |
|
|
112 |
0 |
jcrExpressions.add(jcrExpression); |
113 |
|
|
114 |
0 |
return this; |
115 |
|
} |
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
public Filter addEqualTo(String fieldAttributeName, Object value) { |
121 |
0 |
String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName) + " = " |
122 |
|
+ this.getStringValue(value); |
123 |
0 |
jcrExpressions.add(jcrExpression); |
124 |
|
|
125 |
0 |
return this; |
126 |
|
} |
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
public Filter addGreaterOrEqualThan(String fieldAttributeName, Object value) { |
132 |
0 |
String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName) + " >= " |
133 |
|
+ this.getStringValue(value); |
134 |
0 |
jcrExpressions.add(jcrExpression); |
135 |
|
|
136 |
0 |
return this; |
137 |
|
} |
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
public Filter addGreaterThan(String fieldAttributeName, Object value) { |
143 |
0 |
String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName) + " > " |
144 |
|
+ this.getStringValue(value); |
145 |
0 |
jcrExpressions.add(jcrExpression); |
146 |
|
|
147 |
0 |
return this; |
148 |
|
} |
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
public Filter addLessOrEqualThan(String fieldAttributeName, Object value) { |
154 |
0 |
String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName) + " <= " |
155 |
|
+ this.getStringValue(value); |
156 |
0 |
jcrExpressions.add(jcrExpression); |
157 |
|
|
158 |
0 |
return this; |
159 |
|
} |
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
public Filter addLessThan(String fieldAttributeName, Object value) { |
165 |
0 |
String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName) + " < " |
166 |
|
+ this.getStringValue(value); |
167 |
0 |
jcrExpressions.add(jcrExpression); |
168 |
|
|
169 |
0 |
return this; |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
public Filter addLike(String fieldAttributeName, Object value) { |
176 |
0 |
String jcrExpression = "jcr:like(" + "@" + this.getJcrFieldName(fieldAttributeName) + ", '" |
177 |
|
+ value + "')"; |
178 |
0 |
jcrExpressions.add(jcrExpression); |
179 |
|
|
180 |
0 |
return this; |
181 |
|
} |
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
public Filter addNotEqualTo(String fieldAttributeName, Object value) { |
187 |
0 |
String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName) + " != " |
188 |
|
+ this.getStringValue(value); |
189 |
0 |
jcrExpressions.add(jcrExpression); |
190 |
|
|
191 |
0 |
return this; |
192 |
|
} |
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
public Filter addNotNull(String fieldAttributeName) { |
198 |
0 |
String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName); |
199 |
0 |
jcrExpressions.add(jcrExpression); |
200 |
|
|
201 |
0 |
return this; |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
public Filter addIsNull(String fieldAttributeName) { |
208 |
0 |
String jcrExpression = "not(@" + this.getJcrFieldName(fieldAttributeName) + ")"; |
209 |
0 |
jcrExpressions.add(jcrExpression); |
210 |
|
|
211 |
0 |
return this; |
212 |
|
} |
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
public Filter addOrFilter(Filter filter) { |
218 |
0 |
orJcrExpression = ((FilterImpl) filter).getJcrExpression(); |
219 |
|
|
220 |
0 |
return this; |
221 |
|
} |
222 |
|
|
223 |
|
public Filter addJCRExpression(String jcrExpression) { |
224 |
0 |
jcrExpressions.add(jcrExpression); |
225 |
|
|
226 |
0 |
return this; |
227 |
|
} |
228 |
|
|
229 |
|
private String getJcrFieldName(String fieldAttribute) { |
230 |
0 |
String jcrFieldName = classDescriptor.getJcrName(fieldAttribute); |
231 |
0 |
if (jcrFieldName == null) { |
232 |
0 |
log.error("Impossible to find the jcrFieldName for the attribute :" + fieldAttribute); |
233 |
|
} |
234 |
|
|
235 |
0 |
return jcrFieldName; |
236 |
|
|
237 |
|
} |
238 |
|
|
239 |
|
private String getStringValue(Object value) { |
240 |
0 |
AtomicTypeConverter atomicTypeConverter = (AtomicTypeConverter) atomicTypeConverters.get( |
241 |
|
value.getClass()); |
242 |
|
|
243 |
0 |
return atomicTypeConverter.getStringValue(value); |
244 |
|
} |
245 |
|
|
246 |
|
public String getJcrExpression() { |
247 |
0 |
if ((orJcrExpression == null) || orJcrExpression.equals("")) { |
248 |
0 |
return buildJcrExpression(); |
249 |
|
} |
250 |
|
else { |
251 |
0 |
return "(" + buildJcrExpression() + ") or (" + this.orJcrExpression + ")"; |
252 |
|
} |
253 |
|
} |
254 |
|
|
255 |
|
private String buildJcrExpression() { |
256 |
0 |
int count = 1; |
257 |
0 |
String jcrExp = ""; |
258 |
|
|
259 |
0 |
Iterator criteriaIterator = jcrExpressions.iterator(); |
260 |
0 |
while (criteriaIterator.hasNext()) { |
261 |
0 |
if (count > 1) { |
262 |
0 |
jcrExp += " and "; |
263 |
|
} |
264 |
0 |
jcrExp += (String) criteriaIterator.next(); |
265 |
0 |
count++; |
266 |
|
|
267 |
|
} |
268 |
|
|
269 |
0 |
return jcrExp; |
270 |
|
} |
271 |
|
} |