View Javadoc

1   /**
2    * Copyright 2004-2012 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 org.apache.commons.lang.StringUtils;
25  import org.apache.log4j.Logger;
26  import org.apache.ojb.broker.query.Criteria;
27  import org.apache.ojb.broker.query.Query;
28  import org.apache.ojb.broker.query.QueryFactory;
29  import org.apache.ojb.broker.query.ReportQueryByCriteria;
30  import org.kuali.hr.job.Job;
31  import org.kuali.hr.time.util.TKUtils;
32  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
33  
34  /**
35   * Represents an implementation of {@link JobDao}.
36   */
37  public class JobDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements JobDao {
38  
39      @SuppressWarnings("unused")
40      private static final Logger LOG = Logger.getLogger(JobDaoSpringOjbImpl.class);
41  
42      public void saveOrUpdate(Job job) {
43          this.getPersistenceBrokerTemplate().store(job);
44      }
45  
46      public void saveOrUpdate(List<Job> jobList) {
47          if (jobList != null) {
48              for (Job job : jobList) {
49                  this.saveOrUpdate(job);
50              }
51          }
52      }
53  
54      public Job getPrimaryJob(String principalId, Date payPeriodEndDate) {
55          Criteria root = new Criteria();
56          Criteria effdt = new Criteria();
57          Criteria timestamp = new Criteria();
58  
59          effdt.addEqualTo("principalId", principalId);
60          effdt.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
61          effdt.addLessOrEqualThan("effectiveDate", payPeriodEndDate);
62          ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Job.class, effdt);
63          effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
64  
65          timestamp.addEqualTo("principalId", principalId);
66          timestamp.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
67          timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
68          ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Job.class, timestamp);
69          timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
70  
71          root.addEqualTo("principalId", principalId);
72          root.addEqualTo("effectiveDate", effdtSubQuery);
73          root.addEqualTo("timestamp", timestampSubQuery);
74          root.addEqualTo("primaryIndicator", true);
75  
76          Criteria activeFilter = new Criteria(); // Inner Join For Activity
77          activeFilter.addEqualTo("active", true);
78          root.addAndCriteria(activeFilter);
79  
80          Query query = QueryFactory.newQuery(Job.class, root);
81  
82          return (Job) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
83      }
84  
85      @SuppressWarnings({"unchecked", "rawtypes"})
86      @Override
87      public List<Job> getJobs(String principalId, Date payPeriodEndDate) {
88          List<Job> jobs = new LinkedList<Job>();
89          Criteria root = new Criteria();
90          Criteria effdt = new Criteria();
91          Criteria timestamp = new Criteria();
92  
93          effdt.addEqualTo("principalId", principalId);
94          effdt.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
95          effdt.addLessOrEqualThan("effectiveDate", payPeriodEndDate);
96          ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Job.class, effdt);
97          effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
98  
99          timestamp.addEqualTo("principalId", principalId);
100         timestamp.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
101         timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
102         ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Job.class, timestamp);
103         timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
104 
105         root.addEqualTo("principalId", principalId);
106         root.addEqualTo("effectiveDate", effdtSubQuery);
107         root.addEqualTo("timestamp", timestampSubQuery);
108 
109         Criteria activeFilter = new Criteria(); // Inner Join For Activity
110         activeFilter.addEqualTo("active", true);
111         root.addAndCriteria(activeFilter);
112 
113 
114         Query query = QueryFactory.newQuery(Job.class, root);
115         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
116 
117         if (c != null) {
118             jobs.addAll(c);
119         }
120 
121 
122         return jobs;
123     }
124 
125     public Job getJob(String principalId, Long jobNumber, Date asOfDate) {
126         Criteria root = new Criteria();
127         Criteria effdt = new Criteria();
128         Criteria timestamp = new Criteria();
129 
130         effdt.addEqualTo("principalId", principalId);
131         effdt.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
132         effdt.addLessOrEqualThan("effectiveDate", asOfDate);
133         ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Job.class, effdt);
134         effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
135 
136         timestamp.addEqualTo("principalId", principalId);
137         timestamp.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
138         timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
139         ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Job.class, timestamp);
140         timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
141 
142         root.addEqualTo("principalId", principalId);
143         root.addEqualTo("jobNumber", jobNumber);
144         root.addEqualTo("effectiveDate", effdtSubQuery);
145         root.addEqualTo("timestamp", timestampSubQuery);
146 
147         Criteria activeFilter = new Criteria(); // Inner Join For Activity
148         activeFilter.addEqualTo("active", true);
149         root.addAndCriteria(activeFilter);
150 
151         Query query = QueryFactory.newQuery(Job.class, root);
152         Job job = (Job) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
153         return job;
154     }
155 
156     @SuppressWarnings("unchecked")
157     public List<Job> getActiveJobsForPosition(String positionNbr, Date asOfDate) {
158         Criteria root = new Criteria();
159         Criteria effdt = new Criteria();
160         Criteria timestamp = new Criteria();
161 
162         // OJB's awesome sub query setup part 1
163         effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
164         effdt.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
165         effdt.addLessOrEqualThan("effectiveDate", asOfDate);
166         ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Job.class, effdt);
167         effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
168 
169         // OJB's awesome sub query setup part 2
170         timestamp.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
171         timestamp.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
172         timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
173         ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Job.class, timestamp);
174         timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
175 
176         root.addEqualTo("positionNumber", positionNbr);
177         root.addEqualTo("effectiveDate", effdtSubQuery);
178         root.addEqualTo("timestamp", timestampSubQuery);
179 
180         Criteria activeFilter = new Criteria(); // Inner Join For Activity
181         activeFilter.addEqualTo("active", true);
182         root.addAndCriteria(activeFilter);
183 
184         Query query = QueryFactory.newQuery(Job.class, root);
185         return (List<Job>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
186     }
187 
188     @SuppressWarnings("unchecked")
189     public List<Job> getActiveJobsForPayType(String hrPayType, Date asOfDate) {
190         Criteria root = new Criteria();
191         Criteria effdt = new Criteria();
192         Criteria timestamp = new Criteria();
193 
194         // OJB's awesome sub query setup part 1
195         effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
196         effdt.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
197         effdt.addLessOrEqualThan("effectiveDate", asOfDate);
198         ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Job.class, effdt);
199         effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
200 
201         // OJB's awesome sub query setup part 2
202         timestamp.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
203         timestamp.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
204         timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
205         ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Job.class, timestamp);
206         timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
207 
208         root.addEqualTo("hrPayType", hrPayType);
209         root.addEqualTo("effectiveDate", effdtSubQuery);
210         root.addEqualTo("timestamp", timestampSubQuery);
211 
212         Criteria activeFilter = new Criteria(); // Inner Join For Activity
213         activeFilter.addEqualTo("active", true);
214         root.addAndCriteria(activeFilter);
215 
216         Query query = QueryFactory.newQuery(Job.class, root);
217         return (List<Job>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
218     }
219 
220     @Override
221     public Job getJob(String hrJobId) {
222         Criteria crit = new Criteria();
223         crit.addEqualTo("hrJobId", hrJobId);
224 
225         Query query = QueryFactory.newQuery(Job.class, crit);
226         return (Job) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
227     }
228 
229     @Override
230     public Job getMaxJob(String principalId) {
231         Criteria root = new Criteria();
232         Criteria crit = new Criteria();
233         crit.addEqualTo("principalId", principalId);
234         ReportQueryByCriteria jobNumberSubQuery = QueryFactory.newReportQuery(Job.class, crit);
235         jobNumberSubQuery.setAttributes(new String[]{"max(jobNumber)"});
236 
237         crit.addEqualTo("principalId", principalId);
238         root.addEqualTo("jobNumber", jobNumberSubQuery);
239 
240         Query query = QueryFactory.newQuery(Job.class, root);
241         return (Job) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
242     }
243 
244     @Override
245     public List<Job> getJobs(String principalId, String jobNumber,
246                              String dept, String positionNbr, String payType,
247                              java.sql.Date fromEffdt, java.sql.Date toEffdt, String active, String showHistory) {
248 
249         List<Job> results = new ArrayList<Job>();
250 
251         Criteria crit = new Criteria();
252         Criteria effdtCrit = new Criteria();
253         Criteria timestampCrit = new Criteria();
254 
255         if (fromEffdt != null) {
256             crit.addGreaterOrEqualThan("effectiveDate", fromEffdt);
257         }
258 
259         if (toEffdt != null) {
260             crit.addLessOrEqualThan("effectiveDate", toEffdt);
261         } else {
262             crit.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
263         }
264 
265         if (StringUtils.isNotEmpty(jobNumber)) {
266             crit.addLike("jobNumber", jobNumber);
267         }
268 
269         if (StringUtils.isNotEmpty(principalId)) {
270             crit.addLike("principalId", principalId);
271         }
272 
273         if (StringUtils.isNotEmpty(dept)) {
274             crit.addLike("dept", dept);
275         }
276 
277         if (StringUtils.isNotEmpty(positionNbr)) {
278             crit.addLike("positionNbr", positionNbr);
279         }
280 
281         if (StringUtils.isNotEmpty(payType)) {
282             crit.addLike("payType", payType);
283         }
284 
285         if (StringUtils.isEmpty(active) && StringUtils.equals(showHistory, "Y")) {
286             effdtCrit.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
287             effdtCrit.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
288             ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Job.class, effdtCrit);
289             effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
290 
291             timestampCrit.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
292             timestampCrit.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
293             ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Job.class, timestampCrit);
294             timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
295 
296             crit.addEqualTo("effectiveDate", effdtSubQuery);
297             crit.addEqualTo("timestamp", timestampSubQuery);
298 
299             Query query = QueryFactory.newQuery(Job.class, crit);
300             Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
301             results.addAll(c);
302         } else if (StringUtils.isEmpty(active) && StringUtils.equals(showHistory, "N")) {
303             effdtCrit.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
304             effdtCrit.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
305             ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Job.class, effdtCrit);
306             effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
307 
308             timestampCrit.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
309             timestampCrit.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
310             ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Job.class, timestampCrit);
311             timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
312 
313             crit.addEqualTo("effectiveDate", effdtSubQuery);
314             crit.addEqualTo("timestamp", timestampSubQuery);
315 
316             Query query = QueryFactory.newQuery(Job.class, crit);
317             Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
318             results.addAll(c);
319         } else if (StringUtils.equals(active, "Y") && StringUtils.equals("N", showHistory)) {
320             effdtCrit.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
321             effdtCrit.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
322             ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Job.class, effdtCrit);
323             effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
324 
325             timestampCrit.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
326             timestampCrit.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
327             ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Job.class, timestampCrit);
328             timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
329 
330             crit.addEqualTo("effectiveDate", effdtSubQuery);
331             crit.addEqualTo("timestamp", timestampSubQuery);
332 
333             Criteria activeFilter = new Criteria(); // Inner Join For Activity
334             activeFilter.addEqualTo("active", true);
335             crit.addAndCriteria(activeFilter);
336 
337             Query query = QueryFactory.newQuery(Job.class, crit);
338             Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
339             results.addAll(c);
340         } //return all active records from the database
341         else if (StringUtils.equals(active, "Y") && StringUtils.equals("Y", showHistory)) {
342             Criteria activeFilter = new Criteria(); // Inner Join For Activity
343             activeFilter.addEqualTo("active", true);
344             crit.addAndCriteria(activeFilter);
345             Query query = QueryFactory.newQuery(Job.class, crit);
346             Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
347             results.addAll(c);
348         }
349         //return all inactive records in the database
350         else if (StringUtils.equals(active, "N") && StringUtils.equals(showHistory, "Y")) {
351             effdtCrit.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
352             effdtCrit.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
353             ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Job.class, effdtCrit);
354             effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
355 
356             timestampCrit.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
357             timestampCrit.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
358             ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Job.class, timestampCrit);
359             timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
360 
361             crit.addEqualTo("effectiveDate", effdtSubQuery);
362             crit.addEqualTo("timestamp", timestampSubQuery);
363 
364             Criteria activeFilter = new Criteria(); // Inner Join For Activity
365             activeFilter.addEqualTo("active", false);
366             crit.addAndCriteria(activeFilter);
367             Query query = QueryFactory.newQuery(Job.class, crit);
368             Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
369             results.addAll(c);
370         }
371 
372         //return the most effective inactive rows if there are no active rows <= the curr date
373         else if (StringUtils.equals(active, "N") && StringUtils.equals(showHistory, "N")) {
374             effdtCrit.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
375             effdtCrit.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
376             ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Job.class, effdtCrit);
377             effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
378 
379             timestampCrit.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
380             timestampCrit.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
381             ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Job.class, timestampCrit);
382             timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
383 
384             Criteria activeFilter = new Criteria(); // Inner Join For Activity
385             activeFilter.addEqualTo("active", false);
386             crit.addAndCriteria(activeFilter);
387             Query query = QueryFactory.newQuery(Job.class, crit);
388             Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
389             results.addAll(c);
390         }
391 
392         return results;
393     }
394 
395     @Override
396     public int getJobCount(String principalId, Long jobNumber, String dept) {
397     	Criteria crit = new Criteria();
398     	crit.addEqualTo("jobNumber", jobNumber);
399     	if(principalId != null) {
400     		crit.addEqualTo("principalId", principalId);
401     	}
402 		if(dept != null) {
403 			crit.addEqualTo("dept", dept);
404 		}
405 		Query query = QueryFactory.newQuery(Job.class, crit);
406 		return this.getPersistenceBrokerTemplate().getCount(query);
407     }
408 }