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.getCurrentTargetPersonId();
050                    Date currentDate = TKUtils.getTimelessDate(null);
051    
052            Calendar currentCalendar = Calendar.getInstance();
053            if (leaveForm.getNavString() == null) {
054                leaveForm.setYear(currentCalendar.get(Calendar.YEAR));
055            } else if(leaveForm.getNavString().equals("NEXT")) {
056                leaveForm.setYear(leaveForm.getYear() + 1);
057            } else if(leaveForm.getNavString().equals("PREV")) {
058                leaveForm.setYear(leaveForm.getYear() - 1);
059            }
060            currentCalendar.set(leaveForm.getYear(), 0, 1);
061    //        java.util.Date serviceDate = (principalHRAttributes != null) ? principalHRAttributes.getServiceDate() : TKUtils.getTimelessDate(currentCalendar.getTime());
062            java.util.Date beginDate = TKUtils.getTimelessDate(currentCalendar.getTime());
063            currentCalendar.set(leaveForm.getYear(), 11, 31);
064            java.util.Date endDate = TKUtils.getTimelessDate(currentCalendar.getTime());
065    
066    //        CalendarEntries calendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDatesForLeaveCalendar(principalId, currentDate);
067            CalendarEntries calendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDatesForLeaveCalendar(principalId, beginDate, endDate);
068    
069            //  If the current pay period ends before the current leave calendar ends, then we need to include any planned leave blocks that occur
070            //  in this window between the current pay end and the beginning of the leave planning calendar (the next future leave period).
071            //  The most common scenario occurs when a non-monthly pay period ends before the current leave calendar ends.
072    
073            CalendarEntries payCalendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDates(principalId, beginDate, endDate);
074            Boolean checkLeaveEligible = true;
075            Boolean nonExemptLeaveEligible = TkServiceLocator.getLeaveApprovalService().isActiveAssignmentFoundOnJobFlsaStatus(principalId, TkConstants.FLSA_STATUS_NON_EXEMPT,checkLeaveEligible);
076            if(nonExemptLeaveEligible && calendarEntry != null && payCalendarEntry != null) {
077                if ( payCalendarEntry.getEndPeriodDate().before(calendarEntry.getEndPeriodDate()) ) {
078                    calendarEntry = payCalendarEntry;
079                }
080            }
081    
082                    if(calendarEntry != null) {
083                            if(calendarEntry.getEndLocalDateTime().getMillisOfDay() == 0) {
084                                    // if the time of the end date is the beginning of a day, subtract one day from the end date
085                                    currentDate = new java.sql.Date(TKUtils.addDates(calendarEntry.getEndPeriodDate(), -1).getTime());
086                            } else {
087                                    currentDate = calendarEntry.getEndPeriodDate(); // only show leave requests from planning calendars on leave request page
088                            }
089                    }
090            List<LeaveBlock> plannedLeaves = getLeaveBlocksWithRequestStatus(principalId, beginDate, endDate, LMConstants.REQUEST_STATUS.PLANNED);
091            plannedLeaves.addAll(getLeaveBlocksWithRequestStatus(principalId, beginDate, endDate, LMConstants.REQUEST_STATUS.DEFERRED));
092                    leaveForm.setPlannedLeaves(plannedLeaves);
093                    leaveForm.setPendingLeaves(getLeaveBlocksWithRequestStatus(principalId, beginDate, endDate, LMConstants.REQUEST_STATUS.REQUESTED));
094                    leaveForm.setApprovedLeaves(getLeaveBlocksWithRequestStatus(principalId, beginDate, endDate, LMConstants.REQUEST_STATUS.APPROVED));
095                    leaveForm.setDisapprovedLeaves(getDisapprovedLeaveBlockHistory(principalId, currentDate));
096    
097            leaveForm.setDocuments(getLeaveRequestDocuments(leaveForm));
098                    return forward;
099            }
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    }