001 /** 002 * Copyright 2004-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.hr.time.calendar; 017 018 import org.joda.time.DateTime; 019 import org.kuali.hr.time.service.base.TkServiceLocator; 020 021 import java.io.Serializable; 022 import java.util.ArrayList; 023 import java.util.List; 024 025 public abstract class CalendarParent implements Serializable { 026 private List<CalendarWeek> weeks = new ArrayList<CalendarWeek>(); 027 private CalendarEntries calendarEntry; 028 private DateTime beginDateTime; 029 private DateTime endDateTime; 030 031 public CalendarParent(CalendarEntries calendarEntry) { 032 this.calendarEntry = calendarEntry; 033 if (calendarEntry != null) { 034 this.beginDateTime = calendarEntry.getBeginLocalDateTime().toDateTime(TkServiceLocator.getTimezoneService().getUserTimezoneWithFallback()); 035 this.endDateTime = calendarEntry.getEndLocalDateTime().toDateTime(TkServiceLocator.getTimezoneService().getUserTimezoneWithFallback()); 036 } 037 } 038 039 protected CalendarParent() { 040 } 041 042 043 public DateTime getBeginDateTime() { 044 return beginDateTime; 045 } 046 047 public void setBeginDateTime(DateTime beginDateTime) { 048 this.beginDateTime = beginDateTime; 049 } 050 051 public DateTime getEndDateTime() { 052 return endDateTime; 053 } 054 055 public void setEndDateTime(DateTime endDateTime) { 056 this.endDateTime = endDateTime; 057 } 058 059 public CalendarEntries getCalendarEntry() { 060 return calendarEntry; 061 } 062 063 public void setCalendarEntry(CalendarEntries calendarEntry) { 064 this.calendarEntry = calendarEntry; 065 } 066 067 public List<CalendarWeek> getWeeks() { 068 return weeks; 069 } 070 071 public void setWeeks(List<CalendarWeek> weeks) { 072 this.weeks = weeks; 073 } 074 075 /** 076 * Provides the calendar title / heading. If the Pay Calendar entry spans 077 * multiple months, you get Abbreviated Month name + year of both the 078 * beginning month and the ending month. 079 * 080 * @return String for calendar title use. 081 */ 082 public String getCalendarTitle() { 083 StringBuilder sb = new StringBuilder(); 084 085 if (getBeginDateTime().getMonthOfYear() == getEndDateTime().getMonthOfYear() || 086 (getBeginDateTime().getMonthOfYear() != getEndDateTime().getMonthOfYear() 087 && getEndDateTime().getDayOfMonth() == 1 && getEndDateTime().getSecondOfDay() == 0)) { 088 sb.append(getBeginDateTime().toString("MMMM y")); 089 } else { 090 sb.append(getBeginDateTime().toString("MMM y")); 091 sb.append(" - "); 092 sb.append(getEndDateTime().toString("MMM y")); 093 } 094 095 return sb.toString(); 096 } 097 098 /** 099 * 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 }