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.time.calendar;
17  
18  import org.joda.time.DateTime;
19  import org.kuali.hr.lm.leaveblock.LeaveBlock;
20  import org.kuali.hr.lm.util.LeaveBlockAggregate;
21  import org.kuali.hr.time.earncode.EarnCode;
22  import org.kuali.hr.time.service.base.TkServiceLocator;
23  import org.kuali.hr.time.timeblock.TimeBlock;
24  import org.kuali.hr.time.util.TkConstants;
25  import org.kuali.hr.time.util.TkTimeBlockAggregate;
26  
27  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.Map;
30  
31  public class TkCalendar extends CalendarParent {
32      private List<TkCalendarWeek> weeks = new ArrayList<TkCalendarWeek>();
33      private CalendarEntries payCalEntry;
34      private DateTime beginDateTime;
35      private DateTime endDateTime;
36  
37      public TkCalendar() {}
38  
39      public TkCalendar(CalendarEntries calendarEntry) {
40          super(calendarEntry);
41      }
42  
43      public static TkCalendar getCalendar(TkTimeBlockAggregate tbAggregate, LeaveBlockAggregate lbAggregate) {
44      	TkCalendar tc = new TkCalendar();
45      	 
46          if (tbAggregate != null && lbAggregate != null) {
47          	if(tbAggregate.getDayTimeBlockList().size() != lbAggregate.getDayLeaveBlockList().size()){
48          		throw new RuntimeException("TimeBlockAggregate and LeaveBlockAggregate should have the same size of Day Blocks List");
49          	}
50          		
51          	List<CalendarWeek> weeks = new ArrayList<CalendarWeek>();
52  	        tc.setPayCalEntry(tbAggregate.getPayCalendarEntry());
53  	
54  	         int firstDay = 0;
55  	         if (tc.getBeginDateTime().getDayOfWeek() != 7) {
56  	             firstDay = 0 - tc.getBeginDateTime().getDayOfWeek();   // always render calendar weeks from Sundays
57  	         }
58  	         for (int i = 0; i < tbAggregate.numberOfAggregatedWeeks(); i++) {
59  	             TkCalendarWeek week = new TkCalendarWeek();
60  	             List<List<TimeBlock>> weekBlocks = tbAggregate.getWeekTimeBlocks(i);
61  	             List<List<LeaveBlock>> weekLeaveBlocks = lbAggregate.getWeekLeaveBlocks(i);
62  	             List<CalendarDay> days = new ArrayList<CalendarDay>(7);
63  	
64  	             for (int j = 0; j < weekBlocks.size(); j++) {
65  	                 List<TimeBlock> dayBlocks = weekBlocks.get(j);
66  	                 List<LeaveBlock> dayLeaveBlocks = weekLeaveBlocks.get(j);
67  	                 // Create the individual days.
68  	                 TkCalendarDay day = new TkCalendarDay();
69  	                 day.setTimeblocks(dayBlocks);
70  	                 day.setLeaveBlocks(dayLeaveBlocks);
71  	                 day.setDayNumberString(tc.getDayNumberString(i * 7 + j + firstDay));
72  	                 day.setDayNumberDelta(i * 7 + j + firstDay);
73  	                 day.setDateString(tc.getDateString(day.getDayNumberDelta()));
74  	                 assignDayLunchLabel(day);
75  	                 int dayIndex = i * 7 + j + firstDay;
76  	                 DateTime beginDateTemp = tc.getBeginDateTime().plusDays(dayIndex);
77  	                 day.setGray(false);
78  	                 if (beginDateTemp.isBefore(tc.getBeginDateTime().getMillis())
79  	                         || beginDateTemp.isAfter(tc.getEndDateTime().getMillis())) {
80  	                     day.setGray(true);
81  	                 }
82  	                 if (tc.getEndDateTime().getHourOfDay() == 0 && beginDateTemp.equals(tc.getEndDateTime())) {
83  	                     day.setGray(true);
84  	                 }
85  	                 days.add(day);
86  	             }
87  	             week.setDays(days);
88  	             weeks.add(week);
89  	         }
90  	         tc.setWeeks(weeks);
91  	     } else {
92  	         return null;
93  	     }
94  
95          return tc;
96      }
97      
98      public static TkCalendar getCalendar(TkTimeBlockAggregate aggregate) {
99          TkCalendar tc = new TkCalendar();
100 
101         if (aggregate != null) {
102             List<CalendarWeek> weeks = new ArrayList<CalendarWeek>();
103             tc.setPayCalEntry(aggregate.getPayCalendarEntry());
104 
105             int firstDay = 0;
106             if (tc.getBeginDateTime().getDayOfWeek() != 7) {
107                 firstDay = 0 - tc.getBeginDateTime().getDayOfWeek();   // always render calendar weeks from Sundays
108             }
109             for (int i = 0; i < aggregate.numberOfAggregatedWeeks(); i++) {
110                 TkCalendarWeek week = new TkCalendarWeek();
111                 List<List<TimeBlock>> weekBlocks = aggregate.getWeekTimeBlocks(i);
112                 List<CalendarDay> days = new ArrayList<CalendarDay>(7);
113 
114                 for (int j = 0; j < weekBlocks.size(); j++) {
115                     List<TimeBlock> dayBlocks = weekBlocks.get(j);
116                     // Create the individual days.
117                     TkCalendarDay day = new TkCalendarDay();
118                     day.setTimeblocks(dayBlocks);
119                     day.setDayNumberString(tc.getDayNumberString(i * 7 + j + firstDay));
120                     day.setDayNumberDelta(i * 7 + j + firstDay);
121                     day.setDateString(tc.getDateString(day.getDayNumberDelta()));
122                     assignDayLunchLabel(day);
123                     int dayIndex = i * 7 + j + firstDay;
124                     DateTime beginDateTemp = tc.getBeginDateTime().plusDays(dayIndex);
125                     day.setGray(false);
126                     if (beginDateTemp.isBefore(tc.getBeginDateTime().getMillis())
127                             || beginDateTemp.isAfter(tc.getEndDateTime().getMillis())) {
128                         day.setGray(true);
129                     }
130                     if (tc.getEndDateTime().getHourOfDay() == 0 && beginDateTemp.equals(tc.getEndDateTime())) {
131                         day.setGray(true);
132                     }
133                     days.add(day);
134                 }
135                 week.setDays(days);
136                 weeks.add(week);
137             }
138             tc.setWeeks(weeks);
139         } else {
140             return null;
141         }
142 
143         return tc;
144     }
145 
146     public static void assignDayLunchLabel(TkCalendarDay day) {
147         EarnCode ec = null;
148         String label = "";
149         String id = "";
150         for (TimeBlockRenderer tbr : day.getBlockRenderers()) {
151             for (TimeHourDetailRenderer thdr : tbr.getDetailRenderers()) {
152                 if (thdr.getTitle().equals(TkConstants.LUNCH_EARN_CODE)) {
153                     ec = TkServiceLocator.getEarnCodeService().getEarnCode(thdr.getTitle(), tbr.getTimeBlock().getBeginDate());
154                     if (ec != null) {
155                         label = ec.getDescription() + " : " + thdr.getHours() + " hours";
156                         id = thdr.getTkTimeHourDetailId();
157                     }
158                 }
159             }
160             tbr.setLunchLabel(label);
161             tbr.setLunchLabelId(id);
162 
163             label = "";
164         }
165     }
166 
167     
168     public void assignAssignmentStyle(Map<String, String> styleMap) {
169         for (CalendarWeek aWeek : this.getWeeks()) {
170             for (CalendarDay aDay : aWeek.getDays()) {
171                 for (TimeBlockRenderer tbr : ((TkCalendarDay)aDay).getBlockRenderers()) {
172                     String assignmentKey = tbr.getTimeBlock().getAssignmentKey();
173                     if (assignmentKey != null && styleMap.containsKey(assignmentKey)) {
174                         tbr.setAssignmentClass(styleMap.get(assignmentKey));
175                     } else {
176                         tbr.setAssignmentClass("");
177                     }
178                 }
179                 for (LeaveBlockRenderer lbr : ((TkCalendarDay)aDay).getLeaveBlockRenderers()) {
180                     String assignmentKey = lbr.getLeaveBlock().getAssignmentKey();
181                     if (assignmentKey != null && styleMap.containsKey(assignmentKey)) {
182                         lbr.setAssignmentClass(styleMap.get(assignmentKey));
183                     } else {
184                         lbr.setAssignmentClass("");
185                     }
186                 } 
187                 
188             }
189         }
190     }
191 
192 
193 //    public List<TkCalendarWeek> getWeeks() {
194 //        return weeks;
195 //    }
196 //
197 //    public void setWeeks(List<TkCalendarWeek> weeks) {
198 //        this.weeks = weeks;
199 //    }
200 
201     public CalendarEntries getPayCalEntry() {
202         return payCalEntry;
203     }
204 
205     public void setPayCalEntry(CalendarEntries payCalEntry) {
206         this.payCalEntry = payCalEntry;
207         // Relative time, with time zone added.
208         this.beginDateTime = payCalEntry.getBeginLocalDateTime().toDateTime(TkServiceLocator.getTimezoneService().getUserTimezoneWithFallback());
209         this.endDateTime = payCalEntry.getEndLocalDateTime().toDateTime(TkServiceLocator.getTimezoneService().getUserTimezoneWithFallback());
210     }
211 
212     public DateTime getBeginDateTime() {
213         return beginDateTime;
214     }
215 
216     public DateTime getEndDateTime() {
217         return endDateTime;
218     }
219 
220     /**
221      * Provides the calendar title / heading. If the Pay Calendar entry spans
222      * multiple months, you get Abbreviated Month name + year of both the
223      * beginning month and the ending month.
224      *
225      * @return String for calendar title use.
226      */
227     public String getCalendarTitle() {
228         StringBuilder sb = new StringBuilder();
229 
230         if (getBeginDateTime().getMonthOfYear() == getEndDateTime().getMonthOfYear() ||
231                 (getBeginDateTime().getMonthOfYear() != getEndDateTime().getMonthOfYear()
232                         && getEndDateTime().getDayOfMonth() == 1 && getEndDateTime().getSecondOfDay() == 0)) {
233             sb.append(getBeginDateTime().toString("MMMM y"));
234         } else {
235             sb.append(getBeginDateTime().toString("MMM y"));
236             sb.append(" - ");
237             sb.append(getEndDateTime().toString("MMM y"));
238         }
239 
240         return sb.toString();
241     }
242 
243     /**
244      * Assumption of 7 "days" per week, or 7 "blocks" per row.
245      *
246      * @return A list of string titles for each row block (day)
247      */
248     public List<String> getCalendarDayHeadings() {
249         List<String> dayStrings = new ArrayList<String>(7);
250         // always render from Sunday
251         int firstDay = 0 - getBeginDateTime().getDayOfWeek();
252         int lastDay = firstDay + 7;
253 
254         if (getBeginDateTime().getMinuteOfDay() == 0) {
255             // "Standard" days.
256             for (int i = firstDay; i < lastDay; i++) {
257                 DateTime currDay = getBeginDateTime().plusDays(i);
258                 dayStrings.add(currDay.toString("E"));
259             }
260         } else {
261             // Day Split Strings
262 
263             for (int i = firstDay; i < lastDay; i++) {
264                 StringBuilder builder = new StringBuilder("");
265                 DateTime currStart = getBeginDateTime().plusDays(i);
266                 DateTime currEnd = getBeginDateTime().plusDays(i);
267 
268                 builder.append(currStart.toString("E HH:mm"));
269                 builder.append(" - ");
270                 builder.append(currEnd.toString("E HH:mm"));
271 
272                 dayStrings.add(builder.toString());
273             }
274         }
275 
276         return dayStrings;
277     }
278 
279     public String getCalenadrYear() {
280         return getBeginDateTime().toString("yyyy");
281     }
282 
283     public String getCalendarMonth() {
284         return getBeginDateTime().toString("M");
285     }
286 
287     private String getDayNumberString(int dayDelta) {
288         StringBuilder b = new StringBuilder();
289 
290         if (getBeginDateTime().getMinuteOfDay() == 0) {
291             DateTime currStart = getBeginDateTime().plusDays(dayDelta);
292             b.append(currStart.toString("d"));
293         } else {
294             DateTime currStart = getBeginDateTime().plusDays(dayDelta);
295             DateTime currEnd = getBeginDateTime().plusDays(dayDelta);
296 
297             b.append(currStart.toString("d HH:mm"));
298             b.append(" - ");
299             b.append(currStart.toString("d HH:mm"));
300         }
301 
302         return b.toString();
303     }
304 
305     private String getDateString(int dayDelta) {
306         return getBeginDateTime().plusDays(dayDelta).toString("M/d/yyyy");
307     }
308 
309 }