001    /**
002     * Copyright 2004-2012 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            this.beginDateTime = calendarEntry.getBeginLocalDateTime().toDateTime(TkServiceLocator.getTimezoneService().getUserTimezoneWithFallback());
033            this.endDateTime = calendarEntry.getEndLocalDateTime().toDateTime(TkServiceLocator.getTimezoneService().getUserTimezoneWithFallback());
034        }
035    
036        protected CalendarParent() {
037        }
038    
039    
040        public DateTime getBeginDateTime() {
041            return beginDateTime;
042        }
043    
044        public void setBeginDateTime(DateTime beginDateTime) {
045            this.beginDateTime = beginDateTime;
046        }
047    
048        public DateTime getEndDateTime() {
049            return endDateTime;
050        }
051    
052        public void setEndDateTime(DateTime endDateTime) {
053            this.endDateTime = endDateTime;
054        }
055    
056        public CalendarEntries getCalendarEntry() {
057            return calendarEntry;
058        }
059    
060        public void setCalendarEntry(CalendarEntries calendarEntry) {
061            this.calendarEntry = calendarEntry;
062        }
063    
064        public List<CalendarWeek> getWeeks() {
065            return weeks;
066        }
067    
068        public void setWeeks(List<CalendarWeek> weeks) {
069            this.weeks = weeks;
070        }
071    
072        /**
073         * Provides the calendar title / heading. If the Pay Calendar entry spans
074         * multiple months, you get Abbreviated Month name + year of both the
075         * beginning month and the ending month.
076         *
077         * @return String for calendar title use.
078         */
079        public String getCalendarTitle() {
080            StringBuilder sb = new StringBuilder();
081    
082            if (getBeginDateTime().getMonthOfYear() == getEndDateTime().getMonthOfYear() ||
083                    (getBeginDateTime().getMonthOfYear() != getEndDateTime().getMonthOfYear()
084                            && getEndDateTime().getDayOfMonth() == 1 && getEndDateTime().getSecondOfDay() == 0)) {
085                sb.append(getBeginDateTime().toString("MMMM y"));
086            } else {
087                sb.append(getBeginDateTime().toString("MMM y"));
088                sb.append(" - ");
089                sb.append(getEndDateTime().toString("MMM y"));
090            }
091    
092            return sb.toString();
093        }
094    
095        /**
096         * Assumption of 7 "days" per week, or 7 "blocks" per row.
097         *
098         * @return A list of string titles for each row block (day)
099         */
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    }