1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.timecollection.rule.dao;
17
18 import java.sql.Date;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.List;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.ojb.broker.query.Criteria;
25 import org.apache.ojb.broker.query.Query;
26 import org.apache.ojb.broker.query.QueryFactory;
27 import org.apache.ojb.broker.query.ReportQueryByCriteria;
28 import org.kuali.hr.time.collection.rule.TimeCollectionRule;
29 import org.kuali.hr.time.util.TKUtils;
30 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
31
32 public class TimeCollectionRuleDaoServiceImpl extends PlatformAwareDaoBaseOjb implements TimeCollectionRuleDaoService {
33
34
35
36
37
38
39
40
41 @Override
42 public TimeCollectionRule getTimeCollectionRule(String dept, Long workArea, Date asOfDate) {
43
44
45 TimeCollectionRule timeCollectionRule = new TimeCollectionRule();
46
47
48 timeCollectionRule = getTimeCollectionRuleWildCarded(dept, workArea, asOfDate);
49 if (timeCollectionRule != null) {
50 return timeCollectionRule;
51 }
52
53 timeCollectionRule = getTimeCollectionRuleWildCarded("%", workArea, asOfDate);
54 if (timeCollectionRule != null) {
55 return timeCollectionRule;
56 }
57
58
59 timeCollectionRule = getTimeCollectionRuleWildCarded(dept, -1L, asOfDate);
60 if (timeCollectionRule != null) {
61 return timeCollectionRule;
62 }
63
64
65 timeCollectionRule = getTimeCollectionRuleWildCarded("%", -1L, asOfDate);
66 if (timeCollectionRule != null) {
67 return timeCollectionRule;
68 }
69
70
71 timeCollectionRule = new TimeCollectionRule();
72 timeCollectionRule.setActive(true);
73 timeCollectionRule.setClockUserFl(true);
74 timeCollectionRule.setDept(dept);
75 timeCollectionRule.setWorkArea(workArea);
76
77 return timeCollectionRule;
78 }
79
80 private TimeCollectionRule getTimeCollectionRuleWildCarded(String dept, Long workArea, Date asOfDate) {
81 Criteria root = new Criteria();
82 Criteria effdt = new Criteria();
83 Criteria timestamp = new Criteria();
84
85 effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
86 effdt.addLessOrEqualThan("effectiveDate", asOfDate);
87
88 effdt.addEqualTo("dept", dept);
89 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(TimeCollectionRule.class, effdt);
90 effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
91
92 timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
93 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
94
95 timestamp.addEqualTo("dept", dept);
96 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(TimeCollectionRule.class, timestamp);
97 timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
98
99 root.addEqualTo("dept", dept);
100 root.addEqualTo("workArea", workArea);
101 root.addEqualTo("effectiveDate", effdtSubQuery);
102 root.addEqualTo("timestamp", timestampSubQuery);
103
104
105 Criteria activeFilter = new Criteria();
106 activeFilter.addEqualTo("active", true);
107 root.addAndCriteria(activeFilter);
108
109
110 Query query = QueryFactory.newQuery(TimeCollectionRule.class, root);
111 return (TimeCollectionRule) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
112
113 }
114
115 @Override
116 public TimeCollectionRule getTimeCollectionRule(String tkTimeCollectionRuleId) {
117 Criteria crit = new Criteria();
118 crit.addEqualTo("tkTimeCollectionRuleId", tkTimeCollectionRuleId);
119
120 Query query = QueryFactory.newQuery(TimeCollectionRule.class, crit);
121 return (TimeCollectionRule) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
122
123 }
124
125
126
127
128
129 @Override
130 public TimeCollectionRule getTimeCollectionRule(String dept, Long workArea, String payType, Date asOfDate) {
131
132
133 TimeCollectionRule timeCollectionRule = new TimeCollectionRule();
134
135
136 timeCollectionRule = getTimeCollectionRuleWildCarded(dept, workArea, payType, asOfDate);
137 if (timeCollectionRule != null) {
138 return timeCollectionRule;
139 }
140
141 timeCollectionRule = getTimeCollectionRuleWildCarded("%", workArea, payType, asOfDate);
142 if (timeCollectionRule != null) {
143 return timeCollectionRule;
144 }
145
146
147 timeCollectionRule = getTimeCollectionRuleWildCarded(dept, -1L, payType, asOfDate);
148 if (timeCollectionRule != null) {
149 return timeCollectionRule;
150 }
151
152
153 timeCollectionRule = getTimeCollectionRuleWildCarded(dept, workArea, "%", asOfDate);
154 if (timeCollectionRule != null) {
155 return timeCollectionRule;
156 }
157
158
159 timeCollectionRule = getTimeCollectionRuleWildCarded("%", -1L, payType, asOfDate);
160 if (timeCollectionRule != null) {
161 return timeCollectionRule;
162 }
163
164
165 timeCollectionRule = getTimeCollectionRuleWildCarded("%", workArea, "%", asOfDate);
166 if (timeCollectionRule != null) {
167 return timeCollectionRule;
168 }
169
170
171 timeCollectionRule = getTimeCollectionRuleWildCarded(dept, -1L, "%", asOfDate);
172 if (timeCollectionRule != null) {
173 return timeCollectionRule;
174 }
175
176
177 timeCollectionRule = getTimeCollectionRuleWildCarded("%", -1L, "%", asOfDate);
178 if (timeCollectionRule != null) {
179 return timeCollectionRule;
180 }
181
182
183 timeCollectionRule = new TimeCollectionRule();
184 timeCollectionRule.setActive(true);
185 timeCollectionRule.setClockUserFl(true);
186 timeCollectionRule.setDept(dept);
187 timeCollectionRule.setWorkArea(workArea);
188
189 return timeCollectionRule;
190 }
191
192 private TimeCollectionRule getTimeCollectionRuleWildCarded(String dept, Long workArea, String payType, Date asOfDate) {
193 Criteria root = new Criteria();
194 Criteria effdt = new Criteria();
195 Criteria timestamp = new Criteria();
196
197 effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
198 effdt.addLessOrEqualThan("effectiveDate", asOfDate);
199
200 effdt.addEqualTo("dept", dept);
201 effdt.addEqualTo("payType", payType);
202 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(TimeCollectionRule.class, effdt);
203 effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
204
205 timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
206 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
207
208 timestamp.addEqualTo("dept", dept);
209 timestamp.addEqualTo("payType", payType);
210 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(TimeCollectionRule.class, timestamp);
211 timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
212
213 root.addEqualTo("dept", dept);
214 root.addEqualTo("workArea", workArea);
215 root.addEqualTo("payType", payType);
216 root.addEqualTo("effectiveDate", effdtSubQuery);
217 root.addEqualTo("timestamp", timestampSubQuery);
218
219
220 Criteria activeFilter = new Criteria();
221 activeFilter.addEqualTo("active", true);
222 root.addAndCriteria(activeFilter);
223
224
225 Query query = QueryFactory.newQuery(TimeCollectionRule.class, root);
226 return (TimeCollectionRule) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
227
228 }
229
230 @Override
231 public List<TimeCollectionRule> getTimeCollectionRules(String dept, Long workArea, String payType, String active, String showHistory) {
232
233 List<TimeCollectionRule> results = new ArrayList<TimeCollectionRule>();
234
235 Criteria root = new Criteria();
236 Criteria effdt = new Criteria();
237 Criteria timestamp = new Criteria();
238 if (workArea != null) {
239 effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
240 } else {
241 effdt.addIsNull(Criteria.PARENT_QUERY_PREFIX + "workArea");
242 }
243 effdt.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
244 if (StringUtils.isNotBlank(dept)) {
245 effdt.addEqualTo("dept", dept);
246 }
247 if (StringUtils.isNotBlank(payType)) {
248 effdt.addEqualTo("payType", payType);
249 }
250 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(TimeCollectionRule.class, effdt);
251 effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
252 if (workArea != null) {
253 timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
254 } else {
255 timestamp.addIsNull(Criteria.PARENT_QUERY_PREFIX + "workArea");
256 }
257 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
258 if (StringUtils.isNotBlank(dept)) {
259 timestamp.addEqualTo("dept", dept);
260 }
261 if (StringUtils.isNotBlank(payType)) {
262 timestamp.addEqualTo("payType", payType);
263 }
264 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(TimeCollectionRule.class, timestamp);
265 timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
266
267 if (StringUtils.isNotBlank(dept)) {
268 root.addLike("dept", dept);
269 }
270
271 if (workArea != null) {
272 root.addLike("workArea", workArea);
273 }
274 if (StringUtils.isNotBlank(payType)) {
275 root.addLike("payType", payType);
276 }
277
278
279
280 if (StringUtils.equals(active, "Y")) {
281 Criteria activeFilter = new Criteria();
282 activeFilter.addEqualTo("active", true);
283 root.addAndCriteria(activeFilter);
284 } else if (StringUtils.equals(active, "N")) {
285 Criteria activeFilter = new Criteria();
286 activeFilter.addEqualTo("active", false);
287 root.addAndCriteria(activeFilter);
288 }
289
290 Query query = QueryFactory.newQuery(TimeCollectionRule.class, root);
291 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
292 results.addAll(c);
293
294 return results;
295 }
296 }