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