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.getCurrentTargetPerson().getPrincipalId();
50  		Date currentDate = TKUtils.getTimelessDate(null);
51  
52  		CalendarEntries calendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDatesForLeaveCalendar(principalId, currentDate);
53  
54          //  If the current pay period ends before the current leave calendar ends, then we need to include any planned leave blocks that occur
55          //  in this window between the current pay end and the beginning of the leave planning calendar (the next future leave period).
56          //  The most common scenario occurs when a non-monthly pay period ends before the current leave calendar ends.
57  
58          CalendarEntries payCalendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDates(principalId, currentDate);
59          Boolean checkLeaveEligible = true;
60          Boolean nonExemptLeaveEligible = TkServiceLocator.getLeaveApprovalService().isActiveAssignmentFoundOnJobFlsaStatus(principalId, TkConstants.FLSA_STATUS_NON_EXEMPT,checkLeaveEligible);
61          if(nonExemptLeaveEligible && calendarEntry != null && payCalendarEntry != null) {
62              if ( payCalendarEntry.getEndPeriodDate().before(calendarEntry.getEndPeriodDate()) ) {
63                  calendarEntry = payCalendarEntry;
64              }
65          }
66  
67  		if(calendarEntry != null) {
68  			if(calendarEntry.getEndLocalDateTime().getMillisOfDay() == 0) {
69  				// if the time of the end date is the beginning of a day, subtract one day from the end date
70  				currentDate = new java.sql.Date(TKUtils.addDates(calendarEntry.getEndPeriodDate(), -1).getTime());
71  			} else {
72  				currentDate = calendarEntry.getEndPeriodDate();	// only show leave requests from planning calendars on leave request page
73  			}
74  		}
75          List<LeaveBlock> plannedLeaves = getLeaveBlocksWithRequestStatus(principalId, currentDate, LMConstants.REQUEST_STATUS.PLANNED);
76          plannedLeaves.addAll(getLeaveBlocksWithRequestStatus(principalId, currentDate, LMConstants.REQUEST_STATUS.DEFERRED));
77  		leaveForm.setPlannedLeaves(plannedLeaves);
78  		leaveForm.setPendingLeaves(getLeaveBlocksWithRequestStatus(principalId, currentDate, LMConstants.REQUEST_STATUS.REQUESTED));
79  		leaveForm.setApprovedLeaves(getLeaveBlocksWithRequestStatus(principalId, currentDate, LMConstants.REQUEST_STATUS.APPROVED));
80  		leaveForm.setDisapprovedLeaves(getDisapprovedLeaveBlockHistory(principalId, currentDate));
81  
82          leaveForm.setDocuments(getLeaveRequestDocuments(leaveForm));
83  		return forward;
84  	}
85  
86  
87      private List<LeaveBlock> getLeaveBlocksWithRequestStatus(String principalId, Date currentDate, String requestStatus) {
88          List<LeaveBlock> plannedLeaves = TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principalId, LMConstants.LEAVE_BLOCK_TYPE.LEAVE_CALENDAR, requestStatus, currentDate);
89  
90          Collections.sort(plannedLeaves, new Comparator<LeaveBlock>() {
91              @Override
92              public int compare(LeaveBlock leaveBlock1, LeaveBlock leaveBlock2) {
93                  return ObjectUtils.compare(leaveBlock1.getLeaveDate(), leaveBlock2.getLeaveDate());
94              }
95          });
96  
97          return plannedLeaves;
98      }
99      
100     private List<LeaveBlockHistory> getDisapprovedLeaveBlockHistory(String principalId, Date currentDate) {
101         List<LeaveBlockHistory> historyList = TkServiceLocator.getLeaveBlockHistoryService()
102         	.getLeaveBlockHistories(principalId, LMConstants.REQUEST_STATUS.DISAPPROVED, LMConstants.ACTION.DELETE, currentDate);
103 
104         Collections.sort(historyList, new Comparator<LeaveBlockHistory>() {
105             @Override
106             public int compare(LeaveBlockHistory lbh1, LeaveBlockHistory lbh2) {
107                 return ObjectUtils.compare(lbh1.getLeaveDate(), lbh2.getLeaveDate());
108             }
109         });
110 
111         return historyList;
112     }
113 
114 	  
115 	public ActionForward submitForApproval(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
116 		LeaveRequestForm lf = (LeaveRequestForm) form;
117 		for(LeaveBlock leaveBlock : lf.getPlannedLeaves()) {
118 			// check if check box is checked
119 			//System.out.println("Leave block submit is :: >>>>"+leaveBlock.getSubmit());
120 			if(leaveBlock.getSubmit()) {
121                 LeaveRequestDocument lrd = TkServiceLocator.getLeaveRequestDocumentService().createLeaveRequestDocument(leaveBlock.getLmLeaveBlockId());
122                 TkServiceLocator.getLeaveRequestDocumentService().requestLeave(lrd.getDocumentNumber());
123 		    }
124 		}
125 	    return mapping.findForward("basic");
126 	}
127 
128     private Map<String, LeaveRequestDocument> getLeaveRequestDocuments(LeaveRequestForm form) {
129         Map<String, LeaveRequestDocument> docs = new HashMap<String, LeaveRequestDocument>();
130 
131         if (form == null) {
132             return docs;
133         }
134 
135         for (LeaveBlock leaveBlock : form.getPendingLeaves()) {
136             docs.put(leaveBlock.getLmLeaveBlockId(), getLeaveRequestDocumentService().getLeaveRequestDocument(leaveBlock.getLeaveRequestDocumentId()));
137         }
138         for (LeaveBlock leaveBlock : form.getApprovedLeaves()) {
139             docs.put(leaveBlock.getLmLeaveBlockId(), getLeaveRequestDocumentService().getLeaveRequestDocument(leaveBlock.getLeaveRequestDocumentId()));
140         }
141         for (LeaveBlockHistory lbh : form.getDisapprovedLeaves()) {
142         	List<LeaveRequestDocument> docList = getLeaveRequestDocumentService().getLeaveRequestDocumentsByLeaveBlockId(lbh.getLmLeaveBlockId());
143         	for(LeaveRequestDocument lrd : docList) {
144         		DocumentStatus status = KewApiServiceLocator.getWorkflowDocumentService().getDocumentStatus(lrd.getDocumentNumber());
145 				if(status != null && DocumentStatus.DISAPPROVED.getCode().equals(status.getCode())) {
146 					 docs.put(lbh.getLmLeaveBlockId(), getLeaveRequestDocumentService().getLeaveRequestDocument(lrd.getDocumentNumber()));
147 					 break;
148 				}
149         	}
150            
151         }
152         return docs;
153     }
154 
155     private LeaveRequestDocumentService getLeaveRequestDocumentService() {
156         if (leaveRequestDocumentService == null) {
157             leaveRequestDocumentService = TkServiceLocator.getLeaveRequestDocumentService();
158         }
159         return leaveRequestDocumentService;
160     }
161 	  
162 }