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