001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.job.dao;
017    
018    import java.util.ArrayList;
019    import java.util.Collection;
020    import java.util.Date;
021    import java.util.LinkedList;
022    import java.util.List;
023    
024    import com.google.common.collect.ImmutableList;
025    import org.apache.commons.lang.StringUtils;
026    import org.apache.log4j.Logger;
027    import org.apache.ojb.broker.query.Criteria;
028    import org.apache.ojb.broker.query.Query;
029    import org.apache.ojb.broker.query.QueryFactory;
030    import org.apache.ojb.broker.query.ReportQueryByCriteria;
031    import org.kuali.hr.core.util.OjbSubQueryUtil;
032    import org.kuali.hr.job.Job;
033    import org.kuali.hr.time.util.TKUtils;
034    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
035    
036    /**
037     * Represents an implementation of {@link JobDao}.
038     */
039    public class JobDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements JobDao {
040        private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
041                .add("principalId")
042                .add("jobNumber")
043                .build();
044    
045        @SuppressWarnings("unused")
046        private static final Logger LOG = Logger.getLogger(JobDaoSpringOjbImpl.class);
047    
048        public void saveOrUpdate(Job job) {
049            this.getPersistenceBrokerTemplate().store(job);
050        }
051    
052        public void saveOrUpdate(List<Job> jobList) {
053            if (jobList != null) {
054                for (Job job : jobList) {
055                    this.saveOrUpdate(job);
056                }
057            }
058        }
059    
060        public Job getPrimaryJob(String principalId, Date payPeriodEndDate) {
061            Criteria root = new Criteria();
062    
063            java.sql.Date effDate = null;
064            if (payPeriodEndDate != null) {
065                effDate = new java.sql.Date(payPeriodEndDate.getTime());
066            }
067            root.addEqualTo("principalId", principalId);
068            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Job.class, effDate, EQUAL_TO_FIELDS, false));
069            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Job.class, EQUAL_TO_FIELDS, false));
070            root.addEqualTo("primaryIndicator", true);
071    
072            Criteria activeFilter = new Criteria(); // Inner Join For Activity
073            activeFilter.addEqualTo("active", true);
074            root.addAndCriteria(activeFilter);
075    
076            Query query = QueryFactory.newQuery(Job.class, root);
077    
078            return (Job) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
079        }
080    
081        @SuppressWarnings({"unchecked", "rawtypes"})
082        @Override
083        public List<Job> getJobs(String principalId, Date payPeriodEndDate) {
084            List<Job> jobs = new LinkedList<Job>();
085            Criteria root = new Criteria();
086    
087            java.sql.Date effDate = null;
088            if (payPeriodEndDate != null) {
089                effDate = new java.sql.Date(payPeriodEndDate.getTime());
090            }
091            root.addEqualTo("principalId", principalId);
092            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Job.class, effDate, EQUAL_TO_FIELDS, false));
093            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Job.class, EQUAL_TO_FIELDS, false));
094    
095            Criteria activeFilter = new Criteria(); // Inner Join For Activity
096            activeFilter.addEqualTo("active", true);
097            root.addAndCriteria(activeFilter);
098    
099    
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    }