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.Date;
21  import java.util.List;
22  
23  import org.apache.commons.collections.CollectionUtils;
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.ojb.broker.query.Criteria;
26  import org.apache.ojb.broker.query.Query;
27  import org.apache.ojb.broker.query.QueryFactory;
28  import org.apache.ojb.broker.query.ReportQueryByCriteria;
29  import org.kuali.hr.time.principal.PrincipalHRAttributes;
30  import org.kuali.hr.time.util.TKUtils;
31  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
32  
33  public class PrincipalHRAttributesDaoImpl extends PlatformAwareDaoBaseOjb implements PrincipalHRAttributesDao {
34  
35  	@Override
36  	public PrincipalHRAttributes getPrincipalCalendar(String principalId,
37  			Date asOfDate) {
38  		PrincipalHRAttributes pc = null;
39  
40  		Criteria root = new Criteria();
41  		Criteria effdt = new Criteria();
42  		Criteria timestamp = new Criteria();
43  
44  		effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
45  		effdt.addLessOrEqualThan("effectiveDate", asOfDate);
46  		ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, effdt);
47  		effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
48  
49  		timestamp.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
50  		timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
51  		ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, timestamp);
52  		timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
53  
54  		root.addEqualTo("principalId", principalId);
55  		root.addEqualTo("effectiveDate", effdtSubQuery);
56  		root.addEqualTo("timestamp", timestampSubQuery);
57  
58  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
59  		activeFilter.addEqualTo("active", true);
60  		root.addAndCriteria(activeFilter);
61  		
62  		Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
63  		Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
64  
65  		if (obj != null) {
66  			pc = (PrincipalHRAttributes) obj;
67  		}
68  
69  		return pc;
70  	}
71  
72  	@Override
73  	public void saveOrUpdate(PrincipalHRAttributes principalCalendar) {
74  		this.getPersistenceBrokerTemplate().store(principalCalendar);
75  		
76  	}
77  
78  	@Override
79  	public void saveOrUpdate(List<PrincipalHRAttributes> lstPrincipalCalendar) {
80  		if(lstPrincipalCalendar != null){
81  			for(PrincipalHRAttributes principalCal : lstPrincipalCalendar){
82  				this.getPersistenceBrokerTemplate().store(principalCal);
83  			}
84  		}
85  		
86  	}
87  
88  
89  //    @Override
90  //	public PrincipalHRAttributes getPrincipalHRAttributes(String principalId) {
91  //		Criteria crit = new Criteria();
92  //		crit.addEqualTo("principalId", principalId);
93  //		Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, crit);
94  //		return (PrincipalHRAttributes)this.getPersistenceBrokerTemplate().getObjectByQuery(query);		
95  //	}
96      
97      @Override
98      public PrincipalHRAttributes getInactivePrincipalHRAttributes(String principalId, Date asOfDate) {
99      	PrincipalHRAttributes pc = null;
100 
101 		Criteria root = new Criteria();
102 		Criteria effdt = new Criteria();
103 		Criteria timestamp = new Criteria();
104 
105 		effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
106 		effdt.addGreaterOrEqualThan("effectiveDate", asOfDate);
107 		ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, effdt);
108 		effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
109 
110 		timestamp.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
111 		timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
112 		ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, timestamp);
113 		timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
114 
115 		root.addEqualTo("principalId", principalId);
116 		root.addEqualTo("effectiveDate", effdtSubQuery);
117 		root.addEqualTo("timestamp", timestampSubQuery);
118 
119 		Criteria activeFilter = new Criteria(); // Inner Join For Activity
120 		activeFilter.addEqualTo("active", false);
121 		root.addAndCriteria(activeFilter);
122 		
123 		Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
124 		Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
125 
126 		if (obj != null) {
127 			pc = (PrincipalHRAttributes) obj;
128 		}
129 
130 		return pc;
131     }
132     
133     @Override
134     public PrincipalHRAttributes getPrincipalHRAttributes(String hrPrincipalAttributeId) {
135     	Criteria crit = new Criteria();
136 		crit.addEqualTo("hrPrincipalAttributeId", hrPrincipalAttributeId);
137 		
138 		Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, crit);
139 		return (PrincipalHRAttributes)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
140     }
141     
142     @Override
143     public List<PrincipalHRAttributes> getAllActivePrincipalHrAttributesForPrincipalId(String principalId, Date asOfDate) {
144     	
145     	List<PrincipalHRAttributes> phaList = new ArrayList<PrincipalHRAttributes>();
146     	Criteria root = new Criteria();
147         root.addEqualTo("principalId", principalId);
148         root.addLessOrEqualThan("effectiveDate", asOfDate);
149         root.addEqualTo("active", true);
150         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
151         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
152         if (c != null) {
153             phaList.addAll(c);
154         }
155         return phaList;        
156     }
157     
158     @Override
159     public List<PrincipalHRAttributes> getAllInActivePrincipalHrAttributesForPrincipalId(String principalId, Date asOfDate) {
160     	List<PrincipalHRAttributes> phaList = new ArrayList<PrincipalHRAttributes>();
161     	Criteria root = new Criteria();
162         root.addEqualTo("principalId", principalId);
163         root.addLessOrEqualThan("effectiveDate", asOfDate);
164         root.addEqualTo("active", false);
165 
166         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
167         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
168         if (c != null) {
169             phaList.addAll(c);
170         }
171         return phaList;  
172     }
173     @Override
174     public PrincipalHRAttributes getMaxTimeStampPrincipalHRAttributes(String principalId) {
175     	Criteria root = new Criteria();
176         Criteria crit = new Criteria();
177         
178         crit.addEqualTo("principalId", principalId);
179         ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, crit);
180         timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
181 
182         root.addEqualTo("principalId", principalId);
183         root.addEqualTo("timestamp", timestampSubQuery);
184 
185         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
186         return (PrincipalHRAttributes) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
187     }
188     
189     @Override
190     public List<PrincipalHRAttributes> getActivePrincipalHrAttributesForRange(String principalId, Date startDate, Date endDate) {
191     	List<PrincipalHRAttributes> activeList = new ArrayList<PrincipalHRAttributes>();
192     	Criteria root = new Criteria();
193         root.addEqualTo("principalId", principalId);
194         root.addGreaterOrEqualThan("effectiveDate", startDate);
195         root.addLessOrEqualThan("effectiveDate", endDate);
196         root.addEqualTo("active", true);
197         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
198         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
199         if (c != null) {
200         	activeList.addAll(c);
201         }
202         List<PrincipalHRAttributes> aList = new ArrayList<PrincipalHRAttributes>();
203         aList.addAll(activeList);
204         for(PrincipalHRAttributes aPha : aList) {
205         	List<PrincipalHRAttributes> inactivePhas = this.getInactivePrincipalHRAttributesForRange(principalId, aPha.getEffectiveDate(), endDate);
206         	if(CollectionUtils.isNotEmpty(inactivePhas)) {
207         		for(PrincipalHRAttributes inactive : inactivePhas) {
208         			if(inactive.getTimestamp().after(aPha.getTimestamp())) {
209         				activeList.remove(aPha);
210         			}
211         		}
212         	}
213         }
214         
215         return activeList;   
216     }
217     
218     @Override
219     public List<PrincipalHRAttributes> getInactivePrincipalHRAttributesForRange(String principalId, Date startDate, Date endDate) {
220     	List<PrincipalHRAttributes> inactiveList = new ArrayList<PrincipalHRAttributes>();
221     	Criteria root = new Criteria();
222         root.addEqualTo("principalId", principalId);
223         root.addGreaterOrEqualThan("effectiveDate", startDate);
224         root.addLessOrEqualThan("effectiveDate", endDate);
225         root.addEqualTo("active", false);
226         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
227         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
228         if (c != null) {
229         	inactiveList.addAll(c);
230         }
231         return inactiveList;
232     }
233     
234 	@Override
235     @SuppressWarnings("unchecked")
236     public List<PrincipalHRAttributes> getPrincipalHrAtributes(String principalId, java.sql.Date fromEffdt, java.sql.Date toEffdt, String active, String showHistory) {
237     	List<PrincipalHRAttributes> results = new ArrayList<PrincipalHRAttributes>();
238         
239     	Criteria root = new Criteria();
240     	
241         if (StringUtils.isNotBlank(principalId)) {
242             root.addLike("principalId", principalId);
243         }
244         
245         Criteria effectiveDateFilter = new Criteria();
246         if (fromEffdt != null) {
247             effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
248         }
249         if (toEffdt != null) {
250             effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
251         }
252         if (fromEffdt == null && toEffdt == null) {
253             effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
254         }
255         root.addAndCriteria(effectiveDateFilter);
256 
257         if (StringUtils.isNotBlank(active)) {
258         	Criteria activeFilter = new Criteria();
259             if (StringUtils.equals(active, "Y")) {
260                 activeFilter.addEqualTo("active", true);
261             } else if (StringUtils.equals(active, "N")) {
262                 activeFilter.addEqualTo("active", false);
263             }
264             root.addAndCriteria(activeFilter);
265         }
266         
267         if (StringUtils.equals(showHistory, "N")) {
268             Criteria effdt = new Criteria();
269         	effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
270         	effdt.addAndCriteria(effectiveDateFilter);
271         	ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, effdt);
272             effdtSubQuery.setAttributes(new String[]{"max(effectiveDate)"});
273             root.addEqualTo("effectiveDate", effdtSubQuery);
274             
275             Criteria timestamp = new Criteria();
276        		timestamp.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
277        		timestamp.addAndCriteria(effectiveDateFilter);
278        		ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, timestamp);
279        		timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
280        		root.addEqualTo("timestamp", timestampSubQuery);
281        }
282         
283        Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
284        results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
285        
286        return results;
287     }
288     
289 }