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         root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Assignment.class, asOfDate, ASSIGNMENT_EQUAL_TO_FIELD, true));
196         root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, ASSIGNMENT_EQUAL_TO_FIELD, true));
197 
198 		Criteria activeFilter = new Criteria();
199 		activeFilter.addEqualTo("active", true);
200 		root.addAndCriteria(activeFilter);
201 
202         Query query = QueryFactory.newQuery(Assignment.class, root);
203         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
204 
205         if (c != null) {
206             assignments.addAll(c);
207         }
208 
209         return assignments;
210     }
211 
212     public Assignment getAssignment(String tkAssignmentId) {
213         Criteria crit = new Criteria();
214         crit.addEqualTo("tkAssignmentId", tkAssignmentId);
215         Query query = QueryFactory.newQuery(Assignment.class, crit);
216         return (Assignment) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
217     }
218 
219     // KPME-1129 Kagata
220     @SuppressWarnings({"rawtypes", "unchecked"})
221     public List<Assignment> getActiveAssignmentsForJob(String principalId, Long jobNumber, Date asOfDate) {
222         List<Assignment> assignments = new ArrayList<Assignment>();
223         Criteria root = new Criteria();
224 
225         root.addEqualTo("principalId", principalId);
226         root.addEqualTo("jobNumber", jobNumber);
227         root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Assignment.class, asOfDate, ASSIGNMENT_EQUAL_TO_FIELD, false));
228         root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, ASSIGNMENT_EQUAL_TO_FIELD, false));
229         root.addEqualTo("active", true);
230 
231         Criteria activeFilter = new Criteria(); // Inner Join For Activity
232         activeFilter.addEqualTo("active", true);
233         root.addAndCriteria(activeFilter);
234 
235         Query query = QueryFactory.newQuery(Assignment.class, root);
236         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
237 
238         if (c != null) {
239             assignments.addAll(c);
240         }
241 
242         return assignments;
243     }
244 
245 	@Override
246     @SuppressWarnings("unchecked")
247     public List<Assignment> searchAssignments(Date fromEffdt, Date toEffdt, String principalId, String jobNumber, String dept, String workArea, 
248     										  String active, String showHistory) {
249 
250         List<Assignment> results = new ArrayList<Assignment>();
251         
252         Criteria root = new Criteria();
253 
254         Criteria effectiveDateFilter = new Criteria();
255         if (fromEffdt != null) {
256             effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
257         }
258         if (toEffdt != null) {
259             effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
260         }
261         if (fromEffdt == null && toEffdt == null) {
262             effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
263         }
264         root.addAndCriteria(effectiveDateFilter);
265         
266         if (StringUtils.isNotBlank(principalId)) {
267             root.addLike("principalId", principalId);
268         }
269 
270         if (StringUtils.isNotBlank(jobNumber)) {
271             root.addLike("jobNumber", jobNumber);
272         }
273 
274         if (StringUtils.isNotBlank(dept)) {
275             Criteria workAreaCriteria = new Criteria();
276             Date asOfDate = toEffdt != null ? toEffdt : TKUtils.getCurrentDate();
277             Collection<WorkArea> workAreasForDept = TkServiceLocator.getWorkAreaService().getWorkAreas(dept,asOfDate);
278             if (CollectionUtils.isNotEmpty(workAreasForDept)) {
279                 List<Long> longWorkAreas = new ArrayList<Long>();
280                 for(WorkArea cwa : workAreasForDept){
281                     longWorkAreas.add(cwa.getWorkArea());
282                 }
283                 workAreaCriteria.addIn("workArea", longWorkAreas);
284             }
285             root.addAndCriteria(workAreaCriteria);
286         }
287 
288         if (StringUtils.isNotBlank(workArea)) {
289             OjbSubQueryUtil.addNumericCriteria(root, "workArea", workArea);
290         }
291         
292         if (StringUtils.isNotBlank(active)) {
293         	Criteria activeFilter = new Criteria();
294             if (StringUtils.equals(active, "Y")) {
295                 activeFilter.addEqualTo("active", true);
296             } else if (StringUtils.equals(active, "N")) {
297                 activeFilter.addEqualTo("active", false);
298             }
299             root.addAndCriteria(activeFilter);
300         }
301 
302         if (StringUtils.equals(showHistory, "N")) {
303             root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(Assignment.class, effectiveDateFilter, ASSIGNMENT_EQUAL_TO_FIELD, false));
304             root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, ASSIGNMENT_EQUAL_TO_FIELD, false));
305         }
306         
307         Query query = QueryFactory.newQuery(Assignment.class, root);
308         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
309         
310         return results;
311     }
312     
313     @Override
314     public Assignment getMaxTimestampAssignment(String principalId) {
315     	Criteria root = new Criteria();
316         Criteria crit = new Criteria();
317         
318         crit.addEqualTo("principalId", principalId);
319         ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Assignment.class, crit);
320         timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
321 
322         root.addEqualTo("principalId", principalId);
323         root.addEqualTo("timestamp", timestampSubQuery);
324 
325         Query query = QueryFactory.newQuery(Assignment.class, root);
326         return (Assignment) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
327     }
328 
329     public List<String> getPrincipalIds(List<String> workAreaList, Date effdt, Date startDate, Date endDate) {
330     	List<Assignment> results = this.getAssignments(workAreaList, effdt, startDate, endDate);
331         Set<String> pids = new HashSet<String>();
332         for(Assignment anAssignment : results) {
333         	if(anAssignment != null) {
334         		pids.add(anAssignment.getPrincipalId());
335         	}
336         }
337         List<String> ids = new ArrayList<String>();
338         ids.addAll(pids);
339      	return ids;
340     }
341     
342     public List<Assignment> getAssignments(List<String> workAreaList, Date effdt, Date startDate, Date endDate) {
343     	List<Assignment> results = new ArrayList<Assignment>();
344 		 
345 		Criteria activeRoot = new Criteria();
346      	Criteria inactiveRoot = new Criteria();
347 
348         ReportQueryByCriteria effdtSubQuery = OjbSubQueryUtil.getEffectiveDateSubQueryWithoutFilter(Assignment.class, ASSIGNMENT_EQUAL_TO_FIELD, false);
349         ReportQueryByCriteria activeEffdtSubQuery = OjbSubQueryUtil.getEffectiveDateSubQuery(Assignment.class, effdt, ASSIGNMENT_EQUAL_TO_FIELD, false);
350         ReportQueryByCriteria timestampSubQuery = OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, ASSIGNMENT_EQUAL_TO_FIELD, false);
351 
352         inactiveRoot.addEqualTo("active", "N");
353         inactiveRoot.addIn("workArea", workAreaList);
354         inactiveRoot.addGreaterOrEqualThan("effectiveDate", startDate);
355         inactiveRoot.addLessOrEqualThan("effectiveDate", endDate);
356         inactiveRoot.addEqualTo("effectiveDate", effdtSubQuery);
357         inactiveRoot.addEqualTo("timestamp", timestampSubQuery);
358          
359         activeRoot.addIn("workArea", workAreaList);
360         activeRoot.addEqualTo("active", "Y");
361         activeRoot.addEqualTo("effectiveDate", activeEffdtSubQuery);
362         activeRoot.addEqualTo("timestamp", timestampSubQuery);
363         activeRoot.addOrCriteria(inactiveRoot);
364          
365         Query query = QueryFactory.newQuery(Assignment.class, activeRoot);
366         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
367         if (c != null) {
368         	results.addAll(c);
369         }
370         
371         return results;
372     }
373 
374 }