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