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