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.principal.dao;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.Date;
22  import java.util.HashSet;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Set;
26  
27  import com.google.common.collect.ImmutableList;
28  import org.apache.commons.collections.CollectionUtils;
29  import org.apache.commons.lang.StringUtils;
30  import org.apache.ojb.broker.query.Criteria;
31  import org.apache.ojb.broker.query.Query;
32  import org.apache.ojb.broker.query.QueryFactory;
33  import org.apache.ojb.broker.query.ReportQueryByCriteria;
34  import org.kuali.hr.core.util.OjbSubQueryUtil;
35  import org.kuali.hr.time.principal.PrincipalHRAttributes;
36  import org.kuali.hr.time.util.TKUtils;
37  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
38  
39  public class PrincipalHRAttributesDaoImpl extends PlatformAwareDaoBaseOjb implements PrincipalHRAttributesDao {
40  
41  	@Override
42  	public PrincipalHRAttributes getPrincipalCalendar(String principalId,
43  			java.util.Date asOfDate) {
44  		PrincipalHRAttributes pc = null;
45  
46  		Criteria root = new Criteria();
47  
48          ImmutableList<String> fields = new ImmutableList.Builder<String>()
49                  .add("principalId")
50                  .build();
51  
52  		root.addEqualTo("principalId", principalId);
53          java.sql.Date effDate = asOfDate == null ? null : new java.sql.Date(asOfDate.getTime());
54          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(PrincipalHRAttributes.class, effDate, fields, false));
55          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false));
56  
57  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
58  		activeFilter.addEqualTo("active", true);
59  		root.addAndCriteria(activeFilter);
60  		
61  		Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
62  		Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
63  
64  		if (obj != null) {
65  			pc = (PrincipalHRAttributes) obj;
66  		}
67  
68  		return pc;
69  	}
70  
71  	@Override
72  	public void saveOrUpdate(PrincipalHRAttributes principalCalendar) {
73  		this.getPersistenceBrokerTemplate().store(principalCalendar);
74  		
75  	}
76  
77  	@Override
78  	public void saveOrUpdate(List<PrincipalHRAttributes> lstPrincipalCalendar) {
79  		if(lstPrincipalCalendar != null){
80  			for(PrincipalHRAttributes principalCal : lstPrincipalCalendar){
81  				this.getPersistenceBrokerTemplate().store(principalCal);
82  			}
83  		}
84  		
85  	}
86  
87      @SuppressWarnings({"rawtypes", "unchecked"})
88      public List<PrincipalHRAttributes> getActiveEmployeesForPayCalendar(String payCalendarName, java.util.Date asOfDate) {
89          List<PrincipalHRAttributes> principalHRAttributes = new ArrayList<PrincipalHRAttributes>();
90          Criteria root = new Criteria();
91          
92          root.addEqualTo("payCalendar", payCalendarName);
93          ImmutableList<String> fields = new ImmutableList.Builder<String>()
94                  .add("payCalendar")
95                  .add("principalId")
96                  .build();
97  
98          Criteria activeFilter = new Criteria();
99          activeFilter.addEqualTo("active", true);
100         root.addAndCriteria(activeFilter);
101         java.sql.Date effDate = asOfDate == null ? null : new java.sql.Date(asOfDate.getTime());
102         root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(PrincipalHRAttributes.class, effDate, fields, false));
103         root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false));
104 
105         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
106         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
107 
108         if (c != null) {
109         	principalHRAttributes.addAll(c);
110         }
111 
112         return principalHRAttributes;
113     }
114     
115     @SuppressWarnings({"rawtypes", "unchecked"})
116     public List<PrincipalHRAttributes> getActiveEmployeesForLeaveCalendar(String leaveCalendarName, Date asOfDate) {
117         List<PrincipalHRAttributes> principalHRAttributes = new ArrayList<PrincipalHRAttributes>();
118         Criteria root = new Criteria();
119 
120 
121         root.addEqualTo("leaveCalendar", leaveCalendarName);
122 
123         ImmutableList<String> fields = new ImmutableList.Builder<String>()
124                 .add("leaveCalendar")
125                 .add("principalId")
126                 .build();
127         java.sql.Date effDate = asOfDate == null ? null : new java.sql.Date(asOfDate.getTime());
128         root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(PrincipalHRAttributes.class, effDate, fields, false));
129         root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false));
130 
131         Criteria activeFilter = new Criteria();
132         activeFilter.addEqualTo("active", true);
133         root.addAndCriteria(activeFilter);
134 
135         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
136         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
137 
138         if (c != null) {
139         	principalHRAttributes.addAll(c);
140         }
141 
142         return principalHRAttributes;
143     }
144     
145     public List<String> getActiveEmployeesIdForLeaveCalendarAndIdList(String leaveCalendarName, List<String> pidList, Date asOfDate) {
146     	List<PrincipalHRAttributes> principalHRAttributes = new ArrayList<PrincipalHRAttributes>();
147         Criteria root = new Criteria();
148         
149         root.addEqualTo("leaveCalendar", leaveCalendarName);
150         root.addIn("principalId", pidList);
151 
152         ImmutableList<String> fields = new ImmutableList.Builder<String>()
153                 .add("leaveCalendar")
154                 .add("principalId")
155                 .build();
156         java.sql.Date effDate = asOfDate == null ? null : new java.sql.Date(asOfDate.getTime());
157         root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(PrincipalHRAttributes.class,effDate, fields, false));
158         root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false));
159 
160         Criteria activeFilter = new Criteria();
161         activeFilter.addEqualTo("active", true);
162         root.addAndCriteria(activeFilter);
163 
164         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
165         Collection c = getPersistenceBrokerTemplate().getCollectionByQuery(query);
166         if (c != null) {
167         	principalHRAttributes.addAll(c);
168         }
169         Set<String> pids = new HashSet<String>();
170         for(PrincipalHRAttributes phra : principalHRAttributes) {
171 	       	if(phra != null) {
172 	       	pids.add(phra.getPrincipalId());
173 	       	}
174         }
175         List<String> ids = new ArrayList<String>();
176         ids.addAll(pids);
177         
178         return ids;
179     }
180     
181     public List<String> getActiveEmployeesIdForTimeCalendarAndIdList(String timeCalendarName, List<String> pidList, Date asOfDate) {
182     	List<PrincipalHRAttributes> principalHRAttributes = new ArrayList<PrincipalHRAttributes>();
183         Criteria root = new Criteria();
184         
185         root.addEqualTo("payCalendar", timeCalendarName);
186         root.addIn("principalId", pidList);
187 
188         ImmutableList<String> fields = new ImmutableList.Builder<String>()
189                 .add("payCalendar")
190                 .add("principalId")
191                 .build();
192         java.sql.Date effDate = asOfDate == null ? null : new java.sql.Date(asOfDate.getTime());
193         root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(PrincipalHRAttributes.class, effDate, fields, false));
194         root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false));
195 
196         Criteria activeFilter = new Criteria();
197         activeFilter.addEqualTo("active", true);
198         root.addAndCriteria(activeFilter);
199 
200         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
201         Collection c = getPersistenceBrokerTemplate().getCollectionByQuery(query);
202         if (c != null) {
203         	principalHRAttributes.addAll(c);
204         }
205         Set<String> pids = new HashSet<String>();
206         for(PrincipalHRAttributes phra : principalHRAttributes) {
207 	       	if(phra != null) {
208 	       	pids.add(phra.getPrincipalId());
209 	       	}
210         }
211         List<String> ids = new ArrayList<String>();
212         ids.addAll(pids);
213         
214         return ids;
215     }
216 	
217     // KPME-1250 Kagata
218     @SuppressWarnings({"rawtypes", "unchecked"})
219     public List<PrincipalHRAttributes> getActiveEmployeesForLeavePlan(String leavePlan, java.util.Date asOfDate) {
220 
221         List<PrincipalHRAttributes> principals = new ArrayList<PrincipalHRAttributes>();
222         Criteria root = new Criteria();
223 
224         ImmutableList<String> fields = new ImmutableList.Builder<String>()
225                 .add("leavePlan")
226                 .add("principalId")
227                 .build();
228         java.sql.Date effDate = asOfDate == null ? null : new java.sql.Date(asOfDate.getTime());
229         root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(PrincipalHRAttributes.class, effDate, fields, false));
230         root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false));
231 
232         root.addEqualTo("leavePlan", leavePlan);
233         root.addEqualTo("active", true);
234 
235         Criteria activeFilter = new Criteria(); // Inner Join For Activity
236         activeFilter.addEqualTo("active", true);
237         root.addAndCriteria(activeFilter);
238 
239         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
240         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
241 
242         if (c != null) {
243         	principals.addAll(c);
244         }
245 
246         return principals;
247     }
248 
249     @Override
250     public List<String> getUniqueLeavePayGroupsForPrincipalIds(List<String> principalIds) {
251         if (CollectionUtils.isEmpty(principalIds)) {
252             return Collections.emptyList();
253         }
254         List<String> leaveCalendars = new ArrayList<String>();
255         Criteria crit = new Criteria();
256         crit.addEqualTo("active", true);
257         crit.addIn("principalId", principalIds);
258         ReportQueryByCriteria q = QueryFactory.newReportQuery(PrincipalHRAttributes.class, crit, true);
259         q.setDistinct(true);
260         q.setAttributes(new String[] {"leaveCalendar"});
261         Iterator iter = this.getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(q);
262         while (iter.hasNext()) {
263             Object[] values = (Object[]) iter.next();
264             String leaveCalendar = (String)values[0];
265             if (StringUtils.isNotBlank(leaveCalendar)) {
266                 leaveCalendars.add(leaveCalendar);
267             }
268         }
269         return leaveCalendars;
270     }
271     
272     @SuppressWarnings("rawtypes")
273 	@Override
274     public List<String> getUniqueTimePayGroups() {
275         List<String> payCalendars = new ArrayList<String>();
276         Criteria crit = new Criteria();
277         crit.addEqualTo("active", true);
278         ReportQueryByCriteria q = QueryFactory.newReportQuery(PrincipalHRAttributes.class, crit, true);
279         q.setDistinct(true);
280         q.setAttributes(new String[] {"pay_calendar"});
281         Iterator iter = this.getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(q);
282         while (iter.hasNext()) {
283             Object[] values = (Object[]) iter.next();
284             String leaveCalendar = (String)values[0];
285             if (StringUtils.isNotBlank(leaveCalendar)) {
286             	payCalendars.add(leaveCalendar);
287             }
288         }
289         return payCalendars;
290     }
291 
292 //    @Override
293 //	public PrincipalHRAttributes getPrincipalHRAttributes(String principalId) {
294 //		Criteria crit = new Criteria();
295 //		crit.addEqualTo("principalId", principalId);
296 //		Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, crit);
297 //		return (PrincipalHRAttributes)this.getPersistenceBrokerTemplate().getObjectByQuery(query);		
298 //	}
299     
300     @Override
301     public PrincipalHRAttributes getInactivePrincipalHRAttributes(String principalId, java.util.Date asOfDate) {
302     	PrincipalHRAttributes pc = null;
303 
304 		Criteria root = new Criteria();
305 		Criteria effdt = new Criteria();
306 
307         effdt.addGreaterOrEqualThan("effectiveDate", asOfDate);
308         ImmutableList<String> fields = new ImmutableList.Builder<String>()
309                 .add("leavePlan")
310                 .add("principalId")
311                 .build();
312         root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(PrincipalHRAttributes.class, effdt, fields, false));
313         root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false));
314 
315 		root.addEqualTo("principalId", principalId);
316 
317 		Criteria activeFilter = new Criteria(); // Inner Join For Activity
318 		activeFilter.addEqualTo("active", false);
319 		root.addAndCriteria(activeFilter);
320 		
321 		Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
322 		Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
323 
324 		if (obj != null) {
325 			pc = (PrincipalHRAttributes) obj;
326 		}
327 
328 		return pc;
329     }
330     
331     @Override
332     public PrincipalHRAttributes getPrincipalHRAttributes(String hrPrincipalAttributeId) {
333     	Criteria crit = new Criteria();
334 		crit.addEqualTo("hrPrincipalAttributeId", hrPrincipalAttributeId);
335 		
336 		Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, crit);
337 		return (PrincipalHRAttributes)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
338     }
339     
340     @Override
341     public List<PrincipalHRAttributes> getAllActivePrincipalHrAttributesForPrincipalId(String principalId, java.util.Date asOfDate) {
342     	
343     	List<PrincipalHRAttributes> phaList = new ArrayList<PrincipalHRAttributes>();
344     	Criteria root = new Criteria();
345         root.addEqualTo("principalId", principalId);
346         root.addLessOrEqualThan("effectiveDate", asOfDate);
347         root.addEqualTo("active", true);
348         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
349         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
350         if (c != null) {
351             phaList.addAll(c);
352         }
353         return phaList;        
354     }
355     
356     @Override
357     public List<PrincipalHRAttributes> getAllInActivePrincipalHrAttributesForPrincipalId(String principalId, java.util.Date asOfDate) {
358     	List<PrincipalHRAttributes> phaList = new ArrayList<PrincipalHRAttributes>();
359     	Criteria root = new Criteria();
360         root.addEqualTo("principalId", principalId);
361         root.addLessOrEqualThan("effectiveDate", asOfDate);
362         root.addEqualTo("active", false);
363 
364         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
365         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
366         if (c != null) {
367             phaList.addAll(c);
368         }
369         return phaList;  
370     }
371     @Override
372     public PrincipalHRAttributes getMaxTimeStampPrincipalHRAttributes(String principalId) {
373     	Criteria root = new Criteria();
374         Criteria crit = new Criteria();
375         
376         crit.addEqualTo("principalId", principalId);
377         ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, crit);
378         timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
379 
380         root.addEqualTo("principalId", principalId);
381         root.addEqualTo("timestamp", timestampSubQuery);
382 
383         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
384         return (PrincipalHRAttributes) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
385     }
386     
387     @Override
388     public List<PrincipalHRAttributes> getActivePrincipalHrAttributesForRange(String principalId, java.util.Date startDate, java.util.Date endDate) {
389     	List<PrincipalHRAttributes> activeList = new ArrayList<PrincipalHRAttributes>();
390     	Criteria root = new Criteria();
391         root.addEqualTo("principalId", principalId);
392         root.addGreaterOrEqualThan("effectiveDate", startDate);
393         root.addLessOrEqualThan("effectiveDate", endDate);
394         root.addEqualTo("active", true);
395         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
396         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
397         if (c != null) {
398         	activeList.addAll(c);
399         }
400         List<PrincipalHRAttributes> aList = new ArrayList<PrincipalHRAttributes>();
401         aList.addAll(activeList);
402         for(PrincipalHRAttributes aPha : aList) {
403         	List<PrincipalHRAttributes> inactivePhas = this.getInactivePrincipalHRAttributesForRange(principalId, aPha.getEffectiveDate(), endDate);
404         	if(CollectionUtils.isNotEmpty(inactivePhas)) {
405         		for(PrincipalHRAttributes inactive : inactivePhas) {
406         			if(inactive.getTimestamp().after(aPha.getTimestamp())) {
407         				activeList.remove(aPha);
408         			}
409         		}
410         	}
411         }
412         
413         return activeList;   
414     }
415     
416     @Override
417     public List<PrincipalHRAttributes> getInactivePrincipalHRAttributesForRange(String principalId, java.util.Date startDate, java.util.Date endDate) {
418     	List<PrincipalHRAttributes> inactiveList = new ArrayList<PrincipalHRAttributes>();
419     	Criteria root = new Criteria();
420         root.addEqualTo("principalId", principalId);
421         root.addGreaterOrEqualThan("effectiveDate", startDate);
422         root.addLessOrEqualThan("effectiveDate", endDate);
423         root.addEqualTo("active", false);
424         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
425         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
426         if (c != null) {
427         	inactiveList.addAll(c);
428         }
429         return inactiveList;
430     }
431    
432 	@Override
433     @SuppressWarnings("unchecked")
434     public List<PrincipalHRAttributes> getPrincipalHrAtributes(String principalId, String leavePlan, java.sql.Date fromEffdt, java.sql.Date toEffdt, String active, String showHistory) {
435     	List<PrincipalHRAttributes> results = new ArrayList<PrincipalHRAttributes>();
436         
437     	Criteria root = new Criteria();
438     	
439         if (StringUtils.isNotBlank(principalId)) {
440             root.addLike("principalId", principalId);
441         }
442 
443         if (StringUtils.isNotBlank(leavePlan)) {
444             root.addLike("leavePlan", leavePlan);
445         }
446 
447         Criteria effectiveDateFilter = new Criteria();
448         if (fromEffdt != null) {
449             effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
450         }
451         if (toEffdt != null) {
452             effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
453         }
454         if (fromEffdt == null && toEffdt == null) {
455             effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
456         }
457         root.addAndCriteria(effectiveDateFilter);
458 
459         if (StringUtils.isNotBlank(active)) {
460         	Criteria activeFilter = new Criteria();
461             if (StringUtils.equals(active, "Y")) {
462                 activeFilter.addEqualTo("active", true);
463             } else if (StringUtils.equals(active, "N")) {
464                 activeFilter.addEqualTo("active", false);
465             }
466             root.addAndCriteria(activeFilter);
467         }
468         
469         if (StringUtils.equals(showHistory, "N")) {
470             ImmutableList<String> fields = new ImmutableList.Builder<String>()
471                     .add("principalId")
472                     .add("leavePlan")
473                     .build();
474             root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(PrincipalHRAttributes.class, effectiveDateFilter, fields, false));
475             root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false));
476        }
477         
478        Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
479        results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
480        
481        return results;
482     }
483 
484 }