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 beginDate, Date endDate) {
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(beginDate != null) {
201             root.addGreaterOrEqualThan("leaveDate", beginDate);
202         }
203         if (endDate != null){
204             root.addLessOrEqualThan("leaveDate", endDate);
205         }
206         Query query = QueryFactory.newQuery(LeaveBlock.class, root);
207         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
208         if(c!= null) {
209             leaveBlocks.addAll(c);
210         }
211         return leaveBlocks;
212     }
213 
214 	@Override
215 	public List<LeaveBlock> getLeaveBlocks(String principalId, String leaveBlockType, String requestStatus, Date currentDate) {
216 	    List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
217 	    Criteria root = new Criteria();
218 	    root.addEqualTo("principalId", principalId);
219 	    root.addEqualTo("leaveBlockType", leaveBlockType);
220 	    root.addEqualTo("requestStatus", requestStatus);
221 	    if(currentDate != null) {
222 	    	root.addGreaterThan("leaveDate", currentDate);
223 	    }
224 	    Query query = QueryFactory.newQuery(LeaveBlock.class, root);
225 	    Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
226 	    if(c!= null) {
227 	    	leaveBlocks.addAll(c);
228 	    }
229 		return leaveBlocks;
230 	}
231 	
232 	@Override
233 	public List<LeaveBlock> getLeaveBlocksForDate(String principalId, Date leaveDate) {
234 	    List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
235 	    Criteria root = new Criteria();
236 	    root.addEqualTo("principalId", principalId);
237 	    root.addEqualTo("leaveDate", leaveDate);
238 	    Query query = QueryFactory.newQuery(LeaveBlock.class, root);
239 	    Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
240 	    if(c!= null) {
241 	    	leaveBlocks.addAll(c);
242 	    }
243 		return leaveBlocks;
244 	}
245 
246 	@Override
247 	public List<LeaveBlock> getLeaveBlocks(Date leaveDate, String accrualCategory, String principalId) {
248 		Criteria root = new Criteria();
249 		root.addLessOrEqualThan("timestamp", leaveDate);
250 		root.addEqualTo("accrualCategory", accrualCategory);
251 		root.addEqualTo("principalId", principalId);
252 		Query query = QueryFactory.newQuery(LeaveBlock.class, root);
253         List<LeaveBlock> leaveBlocks = (List<LeaveBlock>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
254 		return leaveBlocks;
255 	}
256 
257     @Override
258     public List<LeaveBlock> getLeaveBlocks(String principalId, String accrualCategory, Date beginDate, Date endDate) {
259         List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
260         Criteria root = new Criteria();
261         root.addEqualTo("principalId", principalId);
262         root.addGreaterOrEqualThan("leaveDate", beginDate);
263         root.addLessOrEqualThan("leaveDate", endDate);
264         root.addEqualTo("accrualCategory", accrualCategory);
265 //        root.addEqualTo("active", true);
266 
267         Query query = QueryFactory.newQuery(LeaveBlock.class, root);
268         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
269 
270         if (c != null) {
271             leaveBlocks.addAll(c);
272         }
273 
274         return leaveBlocks;
275     }
276 
277     @Override
278     public List<LeaveBlock> getFMLALeaveBlocks(String principalId, String accrualCategory, Date beginDate, Date endDate) {
279         List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
280         Criteria root = new Criteria();
281         root.addEqualTo("principalId", principalId);
282         root.addGreaterOrEqualThan("leaveDate", beginDate);
283         root.addLessOrEqualThan("leaveDate", endDate);
284         root.addEqualTo("accrualCategory", accrualCategory);
285         
286         Criteria earnCode = new Criteria();
287         earnCode.addEqualToField("earnCode", Criteria.PARENT_QUERY_PREFIX + "earnCode");
288         earnCode.addEqualTo("fmla", "Y");
289         ReportQueryByCriteria earnCodeSubQuery = QueryFactory.newReportQuery(EarnCode.class, earnCode);
290         root.addEqualTo("earnCode", earnCodeSubQuery);
291         
292         //root.add
293 //        root.addEqualTo("active", true);
294 
295         Query query = QueryFactory.newQuery(LeaveBlock.class, root);
296         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
297 
298         if (c != null) {
299             leaveBlocks.addAll(c);
300         }
301 
302         return leaveBlocks;
303     }
304 	
305 	@Override
306 	public List<LeaveBlock> getNotAccrualGeneratedLeaveBlocksForDate(String principalId, Date leaveDate) {
307 		List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
308 	    Criteria root = new Criteria();
309 	    root.addEqualTo("principalId", principalId);
310 	    root.addEqualTo("leaveDate", leaveDate);
311 	    root.addEqualTo("accrualGenerated", "N");
312 	    Query query = QueryFactory.newQuery(LeaveBlock.class, root);
313 	    Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
314 	    if(c!= null) {
315 	    	leaveBlocks.addAll(c);
316 	    }
317 		return leaveBlocks;
318 	}
319 	
320 	public List<LeaveBlock> getCalendarLeaveBlocks(String principalId, Date beginDate, Date endDate) {
321 		List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
322 		
323         Criteria root = new Criteria();
324         root.addEqualTo("principalId", principalId);
325         root.addGreaterOrEqualThan("leaveDate", beginDate);
326         root.addLessOrEqualThan("leaveDate", endDate);
327         List<String> typeValues = new ArrayList<String>();
328         typeValues.add(LMConstants.LEAVE_BLOCK_TYPE.LEAVE_CALENDAR);
329         typeValues.add(LMConstants.LEAVE_BLOCK_TYPE.TIME_CALENDAR);
330         typeValues.add(LMConstants.LEAVE_BLOCK_TYPE.ACCRUAL_SERVICE);
331         root.addIn("leaveBlockType", typeValues);
332 
333         Query query = QueryFactory.newQuery(LeaveBlock.class, root);
334         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
335 
336         if (c != null) {
337         	leaveBlocks.addAll(c);
338         }
339         return leaveBlocks;
340 
341 	}
342 	
343     public void deleteLeaveBlock(String leaveBlockId) {
344         Criteria crit = new Criteria();
345         crit.addEqualTo("lmLeaveBlockId",leaveBlockId);
346         this.getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(LeaveBlock.class, crit));
347     }
348 	
349     public void deleteLeaveBlocksForDocumentId(String documentId){
350         Criteria crit = new Criteria();
351         crit.addEqualTo("documentId",documentId);
352         this.getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(LeaveBlock.class, crit));
353 
354     }
355     
356       
357     @Override
358     public List<LeaveBlock> getAccrualGeneratedLeaveBlocks(String principalId, Date beginDate, Date endDate) {
359     	List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
360         Criteria root = new Criteria();
361           
362         root.addEqualTo("principalId", principalId);
363         root.addGreaterOrEqualThan("leaveDate", beginDate);
364         root.addLessOrEqualThan("leaveDate", endDate);
365         root.addEqualTo("accrualGenerated", "Y");
366 
367         Query query = QueryFactory.newQuery(LeaveBlock.class, root);
368         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
369         if (c != null) {
370         	leaveBlocks.addAll(c);
371         }
372         return leaveBlocks;
373     }
374     
375     @Override
376     public List<LeaveBlock> getSSTOLeaveBlocks(String principalId, String sstoId, Date accruledDate) {
377     	List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
378         Criteria root = new Criteria();
379           
380         root.addEqualTo("principalId", principalId);
381         root.addEqualTo("leaveDate", accruledDate);
382         root.addEqualTo("scheduleTimeOffId", sstoId);
383 
384         Query query = QueryFactory.newQuery(LeaveBlock.class, root);
385         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
386         if (c != null) {
387         	leaveBlocks.addAll(c);
388         }
389         return leaveBlocks;
390     }
391 
392     @Override
393     public List<LeaveBlock> getABELeaveBlocksSinceTime(String principalId, Timestamp lastRanTime) {
394     	List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
395     	Criteria root = new Criteria();
396          
397 		root.addEqualTo("principalId", principalId);
398 		root.addGreaterThan("timestamp", lastRanTime);
399 		Query query = QueryFactory.newQuery(LeaveBlock.class, root);
400 		Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
401 		List<LeaveBlock> tempList = new ArrayList<LeaveBlock>();
402 		if (c != null) {
403 			tempList.addAll(c);
404 		}
405 		for(LeaveBlock lb : tempList) {
406 			if(lb != null && StringUtils.isNotEmpty(lb.getEarnCode())) {
407 				EarnCode ec = TkServiceLocator.getEarnCodeService().getEarnCode(lb.getEarnCode(), lb.getLeaveDate());
408 				if(ec != null && ec.getEligibleForAccrual().equals("N")) {
409 					leaveBlocks.add(lb);
410 				}
411 			}
412 		}
413     	return leaveBlocks;
414     }
415 
416 }