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