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 java.sql.Date;
19  import java.util.*;
20  
21  import edu.emory.mathcs.backport.java.util.Arrays;
22  import org.apache.commons.collections.CollectionUtils;
23  import org.apache.commons.lang.time.DateUtils;
24  import org.joda.time.DateTime;
25  import org.joda.time.DateTimeConstants;
26  import org.joda.time.LocalDate;
27  import org.kuali.hr.lm.leaveblock.LeaveBlock;
28  import org.kuali.hr.time.service.base.TkServiceLocator;
29  import org.kuali.hr.time.util.TKContext;
30  import org.kuali.hr.time.util.TKUser;
31  import org.kuali.hr.time.util.TKUtils;
32  import org.kuali.hr.time.util.TkConstants;
33  import org.kuali.hr.time.workflow.TimesheetDocumentHeader;
34  
35  import com.google.common.collect.HashMultimap;
36  import com.google.common.collect.Multimap;
37  
38  public class LeaveCalendar extends CalendarParent {
39  
40      private Map<String, String> earnCodeList;
41      
42      public LeaveCalendar(String principalId, CalendarEntries calendarEntry, List<String> assignmentKeys) {
43          super(calendarEntry);
44  
45          DateTime currentDisplayDateTime = getBeginDateTime();
46          DateTime endDisplayDateTime = getEndDateTime();
47  
48          // Fill in the days if the first day or end day is in the middle of the week
49          if (currentDisplayDateTime.getDayOfWeek() != DateTimeConstants.SUNDAY) {
50              currentDisplayDateTime = currentDisplayDateTime.minusDays(currentDisplayDateTime.getDayOfWeek());
51          }
52          if (endDisplayDateTime.getDayOfWeek() != DateTimeConstants.SATURDAY) {
53              endDisplayDateTime = endDisplayDateTime.plusDays(DateTimeConstants.SATURDAY - endDisplayDateTime.getDayOfWeek());
54          }
55  
56          LeaveCalendarWeek leaveCalendarWeek = new LeaveCalendarWeek();
57          Integer dayNumber = 0;
58          List<LeaveBlock> blocks = TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principalId, calendarEntry.getBeginPeriodDate(), calendarEntry.getEndPeriodDate());
59          Map<String, List<LeaveBlock>> leaveBlockMap = new HashMap<String, List<LeaveBlock>>();
60          for (LeaveBlock lb : blocks) {
61              String key = new LocalDate(lb.getLeaveDate()).toString();
62              if (leaveBlockMap.containsKey(key)) {
63                  leaveBlockMap.get(key).add(lb);
64              } else {
65                  leaveBlockMap.put(key, createNewLeaveBlockList(lb));
66              }
67          }
68  
69          while (currentDisplayDateTime.isBefore(endDisplayDateTime) || currentDisplayDateTime.isEqual(endDisplayDateTime)) {
70              LeaveCalendarDay leaveCalendarDay = new LeaveCalendarDay();
71              
72              // If the day is not within the current pay period, mark them as read only (gray)
73              if (currentDisplayDateTime.isBefore(getBeginDateTime()) 
74              		|| currentDisplayDateTime.isEqual(getEndDateTime()) 
75              		|| currentDisplayDateTime.isAfter(getEndDateTime())) {
76                  leaveCalendarDay.setGray(true);
77              } else {
78                  // This is for the div id of the days on the calendar.
79                  // It creates a day id like day_11/01/2011 which will make day parsing easier in the javascript.
80  //                leaveCalendarDay.setDayNumberDelta(currDateTime.toString(TkConstants.DT_BASIC_DATE_FORMAT));
81  //                leaveCalendarDay.setDayNumberDelta(currDateTime.getDayOfMonth());
82                  leaveCalendarDay.setDayNumberDelta(dayNumber);
83      
84                 java.util.Date leaveDate = TKUtils.getTimelessDate(currentDisplayDateTime.toLocalDate().toDateMidnight().toDate());
85                 List<LeaveBlock> lbs = leaveBlockMap.get(currentDisplayDateTime.toLocalDate().toString());
86                 if (lbs == null) {
87                     lbs = Collections.emptyList();
88                 }
89                 // use given assignmentKeys to control leave blocks displayed on the calendar
90                 if(CollectionUtils.isNotEmpty(lbs) && CollectionUtils.isNotEmpty(assignmentKeys)) {
91              	   List<LeaveBlock> leaveBlocks = TkServiceLocator.getLeaveBlockService().filterLeaveBlocksForLeaveCalendar(lbs, assignmentKeys);
92              	   leaveCalendarDay.setLeaveBlocks(leaveBlocks);
93                 } else {
94              	   leaveCalendarDay.setLeaveBlocks(lbs);
95                 }
96                 
97                 if (TkServiceLocator.getPermissionsService().canViewLeaveTabsWithNEStatus()) {
98  	               TimesheetDocumentHeader tdh = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeaderForDate(principalId, leaveDate);
99  	               if (tdh != null) {
100 	            	   if (DateUtils.isSameDay(leaveDate, tdh.getEndDate()) || leaveDate.after(tdh.getEndDate())) {
101 	            		   leaveCalendarDay.setDayEditable(true);
102 	            	   }
103 	               } else {
104 	            	   leaveCalendarDay.setDayEditable(true);
105 	               }
106                } else {
107                    leaveCalendarDay.setDayEditable(true);
108                }
109                
110                dayNumber++;
111             }
112             leaveCalendarDay.setDayNumberString(currentDisplayDateTime.dayOfMonth().getAsShortText());
113             leaveCalendarDay.setDateString(currentDisplayDateTime.toString(TkConstants.DT_BASIC_DATE_FORMAT));
114 
115             leaveCalendarWeek.getDays().add(leaveCalendarDay);
116             
117             if (leaveCalendarWeek.getDays().size() == DateTimeConstants.DAYS_PER_WEEK) {
118                 getWeeks().add(leaveCalendarWeek);
119                 leaveCalendarWeek = new LeaveCalendarWeek();
120             }
121 
122             currentDisplayDateTime = currentDisplayDateTime.plusDays(1);
123         }
124 
125         if (!leaveCalendarWeek.getDays().isEmpty()) {
126             getWeeks().add(leaveCalendarWeek);
127         }
128 
129         boolean isPlanningCal = TkServiceLocator.getLeaveCalendarService().isLeavePlanningCalendar(principalId, calendarEntry.getBeginPeriodDateTime(), calendarEntry.getEndPeriodDateTime());
130         Map<String, String> earnCodes = TkServiceLocator.getEarnCodeService().getEarnCodesForDisplay(principalId, isPlanningCal);
131         setEarnCodeList(earnCodes);
132     }
133 
134     private List<LeaveBlock> createNewLeaveBlockList(LeaveBlock lb){
135         List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
136         leaveBlocks.add(lb);
137         return leaveBlocks;
138     }
139     
140     private Multimap<Date, LeaveBlock> leaveBlockAggregator(String documentId) {
141         List<LeaveBlock> leaveBlocks = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDocumentId(documentId);
142         Multimap<Date, LeaveBlock> leaveBlockAggregrate = HashMultimap.create();
143         for (LeaveBlock leaveBlock : leaveBlocks) {
144             leaveBlockAggregrate.put(leaveBlock.getLeaveDate(), leaveBlock);
145         }
146 
147         return leaveBlockAggregrate;
148     }
149 
150    	public Map<String, String> getEarnCodeList() {
151 		return earnCodeList;
152 	}
153 
154 	public void setEarnCodeList(Map<String, String> earnCodeList) {
155 		this.earnCodeList = earnCodeList;
156 	}
157 }