001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.lm.leaveplan.service;
017    
018    import java.sql.Date;
019    import java.util.Calendar;
020    import java.util.List;
021    
022    import org.joda.time.DateMidnight;
023    import org.joda.time.DateTime;
024    import org.kuali.hr.lm.leaveplan.LeavePlan;
025    import org.kuali.hr.lm.leaveplan.dao.LeavePlanDao;
026    import org.kuali.hr.time.calendar.CalendarEntries;
027    
028    public class LeavePlanServiceImpl implements LeavePlanService {
029    
030            private LeavePlanDao leavePlanDao;
031     
032            public LeavePlanDao getLeavePlanDao() {
033                    return leavePlanDao;
034            }
035    
036    
037            public void setLeavePlanDao(LeavePlanDao leavePlanDao) {
038                    this.leavePlanDao = leavePlanDao;
039            }
040    
041    
042            @Override
043            public LeavePlan getLeavePlan(String lmLeavePlanId) {
044                    return getLeavePlanDao().getLeavePlan(lmLeavePlanId);
045            }
046            
047            @Override
048            public LeavePlan getLeavePlan(String leavePlan, Date asOfDate) {
049                    return getLeavePlanDao().getLeavePlan(leavePlan, asOfDate);
050            }
051       
052            @Override
053            public boolean isValidLeavePlan(String leavePlan){
054                    boolean valid = false;
055                    int count = getLeavePlanDao().getNumberLeavePlan(leavePlan);
056                    valid = (count > 0);
057                    return valid;
058            }
059            
060            @Override
061            public List<LeavePlan> getAllActiveLeavePlan(String leavePlan, Date asOfDate) {
062                     return leavePlanDao.getAllActiveLeavePlan(leavePlan, asOfDate);
063             }
064            @Override
065            public List<LeavePlan> getAllInActiveLeavePlan(String leavePlan, Date asOfDate) {
066                     return leavePlanDao.getAllInActiveLeavePlan(leavePlan, asOfDate);
067             }
068    
069        @Override
070        public List<LeavePlan> getLeavePlans(String leavePlan, String calendarYearStart, String descr, String planningMonths, Date fromEffdt, Date toEffdt, String active, String showHistory) {
071            return leavePlanDao.getLeavePlans(leavePlan, calendarYearStart, descr, planningMonths, fromEffdt, toEffdt, active, showHistory);
072        }
073        
074        @Override
075            public boolean isFirstCalendarPeriodOfLeavePlan(CalendarEntries calendarEntry, String leavePlan, Date asOfDate) {
076                    boolean isFirstCalendarPeriodOfLeavePlan = false;
077            
078            LeavePlan leavePlanObj = getLeavePlan(leavePlan, asOfDate);
079                    
080            if (leavePlanObj != null) {
081                            DateTime calendarEntryEndDate = new DateTime(calendarEntry.getBeginPeriodDate());
082                            
083                            int calendarYearStartMonth = Integer.valueOf(leavePlanObj.getCalendarYearStartMonth());
084                            int calendarYearStartDay = Integer.valueOf(leavePlanObj.getCalendarYearStartDayOfMonth());
085                            int calendarEntryEndDateMonth = calendarEntryEndDate.getMonthOfYear();
086                            int calendarEntryEndDateDay = calendarEntryEndDate.getDayOfMonth();
087                            
088                            isFirstCalendarPeriodOfLeavePlan = (calendarYearStartMonth == calendarEntryEndDateMonth) && (calendarYearStartDay == calendarEntryEndDateDay);
089            }
090            
091            return isFirstCalendarPeriodOfLeavePlan;
092            }
093        
094        @Override
095            public boolean isLastCalendarPeriodOfLeavePlan(CalendarEntries calendarEntry, String leavePlan, Date asOfDate) {
096            boolean isLastCalendarPeriodOfLeavePlan = false;
097            
098            LeavePlan leavePlanObj = getLeavePlan(leavePlan, asOfDate);
099                    
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    }