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.Date;
019 import java.util.List;
020
021 import org.apache.commons.lang3.StringUtils;
022 import org.kuali.hr.job.Job;
023 import org.kuali.hr.lm.LMConstants;
024 import org.kuali.hr.time.calendar.Calendar;
025 import org.kuali.hr.time.calendar.CalendarEntries;
026 import org.kuali.hr.time.calendar.dao.CalendarDao;
027 import org.kuali.hr.time.paytype.PayType;
028 import org.kuali.hr.time.principal.PrincipalHRAttributes;
029 import org.kuali.hr.time.service.base.TkServiceLocator;
030 import org.kuali.hr.time.util.TKUtils;
031 import org.kuali.hr.time.util.TkConstants;
032 import org.kuali.rice.krad.util.ObjectUtils;
033
034 public class CalendarServiceImpl implements CalendarService {
035
036 private CalendarDao calendarDao;
037
038 public void setCalendarDao(CalendarDao calendarDao) {
039 this.calendarDao = calendarDao;
040 }
041
042 @Override
043 public Calendar getCalendar(String hrCalendarId) {
044 return calendarDao.getCalendar(hrCalendarId);
045 }
046
047 @Override
048 public Calendar getCalendarByGroup(String calendarName) {
049 return calendarDao.getCalendarByGroup(calendarName);
050 }
051
052 @Override
053 public CalendarEntries getCalendarDatesByPayEndDate(String principalId, Date payEndDate, String calendarType) {
054 PrincipalHRAttributes principalCalendar = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, payEndDate);
055
056 Calendar calendar = null;
057 if(ObjectUtils.isNull(principalCalendar)) {
058 return null;
059 }
060 if (StringUtils.equalsIgnoreCase(calendarType, TkConstants.PAY_CALENDAR_TYPE)) {
061 calendar = getCalendarByGroup(principalCalendar.getPayCalendar());
062 } else if (StringUtils.equalsIgnoreCase(calendarType, LMConstants.LEAVE_CALENDAR_TYPE)) {
063 calendar = getCalendarByGroup(principalCalendar.getLeaveCalendar());
064 }
065
066 if (calendar == null) {
067 return null;
068 }
069 CalendarEntries calendarEntry = TkServiceLocator.getCalendarEntriesService().getCalendarEntriesByIdAndPeriodEndDate(calendar.getHrCalendarId(), payEndDate);
070
071 if(ObjectUtils.isNotNull(calendarEntry)) {
072 calendarEntry.setCalendarObj(calendar);
073 }
074
075 return calendarEntry;
076 }
077
078 @Override
079 public CalendarEntries getCurrentCalendarDates(String principalId, Date currentDate) {
080 CalendarEntries pcd = null;
081 Calendar calendar = getCalendarByPrincipalIdAndDate(principalId, currentDate, false);
082 if(calendar != null) {
083 pcd = TkServiceLocator.getCalendarEntriesService().getCurrentCalendarEntriesByCalendarId(calendar.getHrCalendarId(), currentDate);
084 if(pcd != null) {
085 pcd.setCalendarObj(calendar);
086 }
087 }
088 return pcd;
089 }
090
091 @Override
092 public CalendarEntries getCurrentCalendarDates(String principalId, Date beginDate, Date endDate) {
093 CalendarEntries pcd = null;
094 Calendar calendar = getCalendarByPrincipalIdAndDate(principalId, beginDate, endDate, false);
095 if(calendar != null) {
096 pcd = TkServiceLocator.getCalendarEntriesService().getCalendarEntriesByCalendarIdAndDateRange(calendar.getHrCalendarId(), beginDate, endDate);
097 if(pcd != null) {
098 pcd.setCalendarObj(calendar);
099 }
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 //throw new RuntimeException("No principal hr attribute setup for "+principalId);
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 }