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.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 import org.kuali.hr.time.detail.web.ActionFormUtils;
027 import org.kuali.hr.time.service.base.TkServiceLocator;
028 import org.kuali.hr.time.util.TKUtils;
029 import org.kuali.rice.krad.service.KRADServiceLocator;
030
031 public class CalendarEntriesServiceImpl implements CalendarEntriesService {
032
033 private CalendarEntriesDao calendarEntriesDao;
034
035 public void setCalendarEntriesDao(CalendarEntriesDao calendarEntriesDao) {
036 this.calendarEntriesDao = calendarEntriesDao;
037 }
038
039 public CalendarEntries getCalendarEntries(String hrCalendarEntriesId) {
040
041 return calendarEntriesDao.getCalendarEntries(hrCalendarEntriesId);
042 }
043
044 @Override
045 public CalendarEntries getCalendarEntriesByIdAndPeriodEndDate(String hrCalendarId, Date endPeriodDate) {
046 return calendarEntriesDao.getCalendarEntriesByIdAndPeriodEndDate(hrCalendarId, endPeriodDate);
047 }
048
049 @Override
050 public CalendarEntries getCalendarEntriesByCalendarIdAndDateRange(
051 String hrCalendarId, Date beginDate, Date endDate) {
052 return calendarEntriesDao.getCalendarEntriesByCalendarIdAndDateRange(hrCalendarId, beginDate, endDate);
053 }
054
055 @Override
056 public CalendarEntries getCurrentCalendarEntriesByCalendarId(
057 String hrCalendarId, Date currentDate) {
058 return calendarEntriesDao.getCurrentCalendarEntriesByCalendarId(hrCalendarId, currentDate);
059 }
060
061 @Override
062 public CalendarEntries getPreviousCalendarEntriesByCalendarId(String hrCalendarId, CalendarEntries pce) {
063 return calendarEntriesDao.getPreviousCalendarEntriesByCalendarId(hrCalendarId, pce);
064 }
065
066 @Override
067 public CalendarEntries getNextCalendarEntriesByCalendarId(String hrCalendarId, CalendarEntries pce) {
068 return calendarEntriesDao.getNextCalendarEntriesByCalendarId(hrCalendarId, pce);
069 }
070
071 public List<CalendarEntries> getCurrentCalendarEntryNeedsScheduled(int thresholdDays, Date asOfDate) {
072 return calendarEntriesDao.getCurrentCalendarEntryNeedsScheduled(thresholdDays, asOfDate);
073 }
074
075 @Override
076 public CalendarEntries createNextCalendarEntry(CalendarEntries calendarEntries, CalendarEntryPeriodType type) {
077 CalendarEntries newEntry = new CalendarEntries();
078 newEntry.setCalendarName(calendarEntries.getCalendarName());
079 newEntry.setHrCalendarId(calendarEntries.getHrCalendarId());
080 newEntry.setCalendarObj(calendarEntries.getCalendarObj());
081
082 if (type == null) {
083 type = CalendarEntryPeriodType.BI_WEEKLY;
084 }
085 if (CalendarEntryPeriodType.WEEKLY.equals(type)
086 || CalendarEntryPeriodType.BI_WEEKLY.equals(type)) {
087 int weekly_multiplier = 2;
088 if (CalendarEntryPeriodType.WEEKLY.equals(type)) {
089 weekly_multiplier = 1;
090 }
091 newEntry.setBeginPeriodDateTime(DateUtils.addWeeks(calendarEntries.getBeginPeriodDateTime(), weekly_multiplier));
092 newEntry.setEndPeriodDateTime(DateUtils.addWeeks(calendarEntries.getEndPeriodDateTime(), weekly_multiplier));
093 newEntry.setBatchInitiateDateTime(DateUtils.addWeeks(calendarEntries.getBatchInitiateDateTime(), weekly_multiplier));
094 newEntry.setBatchEndPayPeriodDateTime(DateUtils.addWeeks(calendarEntries.getBatchEndPayPeriodDateTime(), weekly_multiplier));
095 newEntry.setBatchEmployeeApprovalDateTime(DateUtils.addWeeks(calendarEntries.getBatchEmployeeApprovalDateTime(), weekly_multiplier));
096 newEntry.setBatchSupervisorApprovalDateTime(DateUtils.addWeeks(calendarEntries.getBatchSupervisorApprovalDateTime(), weekly_multiplier));
097 } else if (CalendarEntryPeriodType.MONTHLY.equals(type)) {
098 newEntry.setBeginPeriodDateTime(addMonthToDate(calendarEntries.getBeginPeriodDateTime()));
099 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 }