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.lm.leaveblock.dao;
17  
18  import java.sql.Timestamp;
19  import java.util.*;
20  
21  import org.apache.commons.collections.CollectionUtils;
22  import org.apache.commons.lang3.StringUtils;
23  import org.apache.log4j.Logger;
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.joda.time.DateTime;
29  import org.kuali.hr.lm.LMConstants;
30  import org.kuali.hr.lm.leaveblock.LeaveBlock;
31  import org.kuali.hr.time.earncode.EarnCode;
32  import org.kuali.hr.time.service.base.TkServiceLocator;
33  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
34  
35  public class LeaveBlockDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements LeaveBlockDao {
36  
37      private static final Logger LOG = Logger.getLogger(LeaveBlockDaoSpringOjbImpl.class);
38  
39      @Override
40      public LeaveBlock getLeaveBlock(String leaveBlockId) {
41          Criteria root = new Criteria();
42          root.addEqualTo("lmLeaveBlockId", leaveBlockId);
43  //        root.addEqualTo("active", true);
44  
45          Query query = QueryFactory.newQuery(LeaveBlock.class, root);
46  
47          return (LeaveBlock) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
48      }
49  
50      @Override
51      public List<LeaveBlock> getLeaveBlocksForDocumentId(String documentId) {
52          List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
53          Criteria root = new Criteria();
54          root.addEqualTo("documentId", documentId);
55  //        root.addEqualTo("active", true);
56  
57          Query query = QueryFactory.newQuery(LeaveBlock.class, root);
58          @SuppressWarnings("rawtypes")
59          Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
60  
61          if (c != null) {
62              leaveBlocks.addAll(c);
63          }
64          return leaveBlocks;
65      }
66  
67      @SuppressWarnings("rawtypes")
68      @Override
69      public List<LeaveBlock> getLeaveBlocks(String principalId, Date beginDate, Date endDate) {
70          List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
71          Criteria root = new Criteria();
72          root.addEqualTo("principalId", principalId);
73          root.addGreaterOrEqualThan("leaveDate", beginDate);
74          root.addLessOrEqualThan("leaveDate", endDate);
75  //        root.addEqualTo("active", true);
76  
77          Query query = QueryFactory.newQuery(LeaveBlock.class, root);
78          Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
79  
80          if (c != null) {
81          	leaveBlocks.addAll(c);
82          }
83  
84          return leaveBlocks;
85      }
86  
87      @SuppressWarnings("rawtypes")
88      @Override
89      public List<LeaveBlock> getLeaveBlocksWithType(String principalId, Date beginDate, Date endDate, String leaveBlockType) {
90          List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
91          Criteria root = new Criteria();
92          root.addEqualTo("principalId", principalId);
93          root.addGreaterOrEqualThan("leaveDate", beginDate);
94          root.addLessOrEqualThan("leaveDate", endDate);
95          root.addEqualTo("leaveBlockType", leaveBlockType);
96  //        root.addEqualTo("active", true);
97  
98          Query query = QueryFactory.newQuery(LeaveBlock.class, root);
99          Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
100 
101         if (c != null) {
102             leaveBlocks.addAll(c);
103         }
104 
105         return leaveBlocks;
106     }
107 
108     @Override
109     public List<LeaveBlock> getLeaveBlocksWithAccrualCategory(String principalId, Date beginDate, Date endDate, String accrualCategory) {
110         List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
111         Criteria root = new Criteria();
112         root.addEqualTo("principalId", principalId);
113         root.addGreaterOrEqualThan("leaveDate", beginDate);
114         root.addLessOrEqualThan("leaveDate", endDate);
115         root.addEqualTo("accrualCategory", accrualCategory);
116 //        root.addEqualTo("active", true);
117 
118         Query query = QueryFactory.newQuery(LeaveBlock.class, root);
119         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
120 
121         if (c != null) {
122             leaveBlocks.addAll(c);
123         }
124 
125         return leaveBlocks;
126     }
127 
128     public List<LeaveBlock> getLeaveBlocksSinceCarryOver(String principalId, Map<String, LeaveBlock> carryOverDates, DateTime endDate, boolean includeAllAccrualCategories) {
129         Criteria root = new Criteria();
130         root.addEqualTo("principalId", principalId);
131         if (endDate != null) {
132             root.addLessOrEqualThan("leaveDate", endDate.toDate());
133         }
134 
135         Criteria orCriteria = new Criteria();
136         for (Map.Entry<String, LeaveBlock> entry : carryOverDates.entrySet()) {
137             Criteria crit = new Criteria();
138             crit.addEqualTo("accrualCategory", entry.getKey());
139             crit.addGreaterThan("leaveDate", entry.getValue().getLeaveDate());
140             orCriteria.addOrCriteria(crit);
141         }
142         if (!orCriteria.isEmpty()) {
143             if (CollectionUtils.isNotEmpty(carryOverDates.keySet()) && includeAllAccrualCategories) {
144                 Criteria crit = new Criteria();
145                 crit.addNotIn("accrualCategory", carryOverDates.keySet());
146                 orCriteria.addOrCriteria(crit);
147             }
148             root.addAndCriteria(orCriteria);
149         }
150 
151 
152         Query query = QueryFactory.newQuery(LeaveBlock.class, root);
153         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
154         List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
155         if (c != null) {
156             leaveBlocks.addAll(c);
157         }
158 
159         return leaveBlocks;
160     }
161 
162     public Map<String, LeaveBlock> getLastCarryOverBlocks(String principalId, String leaveBlockType, Date asOfDate) {
163         Map<String, LeaveBlock> carryOver = new HashMap<String, LeaveBlock>();
164         Criteria root = new Criteria();
165         root.addEqualTo("principalId", principalId);
166         root.addEqualTo("leaveBlockType", leaveBlockType);
167 
168         Criteria dateSubquery = new Criteria();
169         dateSubquery.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
170         dateSubquery.addEqualToField("leaveBlockType", Criteria.PARENT_QUERY_PREFIX + "leaveBlockType");
171         dateSubquery.addEqualToField("accrualCategory", Criteria.PARENT_QUERY_PREFIX + "accrualCategory");
172         if (asOfDate != null) {
173             dateSubquery.addLessThan("leaveDate", asOfDate);
174         }
175 
176         ReportQueryByCriteria subQuery = QueryFactory.newReportQuery(LeaveBlock.class, dateSubquery);
177         String[] attributes = new String[] { "max(leaveDate)" };
178         subQuery.setAttributes(attributes);
179 
180         root.addEqualTo("leaveDate", subQuery);
181 
182         Query query = QueryFactory.newQuery(LeaveBlock.class, root);
183         Collection<LeaveBlock> c = (Collection<LeaveBlock>)this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
184         //Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
185         for (LeaveBlock lb : c) {
186             carryOver.put(lb.getAccrualCategory(), lb);
187         }
188 
189         return carryOver;
190 
191     }
192 
193 	@Override
194 	public List<LeaveBlock> getLeaveBlocks(String principalId, String leaveBlockType, String requestStatus, Date currentDate) {
195 	    List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
196 	    Criteria root = new Criteria();
197 	    root.addEqualTo("principalId", principalId);
198 	    root.addEqualTo("leaveBlockType", leaveBlockType);
199 	    root.addEqualTo("requestStatus", requestStatus);
200 	    if(currentDate != null) {
201 	    	root.addGreaterThan("leaveDate", currentDate);
202 	    }
203 	    Query query = QueryFactory.newQuery(LeaveBlock.class, root);
204 	    Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
205 	    if(c!= null) {
206 	    	leaveBlocks.addAll(c);
207 	    }
208 		return leaveBlocks;
209 	}
210 	
211 	@Override
212 	public List<LeaveBlock> getLeaveBlocksForDate(String principalId, Date leaveDate) {
213 	    List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
214 	    Criteria root = new Criteria();
215 	    root.addEqualTo("principalId", principalId);
216 	    root.addEqualTo("leaveDate", leaveDate);
217 	    Query query = QueryFactory.newQuery(LeaveBlock.class, root);
218 	    Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
219 	    if(c!= null) {
220 	    	leaveBlocks.addAll(c);
221 	    }
222 		return leaveBlocks;
223 	}
224 
225 	@Override
226 	public List<LeaveBlock> getLeaveBlocks(Date leaveDate, String accrualCategory, String principalId) {
227 		Criteria root = new Criteria();
228 		root.addLessOrEqualThan("timestamp", leaveDate);
229 		root.addEqualTo("accrualCategory", accrualCategory);
230 		root.addEqualTo("principalId", principalId);
231 		Query query = QueryFactory.newQuery(LeaveBlock.class, root);
232         List<LeaveBlock> leaveBlocks = (List<LeaveBlock>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
233 		return leaveBlocks;
234 	}
235 
236     @Override
237     public List<LeaveBlock> getLeaveBlocks(String principalId, String accrualCategory, Date beginDate, Date endDate) {
238         List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
239         Criteria root = new Criteria();
240         root.addEqualTo("principalId", principalId);
241         root.addGreaterOrEqualThan("leaveDate", beginDate);
242         root.addLessOrEqualThan("leaveDate", endDate);
243         root.addEqualTo("accrualCategory", accrualCategory);
244 //        root.addEqualTo("active", true);
245 
246         Query query = QueryFactory.newQuery(LeaveBlock.class, root);
247         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
248 
249         if (c != null) {
250             leaveBlocks.addAll(c);
251         }
252 
253         return leaveBlocks;
254     }
255 
256     @Override
257     public List<LeaveBlock> getFMLALeaveBlocks(String principalId, String accrualCategory, Date beginDate, Date endDate) {
258         List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
259         Criteria root = new Criteria();
260         root.addEqualTo("principalId", principalId);
261         root.addGreaterOrEqualThan("leaveDate", beginDate);
262         root.addLessOrEqualThan("leaveDate", endDate);
263         root.addEqualTo("accrualCategory", accrualCategory);
264         
265         Criteria earnCode = new Criteria();
266         earnCode.addEqualToField("earnCode", Criteria.PARENT_QUERY_PREFIX + "earnCode");
267         earnCode.addEqualTo("fmla", "Y");
268         ReportQueryByCriteria earnCodeSubQuery = QueryFactory.newReportQuery(EarnCode.class, earnCode);
269         root.addEqualTo("earnCode", earnCodeSubQuery);
270         
271         //root.add
272 //        root.addEqualTo("active", true);
273 
274         Query query = QueryFactory.newQuery(LeaveBlock.class, root);
275         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
276 
277         if (c != null) {
278             leaveBlocks.addAll(c);
279         }
280 
281         return leaveBlocks;
282     }
283 	
284 	@Override
285 	public List<LeaveBlock> getNotAccrualGeneratedLeaveBlocksForDate(String principalId, Date leaveDate) {
286 		List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
287 	    Criteria root = new Criteria();
288 	    root.addEqualTo("principalId", principalId);
289 	    root.addEqualTo("leaveDate", leaveDate);
290 	    root.addEqualTo("accrualGenerated", "N");
291 	    Query query = QueryFactory.newQuery(LeaveBlock.class, root);
292 	    Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
293 	    if(c!= null) {
294 	    	leaveBlocks.addAll(c);
295 	    }
296 		return leaveBlocks;
297 	}
298 	
299 	public List<LeaveBlock> getCalendarLeaveBlocks(String principalId, Date beginDate, Date endDate) {
300 		List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
301 		
302         Criteria root = new Criteria();
303         root.addEqualTo("principalId", principalId);
304         root.addGreaterOrEqualThan("leaveDate", beginDate);
305         root.addLessOrEqualThan("leaveDate", endDate);
306         List<String> typeValues = new ArrayList<String>();
307         typeValues.add(LMConstants.LEAVE_BLOCK_TYPE.LEAVE_CALENDAR);
308         typeValues.add(LMConstants.LEAVE_BLOCK_TYPE.TIME_CALENDAR);
309         typeValues.add(LMConstants.LEAVE_BLOCK_TYPE.ACCRUAL_SERVICE);
310         root.addIn("leaveBlockType", typeValues);
311 
312         Query query = QueryFactory.newQuery(LeaveBlock.class, root);
313         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
314 
315         if (c != null) {
316         	leaveBlocks.addAll(c);
317         }
318         return leaveBlocks;
319 
320 	}
321 	
322     public void deleteLeaveBlock(String leaveBlockId) {
323         Criteria crit = new Criteria();
324         crit.addEqualTo("lmLeaveBlockId",leaveBlockId);
325         this.getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(LeaveBlock.class, crit));
326     }
327 	
328     public void deleteLeaveBlocksForDocumentId(String documentId){
329         Criteria crit = new Criteria();
330         crit.addEqualTo("documentId",documentId);
331         this.getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(LeaveBlock.class, crit));
332 
333     }
334     
335       
336     @Override
337     public List<LeaveBlock> getAccrualGeneratedLeaveBlocks(String principalId, Date beginDate, Date endDate) {
338     	List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
339         Criteria root = new Criteria();
340           
341         root.addEqualTo("principalId", principalId);
342         root.addGreaterOrEqualThan("leaveDate", beginDate);
343         root.addLessOrEqualThan("leaveDate", endDate);
344         root.addEqualTo("accrualGenerated", "Y");
345 
346         Query query = QueryFactory.newQuery(LeaveBlock.class, root);
347         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
348         if (c != null) {
349         	leaveBlocks.addAll(c);
350         }
351         return leaveBlocks;
352     }
353     
354     @Override
355     public List<LeaveBlock> getSSTOLeaveBlocks(String principalId, String sstoId, Date accruledDate) {
356     	List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
357         Criteria root = new Criteria();
358           
359         root.addEqualTo("principalId", principalId);
360         root.addEqualTo("leaveDate", accruledDate);
361         root.addEqualTo("scheduleTimeOffId", sstoId);
362 
363         Query query = QueryFactory.newQuery(LeaveBlock.class, root);
364         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
365         if (c != null) {
366         	leaveBlocks.addAll(c);
367         }
368         return leaveBlocks;
369     }
370 
371     @Override
372     public List<LeaveBlock> getABELeaveBlocksSinceTime(String principalId, Timestamp lastRanTime) {
373     	List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
374     	Criteria root = new Criteria();
375          
376 		root.addEqualTo("principalId", principalId);
377 		root.addGreaterThan("timestamp", lastRanTime);
378 		Query query = QueryFactory.newQuery(LeaveBlock.class, root);
379 		Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
380 		List<LeaveBlock> tempList = new ArrayList<LeaveBlock>();
381 		if (c != null) {
382 			tempList.addAll(c);
383 		}
384 		for(LeaveBlock lb : tempList) {
385 			if(lb != null && StringUtils.isNotEmpty(lb.getEarnCode())) {
386 				EarnCode ec = TkServiceLocator.getEarnCodeService().getEarnCode(lb.getEarnCode(), lb.getLeaveDate());
387 				if(ec != null && ec.getEligibleForAccrual().equals("N")) {
388 					leaveBlocks.add(lb);
389 				}
390 			}
391 		}
392     	return leaveBlocks;
393     }
394 
395 }