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