001 /** 002 * Copyright 2004-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.hr.lm.util; 017 018 import java.util.ArrayList; 019 import java.util.Collections; 020 import java.util.Comparator; 021 import java.util.List; 022 023 import org.joda.time.DateTime; 024 import org.joda.time.Interval; 025 import org.joda.time.LocalDate; 026 import org.joda.time.LocalDateTime; 027 import org.kuali.hr.lm.leaveblock.LeaveBlock; 028 import org.kuali.hr.time.calendar.CalendarEntries; 029 import org.kuali.hr.time.calendar.LeaveCalendar; 030 import org.kuali.hr.time.util.TKUtils; 031 032 public class LeaveBlockAggregate { 033 public List<List<LeaveBlock>> dayLeaveBlockList = new ArrayList<List<LeaveBlock>>(); 034 private CalendarEntries leaveCalendarEntry; 035 private LeaveCalendar leaveCalendar; 036 037 038 /** 039 * Provides the option to refer to the time zone adjusted time for the current 040 * user. 041 * @param LeaveBlocks 042 * @param leaveCalendarEntry 043 * @param leaveCalendar 044 */ 045 public LeaveBlockAggregate(List<LeaveBlock> leaveBlocks, CalendarEntries leaveCalendarEntry, LeaveCalendar leaveCalendar) { 046 this.leaveCalendarEntry = leaveCalendarEntry; 047 this.leaveCalendar = leaveCalendar; 048 List<Interval> dayIntervals = TKUtils.getDaySpanForCalendarEntry(leaveCalendarEntry); 049 for(Interval dayInt : dayIntervals){ 050 List<LeaveBlock> dayLeaveBlocks = new ArrayList<LeaveBlock>(); 051 for(LeaveBlock leaveBlock : leaveBlocks){ 052 LocalDate localDate = new LocalDate(leaveBlock.getLeaveDate()); 053 LocalDate dayIntBegin = new LocalDate(dayInt.getStart()); 054 if(localDate.equals(dayIntBegin)){ 055 dayLeaveBlocks.add(leaveBlock); 056 } 057 } 058 dayLeaveBlockList.add(dayLeaveBlocks); 059 } 060 } 061 062 public LeaveBlockAggregate(List<LeaveBlock> leaveBlocks, CalendarEntries leaveCalendarEntry) { 063 this.leaveCalendarEntry = leaveCalendarEntry; 064 List<Interval> dayIntervals = TKUtils.getDaySpanForCalendarEntry(leaveCalendarEntry); 065 for(Interval dayInt : dayIntervals){ 066 List<LeaveBlock> dayLeaveBlocks = new ArrayList<LeaveBlock>(); 067 for(LeaveBlock leaveBlock : leaveBlocks){ 068 LocalDate localDate = new LocalDate(leaveBlock.getLeaveDate()); 069 LocalDate dayIntBegin = new LocalDate(dayInt.getStart()); 070 if(localDate.equals(dayIntBegin)){ 071 dayLeaveBlocks.add(leaveBlock); 072 } 073 } 074 dayLeaveBlockList.add(dayLeaveBlocks); 075 } 076 } 077 078 /** 079 * build leaveBlockAggregate with given leaveBlocks, calendarEntry and dayIntervals 080 * dayIntervals with full week span is for Time Calendar 081 * @param LeaveBlocks 082 * @param leaveCalendarEntry 083 * @param dayIntervals 084 */ 085 public LeaveBlockAggregate(List<LeaveBlock> leaveBlocks, CalendarEntries leaveCalendarEntry, List<Interval> dayIntervals) { 086 this.leaveCalendarEntry = leaveCalendarEntry; 087 for(Interval dayInt : dayIntervals){ 088 List<LeaveBlock> dayLeaveBlocks = new ArrayList<LeaveBlock>(); 089 DateTime localTime = (new DateTime(dayInt.getStart())).toLocalDateTime().toDateTime(); 090 String intervalStartDateString = TKUtils.getTimelessDate(localTime.toDate()).toString(); 091 092 for(LeaveBlock leaveBlock : leaveBlocks){ 093 // if the interval end time is 0, ie the beginning of a day, use the date string of the interval start time 094 // to check if the leave block should go into this interval. Leave blocks only have leaveDate, there's no leave time 095 if(dayInt.getEnd().getHourOfDay() == 0) { 096 String lbDateString = TKUtils.getTimelessDate(leaveBlock.getLeaveDate()).toString(); 097 if(intervalStartDateString.equals(lbDateString)) { 098 dayLeaveBlocks.add(leaveBlock); 099 } 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 }