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