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 CalendarEntries calendarEntry = TkServiceLocator.getCalendarEntriesService().getCalendarEntriesByIdAndPeriodEndDate(calendar.getHrCalendarId(), payEndDate);
67
68 if(ObjectUtils.isNotNull(calendarEntry))
69 calendarEntry.setCalendarObj(calendar);
70
71 return calendarEntry;
72 }
73
74 @Override
75 public CalendarEntries getCurrentCalendarDates(String principalId, Date currentDate) {
76 CalendarEntries pcd = null;
77 Calendar calendar = getCalendarByPrincipalIdAndDate(principalId, currentDate, false);
78 if(calendar != null) {
79 pcd = TkServiceLocator.getCalendarEntriesService().getCurrentCalendarEntriesByCalendarId(calendar.getHrCalendarId(), currentDate);
80 if(pcd != null) {
81 pcd.setCalendarObj(calendar);
82 }
83 }
84 return pcd;
85 }
86
87 @Override
88 public CalendarEntries getCurrentCalendarDates(String principalId, Date beginDate, Date endDate) {
89 CalendarEntries pcd = null;
90 Calendar calendar = getCalendarByPrincipalIdAndDate(principalId, beginDate, endDate, false);
91 if(calendar != null) {
92 pcd = TkServiceLocator.getCalendarEntriesService().getCalendarEntriesByCalendarIdAndDateRange(calendar.getHrCalendarId(), beginDate, endDate);
93 if(pcd != null) {
94 pcd.setCalendarObj(calendar);
95 }
96 }
97 return pcd;
98 }
99
100 @Override
101 public CalendarEntries getPreviousCalendarEntry(String tkCalendarId, Date beginDateCurrentCalendar){
102 return calendarDao.getPreviousCalendarEntry(tkCalendarId, beginDateCurrentCalendar);
103 }
104
105 @Override
106 public Calendar getCalendarByPrincipalIdAndDate(String principalId, Date beginDate, Date endDate, boolean findLeaveCal) {
107 Calendar pcal = null;
108 List<Job> currentJobs = TkServiceLocator.getJobService().getJobs(principalId, endDate);
109 if(currentJobs.size() < 1){
110 return pcal;
111 }
112 Job job = currentJobs.get(0);
113 if (principalId == null || job == null) {
114 return pcal;
115 } else {
116 PayType payType = job.getPayTypeObj();
117 if (payType == null) {
118 throw new RuntimeException("No paytype setup for "+principalId + " job number: "+job.getJobNumber());
119 }
120
121 PrincipalHRAttributes principalCalendar = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, beginDate);
122 if(principalCalendar == null){
123 return null;
124
125 }
126 if(!findLeaveCal) {
127 pcal = principalCalendar.getCalendar();
128 if (pcal == null){
129 pcal = principalCalendar.getLeaveCalObj();
130 if(pcal == null){
131 return pcal;
132 }
133 }
134 } else {
135 pcal = principalCalendar.getLeaveCalObj();
136 if(pcal == null){
137 return pcal;
138 }
139 }
140 }
141
142 return pcal;
143 }
144
145 @Override
146 public Calendar getCalendarByPrincipalIdAndDate(String principalId, Date asOfDate, boolean findLeaveCal) {
147 Calendar pcal = null;
148 List<Job> currentJobs = TkServiceLocator.getJobService().getJobs(principalId, asOfDate);
149 if(currentJobs.size() < 1){
150 return pcal;
151 }
152 Job job = currentJobs.get(0);
153 if (principalId == null || job == null) {
154 return pcal;
155 } else {
156 PayType payType = job.getPayTypeObj();
157 if (payType == null) {
158 throw new RuntimeException("No paytype setup for "+principalId + " job number: "+job.getJobNumber());
159 }
160
161 PrincipalHRAttributes principalCalendar = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, asOfDate);
162 if(principalCalendar == null){
163 throw new RuntimeException("No principal hr attribute setup for "+principalId);
164 }
165 if(!findLeaveCal) {
166 pcal = principalCalendar.getCalendar();
167 if (pcal == null){
168 pcal = principalCalendar.getLeaveCalObj();
169 if(pcal == null){
170 return pcal;
171 }
172 }
173 } else {
174 pcal = principalCalendar.getLeaveCalObj();
175 if(pcal == null){
176 return pcal;
177 }
178 }
179 }
180
181 return pcal;
182 }
183
184 @Override
185 public CalendarEntries getCurrentCalendarDatesForLeaveCalendar(
186 String principalId, Date currentDate) {
187 CalendarEntries pcd = null;
188 Calendar calendar = getCalendarByPrincipalIdAndDate(principalId, currentDate, true);
189 if(calendar != null) {
190 pcd = TkServiceLocator.getCalendarEntriesService().getCurrentCalendarEntriesByCalendarId(calendar.getHrCalendarId(), currentDate);
191 if(pcd != null) {
192 pcd.setCalendarObj(calendar);
193 }
194 }
195 return pcd;
196 }
197
198 @Override
199 public CalendarEntries getCurrentCalendarDatesForLeaveCalendar(String principalId, Date beginDate, Date endDate) {
200 CalendarEntries pcd = null;
201 Calendar calendar = getCalendarByPrincipalIdAndDate(principalId, beginDate, endDate, true);
202 if(calendar != null) {
203 pcd = TkServiceLocator.getCalendarEntriesService().getCalendarEntriesByCalendarIdAndDateRange(calendar.getHrCalendarId(), beginDate, endDate);
204 if(pcd != null) {
205 pcd.setCalendarObj(calendar);
206 }
207 }
208 return pcd;
209 }
210
211 @Override
212 public List<Calendar> getCalendars(String calendarName, String calendarTypes, String flsaBeginDay, String flsaBeginTime) {
213 return calendarDao.getCalendars(calendarName, calendarTypes, flsaBeginDay, flsaBeginTime);
214 }
215
216 }