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
27 public class CalendarEntriesServiceImpl implements CalendarEntriesService {
28
29 private CalendarEntriesDao calendarEntriesDao;
30
31 public void setCalendarEntriesDao(CalendarEntriesDao calendarEntriesDao) {
32 this.calendarEntriesDao = calendarEntriesDao;
33 }
34
35 public CalendarEntries getCalendarEntries(String hrCalendarEntriesId) {
36
37 return calendarEntriesDao.getCalendarEntries(hrCalendarEntriesId);
38 }
39
40 @Override
41 public CalendarEntries getCalendarEntriesByIdAndPeriodEndDate(String hrCalendarId, Date endPeriodDate) {
42 return calendarEntriesDao.getCalendarEntriesByIdAndPeriodEndDate(hrCalendarId, endPeriodDate);
43 }
44
45 @Override
46 public CalendarEntries getCurrentCalendarEntriesByCalendarId(
47 String hrCalendarId, Date currentDate) {
48 return calendarEntriesDao.getCurrentCalendarEntriesByCalendarId(hrCalendarId, currentDate);
49 }
50
51 @Override
52 public CalendarEntries getPreviousCalendarEntriesByCalendarId(String hrCalendarId, CalendarEntries pce) {
53 return calendarEntriesDao.getPreviousCalendarEntriesByCalendarId(hrCalendarId, pce);
54 }
55
56 @Override
57 public CalendarEntries getNextCalendarEntriesByCalendarId(String hrCalendarId, CalendarEntries pce) {
58 return calendarEntriesDao.getNextCalendarEntriesByCalendarId(hrCalendarId, pce);
59 }
60
61 public List<CalendarEntries> getCurrentCalendarEntryNeedsScheduled(int thresholdDays, Date asOfDate) {
62 return calendarEntriesDao.getCurrentCalendarEntryNeedsScheduled(thresholdDays, asOfDate);
63 }
64
65 @Override
66 public CalendarEntries createNextCalendarEntry(CalendarEntries calendarEntries, CalendarEntryPeriodType type) {
67 CalendarEntries newEntry = new CalendarEntries();
68 newEntry.setCalendarName(calendarEntries.getCalendarName());
69 newEntry.setHrCalendarId(calendarEntries.getHrCalendarId());
70 newEntry.setCalendarObj(calendarEntries.getCalendarObj());
71 newEntry.setBatchInitiateTime(calendarEntries.getBatchInitiateTime());
72 newEntry.setBatchEndPayPeriodTime(calendarEntries.getBatchEndPayPeriodTime());
73 newEntry.setBatchEmployeeApprovalTime(calendarEntries.getBatchEmployeeApprovalTime());
74 newEntry.setBatchSupervisorApprovalTime(calendarEntries.getBatchSupervisorApprovalTime());
75
76 if (type == null) {
77 type = CalendarEntryPeriodType.BI_WEEKLY;
78 }
79 if (CalendarEntryPeriodType.WEEKLY.equals(type)
80 || CalendarEntryPeriodType.BI_WEEKLY.equals(type)) {
81 int weekly_multiplier = 2;
82 if (CalendarEntryPeriodType.WEEKLY.equals(type)) {
83 weekly_multiplier = 1;
84 }
85 newEntry.setBeginPeriodDateTime(DateUtils.addWeeks(calendarEntries.getBeginPeriodDateTime(), weekly_multiplier));
86 newEntry.setEndPeriodDateTime(DateUtils.addWeeks(calendarEntries.getEndPeriodDateTime(), weekly_multiplier));
87 newEntry.setBatchInitiateDate(new java.sql.Date(DateUtils.addWeeks(calendarEntries.getBatchInitiateDate(), weekly_multiplier).getTime()));
88 newEntry.setBatchEndPayPeriodDate(new java.sql.Date(DateUtils.addWeeks(calendarEntries.getBatchEndPayPeriodDate(), weekly_multiplier).getTime()));
89 newEntry.setBatchEmployeeApprovalDate(new java.sql.Date(DateUtils.addWeeks(calendarEntries.getBatchEmployeeApprovalDate(), weekly_multiplier).getTime()));
90 newEntry.setBatchSupervisorApprovalDate(new java.sql.Date(DateUtils.addWeeks(calendarEntries.getBatchSupervisorApprovalDate(), weekly_multiplier).getTime()));
91 } else if (CalendarEntryPeriodType.MONTHLY.equals(type)) {
92 newEntry.setBeginPeriodDateTime(addMonthToDate(calendarEntries.getBeginPeriodDateTime()));
93 newEntry.setEndPeriodDateTime(addMonthToDate(calendarEntries.getEndPeriodDateTime()));
94 newEntry.setBatchInitiateDate(new java.sql.Date(addMonthToDate(calendarEntries.getBatchInitiateDate()).getTime()));
95 newEntry.setBatchEndPayPeriodDate(new java.sql.Date(addMonthToDate(calendarEntries.getBatchEndPayPeriodDate()).getTime()));
96 newEntry.setBatchEmployeeApprovalDate(new java.sql.Date(addMonthToDate(calendarEntries.getBatchEmployeeApprovalDate()).getTime()));
97 newEntry.setBatchSupervisorApprovalDate(new java.sql.Date(addMonthToDate(calendarEntries.getBatchSupervisorApprovalDate()).getTime()));
98 } else if (CalendarEntryPeriodType.SEMI_MONTHLY.equals(type)) {
99 newEntry.setBeginPeriodDateTime(addSemiMonthToDate(calendarEntries.getBeginPeriodDateTime()));
100 newEntry.setEndPeriodDateTime(addSemiMonthToDate(calendarEntries.getEndPeriodDateTime()));
101 newEntry.setBatchInitiateDate(new java.sql.Date(addSemiMonthToDate(calendarEntries.getBatchInitiateDate()).getTime()));
102 newEntry.setBatchEndPayPeriodDate(new java.sql.Date(addSemiMonthToDate(calendarEntries.getBatchEndPayPeriodDate()).getTime()));
103 newEntry.setBatchEmployeeApprovalDate(new java.sql.Date(addSemiMonthToDate(calendarEntries.getBatchEmployeeApprovalDate()).getTime()));
104 newEntry.setBatchSupervisorApprovalDate(new java.sql.Date(addSemiMonthToDate(calendarEntries.getBatchSupervisorApprovalDate()).getTime()));
105 }
106 calendarEntriesDao.saveOrUpdate(newEntry);
107 return getNextCalendarEntriesByCalendarId(calendarEntries.getHrCalendarId(), calendarEntries);
108 }
109
110 private Date addMonthToDate(Date date) {
111 Calendar temp = Calendar.getInstance();
112 temp.setTime(date);
113 boolean lastDayOfMonth = temp.getActualMaximum(Calendar.DATE) == temp.get(Calendar.DATE);
114
115 date = DateUtils.addMonths(date, 1);
116 if (lastDayOfMonth) {
117 temp.setTime(date);
118 temp.set(Calendar.DATE, temp.getActualMaximum(Calendar.DATE));
119 date = temp.getTime();
120 }
121 return date;
122 }
123
124 private Date addSemiMonthToDate(Date date) {
125
126
127
128 Calendar temp = Calendar.getInstance();
129 temp.setTime(date);
130 if (temp.getActualMaximum(Calendar.DATE) == temp.get(Calendar.DATE)) {
131
132 date = DateUtils.addMonths(date, 1);
133 temp.setTime(date);
134 temp.set(Calendar.DATE, 15);
135 } else if (temp.get(Calendar.DATE) == 15) {
136
137 temp.setTime(date);
138 temp.set(Calendar.DATE, temp.getActualMaximum(Calendar.DATE));
139 } else if (temp.get(Calendar.DATE) == 1) {
140
141 temp.setTime(date);
142 temp.set(Calendar.DATE, 16);
143 } else if (temp.get(Calendar.DATE) == 16) {
144
145 date = DateUtils.addMonths(date, 1);
146 temp.setTime(date);
147 temp.set(Calendar.DATE, 1);
148 } else if (temp.get(Calendar.DATE) == 14) {
149
150 temp.setTime(date);
151 temp.set(Calendar.DATE, temp.getActualMaximum(Calendar.DATE) - 1);
152 } else if (temp.getActualMaximum(Calendar.DATE) == temp.get(Calendar.DATE) - 1) {
153
154 date = DateUtils.addMonths(date, 1);
155 temp.setTime(date);
156 temp.set(Calendar.DATE, 14);
157 } else {
158
159 date = DateUtils.addDays(date, 15);
160 temp.setTime(date);
161 }
162
163 return temp.getTime() ;
164 }
165
166 public List<CalendarEntries> getFutureCalendarEntries(String hrCalendarId, Date currentDate, int numberOfEntries) {
167 List<CalendarEntries> calendarEntries = null;
168 calendarEntries = calendarEntriesDao.getFutureCalendarEntries(hrCalendarId, currentDate, numberOfEntries);
169 return calendarEntries;
170 }
171
172 public CalendarEntries getCalendarEntriesByBeginAndEndDate(Date beginPeriodDate, Date endPeriodDate) {
173 return calendarEntriesDao.getCalendarEntriesByBeginAndEndDate(beginPeriodDate, endPeriodDate);
174 }
175
176 public List<CalendarEntries> getAllCalendarEntriesForCalendarId(String hrCalendarId) {
177 return calendarEntriesDao.getAllCalendarEntriesForCalendarId(hrCalendarId);
178 }
179
180 public List<CalendarEntries> getAllCalendarEntriesForCalendarIdAndYear(String hrCalendarId, String year) {
181 return calendarEntriesDao.getAllCalendarEntriesForCalendarIdAndYear(hrCalendarId, year);
182 }
183 }