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.tklm.leave.calendar;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.collections.CollectionUtils;
25  import org.apache.commons.lang.time.DateUtils;
26  import org.apache.log4j.Logger;
27  import org.joda.time.DateTime;
28  import org.joda.time.DateTimeConstants;
29  import org.joda.time.LocalDate;
30  import org.kuali.kpme.core.calendar.CalendarParent;
31  import org.kuali.kpme.core.calendar.entry.CalendarEntry;
32  import org.kuali.kpme.core.service.HrServiceLocator;
33  import org.kuali.kpme.core.util.HrConstants;
34  import org.kuali.kpme.tklm.api.leave.calendar.LeaveCalendarContract;
35  import org.kuali.kpme.tklm.leave.block.LeaveBlock;
36  import org.kuali.kpme.tklm.leave.calendar.web.LeaveCalendarDay;
37  import org.kuali.kpme.tklm.leave.calendar.web.LeaveCalendarWeek;
38  import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
39  import org.kuali.kpme.tklm.leave.workflow.LeaveCalendarDocumentHeader;
40  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
41  import org.kuali.kpme.tklm.time.workflow.TimesheetDocumentHeader;
42  
43  
44  public class LeaveCalendar extends CalendarParent implements LeaveCalendarContract {
45  
46  	private static final long serialVersionUID = -1038397053444003582L;
47  	private Map<String, String> earnCodeList;
48      private static final Logger LOG = Logger.getLogger(LeaveCalendar.class);
49      
50      public LeaveCalendar(String principalId, CalendarEntry calendarEntry, List<String> assignmentKeys) {
51          super(calendarEntry);
52  
53          DateTime currentDisplayDateTime = getBeginDateTime();
54          DateTime endDisplayDateTime = getEndDateTime();
55  
56          // Fill in the days if the first day or end day is in the middle of the week
57          if (currentDisplayDateTime.getDayOfWeek() != DateTimeConstants.SUNDAY) {
58              currentDisplayDateTime = currentDisplayDateTime.minusDays(currentDisplayDateTime.getDayOfWeek());
59          }
60          if (endDisplayDateTime.getDayOfWeek() != DateTimeConstants.SATURDAY) {
61              endDisplayDateTime = endDisplayDateTime.plusDays(DateTimeConstants.SATURDAY - endDisplayDateTime.getDayOfWeek());
62          }
63  
64          LeaveCalendarWeek leaveCalendarWeek = new LeaveCalendarWeek();
65          Integer dayNumber = 0;
66          List<LeaveBlock> blocks = LmServiceLocator.getLeaveBlockService().getLeaveBlocks(principalId, calendarEntry.getBeginPeriodFullDateTime().toLocalDate(), calendarEntry.getEndPeriodFullDateTime().toLocalDate());
67          Map<String, List<LeaveBlock>> leaveBlockMap = new HashMap<String, List<LeaveBlock>>();
68          for (LeaveBlock lb : blocks) {
69              String key = lb.getLeaveLocalDate().toString();
70              if (leaveBlockMap.containsKey(key)) {
71                  leaveBlockMap.get(key).add(lb);
72              } else {
73                  leaveBlockMap.put(key, createNewLeaveBlockList(lb));
74              }
75          }
76  
77          //KPME-2560 If leave calendar document is final status, then User wont be able to add leave blocks to the calendar. 
78          Boolean dayEditableFlag = true;
79          LeaveCalendarDocumentHeader header = LmServiceLocator.getLeaveCalendarDocumentHeaderService().getDocumentHeader(principalId, calendarEntry.getBeginPeriodFullDateTime(), calendarEntry.getEndPeriodFullDateTime());
80          if(header != null && header.getDocumentStatus().equals(HrConstants.ROUTE_STATUS.FINAL))
81          	dayEditableFlag = false;
82          
83          while (currentDisplayDateTime.isBefore(endDisplayDateTime) || currentDisplayDateTime.isEqual(endDisplayDateTime)) {
84              LeaveCalendarDay leaveCalendarDay = new LeaveCalendarDay();
85              
86              // If the day is not within the current pay period, mark them as read only (gray)
87              if (currentDisplayDateTime.isBefore(getBeginDateTime()) 
88              		|| currentDisplayDateTime.isEqual(getEndDateTime()) 
89              		|| currentDisplayDateTime.isAfter(getEndDateTime())) {
90                  leaveCalendarDay.setGray(true);
91              } else {
92                  // This is for the div id of the days on the calendar.
93                  // It creates a day id like day_11/01/2011 which will make day parsing easier in the javascript.
94  //                leaveCalendarDay.setDayNumberDelta(currDateTime.toString(HrConstants.DT_BASIC_DATE_FORMAT));
95  //                leaveCalendarDay.setDayNumberDelta(currDateTime.getDayOfMonth());
96                  leaveCalendarDay.setDayNumberDelta(dayNumber);
97      
98                 LocalDate leaveDate = currentDisplayDateTime.toLocalDate();
99                 List<LeaveBlock> lbs = leaveBlockMap.get(currentDisplayDateTime.toLocalDate().toString());
100                if (lbs == null) {
101                    lbs = Collections.emptyList();
102                }
103                // use given assignmentKeys to control leave blocks displayed on the calendar
104                if(CollectionUtils.isNotEmpty(lbs) && CollectionUtils.isNotEmpty(assignmentKeys)) {
105             	   List<LeaveBlock> leaveBlocks = LmServiceLocator.getLeaveBlockService().filterLeaveBlocksForLeaveCalendar(lbs, assignmentKeys);
106             	   leaveCalendarDay.setLeaveBlocks(leaveBlocks);
107                } else {
108             	   leaveCalendarDay.setLeaveBlocks(lbs);
109                }
110                
111                if (HrServiceLocator.getHRPermissionService().canViewLeaveTabsWithNEStatus()) {
112 	               TimesheetDocumentHeader tdh = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeaderForDate(principalId, leaveDate.toDateTimeAtStartOfDay());
113 	               if (tdh != null) {
114 	            	   if (leaveDate.isAfter(LocalDate.fromDateFields(tdh.getEndDate()))) {
115 	            		   leaveCalendarDay.setDayEditable(true);
116 	            	   }
117 	               } else {
118 	            	   leaveCalendarDay.setDayEditable(true);
119 	               }
120                } else {
121                    leaveCalendarDay.setDayEditable(true);
122                }
123                //KPME-2560 If leave calendar document is final status, then User wont be able to add leave blocks to the calendar. 
124                if(!dayEditableFlag)
125             	   leaveCalendarDay.setDayEditable(false);
126                
127                dayNumber++;
128             }
129             leaveCalendarDay.setDayNumberString(currentDisplayDateTime.dayOfMonth().getAsShortText());
130             leaveCalendarDay.setDateString(currentDisplayDateTime.toString(HrConstants.DT_BASIC_DATE_FORMAT));
131 
132             leaveCalendarWeek.getDays().add(leaveCalendarDay);
133             
134             if (leaveCalendarWeek.getDays().size() == DateTimeConstants.DAYS_PER_WEEK) {
135                 getWeeks().add(leaveCalendarWeek);
136                 leaveCalendarWeek = new LeaveCalendarWeek();
137             }
138 
139             currentDisplayDateTime = currentDisplayDateTime.plusDays(1);
140         }
141 
142         if (!leaveCalendarWeek.getDays().isEmpty()) {
143             getWeeks().add(leaveCalendarWeek);
144         }
145 
146         boolean isPlanningCal = LmServiceLocator.getLeaveCalendarService().isLeavePlanningCalendar(principalId, calendarEntry.getBeginPeriodFullDateTime().toLocalDate(), calendarEntry.getEndPeriodFullDateTime().toLocalDate());
147         Map<String, String> earnCodes = HrServiceLocator.getEarnCodeService().getEarnCodesForDisplay(principalId, isPlanningCal);
148         setEarnCodeList(earnCodes);
149     }
150 
151     private List<LeaveBlock> createNewLeaveBlockList(LeaveBlock lb){
152         List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
153         leaveBlocks.add(lb);
154         return leaveBlocks;
155     }
156 
157    	public Map<String, String> getEarnCodeList() {
158 		return earnCodeList;
159 	}
160 
161 	public void setEarnCodeList(Map<String, String> earnCodeList) {
162 		this.earnCodeList = earnCodeList;
163 	}
164 }