1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krad.criteria; |
17 | |
|
18 | |
import org.apache.ojb.broker.query.Criteria; |
19 | |
import org.apache.ojb.broker.query.Query; |
20 | |
import org.apache.ojb.broker.query.QueryFactory; |
21 | |
import org.joda.time.DateTime; |
22 | |
import org.kuali.rice.core.api.criteria.AndPredicate; |
23 | |
import org.kuali.rice.core.api.criteria.CompositePredicate; |
24 | |
import org.kuali.rice.core.api.criteria.CountFlag; |
25 | |
import org.kuali.rice.core.api.criteria.CriteriaDateTimeValue; |
26 | |
import org.kuali.rice.core.api.criteria.CriteriaLookupService; |
27 | |
import org.kuali.rice.core.api.criteria.CriteriaValue; |
28 | |
import org.kuali.rice.core.api.criteria.EqualIgnoreCasePredicate; |
29 | |
import org.kuali.rice.core.api.criteria.EqualPredicate; |
30 | |
import org.kuali.rice.core.api.criteria.GenericQueryResults; |
31 | |
import org.kuali.rice.core.api.criteria.GreaterThanOrEqualPredicate; |
32 | |
import org.kuali.rice.core.api.criteria.GreaterThanPredicate; |
33 | |
import org.kuali.rice.core.api.criteria.InIgnoreCasePredicate; |
34 | |
import org.kuali.rice.core.api.criteria.InPredicate; |
35 | |
import org.kuali.rice.core.api.criteria.LessThanOrEqualPredicate; |
36 | |
import org.kuali.rice.core.api.criteria.LessThanPredicate; |
37 | |
import org.kuali.rice.core.api.criteria.LikePredicate; |
38 | |
import org.kuali.rice.core.api.criteria.LookupCustomizer; |
39 | |
import org.kuali.rice.core.api.criteria.MultiValuedPredicate; |
40 | |
import org.kuali.rice.core.api.criteria.NotEqualIgnoreCasePredicate; |
41 | |
import org.kuali.rice.core.api.criteria.NotEqualPredicate; |
42 | |
import org.kuali.rice.core.api.criteria.NotInIgnoreCasePredicate; |
43 | |
import org.kuali.rice.core.api.criteria.NotInPredicate; |
44 | |
import org.kuali.rice.core.api.criteria.NotLikePredicate; |
45 | |
import org.kuali.rice.core.api.criteria.NotNullPredicate; |
46 | |
import org.kuali.rice.core.api.criteria.NullPredicate; |
47 | |
import org.kuali.rice.core.api.criteria.OrPredicate; |
48 | |
import org.kuali.rice.core.api.criteria.Predicate; |
49 | |
import org.kuali.rice.core.api.criteria.PropertyPathPredicate; |
50 | |
import org.kuali.rice.core.api.criteria.QueryByCriteria; |
51 | |
import org.kuali.rice.core.api.criteria.SingleValuedPredicate; |
52 | |
import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb; |
53 | |
|
54 | |
import java.sql.Timestamp; |
55 | |
import java.util.ArrayList; |
56 | |
import java.util.HashSet; |
57 | |
import java.util.List; |
58 | |
import java.util.Set; |
59 | |
|
60 | 0 | public class CriteriaLookupDaoOjb extends PlatformAwareDaoBaseOjb implements CriteriaLookupDao { |
61 | |
|
62 | |
@Override |
63 | |
public <T> GenericQueryResults<T> lookup(final Class<T> queryClass, final QueryByCriteria criteria) { |
64 | 0 | return lookup(queryClass, criteria, LookupCustomizer.Builder.<T>create().build()); |
65 | |
} |
66 | |
|
67 | |
@Override |
68 | |
public <T> GenericQueryResults<T> lookup(final Class<T> queryClass, final QueryByCriteria criteria, LookupCustomizer<T> customizer) { |
69 | 0 | if (queryClass == null) { |
70 | 0 | throw new IllegalArgumentException("queryClass is null"); |
71 | |
} |
72 | |
|
73 | 0 | if (criteria == null) { |
74 | 0 | throw new IllegalArgumentException("criteria is null"); |
75 | |
} |
76 | |
|
77 | 0 | if (customizer == null) { |
78 | 0 | throw new IllegalArgumentException("customizer is null"); |
79 | |
} |
80 | |
|
81 | 0 | final Criteria parent = new Criteria(); |
82 | |
|
83 | 0 | if (criteria.getPredicate() != null) { |
84 | 0 | addPredicate(criteria.getPredicate(), parent, customizer.getPredicateTransform()); |
85 | |
} |
86 | |
|
87 | 0 | switch (criteria.getCountFlag()) { |
88 | |
case ONLY: |
89 | 0 | return forCountOnly(queryClass, criteria, parent); |
90 | |
case NONE: |
91 | 0 | return forRowResults(queryClass, criteria, parent, criteria.getCountFlag(), customizer.getResultTransform()); |
92 | |
case INCLUDE: |
93 | 0 | return forRowResults(queryClass, criteria, parent, criteria.getCountFlag(), customizer.getResultTransform()); |
94 | 0 | default: throw new UnsupportedCountFlagException(criteria.getCountFlag()); |
95 | |
} |
96 | |
} |
97 | |
|
98 | |
|
99 | |
private <T> GenericQueryResults<T> forRowResults(final Class<T> queryClass, final QueryByCriteria criteria, final Criteria ojbCriteria, CountFlag flag, LookupCustomizer.Transform<T, T> transform) { |
100 | 0 | final Query ojbQuery = QueryFactory.newQuery(queryClass, ojbCriteria); |
101 | 0 | final GenericQueryResults.Builder<T> results = GenericQueryResults.Builder.<T>create(); |
102 | |
|
103 | 0 | if (flag == CountFlag.INCLUDE) { |
104 | 0 | results.setTotalRowCount(getPersistenceBrokerTemplate().getCount(ojbQuery)); |
105 | |
} |
106 | |
|
107 | |
|
108 | 0 | final int startAtIndex = criteria.getStartAtIndex() != null ? criteria.getStartAtIndex() + 1 : 1; |
109 | 0 | ojbQuery.setStartAtIndex(startAtIndex); |
110 | |
|
111 | 0 | if (criteria.getMaxResults() != null) { |
112 | |
|
113 | |
|
114 | 0 | ojbQuery.setEndAtIndex(criteria.getMaxResults() + startAtIndex); |
115 | |
} |
116 | |
|
117 | |
@SuppressWarnings("unchecked") |
118 | 0 | final List<T> rows = new ArrayList<T>(getPersistenceBrokerTemplate().getCollectionByQuery(ojbQuery)); |
119 | |
|
120 | 0 | if (criteria.getMaxResults() != null && rows.size() > criteria.getMaxResults()) { |
121 | 0 | results.setMoreResultsAvailable(true); |
122 | |
|
123 | 0 | rows.remove(criteria.getMaxResults().intValue()); |
124 | |
} |
125 | |
|
126 | 0 | results.setResults(transformResults(rows, transform)); |
127 | 0 | return results.build(); |
128 | |
} |
129 | |
|
130 | |
private static <T> List<T> transformResults(List<T> results, LookupCustomizer.Transform<T, T> transform) { |
131 | 0 | final List<T> list = new ArrayList<T>(); |
132 | 0 | for (T r : results) { |
133 | 0 | list.add(transform.apply(r)); |
134 | |
} |
135 | 0 | return list; |
136 | |
} |
137 | |
|
138 | |
|
139 | |
private <T> GenericQueryResults<T> forCountOnly(final Class<T> queryClass, final QueryByCriteria criteria, final Criteria ojbCriteria) { |
140 | 0 | final Query ojbQuery = QueryFactory.newQuery(queryClass, ojbCriteria); |
141 | 0 | final GenericQueryResults.Builder<T> results = GenericQueryResults.Builder.<T>create(); |
142 | 0 | results.setTotalRowCount(getPersistenceBrokerTemplate().getCount(ojbQuery)); |
143 | |
|
144 | 0 | return results.build(); |
145 | |
} |
146 | |
|
147 | |
|
148 | |
private void addPredicate(Predicate p, Criteria parent, LookupCustomizer.Transform<Predicate, Predicate> transform) { |
149 | 0 | p = transform.apply(p); |
150 | |
|
151 | 0 | if (p instanceof PropertyPathPredicate) { |
152 | 0 | final String pp = ((PropertyPathPredicate) p).getPropertyPath(); |
153 | 0 | if (p instanceof NotNullPredicate) { |
154 | 0 | parent.addNotNull(pp); |
155 | 0 | } else if (p instanceof NullPredicate) { |
156 | 0 | parent.addIsNull(pp); |
157 | 0 | } else if (p instanceof SingleValuedPredicate) { |
158 | 0 | addSingleValuePredicate((SingleValuedPredicate) p, parent); |
159 | 0 | } else if (p instanceof MultiValuedPredicate) { |
160 | 0 | addMultiValuePredicate((MultiValuedPredicate) p, parent); |
161 | |
} else { |
162 | 0 | throw new UnsupportedPredicateException(p); |
163 | |
} |
164 | 0 | } else if (p instanceof CompositePredicate) { |
165 | 0 | addCompositePredicate((CompositePredicate) p, parent, transform); |
166 | |
} else { |
167 | 0 | throw new UnsupportedPredicateException(p); |
168 | |
} |
169 | 0 | } |
170 | |
|
171 | |
|
172 | |
private void addSingleValuePredicate(SingleValuedPredicate p, Criteria parent) { |
173 | 0 | final Object value = getVal(p.getValue()); |
174 | 0 | final String pp = p.getPropertyPath(); |
175 | 0 | if (p instanceof EqualPredicate) { |
176 | 0 | parent.addEqualTo(pp, value); |
177 | 0 | } else if (p instanceof EqualIgnoreCasePredicate) { |
178 | 0 | parent.addEqualTo(genUpperFunc(pp), ((String) value).toUpperCase()); |
179 | 0 | } else if (p instanceof GreaterThanOrEqualPredicate) { |
180 | 0 | parent.addGreaterOrEqualThan(pp, value); |
181 | 0 | } else if (p instanceof GreaterThanPredicate) { |
182 | 0 | parent.addGreaterThan(pp, value); |
183 | 0 | } else if (p instanceof LessThanOrEqualPredicate) { |
184 | 0 | parent.addLessOrEqualThan(pp, value); |
185 | 0 | } else if (p instanceof LessThanPredicate) { |
186 | 0 | parent.addLessThan(pp, value); |
187 | 0 | } else if (p instanceof LikePredicate) { |
188 | |
|
189 | 0 | parent.addLike(pp, value); |
190 | 0 | } else if (p instanceof NotEqualPredicate) { |
191 | 0 | parent.addNotEqualTo(pp, value); |
192 | 0 | } else if (p instanceof NotEqualIgnoreCasePredicate) { |
193 | 0 | parent.addNotEqualTo(genUpperFunc(pp), ((String) value).toUpperCase()); |
194 | 0 | } else if (p instanceof NotLikePredicate) { |
195 | 0 | parent.addNotLike(pp, value); |
196 | |
} else { |
197 | 0 | throw new UnsupportedPredicateException(p); |
198 | |
} |
199 | 0 | } |
200 | |
|
201 | |
|
202 | |
private void addMultiValuePredicate(MultiValuedPredicate p, Criteria parent) { |
203 | 0 | final String pp = p.getPropertyPath(); |
204 | 0 | if (p instanceof InPredicate) { |
205 | 0 | final Set<?> values = getVals(p.getValues()); |
206 | 0 | parent.addIn(pp, values); |
207 | 0 | } else if (p instanceof InIgnoreCasePredicate) { |
208 | 0 | final Set<String> values = toUpper(getValsUnsafe(((InIgnoreCasePredicate) p).getValues())); |
209 | 0 | parent.addIn(genUpperFunc(pp), values); |
210 | 0 | } else if (p instanceof NotInPredicate) { |
211 | 0 | final Set<?> values = getVals(p.getValues()); |
212 | 0 | parent.addNotIn(pp, values); |
213 | 0 | } else if (p instanceof NotInIgnoreCasePredicate) { |
214 | 0 | final Set<String> values = toUpper(getValsUnsafe(((NotInIgnoreCasePredicate) p).getValues())); |
215 | 0 | parent.addNotIn(genUpperFunc(pp), values); |
216 | 0 | } else { |
217 | 0 | throw new UnsupportedPredicateException(p); |
218 | |
} |
219 | 0 | } |
220 | |
|
221 | |
|
222 | |
private void addCompositePredicate(final CompositePredicate p, final Criteria parent, LookupCustomizer.Transform<Predicate, Predicate> transform) { |
223 | 0 | for (Predicate ip : p.getPredicates()) { |
224 | 0 | final Criteria inner = new Criteria(); |
225 | 0 | addPredicate(ip, inner, transform); |
226 | 0 | if (p instanceof AndPredicate) { |
227 | 0 | parent.addAndCriteria(inner); |
228 | 0 | } else if (p instanceof OrPredicate) { |
229 | 0 | parent.addOrCriteria(inner); |
230 | |
} else { |
231 | 0 | throw new UnsupportedPredicateException(p); |
232 | |
} |
233 | 0 | } |
234 | 0 | } |
235 | |
|
236 | |
private static <U extends CriteriaValue<?>> Object getVal(U toConv) { |
237 | 0 | Object o = toConv.getValue(); |
238 | 0 | if (o instanceof DateTime) { |
239 | 0 | return new Timestamp(((DateTime) o).getMillis()); |
240 | |
} |
241 | 0 | return o; |
242 | |
} |
243 | |
|
244 | |
|
245 | |
@SuppressWarnings("unchecked") |
246 | |
private static <T, U extends CriteriaValue<T>> Set<T> getValsUnsafe(Set<? extends U> toConv) { |
247 | 0 | return (Set<T>) getVals(toConv); |
248 | |
} |
249 | |
|
250 | |
private static Set<?> getVals(Set<? extends CriteriaValue<?>> toConv) { |
251 | 0 | final Set<Object> values = new HashSet<Object>(); |
252 | 0 | for (CriteriaValue<?> value : toConv) { |
253 | 0 | values.add(getVal(value)); |
254 | |
} |
255 | 0 | return values; |
256 | |
} |
257 | |
|
258 | |
|
259 | |
private static Set<String> toUpper(Set<String> strs) { |
260 | 0 | final Set<String> values = new HashSet<String>(); |
261 | 0 | for (String value : strs) { |
262 | 0 | values.add(value.toUpperCase()); |
263 | |
} |
264 | 0 | return values; |
265 | |
} |
266 | |
|
267 | |
private String getUpperFunction() { |
268 | 0 | return getDbPlatform().getUpperCaseFunction(); |
269 | |
} |
270 | |
|
271 | |
private String genUpperFunc(String pp) { |
272 | 0 | return new StringBuilder(getUpperFunction()).append("(").append(pp).append(")").toString(); |
273 | |
} |
274 | |
|
275 | |
|
276 | 0 | private static class UnsupportedPredicateException extends RuntimeException { |
277 | |
private UnsupportedPredicateException(Predicate predicate) { |
278 | 0 | super("Unsupported predicate [" + String.valueOf(predicate) + "]"); |
279 | 0 | } |
280 | |
} |
281 | |
|
282 | |
|
283 | 0 | private static class UnsupportedCountFlagException extends RuntimeException { |
284 | |
private UnsupportedCountFlagException(CountFlag flag) { |
285 | 0 | super("Unsupported predicate [" + String.valueOf(flag) + "]"); |
286 | 0 | } |
287 | |
} |
288 | |
} |