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