1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
42
43
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
49 Interval workScheduleInterval = new Interval(workScheduleEffectiveDate,endWorkScheduleEffectiveDate);
50
51 if(payPeriodInterval.isBefore(workScheduleInterval)){
52 return lstWorkScheduleEntries;
53 }
54
55
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
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
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 }