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  import org.kuali.hr.time.detail.web.ActionFormUtils;
27  import org.kuali.hr.time.service.base.TkServiceLocator;
28  import org.kuali.hr.time.util.TKUtils;
29  import org.kuali.rice.krad.service.KRADServiceLocator;
30  
31  public class CalendarEntriesServiceImpl implements CalendarEntriesService {
32  
33      private CalendarEntriesDao calendarEntriesDao;
34  
35      public void setCalendarEntriesDao(CalendarEntriesDao calendarEntriesDao) {
36          this.calendarEntriesDao = calendarEntriesDao;
37      }
38  
39      public CalendarEntries getCalendarEntries(String hrCalendarEntriesId) {
40  
41          return calendarEntriesDao.getCalendarEntries(hrCalendarEntriesId);
42      }
43  
44      @Override
45      public CalendarEntries getCalendarEntriesByIdAndPeriodEndDate(String hrCalendarId, Date endPeriodDate) {
46          return calendarEntriesDao.getCalendarEntriesByIdAndPeriodEndDate(hrCalendarId, endPeriodDate);
47      }
48  
49      @Override
50      public CalendarEntries getCalendarEntriesByCalendarIdAndDateRange(
51              String hrCalendarId, Date beginDate, Date endDate) {
52          return calendarEntriesDao.getCalendarEntriesByCalendarIdAndDateRange(hrCalendarId, beginDate, endDate);
53      }
54  
55      @Override
56      public CalendarEntries getCurrentCalendarEntriesByCalendarId(
57              String hrCalendarId, Date currentDate) {
58          return calendarEntriesDao.getCurrentCalendarEntriesByCalendarId(hrCalendarId, currentDate);
59      }
60  
61      @Override
62      public CalendarEntries getPreviousCalendarEntriesByCalendarId(String hrCalendarId, CalendarEntries pce) {
63          return calendarEntriesDao.getPreviousCalendarEntriesByCalendarId(hrCalendarId, pce);
64      }
65  
66      @Override
67      public CalendarEntries getNextCalendarEntriesByCalendarId(String hrCalendarId, CalendarEntries pce) {
68          return calendarEntriesDao.getNextCalendarEntriesByCalendarId(hrCalendarId, pce);
69      }
70  
71      public List<CalendarEntries> getCurrentCalendarEntryNeedsScheduled(int thresholdDays, Date asOfDate) {
72          return calendarEntriesDao.getCurrentCalendarEntryNeedsScheduled(thresholdDays, asOfDate);
73      }
74  
75      @Override
76      public CalendarEntries createNextCalendarEntry(CalendarEntries calendarEntries, CalendarEntryPeriodType type) {
77          CalendarEntries newEntry = new CalendarEntries();
78          newEntry.setCalendarName(calendarEntries.getCalendarName());
79          newEntry.setHrCalendarId(calendarEntries.getHrCalendarId());
80          newEntry.setCalendarObj(calendarEntries.getCalendarObj());
81          
82          if (type == null) {
83              type = CalendarEntryPeriodType.BI_WEEKLY;
84          }
85          if (CalendarEntryPeriodType.WEEKLY.equals(type)
86                  || CalendarEntryPeriodType.BI_WEEKLY.equals(type)) {
87              int weekly_multiplier = 2;
88              if (CalendarEntryPeriodType.WEEKLY.equals(type)) {
89                  weekly_multiplier = 1;
90              }
91              newEntry.setBeginPeriodDateTime(DateUtils.addWeeks(calendarEntries.getBeginPeriodDateTime(), weekly_multiplier));
92              newEntry.setEndPeriodDateTime(DateUtils.addWeeks(calendarEntries.getEndPeriodDateTime(), weekly_multiplier));
93              newEntry.setBatchInitiateDateTime(DateUtils.addWeeks(calendarEntries.getBatchInitiateDateTime(), weekly_multiplier));
94              newEntry.setBatchEndPayPeriodDateTime(DateUtils.addWeeks(calendarEntries.getBatchEndPayPeriodDateTime(), weekly_multiplier));
95              newEntry.setBatchEmployeeApprovalDateTime(DateUtils.addWeeks(calendarEntries.getBatchEmployeeApprovalDateTime(), weekly_multiplier));
96              newEntry.setBatchSupervisorApprovalDateTime(DateUtils.addWeeks(calendarEntries.getBatchSupervisorApprovalDateTime(), weekly_multiplier));
97          } else if (CalendarEntryPeriodType.MONTHLY.equals(type)) {
98              newEntry.setBeginPeriodDateTime(addMonthToDate(calendarEntries.getBeginPeriodDateTime()));
99              newEntry.setEndPeriodDateTime(addMonthToDate(calendarEntries.getEndPeriodDateTime()));
100             newEntry.setBatchInitiateDateTime(addMonthToDate(calendarEntries.getBatchInitiateDateTime()));
101             newEntry.setBatchEndPayPeriodDateTime(addMonthToDate(calendarEntries.getBatchEndPayPeriodDateTime()));
102             newEntry.setBatchEmployeeApprovalDateTime(addMonthToDate(calendarEntries.getBatchEmployeeApprovalDateTime()));
103             newEntry.setBatchSupervisorApprovalDateTime(addMonthToDate(calendarEntries.getBatchSupervisorApprovalDateTime()));
104         } else if (CalendarEntryPeriodType.SEMI_MONTHLY.equals(type)) {
105             newEntry.setBeginPeriodDateTime(addSemiMonthToDate(calendarEntries.getBeginPeriodDateTime()));
106             newEntry.setEndPeriodDateTime(addSemiMonthToDate(calendarEntries.getEndPeriodDateTime()));
107             newEntry.setBatchInitiateDateTime(addSemiMonthToDate(calendarEntries.getBatchInitiateDateTime()));
108             newEntry.setBatchEndPayPeriodDateTime(addSemiMonthToDate(calendarEntries.getBatchEndPayPeriodDateTime()));
109             newEntry.setBatchEmployeeApprovalDateTime(addSemiMonthToDate(calendarEntries.getBatchEmployeeApprovalDateTime()));
110             newEntry.setBatchSupervisorApprovalDateTime(addSemiMonthToDate(calendarEntries.getBatchSupervisorApprovalDateTime()));
111         }
112         KRADServiceLocator.getBusinessObjectService().save(newEntry);
113         return getNextCalendarEntriesByCalendarId(calendarEntries.getHrCalendarId(), calendarEntries);
114     }
115 
116     private Date addMonthToDate(Date date) {
117         Calendar temp = Calendar.getInstance();
118         temp.setTime(date);
119         boolean lastDayOfMonth = temp.getActualMaximum(Calendar.DATE) == temp.get(Calendar.DATE);
120 
121         date = DateUtils.addMonths(date, 1);
122         if (lastDayOfMonth) {
123             temp.setTime(date);
124             temp.set(Calendar.DATE, temp.getActualMaximum(Calendar.DATE));
125             date = temp.getTime();
126         }
127         return date;
128     }
129 
130     private Date addSemiMonthToDate(Date date) {
131         //so assuming the common pairs of this are the 1st & 16th, and then 15th and the last day,
132         // and 14th with the last day minus 1
133         //so we'll peek at the current date and try to figure out the best guesses for addition.
134         Calendar temp = Calendar.getInstance();
135         temp.setTime(date);
136         if (temp.getActualMaximum(Calendar.DATE) == temp.get(Calendar.DATE)) {
137             //date is on the last day of the month.  Set next date to the 15th
138             date = DateUtils.addMonths(date, 1);
139             temp.setTime(date);
140             temp.set(Calendar.DATE, 15);
141         } else if (temp.get(Calendar.DATE) == 15) {
142             //we are on the 15th of the month, so now lets go to the end of the month
143             temp.setTime(date);
144             temp.set(Calendar.DATE,  temp.getActualMaximum(Calendar.DATE));
145         } else if (temp.get(Calendar.DATE) == 1) {
146             //first of the month, next would be 16
147             temp.setTime(date);
148             temp.set(Calendar.DATE,  16);
149         } else if (temp.get(Calendar.DATE) == 16) {
150             //16th, so add a month and set day to '1'
151             date = DateUtils.addMonths(date, 1);
152             temp.setTime(date);
153             temp.set(Calendar.DATE, 1);
154         } else if (temp.get(Calendar.DATE) == 14) {
155             //14th day, set next one to last day minus 1
156             temp.setTime(date);
157             temp.set(Calendar.DATE,  temp.getActualMaximum(Calendar.DATE) - 1);
158         } else if (temp.getActualMaximum(Calendar.DATE) == temp.get(Calendar.DATE) - 1) {
159             //date is on the second to last day of the month.  Set next date to the 14th
160             date = DateUtils.addMonths(date, 1);
161             temp.setTime(date);
162             temp.set(Calendar.DATE, 14);
163         } else {
164             // so it isn't one of the common dates... i guess we'll just add 15 days...
165             date = DateUtils.addDays(date, 15);
166             temp.setTime(date);
167         }
168 
169         return temp.getTime() ;
170     }
171 
172     public List<CalendarEntries> getFutureCalendarEntries(String hrCalendarId, Date currentDate, int numberOfEntries) {
173         List<CalendarEntries> calendarEntries = null;
174         calendarEntries = calendarEntriesDao.getFutureCalendarEntries(hrCalendarId, currentDate, numberOfEntries);
175         return calendarEntries;
176     }
177 
178     public List<CalendarEntries> getCalendarEntriesEndingBetweenBeginAndEndDate(String hrCalendarId, Date beginDate, Date endDate) {
179         return calendarEntriesDao.getCalendarEntriesEndingBetweenBeginAndEndDate(hrCalendarId, beginDate, endDate);
180     }
181     
182     public List<CalendarEntries> getAllCalendarEntriesForCalendarId(String hrCalendarId) {
183     	return calendarEntriesDao.getAllCalendarEntriesForCalendarId(hrCalendarId);
184     }
185     
186     public List<CalendarEntries> getAllCalendarEntriesForCalendarIdAndYear(String hrCalendarId, String year) {
187     	return calendarEntriesDao.getAllCalendarEntriesForCalendarIdAndYear(hrCalendarId, year);
188     }
189     
190     public List<CalendarEntries> getAllCalendarEntriesForCalendarIdUpToPlanningMonths(String hrCalendarId, String principalId) {
191     	int planningMonths = ActionFormUtils.getPlanningMonthsForEmployee(principalId);
192     	List<CalendarEntries> futureCalEntries = TkServiceLocator.getCalendarEntriesService().getFutureCalendarEntries(
193     			hrCalendarId,TKUtils.getTimelessDate(null),planningMonths);
194     	CalendarEntries futureCalEntry = null;
195     	if (futureCalEntries != null && !futureCalEntries.isEmpty()) {
196     		futureCalEntry = futureCalEntries.get(futureCalEntries.size() - 1);
197     	}
198     	Date cutOffTime;
199     	if(futureCalEntry != null) {
200     		cutOffTime = futureCalEntry.getEndPeriodDateTime();
201     	} else {
202 	    	CalendarEntries currentCE = TkServiceLocator.getCalendarEntriesService().getCurrentCalendarEntriesByCalendarId(hrCalendarId, TKUtils.getCurrentDate());
203 	    	cutOffTime = currentCE.getEndPeriodDateTime();    	
204     	}
205     	return TkServiceLocator.getCalendarEntriesService().getAllCalendarEntriesForCalendarIdUpToCutOffTime(hrCalendarId, cutOffTime);
206     }
207     
208     public List<CalendarEntries> getAllCalendarEntriesForCalendarIdUpToCutOffTime(String hrCalendarId, Date cutOffTime) {
209     	return calendarEntriesDao.getAllCalendarEntriesForCalendarIdUpToCutOffTime(hrCalendarId, cutOffTime);
210     }
211 }