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