1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.core.util;
17
18
19 import org.apache.commons.collections.CollectionUtils;
20 import org.apache.commons.collections.MapUtils;
21 import org.apache.ojb.broker.query.Criteria;
22 import org.apache.ojb.broker.query.QueryFactory;
23 import org.apache.ojb.broker.query.ReportQueryByCriteria;
24
25 import java.sql.Date;
26 import java.util.List;
27 import java.util.Map;
28
29 public class OjbSubQueryUtil {
30
31 public static ReportQueryByCriteria getEffectiveDateSubQuery(Class clazz, Date asOfDate, List<String> equalToField, boolean checkActive) {
32
33 Criteria effDateFilter = new Criteria();
34 effDateFilter.addLessOrEqualThan("effectiveDate", asOfDate);
35 return OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(clazz, effDateFilter, equalToField, checkActive);
36 }
37
38 public static ReportQueryByCriteria getEffectiveDateSubQueryWithoutFilter(Class clazz, List<String> equalToField, boolean checkActive) {
39 return OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(clazz, null, equalToField, checkActive);
40 }
41
42 public static ReportQueryByCriteria getEffectiveDateSubQueryWithFilter(Class clazz, Criteria effectiveDateFilter, List<String> equalToField, boolean checkActive) {
43 Criteria effdt = new Criteria();
44 if (effectiveDateFilter != null) {
45 effdt.addAndCriteria(effectiveDateFilter);
46 }
47 if (CollectionUtils.isNotEmpty(equalToField)) {
48 for (String field : equalToField) {
49 Criteria keyField = new Criteria();
50 keyField.addEqualToField(field, Criteria.PARENT_QUERY_PREFIX+field);
51 Criteria orNull = new Criteria();
52 orNull.addIsNull(field);
53 orNull.addIsNull(Criteria.PARENT_QUERY_PREFIX+field);
54 keyField.addOrCriteria(orNull);
55 effdt.addAndCriteria(keyField);
56 }
57 }
58 if (checkActive) {
59 effdt.addEqualTo("active", true);
60 }
61 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(clazz, effdt);
62 effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
63
64 return effdtSubQuery;
65 }
66
67 public static ReportQueryByCriteria getTimestampSubQuery(Class clazz, List<String> equalToField, boolean checkActive) {
68 Criteria timestamp = new Criteria();
69
70 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
71
72 if (CollectionUtils.isNotEmpty(equalToField)) {
73 for (String field : equalToField) {
74 Criteria keyField = new Criteria();
75 keyField.addEqualToField(field, Criteria.PARENT_QUERY_PREFIX+field);
76 Criteria orNull = new Criteria();
77 orNull.addIsNull(field);
78 orNull.addIsNull(Criteria.PARENT_QUERY_PREFIX+field);
79 keyField.addOrCriteria(orNull);
80 timestamp.addAndCriteria(keyField);
81 }
82 }
83 if (checkActive) {
84 timestamp.addEqualTo("active", true);
85 }
86 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(clazz, timestamp);
87 timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
88
89 return timestampSubQuery;
90 }
91 }