1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.tklm.time.rules.timecollection.dao;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.apache.ojb.broker.query.Criteria;
23 import org.apache.ojb.broker.query.Query;
24 import org.apache.ojb.broker.query.QueryFactory;
25 import org.joda.time.LocalDate;
26 import org.kuali.kpme.core.util.OjbSubQueryUtil;
27 import org.kuali.kpme.tklm.time.rules.timecollection.TimeCollectionRule;
28 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
29
30 public class TimeCollectionRuleDaoServiceImpl extends PlatformAwareDaoBaseOjb implements TimeCollectionRuleDaoService {
31
32 @Override
33 public TimeCollectionRule getTimeCollectionRule(String tkTimeCollectionRuleId) {
34 Criteria crit = new Criteria();
35 crit.addEqualTo("tkTimeCollectionRuleId", tkTimeCollectionRuleId);
36
37 Query query = QueryFactory.newQuery(TimeCollectionRule.class, crit);
38 return (TimeCollectionRule) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
39
40 }
41
42
43
44
45
46 @Override
47 public TimeCollectionRule getTimeCollectionRule(String dept, Long workArea, String payType, String groupKeyCode, LocalDate asOfDate) {
48
49
50 TimeCollectionRule timeCollectionRule = new TimeCollectionRule();
51
52
53 timeCollectionRule = getTimeCollectionRuleWildCarded(dept, workArea, payType, groupKeyCode, asOfDate);
54 if (timeCollectionRule != null) {
55 return timeCollectionRule;
56 }
57
58 timeCollectionRule = getTimeCollectionRuleWildCarded("%", workArea, payType, groupKeyCode, asOfDate);
59 if (timeCollectionRule != null) {
60 return timeCollectionRule;
61 }
62
63
64 timeCollectionRule = getTimeCollectionRuleWildCarded(dept, -1L, payType, groupKeyCode, asOfDate);
65 if (timeCollectionRule != null) {
66 return timeCollectionRule;
67 }
68
69
70 timeCollectionRule = getTimeCollectionRuleWildCarded(dept, workArea, "%", groupKeyCode, asOfDate);
71 if (timeCollectionRule != null) {
72 return timeCollectionRule;
73 }
74
75
76 timeCollectionRule = getTimeCollectionRuleWildCarded("%", -1L, payType, groupKeyCode, asOfDate);
77 if (timeCollectionRule != null) {
78 return timeCollectionRule;
79 }
80
81
82 timeCollectionRule = getTimeCollectionRuleWildCarded("%", workArea, "%", groupKeyCode, asOfDate);
83 if (timeCollectionRule != null) {
84 return timeCollectionRule;
85 }
86
87
88 timeCollectionRule = getTimeCollectionRuleWildCarded(dept, -1L, "%", groupKeyCode, asOfDate);
89 if (timeCollectionRule != null) {
90 return timeCollectionRule;
91 }
92
93
94 return getTimeCollectionRuleWildCarded("%", -1L, "%", groupKeyCode, asOfDate);
95 }
96
97 private TimeCollectionRule getTimeCollectionRuleWildCarded(String dept, Long workArea, String payType, String groupKeyCode, LocalDate asOfDate) {
98 Criteria root = new Criteria();
99
100 root.addEqualTo("dept", dept);
101 root.addEqualTo("workArea", workArea);
102 root.addEqualTo("payType", payType);
103 root.addEqualTo("groupKeyCode", groupKeyCode);
104 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TimeCollectionRule.class, asOfDate, TimeCollectionRule.BUSINESS_KEYS, false));
105 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(TimeCollectionRule.class, TimeCollectionRule.BUSINESS_KEYS, false));
106
107
108 Criteria activeFilter = new Criteria();
109 activeFilter.addEqualTo("active", true);
110 root.addAndCriteria(activeFilter);
111
112
113 Query query = QueryFactory.newQuery(TimeCollectionRule.class, root);
114 return (TimeCollectionRule) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
115
116 }
117
118 @Override
119 @SuppressWarnings("unchecked")
120 public List<TimeCollectionRule> getTimeCollectionRules(String dept, Long workArea, String payType, String groupKeyCode, String active, String showHistory) {
121 List<TimeCollectionRule> results = new ArrayList<TimeCollectionRule>();
122
123 Criteria root = new Criteria();
124
125 if (StringUtils.isNotBlank(dept)) {
126 root.addLike("dept", dept);
127 }
128
129 if (workArea != null) {
130 OjbSubQueryUtil.addNumericCriteria(root, "workArea", workArea.toString());
131 }
132
133 if (StringUtils.isNotBlank(payType)) {
134 root.addLike("payType", payType);
135 }
136
137 if (StringUtils.isNotBlank(groupKeyCode)) {
138 root.addLike("groupKeyCode", groupKeyCode);
139 }
140
141 if (StringUtils.isNotBlank(active)) {
142 Criteria activeFilter = new Criteria();
143 if (StringUtils.equals(active, "Y")) {
144 activeFilter.addEqualTo("active", true);
145 } else if (StringUtils.equals(active, "N")) {
146 activeFilter.addEqualTo("active", false);
147 }
148 root.addAndCriteria(activeFilter);
149 }
150
151 if (StringUtils.equals(showHistory, "N")) {
152 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithoutFilter(TimeCollectionRule.class, TimeCollectionRule.BUSINESS_KEYS, false));
153 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(TimeCollectionRule.class, TimeCollectionRule.BUSINESS_KEYS, false));
154 }
155
156 Query query = QueryFactory.newQuery(TimeCollectionRule.class, root);
157 results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
158
159 return results;
160 }
161 }