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