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.leaveplan.service;
17  
18  import java.sql.Date;
19  import java.util.Calendar;
20  import java.util.List;
21  
22  import org.joda.time.DateMidnight;
23  import org.joda.time.DateTime;
24  import org.kuali.hr.lm.leaveplan.LeavePlan;
25  import org.kuali.hr.lm.leaveplan.dao.LeavePlanDao;
26  import org.kuali.hr.time.calendar.CalendarEntries;
27  
28  public class LeavePlanServiceImpl implements LeavePlanService {
29  
30  	private LeavePlanDao leavePlanDao;
31   
32  	public LeavePlanDao getLeavePlanDao() {
33  		return leavePlanDao;
34  	}
35  
36  
37  	public void setLeavePlanDao(LeavePlanDao leavePlanDao) {
38  		this.leavePlanDao = leavePlanDao;
39  	}
40  
41  
42  	@Override
43  	public LeavePlan getLeavePlan(String lmLeavePlanId) {
44  		return getLeavePlanDao().getLeavePlan(lmLeavePlanId);
45  	}
46  	
47  	@Override
48  	public LeavePlan getLeavePlan(String leavePlan, Date asOfDate) {
49  		return getLeavePlanDao().getLeavePlan(leavePlan, asOfDate);
50  	}
51     
52  	@Override
53  	public boolean isValidLeavePlan(String leavePlan){
54  		boolean valid = false;
55  		int count = getLeavePlanDao().getNumberLeavePlan(leavePlan);
56  		valid = (count > 0);
57  		return valid;
58  	}
59  	
60  	@Override
61  	public List<LeavePlan> getAllActiveLeavePlan(String leavePlan, Date asOfDate) {
62  		 return leavePlanDao.getAllActiveLeavePlan(leavePlan, asOfDate);
63  	 }
64  	@Override
65  	public List<LeavePlan> getAllInActiveLeavePlan(String leavePlan, Date asOfDate) {
66  		 return leavePlanDao.getAllInActiveLeavePlan(leavePlan, asOfDate);
67  	 }
68  
69      @Override
70      public List<LeavePlan> getLeavePlans(String leavePlan, String calendarYearStart, String descr, String planningMonths, Date fromEffdt, Date toEffdt, String active, String showHistory) {
71          return leavePlanDao.getLeavePlans(leavePlan, calendarYearStart, descr, planningMonths, fromEffdt, toEffdt, active, showHistory);
72      }
73      
74      @Override
75  	public boolean isFirstCalendarPeriodOfLeavePlan(CalendarEntries calendarEntry, String leavePlan, Date asOfDate) {
76  		boolean isFirstCalendarPeriodOfLeavePlan = false;
77      	
78      	LeavePlan leavePlanObj = getLeavePlan(leavePlan, asOfDate);
79  		
80      	if (leavePlanObj != null) {
81  			DateTime calendarEntryEndDate = new DateTime(calendarEntry.getBeginPeriodDate());
82  			
83  			int calendarYearStartMonth = Integer.valueOf(leavePlanObj.getCalendarYearStartMonth());
84  			int calendarYearStartDay = Integer.valueOf(leavePlanObj.getCalendarYearStartDayOfMonth());
85  			int calendarEntryEndDateMonth = calendarEntryEndDate.getMonthOfYear();
86  			int calendarEntryEndDateDay = calendarEntryEndDate.getDayOfMonth();
87  			
88  			isFirstCalendarPeriodOfLeavePlan = (calendarYearStartMonth == calendarEntryEndDateMonth) && (calendarYearStartDay == calendarEntryEndDateDay);
89      	}
90      	
91      	return isFirstCalendarPeriodOfLeavePlan;
92  	}
93      
94      @Override
95  	public boolean isLastCalendarPeriodOfLeavePlan(CalendarEntries calendarEntry, String leavePlan, Date asOfDate) {
96      	boolean isLastCalendarPeriodOfLeavePlan = false;
97      	
98      	LeavePlan leavePlanObj = getLeavePlan(leavePlan, asOfDate);
99  		
100     	if (leavePlanObj != null) {
101 			DateTime calendarEntryEndDate = new DateTime(calendarEntry.getEndPeriodDate());
102 			
103 			int calendarYearStartMonth = Integer.valueOf(leavePlanObj.getCalendarYearStartMonth());
104 			int calendarYearStartDay = Integer.valueOf(leavePlanObj.getCalendarYearStartDayOfMonth());
105 			int calendarEntryEndDateMonth = calendarEntryEndDate.getMonthOfYear();
106 			int calendarEntryEndDateDay = calendarEntryEndDate.getDayOfMonth();
107 			
108 			isLastCalendarPeriodOfLeavePlan = (calendarYearStartMonth == calendarEntryEndDateMonth) && (calendarYearStartDay == calendarEntryEndDateDay);
109     	}
110     	
111     	return isLastCalendarPeriodOfLeavePlan;
112 	}
113 
114     @Override
115     public DateTime getFirstDayOfLeavePlan(String leavePlan, java.util.Date asOfDate) {
116     	//The only thing this method does is tack on the year of the supplied asOfDate to the calendar year start date.
117         LeavePlan lp = getLeavePlan(leavePlan, new Date(asOfDate.getTime()));
118 
119         int priorYearCutOffMonth = Integer.parseInt(lp.getCalendarYearStartMonth());
120         int priorYearCutOffDay = Integer.parseInt(lp.getCalendarYearStartDayOfMonth());
121         Calendar cal = Calendar.getInstance();
122         cal.set(Calendar.MONTH, priorYearCutOffMonth);
123         cal.set(Calendar.DATE, priorYearCutOffDay);
124 
125         DateMidnight cutOffDate = new DateMidnight(asOfDate.getTime()).withMonthOfYear(priorYearCutOffMonth).withDayOfMonth(priorYearCutOffDay);
126         if (asOfDate.before(cutOffDate.toDate())) {
127             cutOffDate = cutOffDate.minusYears(1);
128         }
129         return cutOffDate.toDateTime();
130     }
131 
132     @Override
133     public DateTime getRolloverDayOfLeavePlan(String leavePlan, java.util.Date asOfDate) {
134         LeavePlan lp = getLeavePlan(leavePlan, new Date(asOfDate.getTime()));
135 
136         int priorYearCutOffMonth = Integer.parseInt(lp.getCalendarYearStartMonth());
137         int priorYearCutOffDay = Integer.parseInt(lp.getCalendarYearStartDayOfMonth());
138         Calendar cal = Calendar.getInstance();
139         cal.set(Calendar.MONTH, priorYearCutOffMonth);
140         cal.set(Calendar.DATE, priorYearCutOffDay);
141 
142         DateMidnight cutOffDate = new DateMidnight(asOfDate.getTime()).withMonthOfYear(priorYearCutOffMonth).withDayOfMonth(priorYearCutOffDay);
143         if (asOfDate.after(cutOffDate.toDate())) {
144             cutOffDate = cutOffDate.plusYears(1);
145         }
146         return cutOffDate.toDateTime();
147     }
148 
149 
150 	@Override
151 	public List<LeavePlan> getLeavePlansNeedsCarryOverScheduled(int thresholdDays,
152                                                                 Date asOfDate) {
153 		return leavePlanDao.getLeavePlansNeedsScheduled(thresholdDays, asOfDate);
154 	}
155 	
156 }