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