1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
132
133
134 Calendar temp = Calendar.getInstance();
135 temp.setTime(date);
136 if (temp.getActualMaximum(Calendar.DATE) == temp.get(Calendar.DATE)) {
137
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
143 temp.setTime(date);
144 temp.set(Calendar.DATE, temp.getActualMaximum(Calendar.DATE));
145 } else if (temp.get(Calendar.DATE) == 1) {
146
147 temp.setTime(date);
148 temp.set(Calendar.DATE, 16);
149 } else if (temp.get(Calendar.DATE) == 16) {
150
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
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
160 date = DateUtils.addMonths(date, 1);
161 temp.setTime(date);
162 temp.set(Calendar.DATE, 14);
163 } else {
164
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 }