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            CalendarEntries calendarEntry = TkServiceLocator.getCalendarEntriesService().getCalendarEntriesByIdAndPeriodEndDate(calendar.getHrCalendarId(), payEndDate);
067            
068            if(ObjectUtils.isNotNull(calendarEntry))
069                    calendarEntry.setCalendarObj(calendar);
070            
071            return calendarEntry;
072        }
073    
074            @Override
075            public CalendarEntries getCurrentCalendarDates(String principalId, Date currentDate) {
076                    CalendarEntries pcd = null;
077            Calendar calendar = getCalendarByPrincipalIdAndDate(principalId, currentDate, false);
078            if(calendar != null) {
079                        pcd = TkServiceLocator.getCalendarEntriesService().getCurrentCalendarEntriesByCalendarId(calendar.getHrCalendarId(), currentDate);
080                        if(pcd != null) {
081                            pcd.setCalendarObj(calendar);
082                        }
083            }
084                    return pcd;
085            }
086    
087        @Override
088        public CalendarEntries getCurrentCalendarDates(String principalId, Date beginDate, Date endDate) {
089            CalendarEntries pcd = null;
090            Calendar calendar = getCalendarByPrincipalIdAndDate(principalId, beginDate, endDate, false);
091            if(calendar != null) {
092                pcd = TkServiceLocator.getCalendarEntriesService().getCalendarEntriesByCalendarIdAndDateRange(calendar.getHrCalendarId(), beginDate, endDate);
093                if(pcd != null) {
094                    pcd.setCalendarObj(calendar);
095                }
096            }
097            return pcd;
098        }
099    
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                    //throw new RuntimeException("No principal hr attribute setup for "+principalId);
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    }