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