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