001    /**
002     * Copyright 2004-2012 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.time.calendar.service;
017    
018    import java.util.Calendar;
019    import java.util.Date;
020    import java.util.List;
021    
022    import org.apache.commons.lang.time.DateUtils;
023    import org.kuali.hr.time.calendar.CalendarEntries;
024    import org.kuali.hr.time.calendar.CalendarEntryPeriodType;
025    import org.kuali.hr.time.calendar.dao.CalendarEntriesDao;
026    
027    public class CalendarEntriesServiceImpl implements CalendarEntriesService {
028    
029        private CalendarEntriesDao calendarEntriesDao;
030    
031        public void setCalendarEntriesDao(CalendarEntriesDao calendarEntriesDao) {
032            this.calendarEntriesDao = calendarEntriesDao;
033        }
034    
035        public CalendarEntries getCalendarEntries(String hrCalendarEntriesId) {
036    
037            return calendarEntriesDao.getCalendarEntries(hrCalendarEntriesId);
038        }
039    
040        @Override
041        public CalendarEntries getCalendarEntriesByIdAndPeriodEndDate(String hrCalendarId, Date endPeriodDate) {
042            return calendarEntriesDao.getCalendarEntriesByIdAndPeriodEndDate(hrCalendarId, endPeriodDate);
043        }
044    
045        @Override
046        public CalendarEntries getCurrentCalendarEntriesByCalendarId(
047                String hrCalendarId, Date currentDate) {
048            return calendarEntriesDao.getCurrentCalendarEntriesByCalendarId(hrCalendarId, currentDate);
049        }
050    
051        @Override
052        public CalendarEntries getPreviousCalendarEntriesByCalendarId(String hrCalendarId, CalendarEntries pce) {
053            return calendarEntriesDao.getPreviousCalendarEntriesByCalendarId(hrCalendarId, pce);
054        }
055    
056        @Override
057        public CalendarEntries getNextCalendarEntriesByCalendarId(String hrCalendarId, CalendarEntries pce) {
058            return calendarEntriesDao.getNextCalendarEntriesByCalendarId(hrCalendarId, pce);
059        }
060    
061        public List<CalendarEntries> getCurrentCalendarEntryNeedsScheduled(int thresholdDays, Date asOfDate) {
062            return calendarEntriesDao.getCurrentCalendarEntryNeedsScheduled(thresholdDays, asOfDate);
063        }
064    
065        @Override
066        public CalendarEntries createNextCalendarEntry(CalendarEntries calendarEntries, CalendarEntryPeriodType type) {
067            CalendarEntries newEntry = new CalendarEntries();
068            newEntry.setCalendarName(calendarEntries.getCalendarName());
069            newEntry.setHrCalendarId(calendarEntries.getHrCalendarId());
070            newEntry.setCalendarObj(calendarEntries.getCalendarObj());
071            newEntry.setBatchInitiateTime(calendarEntries.getBatchInitiateTime());
072            newEntry.setBatchEndPayPeriodTime(calendarEntries.getBatchEndPayPeriodTime());
073            newEntry.setBatchEmployeeApprovalTime(calendarEntries.getBatchEmployeeApprovalTime());
074            newEntry.setBatchSupervisorApprovalTime(calendarEntries.getBatchSupervisorApprovalTime());
075    
076            if (type == null) {
077                type = CalendarEntryPeriodType.BI_WEEKLY;
078            }
079            if (CalendarEntryPeriodType.WEEKLY.equals(type)
080                    || CalendarEntryPeriodType.BI_WEEKLY.equals(type)) {
081                int weekly_multiplier = 2;
082                if (CalendarEntryPeriodType.WEEKLY.equals(type)) {
083                    weekly_multiplier = 1;
084                }
085                newEntry.setBeginPeriodDateTime(DateUtils.addWeeks(calendarEntries.getBeginPeriodDateTime(), weekly_multiplier));
086                newEntry.setEndPeriodDateTime(DateUtils.addWeeks(calendarEntries.getEndPeriodDateTime(), weekly_multiplier));
087                newEntry.setBatchInitiateDate(new java.sql.Date(DateUtils.addWeeks(calendarEntries.getBatchInitiateDate(), weekly_multiplier).getTime()));
088                newEntry.setBatchEndPayPeriodDate(new java.sql.Date(DateUtils.addWeeks(calendarEntries.getBatchEndPayPeriodDate(), weekly_multiplier).getTime()));
089                newEntry.setBatchEmployeeApprovalDate(new java.sql.Date(DateUtils.addWeeks(calendarEntries.getBatchEmployeeApprovalDate(), weekly_multiplier).getTime()));
090                newEntry.setBatchSupervisorApprovalDate(new java.sql.Date(DateUtils.addWeeks(calendarEntries.getBatchSupervisorApprovalDate(), weekly_multiplier).getTime()));
091            } else if (CalendarEntryPeriodType.MONTHLY.equals(type)) {
092                newEntry.setBeginPeriodDateTime(addMonthToDate(calendarEntries.getBeginPeriodDateTime()));
093                newEntry.setEndPeriodDateTime(addMonthToDate(calendarEntries.getEndPeriodDateTime()));
094                newEntry.setBatchInitiateDate(new java.sql.Date(addMonthToDate(calendarEntries.getBatchInitiateDate()).getTime()));
095                newEntry.setBatchEndPayPeriodDate(new java.sql.Date(addMonthToDate(calendarEntries.getBatchEndPayPeriodDate()).getTime()));
096                newEntry.setBatchEmployeeApprovalDate(new java.sql.Date(addMonthToDate(calendarEntries.getBatchEmployeeApprovalDate()).getTime()));
097                newEntry.setBatchSupervisorApprovalDate(new java.sql.Date(addMonthToDate(calendarEntries.getBatchSupervisorApprovalDate()).getTime()));
098            } else if (CalendarEntryPeriodType.SEMI_MONTHLY.equals(type)) {
099                newEntry.setBeginPeriodDateTime(addSemiMonthToDate(calendarEntries.getBeginPeriodDateTime()));
100                newEntry.setEndPeriodDateTime(addSemiMonthToDate(calendarEntries.getEndPeriodDateTime()));
101                newEntry.setBatchInitiateDate(new java.sql.Date(addSemiMonthToDate(calendarEntries.getBatchInitiateDate()).getTime()));
102                newEntry.setBatchEndPayPeriodDate(new java.sql.Date(addSemiMonthToDate(calendarEntries.getBatchEndPayPeriodDate()).getTime()));
103                newEntry.setBatchEmployeeApprovalDate(new java.sql.Date(addSemiMonthToDate(calendarEntries.getBatchEmployeeApprovalDate()).getTime()));
104                newEntry.setBatchSupervisorApprovalDate(new java.sql.Date(addSemiMonthToDate(calendarEntries.getBatchSupervisorApprovalDate()).getTime()));
105            }
106            calendarEntriesDao.saveOrUpdate(newEntry);
107            return getNextCalendarEntriesByCalendarId(calendarEntries.getHrCalendarId(), calendarEntries);
108        }
109    
110        private Date addMonthToDate(Date date) {
111            Calendar temp = Calendar.getInstance();
112            temp.setTime(date);
113            boolean lastDayOfMonth = temp.getActualMaximum(Calendar.DATE) == temp.get(Calendar.DATE);
114    
115            date = DateUtils.addMonths(date, 1);
116            if (lastDayOfMonth) {
117                temp.setTime(date);
118                temp.set(Calendar.DATE, temp.getActualMaximum(Calendar.DATE));
119                date = temp.getTime();
120            }
121            return date;
122        }
123    
124        private Date addSemiMonthToDate(Date date) {
125            //so assuming the common pairs of this are the 1st & 16th, and then 15th and the last day,
126            // and 14th with the last day minus 1
127            //so we'll peek at the current date and try to figure out the best guesses for addition.
128            Calendar temp = Calendar.getInstance();
129            temp.setTime(date);
130            if (temp.getActualMaximum(Calendar.DATE) == temp.get(Calendar.DATE)) {
131                //date is on the last day of the month.  Set next date to the 15th
132                date = DateUtils.addMonths(date, 1);
133                temp.setTime(date);
134                temp.set(Calendar.DATE, 15);
135            } else if (temp.get(Calendar.DATE) == 15) {
136                //we are on the 15th of the month, so now lets go to the end of the month
137                temp.setTime(date);
138                temp.set(Calendar.DATE,  temp.getActualMaximum(Calendar.DATE));
139            } else if (temp.get(Calendar.DATE) == 1) {
140                //first of the month, next would be 16
141                temp.setTime(date);
142                temp.set(Calendar.DATE,  16);
143            } else if (temp.get(Calendar.DATE) == 16) {
144                //16th, so add a month and set day to '1'
145                date = DateUtils.addMonths(date, 1);
146                temp.setTime(date);
147                temp.set(Calendar.DATE, 1);
148            } else if (temp.get(Calendar.DATE) == 14) {
149                //14th day, set next one to last day minus 1
150                temp.setTime(date);
151                temp.set(Calendar.DATE,  temp.getActualMaximum(Calendar.DATE) - 1);
152            } else if (temp.getActualMaximum(Calendar.DATE) == temp.get(Calendar.DATE) - 1) {
153                //date is on the second to last day of the month.  Set next date to the 14th
154                date = DateUtils.addMonths(date, 1);
155                temp.setTime(date);
156                temp.set(Calendar.DATE, 14);
157            } else {
158                // so it isn't one of the common dates... i guess we'll just add 15 days...
159                date = DateUtils.addDays(date, 15);
160                temp.setTime(date);
161            }
162    
163            return temp.getTime() ;
164        }
165    
166        public List<CalendarEntries> getFutureCalendarEntries(String hrCalendarId, Date currentDate, int numberOfEntries) {
167            List<CalendarEntries> calendarEntries = null;
168            calendarEntries = calendarEntriesDao.getFutureCalendarEntries(hrCalendarId, currentDate, numberOfEntries);
169            return calendarEntries;
170        }
171    
172        public CalendarEntries getCalendarEntriesByBeginAndEndDate(Date beginPeriodDate, Date endPeriodDate) {
173            return calendarEntriesDao.getCalendarEntriesByBeginAndEndDate(beginPeriodDate, endPeriodDate);
174        }
175        
176        public List<CalendarEntries> getAllCalendarEntriesForCalendarId(String hrCalendarId) {
177            return calendarEntriesDao.getAllCalendarEntriesForCalendarId(hrCalendarId);
178        }
179        
180        public List<CalendarEntries> getAllCalendarEntriesForCalendarIdAndYear(String hrCalendarId, String year) {
181            return calendarEntriesDao.getAllCalendarEntriesForCalendarIdAndYear(hrCalendarId, year);
182        }
183    }