1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krad.dao.impl; |
17 | |
|
18 | |
import org.apache.commons.beanutils.PropertyUtils; |
19 | |
import org.apache.commons.lang.StringUtils; |
20 | |
import org.kuali.rice.core.api.datetime.DateTimeService; |
21 | |
import org.kuali.rice.core.api.search.SearchOperator; |
22 | |
import org.kuali.rice.core.api.util.RiceKeyConstants; |
23 | |
import org.kuali.rice.core.api.util.type.TypeUtils; |
24 | |
import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria; |
25 | |
import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria; |
26 | |
import org.kuali.rice.core.framework.persistence.jpa.metadata.EntityDescriptor; |
27 | |
import org.kuali.rice.core.framework.persistence.jpa.metadata.FieldDescriptor; |
28 | |
import org.kuali.rice.core.framework.persistence.jpa.metadata.MetadataManager; |
29 | |
import org.kuali.rice.core.framework.persistence.ojb.conversion.OjbCharBooleanConversion; |
30 | |
import org.kuali.rice.krad.bo.InactivatableFromTo; |
31 | |
import org.kuali.rice.krad.bo.PersistableBusinessObject; |
32 | |
import org.kuali.rice.krad.bo.PersistableBusinessObjectExtension; |
33 | |
import org.kuali.rice.krad.dao.LookupDao; |
34 | |
import org.kuali.rice.krad.lookup.CollectionIncomplete; |
35 | |
import org.kuali.rice.krad.lookup.LookupUtils; |
36 | |
import org.kuali.rice.krad.service.DataDictionaryService; |
37 | |
import org.kuali.rice.krad.service.KRADServiceLocatorInternal; |
38 | |
import org.kuali.rice.krad.service.KRADServiceLocatorWeb; |
39 | |
import org.kuali.rice.krad.service.PersistenceStructureService; |
40 | |
import org.kuali.rice.krad.util.GlobalVariables; |
41 | |
import org.kuali.rice.krad.util.KRADConstants; |
42 | |
import org.kuali.rice.krad.util.KRADPropertyConstants; |
43 | |
import org.kuali.rice.krad.util.ObjectUtils; |
44 | |
import org.springframework.dao.DataIntegrityViolationException; |
45 | |
|
46 | |
import javax.persistence.EntityManager; |
47 | |
import javax.persistence.PersistenceContext; |
48 | |
import javax.persistence.PersistenceException; |
49 | |
import java.lang.reflect.Field; |
50 | |
import java.math.BigDecimal; |
51 | |
import java.sql.Timestamp; |
52 | |
import java.text.ParseException; |
53 | |
import java.util.ArrayList; |
54 | |
import java.util.Collection; |
55 | |
import java.util.Iterator; |
56 | |
import java.util.List; |
57 | |
import java.util.Map; |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | 0 | public class LookupDaoJpa implements LookupDao { |
63 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupDao.class); |
64 | |
|
65 | |
private DateTimeService dateTimeService; |
66 | |
private PersistenceStructureService persistenceStructureService; |
67 | |
private DataDictionaryService dataDictionaryService; |
68 | |
|
69 | |
@PersistenceContext |
70 | |
private EntityManager entityManager; |
71 | |
|
72 | |
public Long findCountByMap(Object example, Map formProps) { |
73 | 0 | Criteria criteria = new Criteria(example.getClass().getName()); |
74 | |
|
75 | |
|
76 | 0 | Iterator propsIter = formProps.keySet().iterator(); |
77 | 0 | while (propsIter.hasNext()) { |
78 | 0 | String propertyName = (String) propsIter.next(); |
79 | 0 | String searchValue = (String) formProps.get(propertyName); |
80 | |
|
81 | |
|
82 | 0 | if (StringUtils.isBlank(searchValue) || !(PropertyUtils.isWriteable(example, propertyName))) { |
83 | 0 | continue; |
84 | |
} |
85 | |
|
86 | |
|
87 | 0 | Class propertyType = ObjectUtils.getPropertyType(example, propertyName, persistenceStructureService); |
88 | 0 | if (propertyType == null) { |
89 | 0 | continue; |
90 | |
} |
91 | 0 | Boolean caseInsensitive = Boolean.TRUE; |
92 | 0 | if (KRADServiceLocatorWeb.getDataDictionaryService().isAttributeDefined(example.getClass(), propertyName)) { |
93 | 0 | caseInsensitive = !KRADServiceLocatorWeb.getDataDictionaryService().getAttributeForceUppercase(example.getClass(), propertyName); |
94 | |
} |
95 | 0 | if (caseInsensitive == null) { |
96 | 0 | caseInsensitive = Boolean.TRUE; |
97 | |
} |
98 | |
|
99 | 0 | boolean treatWildcardsAndOperatorsAsLiteral = KRADServiceLocatorWeb. |
100 | |
getBusinessObjectDictionaryService().isLookupFieldTreatWildcardsAndOperatorsAsLiteral(example.getClass(), propertyName); |
101 | |
|
102 | 0 | addCriteria(propertyName, searchValue, propertyType, caseInsensitive, treatWildcardsAndOperatorsAsLiteral, criteria); |
103 | 0 | } |
104 | |
|
105 | |
|
106 | 0 | return (Long) new QueryByCriteria(entityManager, criteria).toCountQuery().getSingleResult(); |
107 | |
} |
108 | |
|
109 | |
public Collection findCollectionBySearchHelper(Class businessObjectClass, Map formProps, boolean unbounded, boolean usePrimaryKeyValuesOnly) { |
110 | 0 | PersistableBusinessObject businessObject = checkBusinessObjectClass(businessObjectClass); |
111 | 0 | if (usePrimaryKeyValuesOnly) { |
112 | 0 | return executeSearch(businessObjectClass, getCollectionCriteriaFromMapUsingPrimaryKeysOnly(businessObjectClass, formProps), unbounded); |
113 | |
} else { |
114 | 0 | Criteria crit = getCollectionCriteriaFromMap(businessObject, formProps); |
115 | 0 | return executeSearch(businessObjectClass, crit, unbounded); |
116 | |
} |
117 | |
} |
118 | |
|
119 | |
public Criteria getCollectionCriteriaFromMap(PersistableBusinessObject example, Map formProps) { |
120 | 0 | Criteria criteria = new Criteria(example.getClass().getName()); |
121 | 0 | Iterator propsIter = formProps.keySet().iterator(); |
122 | 0 | while (propsIter.hasNext()) { |
123 | 0 | String propertyName = (String) propsIter.next(); |
124 | 0 | Boolean caseInsensitive = Boolean.TRUE; |
125 | 0 | if (KRADServiceLocatorWeb.getDataDictionaryService().isAttributeDefined(example.getClass(), propertyName)) { |
126 | 0 | caseInsensitive = !KRADServiceLocatorWeb.getDataDictionaryService().getAttributeForceUppercase(example.getClass(), propertyName); |
127 | |
} |
128 | 0 | if (caseInsensitive == null) { |
129 | 0 | caseInsensitive = Boolean.TRUE; |
130 | |
} |
131 | 0 | boolean treatWildcardsAndOperatorsAsLiteral = KRADServiceLocatorWeb. |
132 | |
getBusinessObjectDictionaryService().isLookupFieldTreatWildcardsAndOperatorsAsLiteral(example.getClass(), propertyName); |
133 | 0 | if (formProps.get(propertyName) instanceof Collection) { |
134 | 0 | Iterator iter = ((Collection) formProps.get(propertyName)).iterator(); |
135 | 0 | while (iter.hasNext()) { |
136 | 0 | if (!createCriteria(example, (String) iter.next(), propertyName, caseInsensitive, treatWildcardsAndOperatorsAsLiteral, criteria, formProps)) { |
137 | 0 | throw new RuntimeException("Invalid value in Collection"); |
138 | |
} |
139 | |
} |
140 | 0 | } else { |
141 | 0 | if (!createCriteria(example, (String) formProps.get(propertyName), propertyName, caseInsensitive, treatWildcardsAndOperatorsAsLiteral, criteria, formProps)) { |
142 | 0 | continue; |
143 | |
} |
144 | |
} |
145 | 0 | } |
146 | 0 | return criteria; |
147 | |
} |
148 | |
|
149 | |
public Criteria getCollectionCriteriaFromMapUsingPrimaryKeysOnly(Class businessObjectClass, Map formProps) { |
150 | 0 | PersistableBusinessObject businessObject = checkBusinessObjectClass(businessObjectClass); |
151 | 0 | Criteria criteria = new Criteria(businessObjectClass.getName()); |
152 | 0 | List pkFields = persistenceStructureService.listPrimaryKeyFieldNames(businessObjectClass); |
153 | 0 | Iterator pkIter = pkFields.iterator(); |
154 | 0 | while (pkIter.hasNext()) { |
155 | 0 | String pkFieldName = (String) pkIter.next(); |
156 | 0 | String pkValue = (String) formProps.get(pkFieldName); |
157 | |
|
158 | 0 | if (StringUtils.isBlank(pkValue)) { |
159 | 0 | throw new RuntimeException("Missing pk value for field " + pkFieldName + " when a search based on PK values only is performed."); |
160 | |
} else { |
161 | 0 | for (SearchOperator op : SearchOperator.QUERY_CHARACTERS) { |
162 | 0 | if (pkValue.contains(op.op())) { |
163 | 0 | throw new RuntimeException("Value \"" + pkValue + "\" for PK field " + pkFieldName + " contains wildcard/operator characters."); |
164 | |
} |
165 | |
} |
166 | |
} |
167 | 0 | boolean treatWildcardsAndOperatorsAsLiteral = KRADServiceLocatorWeb. |
168 | |
getBusinessObjectDictionaryService().isLookupFieldTreatWildcardsAndOperatorsAsLiteral(businessObjectClass, pkFieldName); |
169 | 0 | createCriteria(businessObject, pkValue, pkFieldName, false, treatWildcardsAndOperatorsAsLiteral, criteria); |
170 | 0 | } |
171 | 0 | return criteria; |
172 | |
} |
173 | |
|
174 | |
private PersistableBusinessObject checkBusinessObjectClass(Class businessObjectClass) { |
175 | 0 | if (businessObjectClass == null) { |
176 | 0 | throw new IllegalArgumentException("BusinessObject class passed to LookupDao findCollectionBySearchHelper... method was null"); |
177 | |
} |
178 | 0 | PersistableBusinessObject businessObject = null; |
179 | |
try { |
180 | 0 | businessObject = (PersistableBusinessObject) businessObjectClass.newInstance(); |
181 | 0 | } catch (IllegalAccessException e) { |
182 | 0 | throw new RuntimeException("LookupDao could not get instance of " + businessObjectClass.getName(), e); |
183 | 0 | } catch (InstantiationException e) { |
184 | 0 | throw new RuntimeException("LookupDao could not get instance of " + businessObjectClass.getName(), e); |
185 | 0 | } |
186 | 0 | return businessObject; |
187 | |
} |
188 | |
|
189 | |
private Collection executeSearch(Class businessObjectClass, Criteria criteria, boolean unbounded) { |
190 | 0 | Collection<PersistableBusinessObject> searchResults = new ArrayList<PersistableBusinessObject>(); |
191 | 0 | Long matchingResultsCount = null; |
192 | |
try { |
193 | 0 | Integer searchResultsLimit = LookupUtils.getSearchResultsLimit(businessObjectClass); |
194 | 0 | if (!unbounded && (searchResultsLimit != null)) { |
195 | 0 | matchingResultsCount = (Long) new QueryByCriteria(entityManager, criteria).toCountQuery().getSingleResult(); |
196 | 0 | searchResults = new QueryByCriteria(entityManager, criteria).toQuery().setMaxResults(searchResultsLimit).getResultList(); |
197 | |
} else { |
198 | 0 | searchResults = new QueryByCriteria(entityManager, criteria).toQuery().getResultList(); |
199 | |
} |
200 | 0 | if ((matchingResultsCount == null) || (matchingResultsCount.intValue() <= searchResultsLimit.intValue())) { |
201 | 0 | matchingResultsCount = new Long(0); |
202 | |
} |
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | 0 | for (PersistableBusinessObject bo : searchResults) { |
208 | 0 | if (bo.getExtension() != null) { |
209 | 0 | PersistableBusinessObjectExtension boe = bo.getExtension(); |
210 | 0 | EntityDescriptor entity = MetadataManager.getEntityDescriptor(bo.getExtension().getClass()); |
211 | 0 | Criteria extensionCriteria = new Criteria(boe.getClass().getName()); |
212 | 0 | for (FieldDescriptor fieldDescriptor : entity.getPrimaryKeys()) { |
213 | |
try { |
214 | 0 | Field field = bo.getClass().getDeclaredField(fieldDescriptor.getName()); |
215 | 0 | field.setAccessible(true); |
216 | 0 | extensionCriteria.eq(fieldDescriptor.getName(), field.get(bo)); |
217 | 0 | } catch (Exception e) { |
218 | 0 | LOG.error(e.getMessage(), e); |
219 | 0 | } |
220 | |
} |
221 | |
try { |
222 | 0 | boe = (PersistableBusinessObjectExtension) new QueryByCriteria(entityManager, extensionCriteria).toQuery().getSingleResult(); |
223 | 0 | } catch (PersistenceException e) {} |
224 | 0 | bo.setExtension(boe); |
225 | 0 | } |
226 | |
} |
227 | |
|
228 | 0 | List bos = new ArrayList(); |
229 | 0 | bos.addAll(searchResults); |
230 | 0 | searchResults = bos; |
231 | 0 | } catch (DataIntegrityViolationException e) { |
232 | 0 | throw new RuntimeException("LookupDao encountered exception during executeSearch", e); |
233 | 0 | } |
234 | 0 | return new CollectionIncomplete(searchResults, matchingResultsCount); |
235 | |
} |
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
private boolean isWriteable(Object o, String p) throws IllegalArgumentException { |
249 | 0 | if (null == o || null == p) { |
250 | 0 | throw new IllegalArgumentException("Cannot check writeable status with null arguments."); |
251 | |
} |
252 | |
|
253 | 0 | boolean b = false; |
254 | |
|
255 | |
|
256 | 0 | if (!(PropertyUtils.isWriteable(o, p))) { |
257 | |
|
258 | |
|
259 | |
|
260 | 0 | if (-1 != p.indexOf('.')) { |
261 | |
|
262 | 0 | String[] parts = p.split("\\."); |
263 | |
|
264 | |
|
265 | 0 | Class c = ObjectUtils.getPropertyType(o, parts[0], persistenceStructureService); |
266 | |
|
267 | 0 | Object i = null; |
268 | |
|
269 | |
|
270 | |
|
271 | 0 | if (Collection.class.isAssignableFrom(c)) { |
272 | 0 | Map<String, Class> m = persistenceStructureService.listCollectionObjectTypes(o.getClass()); |
273 | 0 | c = m.get(parts[0]); |
274 | |
} |
275 | |
|
276 | |
|
277 | |
try { |
278 | 0 | i = c.newInstance(); |
279 | 0 | StringBuffer sb = new StringBuffer(); |
280 | 0 | for (int x = 1; x < parts.length; x++) { |
281 | 0 | sb.append(1 == x ? "" : ".").append(parts[x]); |
282 | |
} |
283 | 0 | b = isWriteable(i, sb.toString()); |
284 | 0 | } catch (InstantiationException ie) { |
285 | 0 | LOG.info(ie); |
286 | 0 | } catch (IllegalAccessException iae) { |
287 | 0 | LOG.info(iae); |
288 | 0 | } |
289 | 0 | } |
290 | |
} else { |
291 | 0 | b = true; |
292 | |
} |
293 | |
|
294 | 0 | return b; |
295 | |
} |
296 | |
|
297 | |
public boolean createCriteria(Object example, String searchValue, String propertyName, Object criteria) { |
298 | 0 | return createCriteria(example, searchValue, propertyName, false, false, criteria); |
299 | |
} |
300 | |
|
301 | |
public boolean createCriteria(Object example, String searchValue, String propertyName, boolean caseInsensitive, boolean treatWildcardsAndOperatorsAsLiteral, Object criteria) { |
302 | 0 | return createCriteria( example, searchValue, propertyName, false, false, criteria, null ); |
303 | |
} |
304 | |
|
305 | |
public boolean createCriteria(Object example, String searchValue, String propertyName, boolean caseInsensitive, boolean treatWildcardsAndOperatorsAsLiteral, Object criteria, Map searchValues) { |
306 | |
|
307 | 0 | if (!(criteria instanceof Criteria) || StringUtils.isBlank(searchValue) || !isWriteable(example, propertyName)) { |
308 | 0 | return false; |
309 | |
} |
310 | |
|
311 | |
|
312 | 0 | Class propertyType = ObjectUtils.getPropertyType(example, propertyName, persistenceStructureService); |
313 | 0 | if (propertyType == null) { |
314 | 0 | return false; |
315 | |
} |
316 | |
|
317 | |
|
318 | 0 | if (example instanceof InactivatableFromTo) { |
319 | 0 | if (KRADPropertyConstants.ACTIVE.equals(propertyName)) { |
320 | 0 | addInactivateableFromToActiveCriteria(example, searchValue, (Criteria) criteria, searchValues); |
321 | 0 | } else if (KRADPropertyConstants.CURRENT.equals(propertyName)) { |
322 | 0 | addInactivateableFromToCurrentCriteria(example, searchValue, (Criteria) criteria, searchValues); |
323 | 0 | } else if (!KRADPropertyConstants.ACTIVE_AS_OF_DATE.equals(propertyName)) { |
324 | 0 | addCriteria(propertyName, searchValue, propertyType, caseInsensitive, |
325 | |
treatWildcardsAndOperatorsAsLiteral, (Criteria) criteria); |
326 | |
} |
327 | |
} else { |
328 | 0 | addCriteria(propertyName, searchValue, propertyType, caseInsensitive, treatWildcardsAndOperatorsAsLiteral, |
329 | |
(Criteria) criteria); |
330 | |
} |
331 | |
|
332 | 0 | return true; |
333 | |
} |
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
public Object findObjectByMap(Object example, Map formProps) { |
340 | 0 | Criteria jpaCriteria = new Criteria(example.getClass().getName()); |
341 | |
|
342 | 0 | Iterator propsIter = formProps.keySet().iterator(); |
343 | 0 | while (propsIter.hasNext()) { |
344 | 0 | String propertyName = (String) propsIter.next(); |
345 | 0 | String searchValue = ""; |
346 | 0 | if (formProps.get(propertyName) != null) { |
347 | 0 | searchValue = (formProps.get(propertyName)).toString(); |
348 | |
} |
349 | |
|
350 | 0 | if (StringUtils.isNotBlank(searchValue) & PropertyUtils.isWriteable(example, propertyName)) { |
351 | 0 | Class propertyType = ObjectUtils.getPropertyType(example, propertyName, persistenceStructureService); |
352 | 0 | if (TypeUtils.isIntegralClass(propertyType) || TypeUtils.isDecimalClass(propertyType)) { |
353 | 0 | if (propertyType.equals(Long.class)) { |
354 | 0 | jpaCriteria.eq(propertyName, new Long(searchValue)); |
355 | |
} else { |
356 | 0 | jpaCriteria.eq(propertyName, new Integer(searchValue)); |
357 | |
} |
358 | 0 | } else if (TypeUtils.isTemporalClass(propertyType)) { |
359 | 0 | jpaCriteria.eq(propertyName, parseDate(ObjectUtils.clean(searchValue))); |
360 | |
} else { |
361 | 0 | jpaCriteria.eq(propertyName, searchValue); |
362 | |
} |
363 | |
} |
364 | 0 | } |
365 | |
|
366 | 0 | return new QueryByCriteria(entityManager, jpaCriteria).toQuery().getSingleResult(); |
367 | |
} |
368 | |
|
369 | |
private void addCriteria(String propertyName, String propertyValue, Class propertyType, boolean caseInsensitive, boolean treatWildcardsAndOperatorsAsLiteral, Criteria criteria) { |
370 | 0 | String alias = ""; |
371 | 0 | String[] keySplit = propertyName.split("\\."); |
372 | 0 | if (keySplit.length > 1) { |
373 | 0 | alias = keySplit[keySplit.length-2]; |
374 | 0 | String variableKey = keySplit[keySplit.length-1]; |
375 | 0 | for (int j = 0; j < keySplit.length - 1; j++) { |
376 | 0 | if (StringUtils.contains(keySplit[j], Criteria.JPA_ALIAS_PREFIX)) { |
377 | 0 | String tempKey = keySplit[j].substring(keySplit[j].indexOf('\'', keySplit[j].indexOf(Criteria.JPA_ALIAS_PREFIX)) + 1, |
378 | |
keySplit[j].lastIndexOf('\'', keySplit[j].indexOf(Criteria.JPA_ALIAS_SUFFIX))); |
379 | 0 | if (criteria.getAliasIndex(tempKey) == -1) { |
380 | 0 | criteria.join(tempKey, tempKey, false, true); |
381 | |
} |
382 | 0 | } else { |
383 | 0 | if (criteria.getAliasIndex(keySplit[j]) == -1) { |
384 | 0 | criteria.join(keySplit[j], keySplit[j], false, true); |
385 | |
} |
386 | |
} |
387 | |
} |
388 | 0 | if (!StringUtils.contains(propertyName, "__JPA_ALIAS[[")) { |
389 | 0 | propertyName = "__JPA_ALIAS[['" + alias + "']]__." + variableKey; |
390 | |
} |
391 | |
} |
392 | 0 | if (!treatWildcardsAndOperatorsAsLiteral && StringUtils.contains(propertyValue, SearchOperator.OR.op())) { |
393 | 0 | addOrCriteria(propertyName, propertyValue, propertyType, caseInsensitive, criteria); |
394 | 0 | return; |
395 | |
} |
396 | |
|
397 | 0 | if (!treatWildcardsAndOperatorsAsLiteral && StringUtils.contains(propertyValue, SearchOperator.AND.op())) { |
398 | 0 | addAndCriteria(propertyName, propertyValue, propertyType, caseInsensitive, criteria); |
399 | 0 | return; |
400 | |
} |
401 | |
|
402 | 0 | if (StringUtils.containsIgnoreCase(propertyValue, SearchOperator.NULL.op())) { |
403 | 0 | if (StringUtils.contains(propertyValue, SearchOperator.NOT.op())) { |
404 | 0 | criteria.notNull(propertyName); |
405 | |
} |
406 | |
else { |
407 | 0 | criteria.isNull(propertyName); |
408 | |
} |
409 | |
} |
410 | 0 | else if (TypeUtils.isStringClass(propertyType)) { |
411 | |
|
412 | |
|
413 | 0 | if (caseInsensitive) { |
414 | |
|
415 | |
|
416 | 0 | if (StringUtils.contains(propertyName, "__JPA_ALIAS[[")) { |
417 | 0 | propertyName = "UPPER(" + propertyName + ")"; |
418 | |
} else { |
419 | 0 | propertyName = "UPPER(__JPA_ALIAS[[0]]__." + propertyName + ")"; |
420 | |
} |
421 | 0 | propertyValue = propertyValue.toUpperCase(); |
422 | |
} |
423 | 0 | if (!treatWildcardsAndOperatorsAsLiteral && StringUtils.contains(propertyValue, |
424 | |
SearchOperator.NOT.op())) { |
425 | 0 | addNotCriteria(propertyName, propertyValue, propertyType, |
426 | |
caseInsensitive, criteria); |
427 | 0 | } else if ( |
428 | |
!treatWildcardsAndOperatorsAsLiteral && propertyValue != null && ( |
429 | |
StringUtils.contains(propertyValue, SearchOperator.BETWEEN.op()) |
430 | |
|| propertyValue.startsWith(">") |
431 | |
|| propertyValue.startsWith("<") ) ) { |
432 | 0 | addStringRangeCriteria(propertyName, propertyValue, criteria); |
433 | |
} else { |
434 | 0 | if (treatWildcardsAndOperatorsAsLiteral) { |
435 | 0 | propertyValue = StringUtils.replace(propertyValue, "*", "\\*"); |
436 | |
} |
437 | 0 | criteria.like(propertyName, propertyValue); |
438 | |
} |
439 | 0 | } else if (TypeUtils.isIntegralClass(propertyType) || TypeUtils.isDecimalClass(propertyType)) { |
440 | 0 | addNumericRangeCriteria(propertyName, propertyValue, criteria); |
441 | 0 | } else if (TypeUtils.isTemporalClass(propertyType)) { |
442 | 0 | addDateRangeCriteria(propertyName, propertyValue, criteria); |
443 | 0 | } else if (TypeUtils.isBooleanClass(propertyType)) { |
444 | 0 | String temp = ObjectUtils.clean(propertyValue); |
445 | 0 | criteria.eq(propertyName, ("Y".equalsIgnoreCase(temp) || "T".equalsIgnoreCase(temp) || "1".equalsIgnoreCase(temp) || "true".equalsIgnoreCase(temp)) ? true : false); |
446 | 0 | } else { |
447 | 0 | LOG.error("not adding criterion for: " + propertyName + "," + propertyType + "," + propertyValue); |
448 | |
} |
449 | 0 | } |
450 | |
|
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | |
|
456 | |
|
457 | |
|
458 | |
|
459 | |
|
460 | |
protected void addInactivateableFromToActiveCriteria(Object example, String activeSearchValue, Criteria criteria, Map searchValues) { |
461 | 0 | Timestamp activeTimestamp = LookupUtils.getActiveDateTimestampForCriteria(searchValues); |
462 | |
|
463 | 0 | String activeBooleanStr = (String) (new OjbCharBooleanConversion()).javaToSql(activeSearchValue); |
464 | 0 | if (OjbCharBooleanConversion.DATABASE_BOOLEAN_TRUE_STRING_REPRESENTATION.equals(activeBooleanStr)) { |
465 | |
|
466 | 0 | Criteria criteriaBeginDate = new Criteria(example.getClass().getName()); |
467 | 0 | criteriaBeginDate.lte(KRADPropertyConstants.ACTIVE_FROM_DATE, activeTimestamp); |
468 | |
|
469 | 0 | Criteria criteriaBeginDateNull = new Criteria(example.getClass().getName()); |
470 | 0 | criteriaBeginDateNull.isNull(KRADPropertyConstants.ACTIVE_FROM_DATE); |
471 | 0 | criteriaBeginDate.or(criteriaBeginDateNull); |
472 | |
|
473 | 0 | criteria.and(criteriaBeginDate); |
474 | |
|
475 | 0 | Criteria criteriaEndDate = new Criteria(example.getClass().getName()); |
476 | 0 | criteriaEndDate.gt(KRADPropertyConstants.ACTIVE_TO_DATE, activeTimestamp); |
477 | |
|
478 | 0 | Criteria criteriaEndDateNull = new Criteria(example.getClass().getName()); |
479 | 0 | criteriaEndDateNull.isNull(KRADPropertyConstants.ACTIVE_TO_DATE); |
480 | 0 | criteriaEndDate.or(criteriaEndDateNull); |
481 | |
|
482 | 0 | criteria.and(criteriaEndDate); |
483 | 0 | } |
484 | 0 | else if (OjbCharBooleanConversion.DATABASE_BOOLEAN_FALSE_STRING_REPRESENTATION.equals(activeBooleanStr)) { |
485 | |
|
486 | 0 | Criteria criteriaNonActive = new Criteria(example.getClass().getName()); |
487 | 0 | criteriaNonActive.gt(KRADPropertyConstants.ACTIVE_FROM_DATE, activeTimestamp); |
488 | |
|
489 | 0 | Criteria criteriaBeginDateNull = new Criteria(example.getClass().getName()); |
490 | 0 | criteriaBeginDateNull.isNull(KRADPropertyConstants.ACTIVE_FROM_DATE); |
491 | 0 | criteriaNonActive.or(criteriaBeginDateNull); |
492 | |
|
493 | 0 | Criteria criteriaEndDate = new Criteria(example.getClass().getName()); |
494 | 0 | criteriaEndDate.lte(KRADPropertyConstants.ACTIVE_TO_DATE, activeTimestamp); |
495 | 0 | criteriaNonActive.or(criteriaEndDate); |
496 | |
|
497 | 0 | criteria.and(criteriaNonActive); |
498 | |
} |
499 | 0 | } |
500 | |
|
501 | |
|
502 | |
|
503 | |
|
504 | |
|
505 | |
|
506 | |
|
507 | |
|
508 | |
protected void addInactivateableFromToCurrentCriteria(Object example, String currentSearchValue, Criteria criteria, Map searchValues) { |
509 | 0 | Timestamp activeTimestamp = LookupUtils.getActiveDateTimestampForCriteria(searchValues); |
510 | |
|
511 | 0 | List<String> groupByFieldList = dataDictionaryService.getGroupByAttributesForEffectiveDating(example |
512 | |
.getClass()); |
513 | 0 | if (groupByFieldList == null) { |
514 | 0 | return; |
515 | |
} |
516 | |
|
517 | 0 | String alias = "c"; |
518 | |
|
519 | 0 | String jpql = " (select max(" + alias + "." + KRADPropertyConstants.ACTIVE_FROM_DATE + ") from " |
520 | |
+ example.getClass().getName() + " as " + alias + " where "; |
521 | 0 | String activeDateDBStr = KRADServiceLocatorInternal.getDatabasePlatform().getDateSQL(dateTimeService.toDateTimeString(activeTimestamp), null); |
522 | 0 | jpql += alias + "." + KRADPropertyConstants.ACTIVE_FROM_DATE + " <= '" + activeDateDBStr + "'"; |
523 | |
|
524 | |
|
525 | 0 | boolean firstGroupBy = true; |
526 | 0 | String groupByJpql = ""; |
527 | 0 | for (String groupByField : groupByFieldList) { |
528 | 0 | if (!firstGroupBy) { |
529 | 0 | groupByJpql += ", "; |
530 | |
} |
531 | |
|
532 | 0 | jpql += " AND " + alias + "." + groupByField + " = " + criteria.getAlias() + "." + groupByField + " "; |
533 | 0 | groupByJpql += alias + "." + groupByField; |
534 | 0 | firstGroupBy = false; |
535 | |
} |
536 | |
|
537 | 0 | jpql += " group by " + groupByJpql + " )"; |
538 | |
|
539 | 0 | String currentBooleanStr = (String) (new OjbCharBooleanConversion()).javaToSql(currentSearchValue); |
540 | 0 | if (OjbCharBooleanConversion.DATABASE_BOOLEAN_TRUE_STRING_REPRESENTATION.equals(currentBooleanStr)) { |
541 | 0 | jpql = criteria.getAlias() + "." + KRADPropertyConstants.ACTIVE_FROM_DATE + " in " + jpql; |
542 | 0 | } else if (OjbCharBooleanConversion.DATABASE_BOOLEAN_FALSE_STRING_REPRESENTATION.equals(currentBooleanStr)) { |
543 | 0 | jpql = criteria.getAlias() + "." + KRADPropertyConstants.ACTIVE_FROM_DATE + " not in " + jpql; |
544 | |
} |
545 | |
|
546 | 0 | criteria.rawJpql(jpql); |
547 | 0 | } |
548 | |
|
549 | |
private void addOrCriteria(String propertyName, String propertyValue, Class propertyType, boolean caseInsensitive, Criteria criteria) { |
550 | 0 | addLogicalOperatorCriteria(propertyName, propertyValue, propertyType, caseInsensitive, criteria, SearchOperator.OR.op()); |
551 | 0 | } |
552 | |
|
553 | |
private void addAndCriteria(String propertyName, String propertyValue, Class propertyType, boolean caseInsensitive, Criteria criteria) { |
554 | 0 | addLogicalOperatorCriteria(propertyName, propertyValue, propertyType, caseInsensitive, criteria, SearchOperator.AND.op()); |
555 | 0 | } |
556 | |
|
557 | |
private void addNotCriteria(String propertyName, String propertyValue, Class propertyType, boolean caseInsensitive, Criteria criteria) { |
558 | 0 | String[] splitPropVal = StringUtils.split(propertyValue, SearchOperator.NOT.op()); |
559 | |
|
560 | 0 | int strLength = splitPropVal.length; |
561 | |
|
562 | 0 | if (strLength > 1) { |
563 | 0 | String expandedNot = "!" + StringUtils.join(splitPropVal, SearchOperator.AND.op() + SearchOperator.NOT.op()); |
564 | |
|
565 | 0 | addCriteria(propertyName, expandedNot, propertyType, caseInsensitive, false, criteria); |
566 | 0 | } else { |
567 | |
|
568 | 0 | criteria.notLike(propertyName, splitPropVal[0]); |
569 | |
} |
570 | 0 | } |
571 | |
|
572 | |
private void addLogicalOperatorCriteria(String propertyName, String propertyValue, Class propertyType, boolean caseInsensitive, Criteria criteria, String splitValue) { |
573 | 0 | String[] splitPropVal = StringUtils.split(propertyValue, splitValue); |
574 | |
|
575 | 0 | Criteria subCriteria = new Criteria("N/A"); |
576 | 0 | for (int i = 0; i < splitPropVal.length; i++) { |
577 | 0 | Criteria predicate = new Criteria("N/A"); |
578 | |
|
579 | 0 | addCriteria(propertyName, splitPropVal[i], propertyType, caseInsensitive, false, predicate); |
580 | 0 | if (splitValue == SearchOperator.OR.op()) { |
581 | 0 | subCriteria.or(predicate); |
582 | |
} |
583 | 0 | if (splitValue == SearchOperator.AND.op()) { |
584 | 0 | subCriteria.and(predicate); |
585 | |
} |
586 | |
} |
587 | |
|
588 | 0 | criteria.and(subCriteria); |
589 | 0 | } |
590 | |
|
591 | |
private java.sql.Date parseDate(String dateString) { |
592 | 0 | dateString = dateString.trim(); |
593 | |
try { |
594 | 0 | return dateTimeService.convertToSqlDate(dateString); |
595 | 0 | } catch (ParseException ex) { |
596 | 0 | return null; |
597 | |
} |
598 | |
} |
599 | |
|
600 | |
private void addDateRangeCriteria(String propertyName, String propertyValue, Criteria criteria) { |
601 | |
|
602 | 0 | if (StringUtils.contains(propertyValue, SearchOperator.BETWEEN.op())) { |
603 | 0 | String[] rangeValues = StringUtils.split(propertyValue, SearchOperator.BETWEEN.op()); |
604 | 0 | criteria.between(propertyName, parseDate(ObjectUtils.clean(rangeValues[0])), parseDate(ObjectUtils.clean(rangeValues[1]))); |
605 | 0 | } else if (propertyValue.startsWith(">=")) { |
606 | 0 | criteria.gte(propertyName, parseDate(ObjectUtils.clean(propertyValue))); |
607 | 0 | } else if (propertyValue.startsWith("<=")) { |
608 | 0 | criteria.lte(propertyName, parseDate(ObjectUtils.clean(propertyValue))); |
609 | 0 | } else if (propertyValue.startsWith(">")) { |
610 | 0 | criteria.gt(propertyName, parseDate(ObjectUtils.clean(propertyValue))); |
611 | 0 | } else if (propertyValue.startsWith("<")) { |
612 | 0 | criteria.lt(propertyName, parseDate(ObjectUtils.clean(propertyValue))); |
613 | |
} else { |
614 | 0 | criteria.eq(propertyName, parseDate(ObjectUtils.clean(propertyValue))); |
615 | |
} |
616 | 0 | } |
617 | |
|
618 | |
private BigDecimal cleanNumeric(String value) { |
619 | 0 | String cleanedValue = value.replaceAll("[^-0-9.]", ""); |
620 | |
|
621 | 0 | if (cleanedValue.lastIndexOf('-') > 0) { |
622 | 0 | if (cleanedValue.charAt(0) == '-') { |
623 | 0 | cleanedValue = "-" + cleanedValue.replaceAll("-", ""); |
624 | |
} else { |
625 | 0 | cleanedValue = cleanedValue.replaceAll("-", ""); |
626 | |
} |
627 | |
} |
628 | |
|
629 | 0 | int decimalLoc = cleanedValue.lastIndexOf('.'); |
630 | 0 | if (cleanedValue.indexOf('.') != decimalLoc) { |
631 | 0 | cleanedValue = cleanedValue.substring(0, decimalLoc).replaceAll("\\.", "") + cleanedValue.substring(decimalLoc); |
632 | |
} |
633 | |
try { |
634 | 0 | return new BigDecimal(cleanedValue); |
635 | 0 | } catch (NumberFormatException ex) { |
636 | 0 | GlobalVariables.getMessageMap().putError(KRADConstants.DOCUMENT_ERRORS, RiceKeyConstants.ERROR_CUSTOM, new String[] { "Invalid Numeric Input: " + value }); |
637 | 0 | return null; |
638 | |
} |
639 | |
} |
640 | |
|
641 | |
private void addNumericRangeCriteria(String propertyName, String propertyValue, Criteria criteria) { |
642 | |
|
643 | 0 | if (StringUtils.contains(propertyValue, SearchOperator.BETWEEN.op())) { |
644 | 0 | String[] rangeValues = StringUtils.split(propertyValue, SearchOperator.BETWEEN.op()); |
645 | 0 | criteria.between(propertyName, cleanNumeric(rangeValues[0]), cleanNumeric(rangeValues[1])); |
646 | 0 | } else if (propertyValue.startsWith(">=")) { |
647 | 0 | criteria.gte(propertyName, cleanNumeric(propertyValue)); |
648 | 0 | } else if (propertyValue.startsWith("<=")) { |
649 | 0 | criteria.lte(propertyName, cleanNumeric(propertyValue)); |
650 | 0 | } else if (propertyValue.startsWith(">")) { |
651 | 0 | criteria.gt(propertyName, cleanNumeric(propertyValue)); |
652 | 0 | } else if (propertyValue.startsWith("<")) { |
653 | 0 | criteria.lt(propertyName, cleanNumeric(propertyValue)); |
654 | |
} else { |
655 | 0 | criteria.eq(propertyName, cleanNumeric(propertyValue)); |
656 | |
} |
657 | 0 | } |
658 | |
|
659 | |
private void addStringRangeCriteria(String propertyName, String propertyValue, Criteria criteria) { |
660 | |
|
661 | 0 | if (StringUtils.contains(propertyValue, SearchOperator.BETWEEN.op())) { |
662 | 0 | String[] rangeValues = StringUtils.split(propertyValue, SearchOperator.BETWEEN.op()); |
663 | 0 | criteria.between(propertyName, rangeValues[0], rangeValues[1]); |
664 | 0 | } else if (propertyValue.startsWith(">=")) { |
665 | 0 | criteria.gte(propertyName, ObjectUtils.clean(propertyValue)); |
666 | 0 | } else if (propertyValue.startsWith("<=")) { |
667 | 0 | criteria.lte(propertyName, ObjectUtils.clean(propertyValue)); |
668 | 0 | } else if (propertyValue.startsWith(">")) { |
669 | 0 | criteria.gt(propertyName, ObjectUtils.clean(propertyValue)); |
670 | 0 | } else if (propertyValue.startsWith("<")) { |
671 | 0 | criteria.lt(propertyName, ObjectUtils.clean(propertyValue)); |
672 | |
} |
673 | 0 | } |
674 | |
|
675 | |
|
676 | |
|
677 | |
|
678 | |
|
679 | |
public void setDateTimeService(DateTimeService dateTimeService) { |
680 | 0 | this.dateTimeService = dateTimeService; |
681 | 0 | } |
682 | |
|
683 | |
|
684 | |
|
685 | |
|
686 | |
public EntityManager getEntityManager() { |
687 | 0 | return this.entityManager; |
688 | |
} |
689 | |
|
690 | |
|
691 | |
|
692 | |
|
693 | |
public void setEntityManager(EntityManager entityManager) { |
694 | 0 | this.entityManager = entityManager; |
695 | 0 | } |
696 | |
|
697 | |
public void setPersistenceStructureService(PersistenceStructureService persistenceStructureService) { |
698 | 0 | this.persistenceStructureService = persistenceStructureService; |
699 | 0 | } |
700 | |
|
701 | |
public void setDataDictionaryService(DataDictionaryService dataDictionaryService) { |
702 | 0 | this.dataDictionaryService = dataDictionaryService; |
703 | 0 | } |
704 | |
|
705 | |
} |