View Javadoc

1   /**
2    * Copyright 2004-2012 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.ojb.broker.query.Criteria;
25  import org.apache.ojb.broker.query.Query;
26  import org.apache.ojb.broker.query.QueryFactory;
27  import org.apache.ojb.broker.query.ReportQueryByCriteria;
28  import org.kuali.hr.time.principal.PrincipalHRAttributes;
29  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
30  
31  public class PrincipalHRAttributesDaoImpl extends PlatformAwareDaoBaseOjb implements PrincipalHRAttributesDao {
32  
33  	@Override
34  	public PrincipalHRAttributes getPrincipalCalendar(String principalId,
35  			Date asOfDate) {
36  		PrincipalHRAttributes pc = null;
37  
38  		Criteria root = new Criteria();
39  		Criteria effdt = new Criteria();
40  		Criteria timestamp = new Criteria();
41  
42  		effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
43  		effdt.addLessOrEqualThan("effectiveDate", asOfDate);
44  		ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, effdt);
45  		effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
46  
47  		timestamp.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
48  		timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
49  		ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, timestamp);
50  		timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
51  
52  		root.addEqualTo("principalId", principalId);
53  		root.addEqualTo("effectiveDate", effdtSubQuery);
54  		root.addEqualTo("timestamp", timestampSubQuery);
55  
56  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
57  		activeFilter.addEqualTo("active", true);
58  		root.addAndCriteria(activeFilter);
59  		
60  		Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
61  		Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
62  
63  		if (obj != null) {
64  			pc = (PrincipalHRAttributes) obj;
65  		}
66  
67  		return pc;
68  	}
69  
70  	@Override
71  	public void saveOrUpdate(PrincipalHRAttributes principalCalendar) {
72  		this.getPersistenceBrokerTemplate().store(principalCalendar);
73  		
74  	}
75  
76  	@Override
77  	public void saveOrUpdate(List<PrincipalHRAttributes> lstPrincipalCalendar) {
78  		if(lstPrincipalCalendar != null){
79  			for(PrincipalHRAttributes principalCal : lstPrincipalCalendar){
80  				this.getPersistenceBrokerTemplate().store(principalCal);
81  			}
82  		}
83  		
84  	}
85  
86  
87  //    @Override
88  //	public PrincipalHRAttributes getPrincipalHRAttributes(String principalId) {
89  //		Criteria crit = new Criteria();
90  //		crit.addEqualTo("principalId", principalId);
91  //		Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, crit);
92  //		return (PrincipalHRAttributes)this.getPersistenceBrokerTemplate().getObjectByQuery(query);		
93  //	}
94      
95      @Override
96      public PrincipalHRAttributes getInactivePrincipalHRAttributes(String principalId, Date asOfDate) {
97      	PrincipalHRAttributes pc = null;
98  
99  		Criteria root = new Criteria();
100 		Criteria effdt = new Criteria();
101 		Criteria timestamp = new Criteria();
102 
103 		effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
104 		effdt.addGreaterOrEqualThan("effectiveDate", asOfDate);
105 		ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, effdt);
106 		effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
107 
108 		timestamp.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
109 		timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
110 		ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, timestamp);
111 		timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
112 
113 		root.addEqualTo("principalId", principalId);
114 		root.addEqualTo("effectiveDate", effdtSubQuery);
115 		root.addEqualTo("timestamp", timestampSubQuery);
116 
117 		Criteria activeFilter = new Criteria(); // Inner Join For Activity
118 		activeFilter.addEqualTo("active", false);
119 		root.addAndCriteria(activeFilter);
120 		
121 		Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
122 		Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
123 
124 		if (obj != null) {
125 			pc = (PrincipalHRAttributes) obj;
126 		}
127 
128 		return pc;
129     }
130     
131     @Override
132     public PrincipalHRAttributes getPrincipalHRAttributes(String hrPrincipalAttributeId) {
133     	Criteria crit = new Criteria();
134 		crit.addEqualTo("hrPrincipalAttributeId", hrPrincipalAttributeId);
135 		
136 		Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, crit);
137 		return (PrincipalHRAttributes)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
138     }
139     
140     @Override
141     public List<PrincipalHRAttributes> getAllActivePrincipalHrAttributesForPrincipalId(String principalId, Date asOfDate) {
142     	
143     	List<PrincipalHRAttributes> phaList = new ArrayList<PrincipalHRAttributes>();
144     	Criteria root = new Criteria();
145         root.addEqualTo("principalId", principalId);
146         root.addLessOrEqualThan("effectiveDate", asOfDate);
147         root.addEqualTo("active", true);
148         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
149         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
150         if (c != null) {
151             phaList.addAll(c);
152         }
153         return phaList;        
154     }
155     
156     @Override
157     public List<PrincipalHRAttributes> getAllInActivePrincipalHrAttributesForPrincipalId(String principalId, Date asOfDate) {
158     	List<PrincipalHRAttributes> phaList = new ArrayList<PrincipalHRAttributes>();
159     	Criteria root = new Criteria();
160         root.addEqualTo("principalId", principalId);
161         root.addLessOrEqualThan("effectiveDate", asOfDate);
162         root.addEqualTo("active", false);
163 
164         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
165         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
166         if (c != null) {
167             phaList.addAll(c);
168         }
169         return phaList;  
170     }
171     @Override
172     public PrincipalHRAttributes getMaxTimeStampPrincipalHRAttributes(String principalId) {
173     	Criteria root = new Criteria();
174         Criteria crit = new Criteria();
175         
176         crit.addEqualTo("principalId", principalId);
177         ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, crit);
178         timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
179 
180         root.addEqualTo("principalId", principalId);
181         root.addEqualTo("timestamp", timestampSubQuery);
182 
183         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
184         return (PrincipalHRAttributes) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
185     }
186     
187     @Override
188     public List<PrincipalHRAttributes> getActivePrincipalHrAttributesForRange(String principalId, Date startDate, Date endDate) {
189     	List<PrincipalHRAttributes> activeList = new ArrayList<PrincipalHRAttributes>();
190     	Criteria root = new Criteria();
191         root.addEqualTo("principalId", principalId);
192         root.addGreaterOrEqualThan("effectiveDate", startDate);
193         root.addLessOrEqualThan("effectiveDate", endDate);
194         root.addEqualTo("active", true);
195         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
196         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
197         if (c != null) {
198         	activeList.addAll(c);
199         }
200         List<PrincipalHRAttributes> aList = new ArrayList<PrincipalHRAttributes>();
201         aList.addAll(activeList);
202         for(PrincipalHRAttributes aPha : aList) {
203         	List<PrincipalHRAttributes> inactivePhas = this.getInactivePrincipalHRAttributesForRange(principalId, aPha.getEffectiveDate(), endDate);
204         	if(CollectionUtils.isNotEmpty(inactivePhas)) {
205         		for(PrincipalHRAttributes inactive : inactivePhas) {
206         			if(inactive.getTimestamp().after(aPha.getTimestamp())) {
207         				activeList.remove(aPha);
208         			}
209         		}
210         	}
211         }
212         
213         return activeList;   
214     }
215     
216     @Override
217     public List<PrincipalHRAttributes> getInactivePrincipalHRAttributesForRange(String principalId, Date startDate, Date endDate) {
218     	List<PrincipalHRAttributes> inactiveList = new ArrayList<PrincipalHRAttributes>();
219     	Criteria root = new Criteria();
220         root.addEqualTo("principalId", principalId);
221         root.addGreaterOrEqualThan("effectiveDate", startDate);
222         root.addLessOrEqualThan("effectiveDate", endDate);
223         root.addEqualTo("active", false);
224         Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
225         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
226         if (c != null) {
227         	inactiveList.addAll(c);
228         }
229         return inactiveList;
230     }
231 }