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