1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.core.assignment.dao;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Set;
23
24 import org.apache.commons.collections.CollectionUtils;
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.assignment.Assignment;
33 import org.kuali.kpme.core.service.HrServiceLocator;
34 import org.kuali.kpme.core.util.HrContext;
35 import org.kuali.kpme.core.util.OjbSubQueryUtil;
36 import org.kuali.kpme.core.workarea.WorkArea;
37 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
38
39 public class AssignmentDaoOjbImpl extends PlatformAwareDaoBaseOjb implements AssignmentDao {
40
41 private static final Logger LOG = Logger.getLogger(AssignmentDaoOjbImpl.class);
42 @Override
43 public void saveOrUpdate(Assignment assignment) {
44 this.getPersistenceBrokerTemplate().store(assignment);
45 }
46
47 @Override
48 public void saveOrUpdate(List<Assignment> assignments) {
49 if (assignments != null) {
50 for (Assignment assign : assignments) {
51 this.getPersistenceBrokerTemplate().store(assign);
52 }
53 }
54 }
55
56 @Override
57 public void delete(Assignment assignment) {
58 if (assignment != null) {
59 LOG.debug("Deleting assignment:" + assignment.getTkAssignmentId());
60 this.getPersistenceBrokerTemplate().delete(assignment);
61 } else {
62 LOG.warn("Attempt to delete null assignment.");
63 }
64 }
65
66 public Assignment getAssignment(String principalId, Long jobNumber, Long workArea, Long task, LocalDate asOfDate) {
67 Criteria root = new Criteria();
68
69 root.addEqualTo("principalId", principalId);
70 root.addEqualTo("jobNumber", jobNumber);
71 root.addEqualTo("workArea", workArea);
72 root.addEqualTo("task", task);
73 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Assignment.class, asOfDate, Assignment.EQUAL_TO_FIELDS, false));
74 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, Assignment.EQUAL_TO_FIELDS, false));
75
76
77 Criteria activeFilter = new Criteria();
78 activeFilter.addEqualTo("active", true);
79 root.addAndCriteria(activeFilter);
80
81 Query query = QueryFactory.newQuery(Assignment.class, root);
82 Object o = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
83
84 return (Assignment) o;
85 }
86
87
88 @Override
89 public Assignment getAssignmentForTargetPrincipal(Long job, Long workArea, Long task, LocalDate asOfDate) {
90 Criteria root = new Criteria();
91
92 root.addEqualTo("jobNumber", job);
93 root.addEqualTo("workArea", workArea);
94 root.addEqualTo("task", task);
95 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Assignment.class, asOfDate, Assignment.EQUAL_TO_FIELDS, false));
96 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, Assignment.EQUAL_TO_FIELDS, false));
97 root.addEqualTo("principalId", HrContext.getTargetPrincipalId());
98
99
100 Criteria activeFilter = new Criteria();
101 activeFilter.addEqualTo("active", true);
102 root.addAndCriteria(activeFilter);
103
104 Query query = QueryFactory.newQuery(Assignment.class, root);
105 Object o = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
106
107 return (Assignment) o;
108 }
109
110 @SuppressWarnings({"unchecked", "rawtypes"})
111 @Override
112 public List<Assignment> findAssignments(String principalId, LocalDate asOfDate) {
113 List<Assignment> assignments = new ArrayList<Assignment>();
114 Criteria root = new Criteria();
115
116 root.addEqualTo("principalId", principalId);
117 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Assignment.class, asOfDate, Assignment.EQUAL_TO_FIELDS, false));
118 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, Assignment.EQUAL_TO_FIELDS, false));
119
120
121 Criteria activeFilter = new Criteria();
122 activeFilter.addEqualTo("active", true);
123 root.addAndCriteria(activeFilter);
124
125 Query query = QueryFactory.newQuery(Assignment.class, root);
126 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
127
128 if (c != null) {
129 assignments.addAll(c);
130 }
131
132 return assignments;
133 }
134
135 @SuppressWarnings({"unchecked", "rawtypes"})
136 @Override
137 public List<Assignment> findAssignmentsWithinPeriod(String principalId, LocalDate startDate, LocalDate endDate) {
138 List<Assignment> assignments = new ArrayList<Assignment>();
139 Criteria root = new Criteria();
140
141 root.addGreaterOrEqualThan("effectiveDate", startDate.toDate());
142 root.addLessOrEqualThan("effectiveDate", endDate.toDate());
143 root.addEqualTo("principalId", principalId);
144 root.addEqualTo("active", true);
145
146 Query query = QueryFactory.newQuery(Assignment.class, root);
147 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
148
149 if (c != null) {
150 assignments.addAll(c);
151 }
152
153 return assignments;
154 }
155
156 @SuppressWarnings({"rawtypes", "unchecked"})
157 public List<Assignment> getActiveAssignmentsInWorkArea(Long workArea, LocalDate asOfDate) {
158 List<Assignment> assignments = new ArrayList<Assignment>();
159 Criteria root = new Criteria();
160
161 root.addEqualTo("workArea", workArea);
162 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Assignment.class, asOfDate, Assignment.EQUAL_TO_FIELDS, true));
163 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, Assignment.EQUAL_TO_FIELDS, true));
164 root.addEqualTo("active", true);
165
166 Criteria activeFilter = new Criteria();
167 activeFilter.addEqualTo("active", true);
168 root.addAndCriteria(activeFilter);
169
170 Query query = QueryFactory.newQuery(Assignment.class, root);
171 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
172
173 if (c != null) {
174 assignments.addAll(c);
175 }
176
177 return assignments;
178 }
179
180 @Override
181 public List<Assignment> getActiveAssignmentsInWorkAreas(List<Long> workAreas, LocalDate asOfDate) {
182 List<Assignment> assignments = new ArrayList<Assignment>();
183 Criteria root = new Criteria();
184
185 root.addIn("workArea", workAreas);
186 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Assignment.class, asOfDate, Assignment.EQUAL_TO_FIELDS, true));
187 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, Assignment.EQUAL_TO_FIELDS, true));
188 root.addEqualTo("active", true);
189
190 Criteria activeFilter = new Criteria();
191 activeFilter.addEqualTo("active", true);
192 root.addAndCriteria(activeFilter);
193
194 Query query = QueryFactory.newQuery(Assignment.class, root);
195 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
196
197 if (c != null) {
198 assignments.addAll(c);
199 }
200
201 return assignments;
202 }
203
204 public List<Assignment> getActiveAssignments(LocalDate asOfDate) {
205 List<Assignment> assignments = new ArrayList<Assignment>();
206
207 Criteria root = new Criteria();
208 root.addLessOrEqualThan("effectiveDate", asOfDate.toDate());
209
210 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Assignment.class, asOfDate, Assignment.EQUAL_TO_FIELDS, true));
211 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, Assignment.EQUAL_TO_FIELDS, true));
212
213 Criteria activeFilter = new Criteria();
214 activeFilter.addEqualTo("active", true);
215 root.addAndCriteria(activeFilter);
216
217 Query query = QueryFactory.newQuery(Assignment.class, root);
218 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
219
220 if (c != null) {
221 assignments.addAll(c);
222 }
223
224 return assignments;
225 }
226
227 public Assignment getAssignment(String tkAssignmentId) {
228 Criteria crit = new Criteria();
229 crit.addEqualTo("tkAssignmentId", tkAssignmentId);
230 Query query = QueryFactory.newQuery(Assignment.class, crit);
231 return (Assignment) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
232 }
233
234
235 @SuppressWarnings({"rawtypes", "unchecked"})
236 public List<Assignment> getActiveAssignmentsForJob(String principalId, Long jobNumber, LocalDate asOfDate) {
237 List<Assignment> assignments = new ArrayList<Assignment>();
238 Criteria root = new Criteria();
239
240 root.addEqualTo("principalId", principalId);
241 root.addEqualTo("jobNumber", jobNumber);
242 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Assignment.class, asOfDate, Assignment.EQUAL_TO_FIELDS, false));
243 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, Assignment.EQUAL_TO_FIELDS, false));
244 root.addEqualTo("active", true);
245
246 Criteria activeFilter = new Criteria();
247 activeFilter.addEqualTo("active", true);
248 root.addAndCriteria(activeFilter);
249
250 Query query = QueryFactory.newQuery(Assignment.class, root);
251 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
252
253 if (c != null) {
254 assignments.addAll(c);
255 }
256
257 return assignments;
258 }
259
260 @Override
261 @SuppressWarnings("unchecked")
262 public List<Assignment> searchAssignments(LocalDate fromEffdt, LocalDate toEffdt, String principalId, String jobNumber, String dept, String workArea,
263 String active, String showHistory) {
264
265 List<Assignment> results = new ArrayList<Assignment>();
266
267 Criteria root = new Criteria();
268
269 Criteria effectiveDateFilter = new Criteria();
270 if (fromEffdt != null) {
271 effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt.toDate());
272 }
273 if (toEffdt != null) {
274 effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt.toDate());
275 }
276 if (fromEffdt == null && toEffdt == null) {
277 effectiveDateFilter.addLessOrEqualThan("effectiveDate", LocalDate.now().toDate());
278 }
279 root.addAndCriteria(effectiveDateFilter);
280
281 if (StringUtils.isNotBlank(principalId)) {
282 root.addLike("UPPER(`principal_id`)", principalId.toUpperCase());
283 }
284
285
286
287
288
289 if (StringUtils.isNotBlank(jobNumber)) {
290 OjbSubQueryUtil.addNumericCriteria(root, "jobNumber", jobNumber);
291 }
292
293 if (StringUtils.isNotBlank(dept)) {
294 Criteria workAreaCriteria = new Criteria();
295 LocalDate asOfDate = toEffdt != null ? toEffdt : LocalDate.now();
296 List<Long> workAreasForDept = HrServiceLocator.getWorkAreaService().getWorkAreasForDepartment(dept, asOfDate);
297 if (CollectionUtils.isNotEmpty(workAreasForDept)) {
298 workAreaCriteria.addIn("workArea", workAreasForDept);
299 }
300 root.addAndCriteria(workAreaCriteria);
301 }
302
303 if (StringUtils.isNotBlank(workArea)) {
304 OjbSubQueryUtil.addNumericCriteria(root, "workArea", workArea);
305 }
306
307 if (StringUtils.isNotBlank(active)) {
308 Criteria activeFilter = new Criteria();
309 if (StringUtils.equals(active, "Y")) {
310 activeFilter.addEqualTo("active", true);
311 } else if (StringUtils.equals(active, "N")) {
312 activeFilter.addEqualTo("active", false);
313 }
314 root.addAndCriteria(activeFilter);
315 }
316
317 if (StringUtils.equals(showHistory, "N")) {
318 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(Assignment.class, effectiveDateFilter, Assignment.EQUAL_TO_FIELDS, false));
319 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, Assignment.EQUAL_TO_FIELDS, false));
320 }
321
322 Query query = QueryFactory.newQuery(Assignment.class, root);
323 results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
324
325 return results;
326 }
327
328 @Override
329 public Assignment getMaxTimestampAssignment(String principalId) {
330 Criteria root = new Criteria();
331 Criteria crit = new Criteria();
332
333 crit.addEqualTo("principalId", principalId);
334 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Assignment.class, crit);
335 timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
336
337 root.addEqualTo("principalId", principalId);
338 root.addEqualTo("timestamp", timestampSubQuery);
339
340 Query query = QueryFactory.newQuery(Assignment.class, root);
341 return (Assignment) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
342 }
343
344 public List<String> getPrincipalIds(List<String> workAreaList, LocalDate effdt, LocalDate startDate, LocalDate endDate) {
345 List<Assignment> results = this.getAssignments(workAreaList, effdt, startDate, endDate);
346 Set<String> pids = new HashSet<String>();
347 for(Assignment anAssignment : results) {
348 if(anAssignment != null) {
349 pids.add(anAssignment.getPrincipalId());
350 }
351 }
352 List<String> ids = new ArrayList<String>();
353 ids.addAll(pids);
354 return ids;
355 }
356
357 public List<Assignment> getAssignments(List<String> workAreaList, LocalDate effdt, LocalDate startDate, LocalDate endDate) {
358 List<Assignment> results = new ArrayList<Assignment>();
359
360 Criteria activeRoot = new Criteria();
361 Criteria inactiveRoot = new Criteria();
362
363 ReportQueryByCriteria effdtSubQuery = OjbSubQueryUtil.getEffectiveDateSubQueryWithoutFilter(Assignment.class, Assignment.EQUAL_TO_FIELDS, false);
364 ReportQueryByCriteria activeEffdtSubQuery = OjbSubQueryUtil.getEffectiveDateSubQuery(Assignment.class, effdt, Assignment.EQUAL_TO_FIELDS, false);
365 ReportQueryByCriteria timestampSubQuery = OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, Assignment.EQUAL_TO_FIELDS, false);
366
367 inactiveRoot.addEqualTo("active", "N");
368 inactiveRoot.addIn("workArea", workAreaList);
369 inactiveRoot.addGreaterOrEqualThan("effectiveDate", startDate.toDate());
370 inactiveRoot.addLessOrEqualThan("effectiveDate", endDate.toDate());
371 inactiveRoot.addEqualTo("effectiveDate", effdtSubQuery);
372 inactiveRoot.addEqualTo("timestamp", timestampSubQuery);
373
374 activeRoot.addIn("workArea", workAreaList);
375 activeRoot.addEqualTo("active", "Y");
376 activeRoot.addEqualTo("effectiveDate", activeEffdtSubQuery);
377 activeRoot.addEqualTo("timestamp", timestampSubQuery);
378 activeRoot.addOrCriteria(inactiveRoot);
379
380 Query query = QueryFactory.newQuery(Assignment.class, activeRoot);
381 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
382 if (c != null) {
383 results.addAll(c);
384 }
385
386 return results;
387 }
388
389 }