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