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 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(effectiveDate)"});
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(effectiveDate)"});
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(effectiveDate)"});
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(effectiveDate)"});
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(effectiveDate)"});
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     @SuppressWarnings("unchecked")
246     public List<Job> getJobs(String principalId, String jobNumber, String dept, String positionNumber, String hrPayType, Date fromEffdt, Date toEffdt, 
247     						 String active, String showHistory) {
248 
249         List<Job> results = new ArrayList<Job>();
250 
251         Criteria root = new Criteria();
252         
253         if (StringUtils.isNotBlank(principalId)) {
254             root.addLike("principalId", principalId);
255         }
256 
257         if (StringUtils.isNotBlank(jobNumber)) {
258             root.addLike("jobNumber", jobNumber);
259         }
260 
261         if (StringUtils.isNotBlank(dept)) {
262             root.addLike("dept", dept);
263         }
264 
265         if (StringUtils.isNotBlank(positionNumber)) {
266             root.addLike("positionNumber", positionNumber);
267         }
268 
269         if (StringUtils.isNotBlank(hrPayType)) {
270             root.addLike("hrPayType", hrPayType);
271         }
272 
273         Criteria effectiveDateFilter = new Criteria();
274         if (fromEffdt != null) {
275             effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
276         }
277         if (toEffdt != null) {
278             effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
279         }
280         if (fromEffdt == null && toEffdt == null) {
281             effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
282         }
283         root.addAndCriteria(effectiveDateFilter);
284         
285         if (StringUtils.isNotBlank(active)) {
286         	Criteria activeFilter = new Criteria();
287             if (StringUtils.equals(active, "Y")) {
288                 activeFilter.addEqualTo("active", true);
289             } else if (StringUtils.equals(active, "N")) {
290                 activeFilter.addEqualTo("active", false);
291             }
292             root.addAndCriteria(activeFilter);
293         }
294 
295         if (StringUtils.equals(showHistory, "N")) {
296             Criteria effdt = new Criteria();
297             effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
298             effdt.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
299             effdt.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
300             effdt.addEqualToField("positionNumber", Criteria.PARENT_QUERY_PREFIX + "positionNumber");
301             effdt.addEqualToField("hrPayType", Criteria.PARENT_QUERY_PREFIX + "hrPayType");
302             effdt.addAndCriteria(effectiveDateFilter);
303             ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Job.class, effdt);
304             effdtSubQuery.setAttributes(new String[]{"max(effectiveDate)"});
305             root.addEqualTo("effectiveDate", effdtSubQuery);
306 
307             Criteria timestamp = new Criteria();
308             timestamp.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
309             timestamp.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
310             timestamp.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
311             timestamp.addEqualToField("positionNumber", Criteria.PARENT_QUERY_PREFIX + "positionNumber");
312             timestamp.addEqualToField("hrPayType", Criteria.PARENT_QUERY_PREFIX + "hrPayType");
313             timestamp.addAndCriteria(effectiveDateFilter);
314             ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Job.class, timestamp);
315             timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
316             root.addEqualTo("timestamp", timestampSubQuery);
317         }
318         
319         Query query = QueryFactory.newQuery(Job.class, root);
320         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
321 
322         return results;
323     }
324 
325     @Override
326     public int getJobCount(String principalId, Long jobNumber, String dept) {
327     	Criteria crit = new Criteria();
328     	crit.addEqualTo("jobNumber", jobNumber);
329     	if(principalId != null) {
330     		crit.addEqualTo("principalId", principalId);
331     	}
332 		if(dept != null) {
333 			crit.addEqualTo("dept", dept);
334 		}
335 		Query query = QueryFactory.newQuery(Job.class, crit);
336 		return this.getPersistenceBrokerTemplate().getCount(query);
337     }
338 }