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.leavecalendar.web;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    import java.util.Map;
021    import java.util.Map.Entry;
022    
023    import org.apache.commons.lang.StringUtils;
024    import org.apache.commons.lang.time.DateUtils;
025    import org.apache.struts.action.ActionForm;
026    import org.apache.struts.action.ActionForward;
027    import org.apache.struts.action.ActionMapping;
028    import org.apache.struts.action.ActionRedirect;
029    import org.kuali.hr.lm.LMConstants;
030    import org.kuali.hr.lm.accrual.AccrualCategory;
031    import org.kuali.hr.lm.leavecalendar.LeaveCalendarDocument;
032    import org.kuali.hr.lm.leaveplan.LeavePlan;
033    import org.kuali.hr.lm.workflow.service.LeaveCalendarDocumentHeaderService;
034    import org.kuali.hr.time.base.web.TkAction;
035    import org.kuali.hr.time.principal.PrincipalHRAttributes;
036    import org.kuali.hr.time.roles.TkUserRoles;
037    import org.kuali.hr.time.roles.UserRoles;
038    import org.kuali.hr.time.service.base.TkServiceLocator;
039    import org.kuali.hr.time.util.TKContext;
040    import org.kuali.hr.time.util.TKUtils;
041    import org.kuali.hr.time.util.TkConstants;
042    import org.kuali.rice.kew.api.document.DocumentStatus;
043    import org.kuali.rice.krad.exception.AuthorizationException;
044    import org.kuali.rice.krad.util.GlobalVariables;
045    import org.kuali.rice.krad.util.ObjectUtils;
046    
047    import javax.servlet.http.HttpServletRequest;
048    import javax.servlet.http.HttpServletResponse;
049    
050    
051    public class LeaveCalendarSubmitAction extends TkAction {
052        @Override
053        protected void checkTKAuthorization(ActionForm form, String methodToCall) throws AuthorizationException {
054            LeaveCalendarSubmitForm lcf = (LeaveCalendarSubmitForm)form;
055    
056            String principal = TKContext.getPrincipalId();
057            UserRoles roles = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId());
058    
059            LeaveCalendarDocument document = TkServiceLocator.getLeaveCalendarService().getLeaveCalendarDocument(lcf.getDocumentId());
060            if (!roles.isDocumentWritable(document)) {
061                throw new AuthorizationException(principal, "LeaveCalendarSubmitAction", "");
062            }
063        }
064        
065        public ActionForward approveLeaveCalendar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
066            String documentId = request.getParameter("documentId");
067            String action = request.getParameter("action");
068            LeaveCalendarSubmitForm lcf = (LeaveCalendarSubmitForm)form;
069            LeaveCalendarDocument document = TkServiceLocator.getLeaveCalendarService().getLeaveCalendarDocument(documentId);
070    
071            // Switched to grab the target (chain, resolution: target -> backdoor -> actual) user.
072            // Approvals still using backdoor > actual
073            if (StringUtils.equals(action, TkConstants.DOCUMENT_ACTIONS.ROUTE)) {
074                if (DocumentStatus.INITIATED.getCode().equals(document.getDocumentHeader().getDocumentStatus())
075                        || DocumentStatus.SAVED.getCode().equals(document.getDocumentHeader().getDocumentStatus())) {
076                            Map<String,ArrayList<String>> eligibilities = TkServiceLocator.getBalanceTransferService().getEligibleTransfers(document.getCalendarEntry(), document.getPrincipalId());
077                            List<String> eligibleTransfers = new ArrayList<String>();
078                            eligibleTransfers.addAll(eligibilities.get(LMConstants.MAX_BAL_ACTION_FREQ.LEAVE_APPROVE));
079                            eligibleTransfers.addAll(eligibilities.get(LMConstants.MAX_BAL_ACTION_FREQ.YEAR_END));
080                            if(!eligibleTransfers.isEmpty()) {
081                            int categoryCounter = 0;
082                                    StringBuilder sb = new StringBuilder();
083                            ActionRedirect redirect = new ActionRedirect();
084                            for(String accrualRuleId : eligibleTransfers) {
085                                    sb.append("&accrualCategory"+categoryCounter+"="+accrualRuleId);
086                            }
087                            redirect.setPath("/BalanceTransfer.do?"+request.getQueryString()+sb.toString());
088                            return redirect;
089                            }
090                            eligibilities = TkServiceLocator.getLeavePayoutService().getEligiblePayouts(document.getCalendarEntry(),document.getPrincipalId());
091                            List<String> eligiblePayouts = new ArrayList<String>();
092                            eligiblePayouts.addAll(eligibilities.get(LMConstants.MAX_BAL_ACTION_FREQ.LEAVE_APPROVE));
093                            eligiblePayouts.addAll(eligibilities.get(LMConstants.MAX_BAL_ACTION_FREQ.YEAR_END));
094                            if(!eligiblePayouts.isEmpty()) {
095                                    int categoryCounter = 0;
096                            StringBuilder sb = new StringBuilder();
097                            ActionRedirect redirect = new ActionRedirect();
098                            for(String accrualRuleId : eligiblePayouts) {
099                                    sb.append("&accrualCategory"+categoryCounter+"="+accrualRuleId);
100                            }
101                            redirect.setPath("/LeavePayout.do?"+request.getQueryString()+sb.toString());
102                            return redirect;
103                            }
104                    TkServiceLocator.getLeaveCalendarService().routeLeaveCalendar(TKContext.getTargetPrincipalId(), document);
105                }
106            } else if (StringUtils.equals(action, TkConstants.DOCUMENT_ACTIONS.APPROVE)) {
107                if (TkServiceLocator.getLeaveCalendarService().isReadyToApprove(document)) {
108                    if (document.getDocumentHeader().getDocumentStatus().equals(DocumentStatus.ENROUTE.getCode())) {
109                        TkServiceLocator.getLeaveCalendarService().approveLeaveCalendar(TKContext.getPrincipalId(), document);
110                    }
111                } else {
112                    //ERROR!!!!
113                }
114            } else if (StringUtils.equals(action, TkConstants.DOCUMENT_ACTIONS.DISAPPROVE)) {
115                if (document.getDocumentHeader().getDocumentStatus().equals(DocumentStatus.ENROUTE.getCode())) {
116                    TkServiceLocator.getLeaveCalendarService().disapproveLeaveCalendar(TKContext.getPrincipalId(), document);
117                }
118            }
119            ActionRedirect rd = new ActionRedirect(mapping.findForward("leaveCalendarRedirect"));
120            TkServiceLocator.getTkSearchableAttributeService().updateSearchableAttribute(document, document.getAsOfDate());
121            rd.addParameter("documentId", documentId);
122    
123            return rd;
124        }
125    
126        public ActionForward approveApprovalTab(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
127            LeaveCalendarSubmitForm lcf = (LeaveCalendarSubmitForm)form;
128            LeaveCalendarDocument document = TkServiceLocator.getLeaveCalendarService().getLeaveCalendarDocument(lcf.getDocumentId());
129    
130            // Switched to grab the target (chain, resolution: target -> backdoor -> actual) user.
131            // Approvals still using backdoor > actual
132            if (StringUtils.equals(lcf.getAction(), TkConstants.DOCUMENT_ACTIONS.ROUTE)) {
133                if (document.getDocumentHeader().getDocumentStatus().equals(DocumentStatus.INITIATED.getCode())) {
134                    TkServiceLocator.getLeaveCalendarService().routeLeaveCalendar(TKContext.getTargetPrincipalId(), document);
135                }
136            } else if (StringUtils.equals(lcf.getAction(), TkConstants.DOCUMENT_ACTIONS.APPROVE)) {
137                //Todo:  check for unfinalized BalanceTransfer on current leave calendar.
138                if (TkServiceLocator.getLeaveCalendarService().isReadyToApprove(document)) {
139                    if (document.getDocumentHeader().getDocumentStatus().equals(DocumentStatus.ENROUTE.getCode())) {
140                        TkServiceLocator.getLeaveCalendarService().approveLeaveCalendar(TKContext.getPrincipalId(), document);
141                    }
142                } else {
143                    //ERROR!!!!
144                }
145    
146            } else if (StringUtils.equals(lcf.getAction(), TkConstants.DOCUMENT_ACTIONS.DISAPPROVE)) {
147                if (document.getDocumentHeader().getDocumentStatus().equals(DocumentStatus.ENROUTE.getCode())) {
148                    TkServiceLocator.getLeaveCalendarService().disapproveLeaveCalendar(TKContext.getPrincipalId(), document);
149                }
150            }
151    
152            TKContext.getUser().clearTargetUser();
153            return new ActionRedirect(mapping.findForward("approverRedirect"));
154    
155        }
156    
157    }