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         Criteria effdt = new Criteria();
224         Criteria timestamp = new Criteria();
225 
226         ImmutableList<String> fields = new ImmutableList.Builder<String>()
227                 .add("leavePlan")
228                 .add("principalId")
229                 .build();
230         java.sql.Date effDate = asOfDate == null ? null : new java.sql.Date(asOfDate.getTime());
231         root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(PrincipalHRAttributes.class, effDate, fields, false));
232         root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false));
233 
234         root.addEqualTo("leavePlan", leavePlan);
235         root.addEqualTo("active", true);
236 
237         Criteria activeFilter = new Criteria(); // Inner Join For Activity
238         activeFilter.addEqualTo("active", true);
239         root.addAndCriteria(activeFilter);
240 
241         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
242         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
243 
244         if (c != null) {
245         	principals.addAll(c);
246         }
247 
248         return principals;
249     }
250 
251     @Override
252     public List<String> getUniqueLeavePayGroupsForPrincipalIds(List<String> principalIds) {
253         if (CollectionUtils.isEmpty(principalIds)) {
254             return Collections.emptyList();
255         }
256         List<String> leaveCalendars = new ArrayList<String>();
257         Criteria crit = new Criteria();
258         crit.addEqualTo("active", true);
259         crit.addIn("principalId", principalIds);
260         ReportQueryByCriteria q = QueryFactory.newReportQuery(PrincipalHRAttributes.class, crit, true);
261         q.setDistinct(true);
262         q.setAttributes(new String[] {"leaveCalendar"});
263         Iterator iter = this.getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(q);
264         while (iter.hasNext()) {
265             Object[] values = (Object[]) iter.next();
266             String leaveCalendar = (String)values[0];
267             if (StringUtils.isNotBlank(leaveCalendar)) {
268                 leaveCalendars.add(leaveCalendar);
269             }
270         }
271         return leaveCalendars;
272     }
273     
274     @SuppressWarnings("rawtypes")
275 	@Override
276     public List<String> getUniqueTimePayGroups() {
277         List<String> payCalendars = new ArrayList<String>();
278         Criteria crit = new Criteria();
279         crit.addEqualTo("active", true);
280         ReportQueryByCriteria q = QueryFactory.newReportQuery(PrincipalHRAttributes.class, crit, true);
281         q.setDistinct(true);
282         q.setAttributes(new String[] {"pay_calendar"});
283         Iterator iter = this.getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(q);
284         while (iter.hasNext()) {
285             Object[] values = (Object[]) iter.next();
286             String leaveCalendar = (String)values[0];
287             if (StringUtils.isNotBlank(leaveCalendar)) {
288             	payCalendars.add(leaveCalendar);
289             }
290         }
291         return payCalendars;
292     }
293 
294 //    @Override
295 //	public PrincipalHRAttributes getPrincipalHRAttributes(String principalId) {
296 //		Criteria crit = new Criteria();
297 //		crit.addEqualTo("principalId", principalId);
298 //		Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, crit);
299 //		return (PrincipalHRAttributes)this.getPersistenceBrokerTemplate().getObjectByQuery(query);		
300 //	}
301     
302     @Override
303     public PrincipalHRAttributes getInactivePrincipalHRAttributes(String principalId, java.util.Date asOfDate) {
304     	PrincipalHRAttributes pc = null;
305 
306 		Criteria root = new Criteria();
307 		Criteria effdt = new Criteria();
308 		Criteria timestamp = new Criteria();
309 
310         effdt.addGreaterOrEqualThan("effectiveDate", asOfDate);
311         ImmutableList<String> fields = new ImmutableList.Builder<String>()
312                 .add("leavePlan")
313                 .add("principalId")
314                 .build();
315         root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(PrincipalHRAttributes.class, effdt, fields, false));
316         root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false));
317 
318 		root.addEqualTo("principalId", principalId);
319 
320 		Criteria activeFilter = new Criteria(); // Inner Join For Activity
321 		activeFilter.addEqualTo("active", false);
322 		root.addAndCriteria(activeFilter);
323 		
324 		Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
325 		Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
326 
327 		if (obj != null) {
328 			pc = (PrincipalHRAttributes) obj;
329 		}
330 
331 		return pc;
332     }
333     
334     @Override
335     public PrincipalHRAttributes getPrincipalHRAttributes(String hrPrincipalAttributeId) {
336     	Criteria crit = new Criteria();
337 		crit.addEqualTo("hrPrincipalAttributeId", hrPrincipalAttributeId);
338 		
339 		Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, crit);
340 		return (PrincipalHRAttributes)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
341     }
342     
343     @Override
344     public List<PrincipalHRAttributes> getAllActivePrincipalHrAttributesForPrincipalId(String principalId, java.util.Date asOfDate) {
345     	
346     	List<PrincipalHRAttributes> phaList = new ArrayList<PrincipalHRAttributes>();
347     	Criteria root = new Criteria();
348         root.addEqualTo("principalId", principalId);
349         root.addLessOrEqualThan("effectiveDate", asOfDate);
350         root.addEqualTo("active", true);
351         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
352         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
353         if (c != null) {
354             phaList.addAll(c);
355         }
356         return phaList;        
357     }
358     
359     @Override
360     public List<PrincipalHRAttributes> getAllInActivePrincipalHrAttributesForPrincipalId(String principalId, java.util.Date asOfDate) {
361     	List<PrincipalHRAttributes> phaList = new ArrayList<PrincipalHRAttributes>();
362     	Criteria root = new Criteria();
363         root.addEqualTo("principalId", principalId);
364         root.addLessOrEqualThan("effectiveDate", asOfDate);
365         root.addEqualTo("active", false);
366 
367         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
368         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
369         if (c != null) {
370             phaList.addAll(c);
371         }
372         return phaList;  
373     }
374     @Override
375     public PrincipalHRAttributes getMaxTimeStampPrincipalHRAttributes(String principalId) {
376     	Criteria root = new Criteria();
377         Criteria crit = new Criteria();
378         
379         crit.addEqualTo("principalId", principalId);
380         ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, crit);
381         timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
382 
383         root.addEqualTo("principalId", principalId);
384         root.addEqualTo("timestamp", timestampSubQuery);
385 
386         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
387         return (PrincipalHRAttributes) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
388     }
389     
390     @Override
391     public List<PrincipalHRAttributes> getActivePrincipalHrAttributesForRange(String principalId, java.util.Date startDate, java.util.Date endDate) {
392     	List<PrincipalHRAttributes> activeList = new ArrayList<PrincipalHRAttributes>();
393     	Criteria root = new Criteria();
394         root.addEqualTo("principalId", principalId);
395         root.addGreaterOrEqualThan("effectiveDate", startDate);
396         root.addLessOrEqualThan("effectiveDate", endDate);
397         root.addEqualTo("active", true);
398         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
399         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
400         if (c != null) {
401         	activeList.addAll(c);
402         }
403         List<PrincipalHRAttributes> aList = new ArrayList<PrincipalHRAttributes>();
404         aList.addAll(activeList);
405         for(PrincipalHRAttributes aPha : aList) {
406         	List<PrincipalHRAttributes> inactivePhas = this.getInactivePrincipalHRAttributesForRange(principalId, aPha.getEffectiveDate(), endDate);
407         	if(CollectionUtils.isNotEmpty(inactivePhas)) {
408         		for(PrincipalHRAttributes inactive : inactivePhas) {
409         			if(inactive.getTimestamp().after(aPha.getTimestamp())) {
410         				activeList.remove(aPha);
411         			}
412         		}
413         	}
414         }
415         
416         return activeList;   
417     }
418     
419     @Override
420     public List<PrincipalHRAttributes> getInactivePrincipalHRAttributesForRange(String principalId, java.util.Date startDate, java.util.Date endDate) {
421     	List<PrincipalHRAttributes> inactiveList = new ArrayList<PrincipalHRAttributes>();
422     	Criteria root = new Criteria();
423         root.addEqualTo("principalId", principalId);
424         root.addGreaterOrEqualThan("effectiveDate", startDate);
425         root.addLessOrEqualThan("effectiveDate", endDate);
426         root.addEqualTo("active", false);
427         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
428         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
429         if (c != null) {
430         	inactiveList.addAll(c);
431         }
432         return inactiveList;
433     }
434    
435 	@Override
436     @SuppressWarnings("unchecked")
437     public List<PrincipalHRAttributes> getPrincipalHrAtributes(String principalId, String leavePlan, java.sql.Date fromEffdt, java.sql.Date toEffdt, String active, String showHistory) {
438     	List<PrincipalHRAttributes> results = new ArrayList<PrincipalHRAttributes>();
439         
440     	Criteria root = new Criteria();
441     	
442         if (StringUtils.isNotBlank(principalId)) {
443             root.addLike("principalId", principalId);
444         }
445 
446         if (StringUtils.isNotBlank(leavePlan)) {
447             root.addLike("leavePlan", leavePlan);
448         }
449 
450         Criteria effectiveDateFilter = new Criteria();
451         if (fromEffdt != null) {
452             effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
453         }
454         if (toEffdt != null) {
455             effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
456         }
457         if (fromEffdt == null && toEffdt == null) {
458             effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
459         }
460         root.addAndCriteria(effectiveDateFilter);
461 
462         if (StringUtils.isNotBlank(active)) {
463         	Criteria activeFilter = new Criteria();
464             if (StringUtils.equals(active, "Y")) {
465                 activeFilter.addEqualTo("active", true);
466             } else if (StringUtils.equals(active, "N")) {
467                 activeFilter.addEqualTo("active", false);
468             }
469             root.addAndCriteria(activeFilter);
470         }
471         
472         if (StringUtils.equals(showHistory, "N")) {
473             ImmutableList<String> fields = new ImmutableList.Builder<String>()
474                     .add("principalId")
475                     .add("leavePlan")
476                     .build();
477             root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(PrincipalHRAttributes.class, effectiveDateFilter, fields, false));
478             root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false));
479        }
480         
481        Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
482        results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
483        
484        return results;
485     }
486 
487 }