001 /**
002 * Copyright 2004-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.hr.lm.leave.web;
017
018 import edu.emory.mathcs.backport.java.util.Collections;
019 import org.apache.commons.lang3.ObjectUtils;
020 import org.apache.struts.action.ActionForm;
021 import org.apache.struts.action.ActionForward;
022 import org.apache.struts.action.ActionMapping;
023 import org.kuali.hr.lm.LMConstants;
024 import org.kuali.hr.lm.leaveblock.LeaveBlock;
025 import org.kuali.hr.lm.leaveblock.LeaveBlockHistory;
026 import org.kuali.hr.lm.leaverequest.service.LeaveRequestDocumentService;
027 import org.kuali.hr.lm.workflow.LeaveRequestDocument;
028 import org.kuali.hr.time.base.web.TkAction;
029 import org.kuali.hr.time.calendar.CalendarEntries;
030 import org.kuali.hr.time.service.base.TkServiceLocator;
031 import org.kuali.hr.time.util.TKUser;
032 import org.kuali.hr.time.util.TKUtils;
033 import org.kuali.hr.time.util.TkConstants;
034 import org.kuali.rice.kew.api.KewApiServiceLocator;
035 import org.kuali.rice.kew.api.document.DocumentStatus;
036
037 import javax.servlet.http.HttpServletRequest;
038 import javax.servlet.http.HttpServletResponse;
039 import java.sql.Date;
040 import java.util.*;
041
042 public class LeaveRequestAction extends TkAction {
043 LeaveRequestDocumentService leaveRequestDocumentService;
044
045 @Override
046 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
047 ActionForward forward = super.execute(mapping, form, request, response);
048 LeaveRequestForm leaveForm = (LeaveRequestForm) form;
049 String principalId = TKUser.getCurrentTargetPerson().getPrincipalId();
050 Date currentDate = TKUtils.getTimelessDate(null);
051
052 CalendarEntries calendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDatesForLeaveCalendar(principalId, currentDate);
053
054 // If the current pay period ends before the current leave calendar ends, then we need to include any planned leave blocks that occur
055 // in this window between the current pay end and the beginning of the leave planning calendar (the next future leave period).
056 // The most common scenario occurs when a non-monthly pay period ends before the current leave calendar ends.
057
058 CalendarEntries payCalendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDates(principalId, currentDate);
059 Boolean checkLeaveEligible = true;
060 Boolean nonExemptLeaveEligible = TkServiceLocator.getLeaveApprovalService().isActiveAssignmentFoundOnJobFlsaStatus(principalId, TkConstants.FLSA_STATUS_NON_EXEMPT,checkLeaveEligible);
061 if(nonExemptLeaveEligible && calendarEntry != null && payCalendarEntry != null) {
062 if ( payCalendarEntry.getEndPeriodDate().before(calendarEntry.getEndPeriodDate()) ) {
063 calendarEntry = payCalendarEntry;
064 }
065 }
066
067 if(calendarEntry != null) {
068 if(calendarEntry.getEndLocalDateTime().getMillisOfDay() == 0) {
069 // if the time of the end date is the beginning of a day, subtract one day from the end date
070 currentDate = new java.sql.Date(TKUtils.addDates(calendarEntry.getEndPeriodDate(), -1).getTime());
071 } else {
072 currentDate = calendarEntry.getEndPeriodDate(); // only show leave requests from planning calendars on leave request page
073 }
074 }
075 List<LeaveBlock> plannedLeaves = getLeaveBlocksWithRequestStatus(principalId, currentDate, LMConstants.REQUEST_STATUS.PLANNED);
076 plannedLeaves.addAll(getLeaveBlocksWithRequestStatus(principalId, currentDate, LMConstants.REQUEST_STATUS.DEFERRED));
077 leaveForm.setPlannedLeaves(plannedLeaves);
078 leaveForm.setPendingLeaves(getLeaveBlocksWithRequestStatus(principalId, currentDate, LMConstants.REQUEST_STATUS.REQUESTED));
079 leaveForm.setApprovedLeaves(getLeaveBlocksWithRequestStatus(principalId, currentDate, LMConstants.REQUEST_STATUS.APPROVED));
080 leaveForm.setDisapprovedLeaves(getDisapprovedLeaveBlockHistory(principalId, currentDate));
081
082 leaveForm.setDocuments(getLeaveRequestDocuments(leaveForm));
083 return forward;
084 }
085
086
087 private List<LeaveBlock> getLeaveBlocksWithRequestStatus(String principalId, Date currentDate, String requestStatus) {
088 List<LeaveBlock> plannedLeaves = TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principalId, LMConstants.LEAVE_BLOCK_TYPE.LEAVE_CALENDAR, requestStatus, currentDate);
089
090 Collections.sort(plannedLeaves, new Comparator<LeaveBlock>() {
091 @Override
092 public int compare(LeaveBlock leaveBlock1, LeaveBlock leaveBlock2) {
093 return ObjectUtils.compare(leaveBlock1.getLeaveDate(), leaveBlock2.getLeaveDate());
094 }
095 });
096
097 return plannedLeaves;
098 }
099
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 }