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.Date;
19 import java.util.List;
20
21 import org.apache.commons.lang3.StringUtils;
22 import org.kuali.hr.job.Job;
23 import org.kuali.hr.lm.LMConstants;
24 import org.kuali.hr.time.calendar.Calendar;
25 import org.kuali.hr.time.calendar.CalendarEntries;
26 import org.kuali.hr.time.calendar.dao.CalendarDao;
27 import org.kuali.hr.time.paytype.PayType;
28 import org.kuali.hr.time.principal.PrincipalHRAttributes;
29 import org.kuali.hr.time.service.base.TkServiceLocator;
30 import org.kuali.hr.time.util.TKUtils;
31 import org.kuali.hr.time.util.TkConstants;
32 import org.kuali.rice.krad.util.ObjectUtils;
33
34 public class CalendarServiceImpl implements CalendarService {
35
36 private CalendarDao calendarDao;
37
38 public void setCalendarDao(CalendarDao calendarDao) {
39 this.calendarDao = calendarDao;
40 }
41
42 @Override
43 public Calendar getCalendar(String hrCalendarId) {
44 return calendarDao.getCalendar(hrCalendarId);
45 }
46
47 @Override
48 public Calendar getCalendarByGroup(String calendarName) {
49 return calendarDao.getCalendarByGroup(calendarName);
50 }
51
52 @Override
53 public CalendarEntries getCalendarDatesByPayEndDate(String principalId, Date payEndDate, String calendarType) {
54 PrincipalHRAttributes principalCalendar = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, payEndDate);
55
56 Calendar calendar = null;
57 if(ObjectUtils.isNull(principalCalendar)) {
58 return null;
59 }
60 if (StringUtils.equalsIgnoreCase(calendarType, TkConstants.PAY_CALENDAR_TYPE)) {
61 calendar = getCalendarByGroup(principalCalendar.getPayCalendar());
62 } else if (StringUtils.equalsIgnoreCase(calendarType, LMConstants.LEAVE_CALENDAR_TYPE)) {
63 calendar = getCalendarByGroup(principalCalendar.getLeaveCalendar());
64 }
65
66 if (calendar == null) {
67 return null;
68 }
69 CalendarEntries calendarEntry = TkServiceLocator.getCalendarEntriesService().getCalendarEntriesByIdAndPeriodEndDate(calendar.getHrCalendarId(), payEndDate);
70
71 if(ObjectUtils.isNotNull(calendarEntry)) {
72 calendarEntry.setCalendarObj(calendar);
73 }
74
75 return calendarEntry;
76 }
77
78 @Override
79 public CalendarEntries getCurrentCalendarDates(String principalId, Date currentDate) {
80 CalendarEntries pcd = null;
81 Calendar calendar = getCalendarByPrincipalIdAndDate(principalId, currentDate, false);
82 if(calendar != null) {
83 pcd = TkServiceLocator.getCalendarEntriesService().getCurrentCalendarEntriesByCalendarId(calendar.getHrCalendarId(), currentDate);
84 if(pcd != null) {
85 pcd.setCalendarObj(calendar);
86 }
87 }
88 return pcd;
89 }
90
91 @Override
92 public CalendarEntries getCurrentCalendarDates(String principalId, Date beginDate, Date endDate) {
93 CalendarEntries pcd = null;
94 Calendar calendar = getCalendarByPrincipalIdAndDate(principalId, beginDate, endDate, false);
95 if(calendar != null) {
96 pcd = TkServiceLocator.getCalendarEntriesService().getCalendarEntriesByCalendarIdAndDateRange(calendar.getHrCalendarId(), beginDate, endDate);
97 if(pcd != null) {
98 pcd.setCalendarObj(calendar);
99 }
100 }
101 return pcd;
102 }
103
104 @Override
105 public CalendarEntries getPreviousCalendarEntry(String tkCalendarId, Date beginDateCurrentCalendar){
106 return calendarDao.getPreviousCalendarEntry(tkCalendarId, beginDateCurrentCalendar);
107 }
108
109 @Override
110 public Calendar getCalendarByPrincipalIdAndDate(String principalId, Date beginDate, Date endDate, boolean findLeaveCal) {
111 Calendar pcal = null;
112 List<Job> currentJobs = TkServiceLocator.getJobService().getJobs(principalId, endDate);
113 if(currentJobs.size() < 1){
114 return pcal;
115 }
116 Job job = currentJobs.get(0);
117 if (principalId == null || job == null) {
118 return pcal;
119 } else {
120 PayType payType = job.getPayTypeObj();
121 if (payType == null) {
122 throw new RuntimeException("No paytype setup for "+principalId + " job number: "+job.getJobNumber());
123 }
124
125 PrincipalHRAttributes principalCalendar = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, beginDate);
126 if(principalCalendar == null){
127 return null;
128
129 }
130 if(!findLeaveCal) {
131 pcal = principalCalendar.getCalendar();
132 if (pcal == null){
133 pcal = principalCalendar.getLeaveCalObj();
134 if(pcal == null){
135 return pcal;
136 }
137 }
138 } else {
139 pcal = principalCalendar.getLeaveCalObj();
140 if(pcal == null){
141 return pcal;
142 }
143 }
144 }
145
146 return pcal;
147 }
148
149 @Override
150 public Calendar getCalendarByPrincipalIdAndDate(String principalId, Date asOfDate, boolean findLeaveCal) {
151 Calendar pcal = null;
152 List<Job> currentJobs = TkServiceLocator.getJobService().getJobs(principalId, asOfDate);
153 if(currentJobs.size() < 1){
154 return pcal;
155 }
156 Job job = currentJobs.get(0);
157 if (principalId == null || job == null) {
158 return pcal;
159 } else {
160 PayType payType = job.getPayTypeObj();
161 if (payType == null) {
162 throw new RuntimeException("No paytype setup for "+principalId + " job number: "+job.getJobNumber());
163 }
164
165 PrincipalHRAttributes principalCalendar = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, asOfDate);
166 if(principalCalendar == null){
167 throw new RuntimeException("No principal hr attribute setup for "+principalId);
168 }
169 if(!findLeaveCal) {
170 pcal = principalCalendar.getCalendar();
171 if (pcal == null){
172 pcal = principalCalendar.getLeaveCalObj();
173 if(pcal == null){
174 return pcal;
175 }
176 }
177 } else {
178 pcal = principalCalendar.getLeaveCalObj();
179 if(pcal == null){
180 return pcal;
181 }
182 }
183 }
184
185 return pcal;
186 }
187
188 @Override
189 public CalendarEntries getCurrentCalendarDatesForLeaveCalendar(
190 String principalId, Date currentDate) {
191 CalendarEntries pcd = null;
192 Calendar calendar = getCalendarByPrincipalIdAndDate(principalId, currentDate, true);
193 if(calendar != null) {
194 pcd = TkServiceLocator.getCalendarEntriesService().getCurrentCalendarEntriesByCalendarId(calendar.getHrCalendarId(), currentDate);
195 if(pcd != null) {
196 pcd.setCalendarObj(calendar);
197 }
198 }
199 return pcd;
200 }
201
202 @Override
203 public CalendarEntries getCurrentCalendarDatesForLeaveCalendar(String principalId, Date beginDate, Date endDate) {
204 CalendarEntries pcd = null;
205 Calendar calendar = getCalendarByPrincipalIdAndDate(principalId, beginDate, endDate, true);
206 if(calendar != null) {
207 pcd = TkServiceLocator.getCalendarEntriesService().getCalendarEntriesByCalendarIdAndDateRange(calendar.getHrCalendarId(), beginDate, endDate);
208 if(pcd != null) {
209 pcd.setCalendarObj(calendar);
210 }
211 }
212 return pcd;
213 }
214
215 @Override
216 public List<Calendar> getCalendars(String calendarName, String calendarTypes, String flsaBeginDay, String flsaBeginTime) {
217 return calendarDao.getCalendars(calendarName, calendarTypes, flsaBeginDay, flsaBeginTime);
218 }
219
220 }