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.request.web;
17  
18  import java.util.Collections;
19  import java.util.Comparator;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.apache.commons.lang.ObjectUtils;
28  import org.apache.struts.action.ActionForm;
29  import org.apache.struts.action.ActionForward;
30  import org.apache.struts.action.ActionMapping;
31  import org.joda.time.DateTime;
32  import org.joda.time.LocalDate;
33  import org.kuali.kpme.core.calendar.entry.CalendarEntry;
34  import org.kuali.kpme.core.service.HrServiceLocator;
35  import org.kuali.kpme.core.util.HrConstants;
36  import org.kuali.kpme.core.util.HrContext;
37  import org.kuali.kpme.core.web.KPMEAction;
38  import org.kuali.kpme.tklm.common.LMConstants;
39  import org.kuali.kpme.tklm.leave.block.LeaveBlock;
40  import org.kuali.kpme.tklm.leave.block.LeaveBlockHistory;
41  import org.kuali.kpme.tklm.leave.request.service.LeaveRequestDocumentService;
42  import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
43  import org.kuali.kpme.tklm.leave.workflow.LeaveRequestDocument;
44  import org.kuali.rice.kew.api.KewApiServiceLocator;
45  import org.kuali.rice.kew.api.document.DocumentStatus;
46  
47  public class LeaveRequestAction extends KPMEAction {
48      LeaveRequestDocumentService leaveRequestDocumentService;
49  
50  	@Override
51  	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
52  		ActionForward forward = super.execute(mapping, form, request, response);
53  		LeaveRequestForm leaveForm = (LeaveRequestForm) form;
54  		String principalId = HrContext.getTargetPrincipalId();
55  		DateTime currentDate = LocalDate.now().toDateTimeAtStartOfDay();
56  
57          if (leaveForm.getNavString() == null) {
58              leaveForm.setYear(LocalDate.now().getYear());
59          } else if(leaveForm.getNavString().equals("NEXT")) {
60              leaveForm.setYear(leaveForm.getYear() + 1);
61          } else if(leaveForm.getNavString().equals("PREV")) {
62              leaveForm.setYear(leaveForm.getYear() - 1);
63          }
64          LocalDate beginDate = new LocalDate(leaveForm.getYear(), 1, 1);
65          LocalDate endDate = new LocalDate(leaveForm.getYear(), 12, 31);
66  
67  //        CalendarEntry calendarEntry = HrServiceLocator.getCalendarService().getCurrentCalendarDatesForLeaveCalendar(principalId, currentDate);
68          CalendarEntry calendarEntry = HrServiceLocator.getCalendarEntryService().getCurrentCalendarDatesForLeaveCalendar(principalId, beginDate.toDateTimeAtStartOfDay(), endDate.toDateTimeAtStartOfDay());
69  
70          //  If the current pay period ends before the current leave calendar ends, then we need to include any planned leave blocks that occur
71          //  in this window between the current pay end and the beginning of the leave planning calendar (the next future leave period).
72          //  The most common scenario occurs when a non-monthly pay period ends before the current leave calendar ends.
73  
74          CalendarEntry payCalendarEntry = HrServiceLocator.getCalendarEntryService().getCurrentCalendarDates(principalId, beginDate.toDateTimeAtStartOfDay(), endDate.toDateTimeAtStartOfDay());
75          Boolean checkLeaveEligible = true;
76          Boolean nonExemptLeaveEligible = LmServiceLocator.getLeaveApprovalService().isActiveAssignmentFoundOnJobFlsaStatus(principalId, HrConstants.FLSA_STATUS_NON_EXEMPT,checkLeaveEligible);
77          if(nonExemptLeaveEligible && calendarEntry != null && payCalendarEntry != null) {
78              if ( payCalendarEntry.getEndPeriodDate().before(calendarEntry.getEndPeriodDate()) ) {
79                  calendarEntry = payCalendarEntry;
80              }
81          }
82  
83  		if(calendarEntry != null) {
84  			if(calendarEntry.getEndPeriodLocalDateTime().getMillisOfDay() == 0) {
85  				// if the time of the end date is the beginning of a day, subtract one day from the end date
86  				currentDate = calendarEntry.getEndPeriodFullDateTime().minusDays(1);
87  			} else {
88  				currentDate = calendarEntry.getEndPeriodFullDateTime();	// only show leave requests from planning calendars on leave request page
89  			}
90  		}
91          List<LeaveBlock> plannedLeaves = getLeaveBlocksWithRequestStatus(principalId, beginDate, endDate, HrConstants.REQUEST_STATUS.PLANNED);
92          plannedLeaves.addAll(getLeaveBlocksWithRequestStatus(principalId, beginDate, endDate, HrConstants.REQUEST_STATUS.DEFERRED));
93  		leaveForm.setPlannedLeaves(plannedLeaves);
94  		leaveForm.setPendingLeaves(getLeaveBlocksWithRequestStatus(principalId, beginDate, endDate, HrConstants.REQUEST_STATUS.REQUESTED));
95  		leaveForm.setApprovedLeaves(getLeaveBlocksWithRequestStatus(principalId, beginDate, endDate, HrConstants.REQUEST_STATUS.APPROVED));
96  		leaveForm.setDisapprovedLeaves(getDisapprovedLeaveBlockHistory(principalId, currentDate.toLocalDate()));
97  
98          leaveForm.setDocuments(getLeaveRequestDocuments(leaveForm));
99  		return forward;
100 	}
101 
102 
103     private List<LeaveBlock> getLeaveBlocksWithRequestStatus(String principalId, LocalDate beginDate, LocalDate endDate, String requestStatus) {
104         List<LeaveBlock> plannedLeaves = LmServiceLocator.getLeaveBlockService().getLeaveBlocks(principalId, LMConstants.LEAVE_BLOCK_TYPE.LEAVE_CALENDAR, requestStatus, beginDate, endDate);
105 
106         Collections.sort(plannedLeaves, new Comparator<LeaveBlock>() {
107             @Override
108             public int compare(LeaveBlock leaveBlock1, LeaveBlock leaveBlock2) {
109                 return ObjectUtils.compare(leaveBlock1.getLeaveDate(), leaveBlock2.getLeaveDate());
110             }
111         });
112 
113         return plannedLeaves;
114     }
115     
116     private List<LeaveBlockHistory> getDisapprovedLeaveBlockHistory(String principalId, LocalDate currentDate) {
117         List<LeaveBlockHistory> historyList = LmServiceLocator.getLeaveBlockHistoryService()
118         	.getLeaveBlockHistories(principalId, HrConstants.REQUEST_STATUS.DISAPPROVED, HrConstants.ACTION.DELETE, currentDate);
119 
120         Collections.sort(historyList, new Comparator<LeaveBlockHistory>() {
121             @Override
122             public int compare(LeaveBlockHistory lbh1, LeaveBlockHistory lbh2) {
123                 return ObjectUtils.compare(lbh1.getLeaveDate(), lbh2.getLeaveDate());
124             }
125         });
126 
127         return historyList;
128     }
129 
130 	  
131 	public ActionForward submitForApproval(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
132 		LeaveRequestForm lf = (LeaveRequestForm) form;
133 		for(LeaveBlock leaveBlock : lf.getPlannedLeaves()) {
134 			// check if check box is checked
135 			if(leaveBlock.isSubmit()) {
136                 LeaveRequestDocument lrd = LmServiceLocator.getLeaveRequestDocumentService().createLeaveRequestDocument(leaveBlock.getLmLeaveBlockId());
137                 LmServiceLocator.getLeaveRequestDocumentService().requestLeave(lrd.getDocumentNumber());
138 		    }
139 		}
140 	    return mapping.findForward("basic");
141 	}
142 
143     private Map<String, LeaveRequestDocument> getLeaveRequestDocuments(LeaveRequestForm form) {
144         Map<String, LeaveRequestDocument> docs = new HashMap<String, LeaveRequestDocument>();
145 
146         if (form == null) {
147             return docs;
148         }
149 
150         for (LeaveBlock leaveBlock : form.getPendingLeaves()) {
151         	if(leaveBlock.getLeaveRequestDocumentId() != null && !leaveBlock.getLeaveRequestDocumentId().isEmpty()){
152         		docs.put(leaveBlock.getLmLeaveBlockId(), getLeaveRequestDocumentService().getLeaveRequestDocument(leaveBlock.getLeaveRequestDocumentId()));	
153         	}
154         }
155         for (LeaveBlock leaveBlock : form.getApprovedLeaves()) {        	
156         	if(leaveBlock.getLeaveRequestDocumentId() != null && !leaveBlock.getLeaveRequestDocumentId().isEmpty()){
157             docs.put(leaveBlock.getLmLeaveBlockId(), getLeaveRequestDocumentService().getLeaveRequestDocument(leaveBlock.getLeaveRequestDocumentId()));
158         	}
159         }
160         for (LeaveBlockHistory lbh : form.getDisapprovedLeaves()) {
161         	List<LeaveRequestDocument> docList = getLeaveRequestDocumentService().getLeaveRequestDocumentsByLeaveBlockId(lbh.getLmLeaveBlockId());
162         	for(LeaveRequestDocument lrd : docList) {
163         		if(lrd.getDocumentNumber() != null && !lrd.getDocumentNumber().isEmpty()){
164 	        		DocumentStatus status = KewApiServiceLocator.getWorkflowDocumentService().getDocumentStatus(lrd.getDocumentNumber());
165 					if(status != null && DocumentStatus.DISAPPROVED.getCode().equals(status.getCode())) {
166 	    				//KPME-2214
167 						//*getLeaveRequestDocumentService().getLeaveRequestDocument(lrd.getDocumentNumber())* is same as lrd within docList fethced . No need to retrieve again
168 	    				//docs.put(lbh.getLmLeaveBlockId(), getLeaveRequestDocumentService().getLeaveRequestDocument(lrd.getDocumentNumber()));						 
169 						docs.put(lbh.getLmLeaveBlockId(), lrd);
170 						break;
171 					}			 
172         		}
173         	}
174            
175         }
176         return docs;
177     }
178 
179     private LeaveRequestDocumentService getLeaveRequestDocumentService() {
180         if (leaveRequestDocumentService == null) {
181             leaveRequestDocumentService = LmServiceLocator.getLeaveRequestDocumentService();
182         }
183         return leaveRequestDocumentService;
184     }
185 	  
186 }