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.time.calendar.service;
17  
18  import java.util.Calendar;
19  import java.util.Date;
20  import java.util.List;
21  
22  import org.apache.commons.lang.time.DateUtils;
23  import org.kuali.hr.time.calendar.CalendarEntries;
24  import org.kuali.hr.time.calendar.CalendarEntryPeriodType;
25  import org.kuali.hr.time.calendar.dao.CalendarEntriesDao;
26  
27  public class CalendarEntriesServiceImpl implements CalendarEntriesService {
28  
29      private CalendarEntriesDao calendarEntriesDao;
30  
31      public void setCalendarEntriesDao(CalendarEntriesDao calendarEntriesDao) {
32          this.calendarEntriesDao = calendarEntriesDao;
33      }
34  
35      public CalendarEntries getCalendarEntries(String hrCalendarEntriesId) {
36  
37          return calendarEntriesDao.getCalendarEntries(hrCalendarEntriesId);
38      }
39  
40      @Override
41      public CalendarEntries getCalendarEntriesByIdAndPeriodEndDate(String hrCalendarId, Date endPeriodDate) {
42          return calendarEntriesDao.getCalendarEntriesByIdAndPeriodEndDate(hrCalendarId, endPeriodDate);
43      }
44  
45      @Override
46      public CalendarEntries getCurrentCalendarEntriesByCalendarId(
47              String hrCalendarId, Date currentDate) {
48          return calendarEntriesDao.getCurrentCalendarEntriesByCalendarId(hrCalendarId, currentDate);
49      }
50  
51      @Override
52      public CalendarEntries getPreviousCalendarEntriesByCalendarId(String hrCalendarId, CalendarEntries pce) {
53          return calendarEntriesDao.getPreviousCalendarEntriesByCalendarId(hrCalendarId, pce);
54      }
55  
56      @Override
57      public CalendarEntries getNextCalendarEntriesByCalendarId(String hrCalendarId, CalendarEntries pce) {
58          return calendarEntriesDao.getNextCalendarEntriesByCalendarId(hrCalendarId, pce);
59      }
60  
61      public List<CalendarEntries> getCurrentCalendarEntryNeedsScheduled(int thresholdDays, Date asOfDate) {
62          return calendarEntriesDao.getCurrentCalendarEntryNeedsScheduled(thresholdDays, asOfDate);
63      }
64  
65      @Override
66      public CalendarEntries createNextCalendarEntry(CalendarEntries calendarEntries, CalendarEntryPeriodType type) {
67          CalendarEntries newEntry = new CalendarEntries();
68          newEntry.setCalendarName(calendarEntries.getCalendarName());
69          newEntry.setHrCalendarId(calendarEntries.getHrCalendarId());
70          newEntry.setCalendarObj(calendarEntries.getCalendarObj());
71          newEntry.setBatchInitiateTime(calendarEntries.getBatchInitiateTime());
72          newEntry.setBatchEndPayPeriodTime(calendarEntries.getBatchEndPayPeriodTime());
73          newEntry.setBatchEmployeeApprovalTime(calendarEntries.getBatchEmployeeApprovalTime());
74          newEntry.setBatchSupervisorApprovalTime(calendarEntries.getBatchSupervisorApprovalTime());
75  
76          if (type == null) {
77              type = CalendarEntryPeriodType.BI_WEEKLY;
78          }
79          if (CalendarEntryPeriodType.WEEKLY.equals(type)
80                  || CalendarEntryPeriodType.BI_WEEKLY.equals(type)) {
81              int weekly_multiplier = 2;
82              if (CalendarEntryPeriodType.WEEKLY.equals(type)) {
83                  weekly_multiplier = 1;
84              }
85              newEntry.setBeginPeriodDateTime(DateUtils.addWeeks(calendarEntries.getBeginPeriodDateTime(), weekly_multiplier));
86              newEntry.setEndPeriodDateTime(DateUtils.addWeeks(calendarEntries.getEndPeriodDateTime(), weekly_multiplier));
87              newEntry.setBatchInitiateDate(new java.sql.Date(DateUtils.addWeeks(calendarEntries.getBatchInitiateDate(), weekly_multiplier).getTime()));
88              newEntry.setBatchEndPayPeriodDate(new java.sql.Date(DateUtils.addWeeks(calendarEntries.getBatchEndPayPeriodDate(), weekly_multiplier).getTime()));
89              newEntry.setBatchEmployeeApprovalDate(new java.sql.Date(DateUtils.addWeeks(calendarEntries.getBatchEmployeeApprovalDate(), weekly_multiplier).getTime()));
90              newEntry.setBatchSupervisorApprovalDate(new java.sql.Date(DateUtils.addWeeks(calendarEntries.getBatchSupervisorApprovalDate(), weekly_multiplier).getTime()));
91          } else if (CalendarEntryPeriodType.MONTHLY.equals(type)) {
92              newEntry.setBeginPeriodDateTime(addMonthToDate(calendarEntries.getBeginPeriodDateTime()));
93              newEntry.setEndPeriodDateTime(addMonthToDate(calendarEntries.getEndPeriodDateTime()));
94              newEntry.setBatchInitiateDate(new java.sql.Date(addMonthToDate(calendarEntries.getBatchInitiateDate()).getTime()));
95              newEntry.setBatchEndPayPeriodDate(new java.sql.Date(addMonthToDate(calendarEntries.getBatchEndPayPeriodDate()).getTime()));
96              newEntry.setBatchEmployeeApprovalDate(new java.sql.Date(addMonthToDate(calendarEntries.getBatchEmployeeApprovalDate()).getTime()));
97              newEntry.setBatchSupervisorApprovalDate(new java.sql.Date(addMonthToDate(calendarEntries.getBatchSupervisorApprovalDate()).getTime()));
98          } else if (CalendarEntryPeriodType.SEMI_MONTHLY.equals(type)) {
99              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 }