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