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.workschedule.service;
17  
18  import java.sql.Date;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.joda.time.DateTime;
23  import org.joda.time.Interval;
24  import org.kuali.hr.time.util.TKUtils;
25  import org.kuali.hr.time.util.TkConstants;
26  import org.kuali.hr.time.workschedule.WorkSchedule;
27  import org.kuali.hr.time.workschedule.WorkScheduleEntry;
28  import org.kuali.hr.time.workschedule.dao.WorkScheduleDao;
29  
30  public class WorkScheduleServiceImpl implements WorkScheduleService {
31  
32  	private WorkScheduleDao workScheduleDao = null;
33  
34      @Override
35      public WorkSchedule getWorkSchedule(Long workSchedule, Date asOfDate) {
36          return workScheduleDao.getWorkSchedule(workSchedule, asOfDate);
37      }
38  
39      public List<WorkScheduleEntry> getWorkSchedEntries(WorkSchedule workSchedule, java.util.Date beginDateTime, java.util.Date endDateTime){
40  		List<WorkScheduleEntry> lstWorkScheduleEntries = new ArrayList<WorkScheduleEntry>();
41  		//increment to the appropriate range based on the beginDate and endDate
42  		//cycle through and return an appropriate list of workschedule entries based on
43  		//the beginDateTime, endDatetime based in
44  		DateTime workScheduleEffectiveDate = new DateTime(workSchedule.getEffectiveDate());
45  		DateTime endWorkScheduleEffectiveDate = workScheduleEffectiveDate.plusDays(TkConstants.LENGTH_OF_WORK_SCHEDULE);
46  
47  		Interval payPeriodInterval = new Interval(beginDateTime.getTime(), endDateTime.getTime());
48  		//create interval for work schedule effdt + TkConstants.LENGTH_OF_WORK_SCHEDULE days
49  		Interval workScheduleInterval = new Interval(workScheduleEffectiveDate,endWorkScheduleEffectiveDate);
50  		//if before pay period interval then return an empty list back
51  		if(payPeriodInterval.isBefore(workScheduleInterval)){
52  			return lstWorkScheduleEntries;
53  		}
54  
55  		//determine the starting index of work schedule entries for the pay period
56  		int indexOfWorkSchedule = 0;
57  		Interval workSchedGap = payPeriodInterval.gap(workScheduleInterval);
58  		if(workSchedGap!= null){
59  			int days = TKUtils.convertMillisToWholeDays(workSchedGap.getEndMillis()-workSchedGap.getStartMillis());
60  			if(days < TkConstants.LENGTH_OF_WORK_SCHEDULE){
61  				indexOfWorkSchedule = days;
62  			}
63  			//determine offset
64  			indexOfWorkSchedule = days % TkConstants.LENGTH_OF_WORK_SCHEDULE;
65  		} else{
66  			Interval workSchedOverlap = payPeriodInterval.overlap(workScheduleInterval);
67  			if(workSchedOverlap != null){
68  				int days = TKUtils.convertMillisToWholeDays(workSchedOverlap.toDurationMillis());
69  
70  				indexOfWorkSchedule = TkConstants.LENGTH_OF_WORK_SCHEDULE - days;
71  			}
72  		}
73  
74  		int numberOfDaysInPayPeriod = TKUtils.convertMillisToWholeDays(payPeriodInterval.toDurationMillis());
75  		for(int i = 0 ;i<numberOfDaysInPayPeriod;i++){
76  			indexOfWorkSchedule += i;
77  			if(indexOfWorkSchedule >= TkConstants.LENGTH_OF_WORK_SCHEDULE){
78  				indexOfWorkSchedule = 0;
79  			}
80  			//this assumes sorted by index of day
81  			lstWorkScheduleEntries.add(workSchedule.getWorkScheduleEntries().get(indexOfWorkSchedule));
82  		}
83  		return lstWorkScheduleEntries;
84  	}
85  
86  
87  	@Override
88  	public void saveOrUpdate(WorkSchedule workSchedule) {
89  		workScheduleDao.saveOrUpdate(workSchedule);
90  	}
91  
92  	@Override
93  	public void saveOrUpdate(List<WorkSchedule> workSchedules) {
94  		workScheduleDao.saveOrUpdate(workSchedules);
95  	}
96  
97  	public void setWorkScheduleDao(WorkScheduleDao workScheduleDao) {
98  		this.workScheduleDao = workScheduleDao;
99  	}
100 
101 }