View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.assignment.dao;
17  
18  import java.sql.Date;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Set;
24  
25  import com.google.common.collect.ImmutableList;
26  import org.apache.commons.collections.CollectionUtils;
27  import org.apache.commons.lang.StringUtils;
28  import org.apache.log4j.Logger;
29  import org.apache.ojb.broker.query.Criteria;
30  import org.apache.ojb.broker.query.Query;
31  import org.apache.ojb.broker.query.QueryFactory;
32  import org.apache.ojb.broker.query.ReportQueryByCriteria;
33  import org.kuali.hr.core.util.OjbSubQueryUtil;
34  import org.kuali.hr.time.assignment.Assignment;
35  import org.kuali.hr.time.service.base.TkServiceLocator;
36  import org.kuali.hr.time.util.TKContext;
37  import org.kuali.hr.time.util.TKUtils;
38  import org.kuali.hr.time.workarea.WorkArea;
39  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
40  
41  public class AssignmentDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements AssignmentDao {
42  
43      private static final Logger LOG = Logger.getLogger(AssignmentDaoSpringOjbImpl.class);
44      private static final ImmutableList<String> ASSIGNMENT_EQUAL_TO_FIELD = new ImmutableList.Builder<String>()
45              .add("jobNumber")
46              .add("workArea")
47              .add("task")
48              .add("principalId")
49              .build();
50  
51      @Override
52      public void saveOrUpdate(Assignment assignment) {
53          this.getPersistenceBrokerTemplate().store(assignment);
54      }
55  
56      @Override
57      public void saveOrUpdate(List<Assignment> assignments) {
58          if (assignments != null) {
59              for (Assignment assign : assignments) {
60                  this.getPersistenceBrokerTemplate().store(assign);
61              }
62          }
63      }
64  
65      @Override
66      public void delete(Assignment assignment) {
67          if (assignment != null) {
68              LOG.debug("Deleting assignment:" + assignment.getTkAssignmentId());
69              this.getPersistenceBrokerTemplate().delete(assignment);
70          } else {
71              LOG.warn("Attempt to delete null assignment.");
72          }
73      }
74  
75      public Assignment getAssignment(String principalId, Long jobNumber, Long workArea, Long task, Date asOfDate) {
76          Criteria root = new Criteria();
77  
78          root.addEqualTo("principalId", principalId);
79          root.addEqualTo("jobNumber", jobNumber);
80          root.addEqualTo("workArea", workArea);
81          root.addEqualTo("task", task);
82          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Assignment.class, asOfDate, ASSIGNMENT_EQUAL_TO_FIELD, false));
83          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, ASSIGNMENT_EQUAL_TO_FIELD, false));
84          //root.addEqualTo("active", true);
85  
86          Criteria activeFilter = new Criteria(); // Inner Join For Activity
87          activeFilter.addEqualTo("active", true);
88          root.addAndCriteria(activeFilter);
89  
90          Query query = QueryFactory.newQuery(Assignment.class, root);
91          Object o = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
92  
93          return (Assignment) o;
94      }
95  
96  
97      @Override
98      public Assignment getAssignment(Long job, Long workArea, Long task, Date asOfDate) {
99          Criteria root = new Criteria();
100 
101         root.addEqualTo("jobNumber", job);
102         root.addEqualTo("workArea", workArea);
103         root.addEqualTo("task", task);
104         root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Assignment.class, asOfDate, ASSIGNMENT_EQUAL_TO_FIELD, false));
105         root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, ASSIGNMENT_EQUAL_TO_FIELD, false));
106         root.addEqualTo("principalId", TKContext.getTargetPrincipalId());
107         //root.addEqualTo("active", true);
108 
109         Criteria activeFilter = new Criteria(); // Inner Join For Activity
110         activeFilter.addEqualTo("active", true);
111         root.addAndCriteria(activeFilter);
112 
113         Query query = QueryFactory.newQuery(Assignment.class, root);
114         Object o = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
115 
116         return (Assignment) o;
117     }
118 
119     @SuppressWarnings({"unchecked", "rawtypes"})
120     @Override
121     public List<Assignment> findAssignments(String principalId, Date asOfDate) {
122         List<Assignment> assignments = new ArrayList<Assignment>();
123         Criteria root = new Criteria();
124 
125         root.addEqualTo("principalId", principalId);
126         root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Assignment.class, asOfDate, ASSIGNMENT_EQUAL_TO_FIELD, false));
127         root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, ASSIGNMENT_EQUAL_TO_FIELD, false));
128         //root.addEqualTo("active", true);
129 
130         Criteria activeFilter = new Criteria(); // Inner Join For Activity
131         activeFilter.addEqualTo("active", true);
132         root.addAndCriteria(activeFilter);
133 
134         Query query = QueryFactory.newQuery(Assignment.class, root);
135         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
136 
137         if (c != null) {
138             assignments.addAll(c);
139         }
140 
141         return assignments;
142     }
143 
144     @SuppressWarnings({"unchecked", "rawtypes"})
145     @Override
146     public List<Assignment> findAssignmentsWithinPeriod(String principalId, Date startDate, Date endDate) {
147         List<Assignment> assignments = new ArrayList<Assignment>();
148         Criteria root = new Criteria();
149 
150         root.addGreaterOrEqualThan("effectiveDate", startDate);
151         root.addLessOrEqualThan("effectiveDate", endDate);
152         root.addEqualTo("principalId", principalId);
153         root.addEqualTo("active", true);
154 
155         Query query = QueryFactory.newQuery(Assignment.class, root);
156         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
157 
158         if (c != null) {
159             assignments.addAll(c);
160         }
161 
162         return assignments;
163     }
164 
165     @SuppressWarnings({"rawtypes", "unchecked"})
166     public List<Assignment> getActiveAssignmentsInWorkArea(Long workArea, Date asOfDate) {
167         List<Assignment> assignments = new ArrayList<Assignment>();
168         Criteria root = new Criteria();
169 
170         root.addEqualTo("workArea", workArea);
171         root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Assignment.class, asOfDate, ASSIGNMENT_EQUAL_TO_FIELD, true));
172         root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, ASSIGNMENT_EQUAL_TO_FIELD, true));
173         root.addEqualTo("active", true);
174 
175         Criteria activeFilter = new Criteria(); // Inner Join For Activity
176         activeFilter.addEqualTo("active", true);
177         root.addAndCriteria(activeFilter);
178 
179         Query query = QueryFactory.newQuery(Assignment.class, root);
180         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
181 
182         if (c != null) {
183             assignments.addAll(c);
184         }
185 
186         return assignments;
187     }
188 
189     public List<Assignment> getActiveAssignments(Date asOfDate) {
190         List<Assignment> assignments = new ArrayList<Assignment>();
191         
192         Criteria root = new Criteria();
193         root.addLessOrEqualThan("effectiveDate", asOfDate);
194 
195         Criteria timestamp = new Criteria();
196         timestamp.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
197         timestamp.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
198         timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
199         timestamp.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
200         timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
201         ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Assignment.class, timestamp);
202         timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
203         root.addEqualTo("timestamp", timestampSubQuery);
204         
205 		Criteria activeFilter = new Criteria();
206 		activeFilter.addEqualTo("active", true);
207 		root.addAndCriteria(activeFilter);
208 
209         Query query = QueryFactory.newQuery(Assignment.class, root);
210         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
211 
212         if (c != null) {
213             assignments.addAll(c);
214         }
215 
216         return assignments;
217     }
218 
219     public Assignment getAssignment(String tkAssignmentId) {
220         Criteria crit = new Criteria();
221         crit.addEqualTo("tkAssignmentId", tkAssignmentId);
222         Query query = QueryFactory.newQuery(Assignment.class, crit);
223         return (Assignment) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
224     }
225 
226     // KPME-1129 Kagata
227     @SuppressWarnings({"rawtypes", "unchecked"})
228     public List<Assignment> getActiveAssignmentsForJob(String principalId, Long jobNumber, Date asOfDate) {
229         List<Assignment> assignments = new ArrayList<Assignment>();
230         Criteria root = new Criteria();
231 
232         root.addEqualTo("principalId", principalId);
233         root.addEqualTo("jobNumber", jobNumber);
234         root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Assignment.class, asOfDate, ASSIGNMENT_EQUAL_TO_FIELD, false));
235         root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, ASSIGNMENT_EQUAL_TO_FIELD, false));
236         root.addEqualTo("active", true);
237 
238         Criteria activeFilter = new Criteria(); // Inner Join For Activity
239         activeFilter.addEqualTo("active", true);
240         root.addAndCriteria(activeFilter);
241 
242         Query query = QueryFactory.newQuery(Assignment.class, root);
243         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
244 
245         if (c != null) {
246             assignments.addAll(c);
247         }
248 
249         return assignments;
250     }
251 
252 	@Override
253     @SuppressWarnings("unchecked")
254     public List<Assignment> searchAssignments(Date fromEffdt, Date toEffdt, String principalId, String jobNumber, String dept, String workArea, 
255     										  String active, String showHistory) {
256 
257         List<Assignment> results = new ArrayList<Assignment>();
258         
259         Criteria root = new Criteria();
260 
261         Criteria effectiveDateFilter = new Criteria();
262         if (fromEffdt != null) {
263             effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
264         }
265         if (toEffdt != null) {
266             effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
267         }
268         if (fromEffdt == null && toEffdt == null) {
269             effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
270         }
271         root.addAndCriteria(effectiveDateFilter);
272         
273         if (StringUtils.isNotBlank(principalId)) {
274             root.addLike("principalId", principalId);
275         }
276 
277         if (StringUtils.isNotBlank(jobNumber)) {
278             root.addLike("jobNumber", jobNumber);
279         }
280 
281         if (StringUtils.isNotBlank(dept)) {
282             Criteria workAreaCriteria = new Criteria();
283             Date asOfDate = toEffdt != null ? toEffdt : TKUtils.getCurrentDate();
284             Collection<WorkArea> workAreasForDept = TkServiceLocator.getWorkAreaService().getWorkAreas(dept,asOfDate);
285             if (CollectionUtils.isNotEmpty(workAreasForDept)) {
286                 List<Long> longWorkAreas = new ArrayList<Long>();
287                 for(WorkArea cwa : workAreasForDept){
288                     longWorkAreas.add(cwa.getWorkArea());
289                 }
290                 workAreaCriteria.addIn("workArea", longWorkAreas);
291             }
292             root.addAndCriteria(workAreaCriteria);
293         }
294 
295         if (StringUtils.isNotBlank(workArea)) {
296             root.addLike("workArea", workArea);
297         }
298         
299         if (StringUtils.isNotBlank(active)) {
300         	Criteria activeFilter = new Criteria();
301             if (StringUtils.equals(active, "Y")) {
302                 activeFilter.addEqualTo("active", true);
303             } else if (StringUtils.equals(active, "N")) {
304                 activeFilter.addEqualTo("active", false);
305             }
306             root.addAndCriteria(activeFilter);
307         }
308 
309         if (StringUtils.equals(showHistory, "N")) {
310             root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(Assignment.class, effectiveDateFilter, ASSIGNMENT_EQUAL_TO_FIELD, false));
311             root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, ASSIGNMENT_EQUAL_TO_FIELD, false));
312         }
313         
314         Query query = QueryFactory.newQuery(Assignment.class, root);
315         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
316         
317         return results;
318     }
319     
320     @Override
321     public Assignment getMaxTimestampAssignment(String principalId) {
322     	Criteria root = new Criteria();
323         Criteria crit = new Criteria();
324         
325         crit.addEqualTo("principalId", principalId);
326         ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Assignment.class, crit);
327         timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
328 
329         root.addEqualTo("principalId", principalId);
330         root.addEqualTo("timestamp", timestampSubQuery);
331 
332         Query query = QueryFactory.newQuery(Assignment.class, root);
333         return (Assignment) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
334     }
335 
336     public List<String> getPrincipalIds(List<String> workAreaList, Date effdt, Date startDate, Date endDate) {
337     	List<Assignment> results = this.getAssignments(workAreaList, effdt, startDate, endDate);
338         Set<String> pids = new HashSet<String>();
339         for(Assignment anAssignment : results) {
340         	if(anAssignment != null) {
341         		pids.add(anAssignment.getPrincipalId());
342         	}
343         }
344         List<String> ids = new ArrayList<String>();
345         ids.addAll(pids);
346      	return ids;
347     }
348     
349     public List<Assignment> getAssignments(List<String> workAreaList, Date effdt, Date startDate, Date endDate) {
350     	List<Assignment> results = new ArrayList<Assignment>();
351 		 
352 		Criteria activeRoot = new Criteria();	
353      	Criteria activEeffdtCrit = new Criteria();
354      	Criteria effdtCrit = new Criteria();
355      	Criteria inactiveRoot = new Criteria();
356 
357         ReportQueryByCriteria effdtSubQuery = OjbSubQueryUtil.getEffectiveDateSubQueryWithoutFilter(Assignment.class, ASSIGNMENT_EQUAL_TO_FIELD, false);
358         ReportQueryByCriteria activeEffdtSubQuery = OjbSubQueryUtil.getEffectiveDateSubQuery(Assignment.class, effdt, ASSIGNMENT_EQUAL_TO_FIELD, false);
359         ReportQueryByCriteria timestampSubQuery = OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, ASSIGNMENT_EQUAL_TO_FIELD, false);
360 
361         inactiveRoot.addEqualTo("active", "N");
362         inactiveRoot.addIn("workArea", workAreaList);
363         inactiveRoot.addGreaterOrEqualThan("effectiveDate", startDate);
364         inactiveRoot.addLessOrEqualThan("effectiveDate", endDate);
365         inactiveRoot.addEqualTo("effectiveDate", effdtSubQuery);
366         inactiveRoot.addEqualTo("timestamp", timestampSubQuery);
367          
368         activeRoot.addIn("workArea", workAreaList);
369         activeRoot.addEqualTo("active", "Y");
370         activeRoot.addEqualTo("effectiveDate", activeEffdtSubQuery);
371         activeRoot.addEqualTo("timestamp", timestampSubQuery);
372         activeRoot.addOrCriteria(inactiveRoot);
373          
374         Query query = QueryFactory.newQuery(Assignment.class, activeRoot);
375         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
376         if (c != null) {
377         	results.addAll(c);
378         }
379         
380         return results;
381     }
382 
383 }