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