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.ArrayList;
019    import java.util.Date;
020    import java.util.List;
021    
022    import org.apache.commons.lang3.StringUtils;
023    import org.kuali.hr.job.Job;
024    import org.kuali.hr.lm.LMConstants;
025    import org.kuali.hr.time.calendar.Calendar;
026    import org.kuali.hr.time.calendar.CalendarEntries;
027    import org.kuali.hr.time.calendar.dao.CalendarDao;
028    import org.kuali.hr.time.paytype.PayType;
029    import org.kuali.hr.time.principal.PrincipalHRAttributes;
030    import org.kuali.hr.time.service.base.TkServiceLocator;
031    import org.kuali.hr.time.util.TkConstants;
032    
033    public class CalendarServiceImpl implements CalendarService {
034    
035            private CalendarDao calendarDao;
036    
037            public void setCalendarDao(CalendarDao calendarDao) {
038                    this.calendarDao = calendarDao;
039            }
040    
041            @Override
042            public Calendar getCalendar(String hrCalendarId) {
043                    return calendarDao.getCalendar(hrCalendarId);
044            }
045    
046            @Override
047            public Calendar getCalendarByGroup(String calendarName) {
048                    return calendarDao.getCalendarByGroup(calendarName);
049            }
050    
051        @Override
052        public CalendarEntries getCalendarDatesByPayEndDate(String principalId, Date payEndDate, String calendarType) {
053            PrincipalHRAttributes principalCalendar = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, payEndDate);
054            
055            Calendar calendar = null;
056            if (StringUtils.equalsIgnoreCase(calendarType, TkConstants.PAY_CALENDAR_TYPE)) {
057                    calendar = getCalendarByGroup(principalCalendar.getPayCalendar());
058            } else if (StringUtils.equalsIgnoreCase(calendarType, LMConstants.LEAVE_CALENDAR_TYPE)) {
059                    calendar = getCalendarByGroup(principalCalendar.getLeaveCalendar());
060            }
061            
062            CalendarEntries calendarEntry = TkServiceLocator.getCalendarEntriesService().getCalendarEntriesByIdAndPeriodEndDate(calendar.getHrCalendarId(), payEndDate);
063            calendarEntry.setCalendarObj(calendar);
064            
065            return calendarEntry;
066        }
067    
068            @Override
069            public CalendarEntries getCurrentCalendarDates(String principalId, Date currentDate) {
070                    CalendarEntries pcd = null;
071            Calendar calendar = getCalendarByPrincipalIdAndDate(principalId, currentDate, false);
072            if(calendar != null) {
073                        pcd = TkServiceLocator.getCalendarEntriesService().getCurrentCalendarEntriesByCalendarId(calendar.getHrCalendarId(), currentDate);
074                        if(pcd != null) {
075                            pcd.setCalendarObj(calendar);
076                        }
077            }
078                    return pcd;
079            }
080    
081        @Override
082            public CalendarEntries getPreviousCalendarEntry(String tkCalendarId, Date beginDateCurrentCalendar){
083                    return calendarDao.getPreviousCalendarEntry(tkCalendarId, beginDateCurrentCalendar);
084            }
085    
086            @Override
087            public Calendar getCalendarByPrincipalIdAndDate(String principalId, Date asOfDate, boolean findLeaveCal) {
088                    Calendar pcal = null;
089            List<Job> currentJobs = TkServiceLocator.getJobService().getJobs(principalId, asOfDate);
090            if(currentJobs.size() < 1){
091               return pcal;
092            }
093            Job job = currentJobs.get(0);
094            if (principalId == null || job == null) {
095                return pcal;
096            } else {
097                PayType payType = job.getPayTypeObj();
098                if (payType == null)  {
099                    throw new RuntimeException("No paytype setup for "+principalId + " job number: "+job.getJobNumber());
100                }
101    
102                PrincipalHRAttributes principalCalendar = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, asOfDate);
103                if(principalCalendar == null){
104                    throw new RuntimeException("No principal hr attribute setup for "+principalId);
105                }
106                if(!findLeaveCal) {
107                    pcal = principalCalendar.getCalendar();
108                    if (pcal == null){
109                            pcal = principalCalendar.getLeaveCalObj();
110                            if(pcal == null){
111                                    return pcal;
112                            }
113                    }
114                } else {
115                            pcal = principalCalendar.getLeaveCalObj();
116                            if(pcal == null){
117                                    return pcal;
118                            }
119                }
120            }
121    
122            return pcal;
123            }
124    
125            @Override
126            public CalendarEntries getCurrentCalendarDatesForLeaveCalendar(
127                            String principalId, Date currentDate) {
128                    CalendarEntries pcd = null;
129            Calendar calendar = getCalendarByPrincipalIdAndDate(principalId, currentDate, true);
130            if(calendar != null) {
131                        pcd = TkServiceLocator.getCalendarEntriesService().getCurrentCalendarEntriesByCalendarId(calendar.getHrCalendarId(), currentDate);
132                        if(pcd != null) {
133                            pcd.setCalendarObj(calendar);
134                        }
135            }
136                    return pcd;
137            }
138    
139        @Override
140        public List<Calendar> getCalendars(String calendarName, String calendarTypes, String flsaBeginDay, String flsaBeginTime) {
141            return  calendarDao.getCalendars(calendarName, calendarTypes, flsaBeginDay, flsaBeginTime);
142        }
143    
144    }