1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.util;
17
18 import java.sql.Time;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Comparator;
22 import java.util.List;
23
24 import org.joda.time.DateTime;
25 import org.joda.time.DateTimeZone;
26 import org.joda.time.Interval;
27 import org.joda.time.LocalDateTime;
28 import org.joda.time.LocalTime;
29 import org.kuali.hr.time.calendar.Calendar;
30 import org.kuali.hr.time.calendar.CalendarEntries;
31 import org.kuali.hr.time.flsa.FlsaDay;
32 import org.kuali.hr.time.flsa.FlsaWeek;
33 import org.kuali.hr.time.service.base.TkServiceLocator;
34 import org.kuali.hr.time.timeblock.TimeBlock;
35
36 public class TkTimeBlockAggregate {
37 public List<List<TimeBlock>> dayTimeBlockList = new ArrayList<List<TimeBlock>>();
38 private CalendarEntries payCalendarEntry;
39 private Calendar payCalendar;
40
41
42
43
44
45
46
47 public TkTimeBlockAggregate(List<TimeBlock> timeBlocks, CalendarEntries payCalendarEntry){
48 this(timeBlocks, payCalendarEntry, TkServiceLocator.getCalendarService().getCalendar(payCalendarEntry.getHrCalendarId()));
49 }
50
51
52
53
54
55
56
57
58 public TkTimeBlockAggregate(List<TimeBlock> timeBlocks, CalendarEntries payCalendarEntry, Calendar payCalendar) {
59 this(timeBlocks, payCalendarEntry, payCalendar, false);
60 }
61
62
63
64
65
66
67
68
69
70 public TkTimeBlockAggregate(List<TimeBlock> timeBlocks, CalendarEntries payCalendarEntry, Calendar payCalendar, boolean useUserTimeZone) {
71 this.payCalendarEntry = payCalendarEntry;
72 this.payCalendar = payCalendar;
73
74 List<Interval> dayIntervals = TKUtils.getDaySpanForCalendarEntry(payCalendarEntry);
75 for(Interval dayInt : dayIntervals){
76 List<TimeBlock> dayTimeBlocks = new ArrayList<TimeBlock>();
77 for(TimeBlock timeBlock : timeBlocks){
78
79
80
81
82
83
84 DateTime beginTime = useUserTimeZone ? timeBlock.getBeginTimeDisplay() : new DateTime(timeBlock.getBeginTimestamp(), TKUtils.getSystemDateTimeZone());
85 DateTime endTime = useUserTimeZone ? timeBlock.getEndTimeDisplay() : new DateTime(timeBlock.getEndTimestamp(), TKUtils.getSystemDateTimeZone());
86 if(dayInt.contains(beginTime)){
87 if(dayInt.contains(endTime) || endTime.compareTo(dayInt.getEnd()) == 0){
88
89 if(beginTime.getHourOfDay() < dayInt.getStart().getHourOfDay()) {
90 timeBlock.setPushBackward(true);
91 }
92
93 dayTimeBlocks.add(timeBlock);
94 }
95 }
96 }
97 dayTimeBlockList.add(dayTimeBlocks);
98 }
99 }
100
101 public TkTimeBlockAggregate(List<TimeBlock> timeBlocks, CalendarEntries payCalendarEntry, Calendar payCalendar, boolean useUserTimeZone, List<Interval> dayIntervals) {
102 this.payCalendarEntry = payCalendarEntry;
103 this.payCalendar = payCalendar;
104
105 for(Interval dayInt : dayIntervals){
106 List<TimeBlock> dayTimeBlocks = new ArrayList<TimeBlock>();
107 for(TimeBlock timeBlock : timeBlocks){
108
109
110
111
112
113
114 DateTime beginTime = useUserTimeZone ? timeBlock.getBeginTimeDisplay() : new DateTime(timeBlock.getBeginTimestamp(), TKUtils.getSystemDateTimeZone());
115 DateTime endTime = useUserTimeZone ? timeBlock.getEndTimeDisplay() : new DateTime(timeBlock.getEndTimestamp(), TKUtils.getSystemDateTimeZone());
116 if(dayInt.contains(beginTime)){
117 if(dayInt.contains(endTime) || endTime.compareTo(dayInt.getEnd()) == 0){
118
119 if(beginTime.getHourOfDay() < dayInt.getStart().getHourOfDay()) {
120 timeBlock.setPushBackward(true);
121 }
122
123 dayTimeBlocks.add(timeBlock);
124 }
125 }
126 }
127 dayTimeBlockList.add(dayTimeBlocks);
128 }
129
130 }
131
132
133 public List<TimeBlock> getFlattenedTimeBlockList(){
134 List<TimeBlock> lstTimeBlocks = new ArrayList<TimeBlock>();
135 for(List<TimeBlock> timeBlocks : dayTimeBlockList){
136 lstTimeBlocks.addAll(timeBlocks);
137 }
138
139 Collections.sort(lstTimeBlocks, new Comparator<TimeBlock>() {
140 public int compare(TimeBlock tb1, TimeBlock tb2) {
141 if (tb1 != null && tb2 != null)
142 return tb1.getBeginTimestamp().compareTo(tb2.getBeginTimestamp());
143 return 0;
144 }
145 });
146
147 return lstTimeBlocks;
148 }
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164 public List<List<TimeBlock>> getWeekTimeBlocks(int week){
165 int startIndex = week*7;
166 int endIndex = (week*7)+7;
167 endIndex = endIndex > dayTimeBlockList.size() ? dayTimeBlockList.size() : endIndex;
168
169
170 List<List<TimeBlock>> wList = dayTimeBlockList.subList(startIndex, endIndex);
171 for (List<TimeBlock> dList : wList) {
172 Collections.sort(dList, new Comparator<TimeBlock>() {
173 public int compare(TimeBlock tb1, TimeBlock tb2) {
174 if (tb1 != null && tb2 != null)
175 return tb1.getBeginTimestamp().compareTo(tb2.getBeginTimestamp());
176 return 0;
177 }
178 });
179 }
180
181 return wList;
182 }
183
184
185
186
187
188
189
190
191
192 public List<FlsaWeek> getFlsaWeeks(DateTimeZone zone){
193 int flsaDayConstant = payCalendar.getFlsaBeginDayConstant();
194 Time flsaBeginTime = payCalendar.getFlsaBeginTime();
195
196
197
198 LocalTime flsaBeginLocalTime = LocalTime.fromDateFields(flsaBeginTime);
199
200
201
202
203
204 LocalDateTime startLDT = payCalendarEntry.getBeginLocalDateTime();
205
206
207
208 List<FlsaWeek> flsaWeeks = new ArrayList<FlsaWeek>();
209 List<TimeBlock> flatSortedBlockList = getFlattenedTimeBlockList();
210 FlsaWeek currentWeek = new FlsaWeek(flsaDayConstant, flsaBeginLocalTime, LocalTime.fromDateFields(payCalendarEntry.getBeginPeriodDateTime()));
211
212 flsaWeeks.add(currentWeek);
213
214 for (int i = 0; i<dayTimeBlockList.size(); i++) {
215 LocalDateTime currentDate = startLDT.plusDays(i);
216 FlsaDay flsaDay = new FlsaDay(currentDate, flatSortedBlockList, zone);
217
218 if (currentDate.getDayOfWeek() == flsaDayConstant) {
219 currentWeek = new FlsaWeek(flsaDayConstant, flsaBeginLocalTime, flsaBeginLocalTime);
220 flsaWeeks.add(currentWeek);
221
222 currentWeek.addFlsaDay(flsaDay);
223 } else {
224
225 currentWeek.addFlsaDay(flsaDay);
226 }
227 }
228
229 return flsaWeeks;
230 }
231
232
233
234
235 public int numberOfAggregatedWeeks() {
236 int weeks = 0;
237
238 if (this.dayTimeBlockList.size() > 0) {
239 weeks = this.dayTimeBlockList.size() / 7;
240 if (this.dayTimeBlockList.size() % 7 > 0)
241 weeks++;
242 }
243
244 return weeks;
245 }
246
247 public List<List<TimeBlock>> getDayTimeBlockList() {
248 return dayTimeBlockList;
249 }
250
251 public CalendarEntries getPayCalendarEntry() {
252 return payCalendarEntry;
253 }
254
255 public void setPayCalendarEntry(CalendarEntries payCalendarEntry) {
256 this.payCalendarEntry = payCalendarEntry;
257 }
258
259 public Calendar getPayCalendar() {
260 return payCalendar;
261 }
262
263 public void setPayCalendar(Calendar payCalendar) {
264 this.payCalendar = payCalendar;
265 }
266
267 }