1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.lm.util;
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.Comparator;
21 import java.util.List;
22
23 import org.joda.time.DateTime;
24 import org.joda.time.Interval;
25 import org.joda.time.LocalDate;
26 import org.joda.time.LocalDateTime;
27 import org.kuali.hr.lm.leaveblock.LeaveBlock;
28 import org.kuali.hr.time.calendar.CalendarEntries;
29 import org.kuali.hr.time.calendar.LeaveCalendar;
30 import org.kuali.hr.time.util.TKUtils;
31
32 public class LeaveBlockAggregate {
33 public List<List<LeaveBlock>> dayLeaveBlockList = new ArrayList<List<LeaveBlock>>();
34 private CalendarEntries leaveCalendarEntry;
35 private LeaveCalendar leaveCalendar;
36
37
38
39
40
41
42
43
44
45 public LeaveBlockAggregate(List<LeaveBlock> leaveBlocks, CalendarEntries leaveCalendarEntry, LeaveCalendar leaveCalendar) {
46 this.leaveCalendarEntry = leaveCalendarEntry;
47 this.leaveCalendar = leaveCalendar;
48 List<Interval> dayIntervals = TKUtils.getDaySpanForCalendarEntry(leaveCalendarEntry);
49 for(Interval dayInt : dayIntervals){
50 List<LeaveBlock> dayLeaveBlocks = new ArrayList<LeaveBlock>();
51 for(LeaveBlock leaveBlock : leaveBlocks){
52 LocalDate localDate = new LocalDate(leaveBlock.getLeaveDate());
53 LocalDate dayIntBegin = new LocalDate(dayInt.getStart());
54 if(localDate.equals(dayIntBegin)){
55 dayLeaveBlocks.add(leaveBlock);
56 }
57 }
58 dayLeaveBlockList.add(dayLeaveBlocks);
59 }
60 }
61
62 public LeaveBlockAggregate(List<LeaveBlock> leaveBlocks, CalendarEntries leaveCalendarEntry) {
63 this.leaveCalendarEntry = leaveCalendarEntry;
64 List<Interval> dayIntervals = TKUtils.getDaySpanForCalendarEntry(leaveCalendarEntry);
65 for(Interval dayInt : dayIntervals){
66 List<LeaveBlock> dayLeaveBlocks = new ArrayList<LeaveBlock>();
67 for(LeaveBlock leaveBlock : leaveBlocks){
68 LocalDate localDate = new LocalDate(leaveBlock.getLeaveDate());
69 LocalDate dayIntBegin = new LocalDate(dayInt.getStart());
70 if(localDate.equals(dayIntBegin)){
71 dayLeaveBlocks.add(leaveBlock);
72 }
73 }
74 dayLeaveBlockList.add(dayLeaveBlocks);
75 }
76 }
77
78
79
80
81
82
83
84
85 public LeaveBlockAggregate(List<LeaveBlock> leaveBlocks, CalendarEntries leaveCalendarEntry, List<Interval> dayIntervals) {
86 this.leaveCalendarEntry = leaveCalendarEntry;
87 for(Interval dayInt : dayIntervals){
88 List<LeaveBlock> dayLeaveBlocks = new ArrayList<LeaveBlock>();
89 DateTime localTime = (new DateTime(dayInt.getStart())).toLocalDateTime().toDateTime();
90 String intervalStartDateString = TKUtils.getTimelessDate(localTime.toDate()).toString();
91
92 for(LeaveBlock leaveBlock : leaveBlocks){
93
94
95 if(dayInt.getEnd().getHourOfDay() == 0) {
96 String lbDateString = TKUtils.getTimelessDate(leaveBlock.getLeaveDate()).toString();
97 if(intervalStartDateString.equals(lbDateString)) {
98 dayLeaveBlocks.add(leaveBlock);
99 }
100 } else {
101 LocalDate localDate = new LocalDate(leaveBlock.getLeaveDate());
102 LocalDate dayIntBegin = new LocalDate(dayInt.getStart());
103 if(localDate.equals(dayIntBegin)){
104 dayLeaveBlocks.add(leaveBlock);
105 }
106 }
107 }
108 dayLeaveBlockList.add(dayLeaveBlocks);
109 }
110 }
111
112 public List<LeaveBlock> getFlattenedLeaveBlockList(){
113 List<LeaveBlock> lstLeaveBlocks = new ArrayList<LeaveBlock>();
114 for(List<LeaveBlock> leaveBlocks : dayLeaveBlockList){
115 lstLeaveBlocks.addAll(leaveBlocks);
116 }
117
118 Collections.sort(lstLeaveBlocks, new Comparator<LeaveBlock>() {
119 public int compare(LeaveBlock tb1, LeaveBlock tb2) {
120 if (tb1 != null && tb2 != null) {
121 if (tb1.getTimestamp() != null && tb2.getTimestamp() != null) {
122 return tb1.getTimestamp().compareTo(tb2.getTimestamp());
123 }
124 }
125 return 0;
126 }
127 });
128
129 return lstLeaveBlocks;
130 }
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146 public List<List<LeaveBlock>> getWeekLeaveBlocks(int week){
147 int startIndex = week*7;
148 int endIndex = (week*7)+7;
149 endIndex = endIndex > dayLeaveBlockList.size() ? dayLeaveBlockList.size() : endIndex;
150
151
152 List<List<LeaveBlock>> wList = dayLeaveBlockList.subList(startIndex, endIndex);
153 for (List<LeaveBlock> dList : wList) {
154 Collections.sort(dList, new Comparator<LeaveBlock>() {
155 public int compare(LeaveBlock tb1, LeaveBlock tb2) {
156 if (tb1 != null && tb2 != null)
157 return tb1.getTimestamp().compareTo(tb2.getTimestamp());
158 return 0;
159 }
160 });
161 }
162
163 return wList;
164 }
165
166
167
168
169 public int numberOfAggregatedWeeks() {
170 int weeks = 0;
171
172 if (this.dayLeaveBlockList.size() > 0) {
173 weeks = this.dayLeaveBlockList.size() / 7;
174 if (this.dayLeaveBlockList.size() % 7 > 0)
175 weeks++;
176 }
177
178 return weeks;
179 }
180
181 public List<List<LeaveBlock>> getDayLeaveBlockList() {
182 return dayLeaveBlockList;
183 }
184
185 public CalendarEntries getleaveCalendarEntry() {
186 return leaveCalendarEntry;
187 }
188
189 public void setleaveCalendarEntry(CalendarEntries leaveCalendarEntry) {
190 this.leaveCalendarEntry = leaveCalendarEntry;
191 }
192
193 public LeaveCalendar getLeaveCalendar() {
194 return leaveCalendar;
195 }
196
197 public void setLeaveCalendar(LeaveCalendar leaveCalendar) {
198 this.leaveCalendar = leaveCalendar;
199 }
200
201 }