View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.calendar.service;
17  
18  import java.util.ArrayList;
19  import java.util.Date;
20  import java.util.List;
21  
22  import org.apache.commons.lang3.StringUtils;
23  import org.kuali.hr.job.Job;
24  import org.kuali.hr.lm.LMConstants;
25  import org.kuali.hr.time.calendar.Calendar;
26  import org.kuali.hr.time.calendar.CalendarEntries;
27  import org.kuali.hr.time.calendar.dao.CalendarDao;
28  import org.kuali.hr.time.paytype.PayType;
29  import org.kuali.hr.time.principal.PrincipalHRAttributes;
30  import org.kuali.hr.time.service.base.TkServiceLocator;
31  import org.kuali.hr.time.util.TkConstants;
32  
33  public class CalendarServiceImpl implements CalendarService {
34  
35  	private CalendarDao calendarDao;
36  
37  	public void setCalendarDao(CalendarDao calendarDao) {
38  		this.calendarDao = calendarDao;
39  	}
40  
41  	@Override
42  	public Calendar getCalendar(String hrCalendarId) {
43  		return calendarDao.getCalendar(hrCalendarId);
44  	}
45  
46  	@Override
47  	public Calendar getCalendarByGroup(String calendarName) {
48  		return calendarDao.getCalendarByGroup(calendarName);
49  	}
50  
51      @Override
52      public CalendarEntries getCalendarDatesByPayEndDate(String principalId, Date payEndDate, String calendarType) {
53          PrincipalHRAttributes principalCalendar = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, payEndDate);
54          
55          Calendar calendar = null;
56          if (StringUtils.equalsIgnoreCase(calendarType, TkConstants.PAY_CALENDAR_TYPE)) {
57          	calendar = getCalendarByGroup(principalCalendar.getPayCalendar());
58          } else if (StringUtils.equalsIgnoreCase(calendarType, LMConstants.LEAVE_CALENDAR_TYPE)) {
59          	calendar = getCalendarByGroup(principalCalendar.getLeaveCalendar());
60          }
61          
62          CalendarEntries calendarEntry = TkServiceLocator.getCalendarEntriesService().getCalendarEntriesByIdAndPeriodEndDate(calendar.getHrCalendarId(), payEndDate);
63          calendarEntry.setCalendarObj(calendar);
64          
65          return calendarEntry;
66      }
67  
68  	@Override
69  	public CalendarEntries getCurrentCalendarDates(String principalId, Date currentDate) {
70  		CalendarEntries pcd = null;
71          Calendar calendar = getCalendarByPrincipalIdAndDate(principalId, currentDate, false);
72          if(calendar != null) {
73  		    pcd = TkServiceLocator.getCalendarEntriesService().getCurrentCalendarEntriesByCalendarId(calendar.getHrCalendarId(), currentDate);
74  		    if(pcd != null) {
75  		    	pcd.setCalendarObj(calendar);
76  		    }
77          }
78  		return pcd;
79  	}
80  
81      @Override
82  	public CalendarEntries getPreviousCalendarEntry(String tkCalendarId, Date beginDateCurrentCalendar){
83  		return calendarDao.getPreviousCalendarEntry(tkCalendarId, beginDateCurrentCalendar);
84  	}
85  
86  	@Override
87  	public Calendar getCalendarByPrincipalIdAndDate(String principalId, Date asOfDate, boolean findLeaveCal) {
88  		Calendar pcal = null;
89          List<Job> currentJobs = TkServiceLocator.getJobService().getJobs(principalId, asOfDate);
90          if(currentJobs.size() < 1){
91             return pcal;
92          }
93          Job job = currentJobs.get(0);
94          if (principalId == null || job == null) {
95              return pcal;
96          } else {
97              PayType payType = job.getPayTypeObj();
98              if (payType == null)  {
99                  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 }