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.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       * Provides the option to refer to the time zone adjusted time for the current
40       * user.
41       * @param LeaveBlocks
42       * @param leaveCalendarEntry
43       * @param leaveCalendar
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       *  build leaveBlockAggregate with given leaveBlocks, calendarEntry and dayIntervals
80       *  dayIntervals with full week span is for Time Calendar
81       * @param LeaveBlocks
82       * @param leaveCalendarEntry
83       * @param dayIntervals
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  				// if the interval end time is 0, ie the beginning of a day, use the date string of the interval start time
94  				// to check if the leave block should go into this interval. Leave blocks only have leaveDate, there's no leave time
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>() { // Sort the Leave Blocks
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 	 * Provides a way to access all of the leave blocks for a given week.
134 	 *
135 	 * Outer list is 0 indexed list representing days in a week.
136 	 * Inner List are all of the time blocks for that day.
137 	 *
138 	 * Ex.
139 	 *
140 	 * List<List<LeaveBlock>> week0 = getWeekLeaveBlocks(0);
141 	 * List<LeaveBlock> day0 = week0.get(0);
142 	 *
143 	 * @param week
144 	 * @return
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         // Need to sort each day by clock time.
152         List<List<LeaveBlock>> wList = dayLeaveBlockList.subList(startIndex, endIndex);
153         for (List<LeaveBlock> dList : wList) {
154             Collections.sort(dList, new Comparator<LeaveBlock>() { // Sort the Leave Blocks
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 	 * @return the total number of weeks that this object represents.
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 }