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.service.base.TkServiceLocator;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  public abstract class CalendarParent {
25      private List<CalendarWeek> weeks = new ArrayList<CalendarWeek>();
26      private CalendarEntries calendarEntry;
27      private DateTime beginDateTime;
28      private DateTime endDateTime;
29  
30      public CalendarParent(CalendarEntries calendarEntry) {
31          this.calendarEntry = calendarEntry;
32          this.beginDateTime = calendarEntry.getBeginLocalDateTime().toDateTime(TkServiceLocator.getTimezoneService().getUserTimezoneWithFallback());
33          this.endDateTime = calendarEntry.getEndLocalDateTime().toDateTime(TkServiceLocator.getTimezoneService().getUserTimezoneWithFallback());
34      }
35  
36      protected CalendarParent() {
37      }
38  
39  
40      public DateTime getBeginDateTime() {
41          return beginDateTime;
42      }
43  
44      public void setBeginDateTime(DateTime beginDateTime) {
45          this.beginDateTime = beginDateTime;
46      }
47  
48      public DateTime getEndDateTime() {
49          return endDateTime;
50      }
51  
52      public void setEndDateTime(DateTime endDateTime) {
53          this.endDateTime = endDateTime;
54      }
55  
56      public CalendarEntries getCalendarEntry() {
57          return calendarEntry;
58      }
59  
60      public void setCalendarEntry(CalendarEntries calendarEntry) {
61          this.calendarEntry = calendarEntry;
62      }
63  
64      public List<CalendarWeek> getWeeks() {
65          return weeks;
66      }
67  
68      public void setWeeks(List<CalendarWeek> weeks) {
69          this.weeks = weeks;
70      }
71  
72      /**
73       * Provides the calendar title / heading. If the Pay Calendar entry spans
74       * multiple months, you get Abbreviated Month name + year of both the
75       * beginning month and the ending month.
76       *
77       * @return String for calendar title use.
78       */
79      public String getCalendarTitle() {
80          StringBuilder sb = new StringBuilder();
81  
82          if (getBeginDateTime().getMonthOfYear() == getEndDateTime().getMonthOfYear() ||
83                  (getBeginDateTime().getMonthOfYear() != getEndDateTime().getMonthOfYear()
84                          && getEndDateTime().getDayOfMonth() == 1 && getEndDateTime().getSecondOfDay() == 0)) {
85              sb.append(getBeginDateTime().toString("MMMM y"));
86          } else {
87              sb.append(getBeginDateTime().toString("MMM y"));
88              sb.append(" - ");
89              sb.append(getEndDateTime().toString("MMM y"));
90          }
91  
92          return sb.toString();
93      }
94  
95      /**
96       * Assumption of 7 "days" per week, or 7 "blocks" per row.
97       *
98       * @return A list of string titles for each row block (day)
99       */
100     public List<String> getCalendarDayHeadings() {
101         List<String> dayStrings = new ArrayList<String>(7);
102         // always render from Sunday
103         int firstDay = 0 - getBeginDateTime().getDayOfWeek();
104         int lastDay = firstDay + 7;
105 
106         if (getBeginDateTime().getMinuteOfDay() == 0) {
107             // "Standard" days.
108             for (int i = firstDay; i < lastDay; i++) {
109                 DateTime currDay = getBeginDateTime().plusDays(i);
110                 dayStrings.add(currDay.toString("E"));
111             }
112         } else {
113             // Day Split Strings
114             for (int i = firstDay; i < lastDay; i++) {
115                 StringBuilder builder = new StringBuilder("");
116                 DateTime currStart = getBeginDateTime().plusDays(i);
117                 DateTime currEnd = getBeginDateTime().plusDays(i);
118 
119                 builder.append(currStart.toString("E HH:mm"));
120                 builder.append(" - ");
121                 builder.append(currEnd.toString("E HH:mm"));
122 
123                 dayStrings.add(builder.toString());
124             }
125         }
126 
127         return dayStrings;
128     }
129 
130 }