1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.core.job.dao;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.HashSet;
21 import java.util.LinkedList;
22 import java.util.List;
23 import java.util.Set;
24
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.log4j.Logger;
27 import org.apache.ojb.broker.query.Criteria;
28 import org.apache.ojb.broker.query.Query;
29 import org.apache.ojb.broker.query.QueryFactory;
30 import org.apache.ojb.broker.query.ReportQueryByCriteria;
31 import org.joda.time.LocalDate;
32 import org.kuali.kpme.core.job.Job;
33 import org.kuali.kpme.core.util.OjbSubQueryUtil;
34 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
35
36 import com.google.common.collect.ImmutableList;
37
38
39
40
41 public class JobDaoOjbImpl extends PlatformAwareDaoBaseOjb implements JobDao {
42
43
44 @SuppressWarnings("unused")
45 private static final Logger LOG = Logger.getLogger(JobDaoOjbImpl.class);
46
47 public void saveOrUpdate(Job job) {
48 this.getPersistenceBrokerTemplate().store(job);
49 }
50
51 public void saveOrUpdate(List<Job> jobList) {
52 if (jobList != null) {
53 for (Job job : jobList) {
54 this.saveOrUpdate(job);
55 }
56 }
57 }
58
59 public Job getPrimaryJob(String principalId, LocalDate payPeriodEndDate) {
60 Criteria root = new Criteria();
61 root.addEqualTo("principalId", principalId);
62 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Job.class, payPeriodEndDate, Job.EQUAL_TO_FIELDS, false));
63 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Job.class, Job.EQUAL_TO_FIELDS, false));
64 root.addEqualTo("primaryIndicator", true);
65
66 Criteria activeFilter = new Criteria();
67 activeFilter.addEqualTo("active", true);
68 root.addAndCriteria(activeFilter);
69
70 Query query = QueryFactory.newQuery(Job.class, root);
71
72 return (Job) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
73 }
74
75 @SuppressWarnings({"unchecked", "rawtypes"})
76 @Override
77 public List<Job> getJobs(String principalId, LocalDate payPeriodEndDate) {
78 List<Job> jobs = new LinkedList<Job>();
79 Criteria root = new Criteria();
80 root.addEqualTo("principalId", principalId);
81 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Job.class, payPeriodEndDate, Job.EQUAL_TO_FIELDS, false));
82 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Job.class, Job.EQUAL_TO_FIELDS, false));
83
84 Criteria activeFilter = new Criteria();
85 activeFilter.addEqualTo("active", true);
86 root.addAndCriteria(activeFilter);
87
88
89 Query query = QueryFactory.newQuery(Job.class, root);
90 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
91
92 if (c != null) {
93 jobs.addAll(c);
94 }
95
96
97 return jobs;
98 }
99
100 public Job getJob(String principalId, Long jobNumber, LocalDate asOfDate) {
101 Criteria root = new Criteria();
102 root.addEqualTo("principalId", principalId);
103 root.addEqualTo("jobNumber", jobNumber);
104 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Job.class, asOfDate, Job.EQUAL_TO_FIELDS, false));
105 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Job.class, Job.EQUAL_TO_FIELDS, false));
106
107 Criteria activeFilter = new Criteria();
108 activeFilter.addEqualTo("active", true);
109 root.addAndCriteria(activeFilter);
110
111 Query query = QueryFactory.newQuery(Job.class, root);
112 Job job = (Job) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
113 return job;
114 }
115
116 @SuppressWarnings("unchecked")
117 public List<Job> getActiveJobsForPayType(String hrPayType, LocalDate asOfDate) {
118 Criteria root = new Criteria();
119 root.addEqualTo("hrPayType", hrPayType);
120 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Job.class, asOfDate, Job.EQUAL_TO_FIELDS, false));
121 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Job.class, Job.EQUAL_TO_FIELDS, false));
122
123 Criteria activeFilter = new Criteria();
124 activeFilter.addEqualTo("active", true);
125 root.addAndCriteria(activeFilter);
126
127 Query query = QueryFactory.newQuery(Job.class, root);
128 return (List<Job>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
129 }
130
131 @Override
132 public Job getJob(String hrJobId) {
133 Criteria crit = new Criteria();
134 crit.addEqualTo("hrJobId", hrJobId);
135
136 Query query = QueryFactory.newQuery(Job.class, crit);
137 return (Job) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
138 }
139
140 @Override
141 public Job getMaxJob(String principalId) {
142 Criteria root = new Criteria();
143 Criteria crit = new Criteria();
144 crit.addEqualTo("principalId", principalId);
145 ReportQueryByCriteria jobNumberSubQuery = QueryFactory.newReportQuery(Job.class, crit);
146 jobNumberSubQuery.setAttributes(new String[]{"max(jobNumber)"});
147
148 crit.addEqualTo("principalId", principalId);
149 root.addEqualTo("jobNumber", jobNumberSubQuery);
150
151 Query query = QueryFactory.newQuery(Job.class, root);
152 return (Job) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
153 }
154
155 @Override
156 @SuppressWarnings("unchecked")
157 public List<Job> getJobs(String principalId, String jobNumber, String dept, String positionNumber, String hrPayType, LocalDate fromEffdt, LocalDate toEffdt,
158 String active, String showHistory) {
159
160 List<Job> results = new ArrayList<Job>();
161
162 Criteria root = new Criteria();
163
164 if (StringUtils.isNotBlank(principalId)) {
165 root.addLike("UPPER(`principal_id`)", principalId.toUpperCase());
166 }
167
168 if (StringUtils.isNotBlank(jobNumber)) {
169 root.addLike("jobNumber", jobNumber);
170 }
171
172 if (StringUtils.isNotBlank(dept)) {
173 root.addLike("dept", dept);
174 }
175
176 if (StringUtils.isNotBlank(positionNumber)) {
177 root.addLike("positionNumber", positionNumber);
178 }
179
180 if (StringUtils.isNotBlank(hrPayType)) {
181 root.addLike("UPPER(`hr_paytype`)", hrPayType.toUpperCase());
182 }
183
184 Criteria effectiveDateFilter = new Criteria();
185 if (fromEffdt != null) {
186 effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt.toDate());
187 }
188 if (toEffdt != null) {
189 effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt.toDate());
190 }
191 if (fromEffdt == null && toEffdt == null) {
192 effectiveDateFilter.addLessOrEqualThan("effectiveDate", LocalDate.now().toDate());
193 }
194 root.addAndCriteria(effectiveDateFilter);
195
196 if (StringUtils.isNotBlank(active)) {
197 Criteria activeFilter = new Criteria();
198 if (StringUtils.equals(active, "Y")) {
199 activeFilter.addEqualTo("active", true);
200 } else if (StringUtils.equals(active, "N")) {
201 activeFilter.addEqualTo("active", false);
202 }
203 root.addAndCriteria(activeFilter);
204 }
205
206
207 if (StringUtils.equals(showHistory, "N")) {
208 ImmutableList<String> fields = new ImmutableList.Builder<String>()
209 .add("principalId")
210 .add("jobNumber")
211 .add("dept")
212 .add("positionNumber")
213 .add("hrPayType")
214 .build();
215 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(Job.class, effectiveDateFilter, Job.EQUAL_TO_FIELDS, false));
216 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Job.class, Job.EQUAL_TO_FIELDS, false));
217 }
218
219 Query query = QueryFactory.newQuery(Job.class, root);
220 results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
221
222 return results;
223 }
224
225 @Override
226 public int getJobCount(String principalId, Long jobNumber, String dept) {
227 Criteria crit = new Criteria();
228 crit.addEqualTo("jobNumber", jobNumber);
229 if(principalId != null) {
230 crit.addEqualTo("principalId", principalId);
231 }
232 if(dept != null) {
233 crit.addEqualTo("dept", dept);
234 }
235 Query query = QueryFactory.newQuery(Job.class, crit);
236 return this.getPersistenceBrokerTemplate().getCount(query);
237 }
238
239 @SuppressWarnings("unchecked")
240 public List<Job> getActiveLeaveJobs(String principalId, LocalDate asOfDate) {
241 Criteria root = new Criteria();
242
243 root.addEqualTo("principalId", principalId);
244 root.addEqualTo("eligibleForLeave", true);
245 root.addEqualTo("active", true);
246 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Job.class, asOfDate, Job.EQUAL_TO_FIELDS, false));
247 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Job.class, Job.EQUAL_TO_FIELDS, false));
248
249 Query query = QueryFactory.newQuery(Job.class, root);
250 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
251
252 List<Job> jobList = new ArrayList<Job>();
253 jobList.addAll(c);
254 List<Job> aList = new ArrayList<Job>();
255 aList.addAll(jobList);
256 for(Job aJob : aList) {
257 List<Job> inactiveJobs = this.getInactiveLeaveJobs(aJob.getJobNumber(), aJob.getEffectiveLocalDate());
258 if(!inactiveJobs.isEmpty()) {
259 jobList.remove(aJob);
260 }
261 }
262 return jobList;
263 }
264
265 @Override
266 @SuppressWarnings("unchecked")
267 public List<Job> getInactiveLeaveJobs(Long jobNumber, LocalDate asOfDate) {
268 Criteria root = new Criteria();
269 ImmutableList<String> fields = new ImmutableList.Builder<String>()
270 .add("jobNumber")
271 .build();
272
273 root.addEqualTo("jobNumber", jobNumber);
274 root.addEqualTo("eligibleForLeave", true);
275 root.addEqualTo("active", false);
276 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Job.class, asOfDate, fields, false));
277 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Job.class, fields, false));
278
279 Query query = QueryFactory.newQuery(Job.class, root);
280 return (List<Job>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
281 }
282
283 @Override
284 public List<Job> getAllActiveLeaveJobs(String principalId, LocalDate asOfDate) {
285 Criteria root = new Criteria();
286 root.addEqualTo("principalId", principalId);
287 root.addLessOrEqualThan("effectiveDate", asOfDate.toDate());
288 root.addEqualTo("active", true);
289 root.addEqualTo("eligibleForLeave", true);
290
291 Query query = QueryFactory.newQuery(Job.class, root);
292 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
293
294 List<Job> jobs = new LinkedList<Job>();
295 if (c != null) {
296 jobs.addAll(c);
297 }
298 return jobs;
299 }
300
301 public List<Job> getAllInActiveLeaveJobsInRange(String principalId, LocalDate endDate) {
302 Criteria root = new Criteria();
303 root.addEqualTo("principalId", principalId);
304 root.addLessOrEqualThan("effectiveDate", endDate.toDate());
305 root.addEqualTo("active", false);
306 Query query = QueryFactory.newQuery(Job.class, root);
307 return (List<Job>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
308 }
309
310 @Override
311 public Job getMaxTimestampJob(String principalId) {
312 Criteria root = new Criteria();
313 Criteria crit = new Criteria();
314
315 crit.addEqualTo("principalId", principalId);
316 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Job.class, crit);
317 timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
318
319 root.addEqualTo("principalId", principalId);
320 root.addEqualTo("timestamp", timestampSubQuery);
321
322 Query query = QueryFactory.newQuery(Job.class, root);
323 return (Job) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
324 }
325
326
327 @SuppressWarnings("unchecked")
328 public List<String> getPrincipalIdsInPosition(String positionNumber, LocalDate asOfDate) {
329 Set<String> principalIdsInPosition = new HashSet<String>();
330
331 Criteria root = new Criteria();
332 root.addEqualTo("positionNumber", positionNumber);
333 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Job.class, asOfDate, Job.EQUAL_TO_FIELDS, false));
334 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Job.class, Job.EQUAL_TO_FIELDS, false));
335
336 Criteria activeFilter = new Criteria();
337 activeFilter.addEqualTo("active", true);
338 root.addAndCriteria(activeFilter);
339
340 Query query = QueryFactory.newQuery(Job.class, root);
341 Collection<Job> jobs = getPersistenceBrokerTemplate().getCollectionByQuery(query);
342
343 for (Job job : jobs) {
344 principalIdsInPosition.add(job.getPrincipalId());
345 }
346
347 return new ArrayList<String>(principalIdsInPosition);
348 }
349
350 }