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