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.util.ArrayList;
022    import java.util.List;
023    
024    public abstract class CalendarParent {
025        private List<CalendarWeek> weeks = new ArrayList<CalendarWeek>();
026        private CalendarEntries calendarEntry;
027        private DateTime beginDateTime;
028        private DateTime endDateTime;
029    
030        public CalendarParent(CalendarEntries calendarEntry) {
031            this.calendarEntry = calendarEntry;
032            if (calendarEntry != null) {
033                this.beginDateTime = calendarEntry.getBeginLocalDateTime().toDateTime(TkServiceLocator.getTimezoneService().getUserTimezoneWithFallback());
034                this.endDateTime = calendarEntry.getEndLocalDateTime().toDateTime(TkServiceLocator.getTimezoneService().getUserTimezoneWithFallback());
035            }
036        }
037    
038        protected CalendarParent() {
039        }
040    
041    
042        public DateTime getBeginDateTime() {
043            return beginDateTime;
044        }
045    
046        public void setBeginDateTime(DateTime beginDateTime) {
047            this.beginDateTime = beginDateTime;
048        }
049    
050        public DateTime getEndDateTime() {
051            return endDateTime;
052        }
053    
054        public void setEndDateTime(DateTime endDateTime) {
055            this.endDateTime = endDateTime;
056        }
057    
058        public CalendarEntries getCalendarEntry() {
059            return calendarEntry;
060        }
061    
062        public void setCalendarEntry(CalendarEntries calendarEntry) {
063            this.calendarEntry = calendarEntry;
064        }
065    
066        public List<CalendarWeek> getWeeks() {
067            return weeks;
068        }
069    
070        public void setWeeks(List<CalendarWeek> weeks) {
071            this.weeks = weeks;
072        }
073    
074        /**
075         * Provides the calendar title / heading. If the Pay Calendar entry spans
076         * multiple months, you get Abbreviated Month name + year of both the
077         * beginning month and the ending month.
078         *
079         * @return String for calendar title use.
080         */
081        public String getCalendarTitle() {
082            StringBuilder sb = new StringBuilder();
083    
084            if (getBeginDateTime().getMonthOfYear() == getEndDateTime().getMonthOfYear() ||
085                    (getBeginDateTime().getMonthOfYear() != getEndDateTime().getMonthOfYear()
086                            && getEndDateTime().getDayOfMonth() == 1 && getEndDateTime().getSecondOfDay() == 0)) {
087                sb.append(getBeginDateTime().toString("MMMM y"));
088            } else {
089                sb.append(getBeginDateTime().toString("MMM y"));
090                sb.append(" - ");
091                sb.append(getEndDateTime().toString("MMM y"));
092            }
093    
094            return sb.toString();
095        }
096    
097        /**
098         * Assumption of 7 "days" per week, or 7 "blocks" per row.
099         *
100         * @return A list of string titles for each row block (day)
101         */
102        public List<String> getCalendarDayHeadings() {
103            List<String> dayStrings = new ArrayList<String>(7);
104            // always render from Sunday
105            int firstDay = 0 - getBeginDateTime().getDayOfWeek();
106            int lastDay = firstDay + 7;
107    
108            if (getBeginDateTime().getMinuteOfDay() == 0) {
109                // "Standard" days.
110                for (int i = firstDay; i < lastDay; i++) {
111                    DateTime currDay = getBeginDateTime().plusDays(i);
112                    dayStrings.add(currDay.toString("E"));
113                }
114            } else {
115                // Day Split Strings
116                for (int i = firstDay; i < lastDay; i++) {
117                    StringBuilder builder = new StringBuilder("");
118                    DateTime currStart = getBeginDateTime().plusDays(i);
119                    DateTime currEnd = getBeginDateTime().plusDays(i);
120    
121                    builder.append(currStart.toString("E HH:mm"));
122                    builder.append(" - ");
123                    builder.append(currEnd.toString("E HH:mm"));
124    
125                    dayStrings.add(builder.toString());
126                }
127            }
128    
129            return dayStrings;
130        }
131    
132    }