View Javadoc

1   /**
2    * Copyright 2004-2013 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.hr.time.detail.web;
17  
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.Map.Entry;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.apache.struts.action.ActionForm;
29  import org.apache.struts.action.ActionForward;
30  import org.apache.struts.action.ActionMapping;
31  import org.apache.struts.action.ActionRedirect;
32  import org.joda.time.Interval;
33  import org.kuali.hr.lm.LMConstants;
34  import org.kuali.hr.lm.leavecalendar.LeaveCalendarDocument;
35  import org.kuali.hr.time.base.web.TkAction;
36  import org.kuali.hr.time.calendar.Calendar;
37  import org.kuali.hr.time.principal.PrincipalHRAttributes;
38  import org.kuali.hr.time.roles.TkUserRoles;
39  import org.kuali.hr.time.roles.UserRoles;
40  import org.kuali.hr.time.service.base.TkServiceLocator;
41  import org.kuali.hr.time.timesheet.TimesheetDocument;
42  import org.kuali.hr.time.util.TKContext;
43  import org.kuali.hr.time.util.TKUtils;
44  import org.kuali.hr.time.util.TkConstants;
45  import org.kuali.rice.kew.api.document.DocumentStatus;
46  import org.kuali.rice.krad.exception.AuthorizationException;
47  import org.kuali.rice.krad.util.GlobalVariables;
48  import org.kuali.rice.krad.util.ObjectUtils;
49  
50  public class TimesheetSubmitAction extends TkAction {
51  
52      @Override
53      protected void checkTKAuthorization(ActionForm form, String methodToCall) throws AuthorizationException {
54          TimesheetSubmitActionForm tsaf = (TimesheetSubmitActionForm)form;
55  
56          String principal = TKContext.getPrincipalId();
57          UserRoles roles = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId());
58  
59          TimesheetDocument document = TkServiceLocator.getTimesheetService().getTimesheetDocument(tsaf.getDocumentId());
60          if (!roles.isDocumentWritable(document)) {
61              throw new AuthorizationException(principal, "TimesheetSubmitAction", "");
62          }
63      }
64  
65  
66  
67  
68      public ActionForward approveTimesheet(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
69          TimesheetSubmitActionForm tsaf = (TimesheetSubmitActionForm)form;
70          TimesheetDocument document = TkServiceLocator.getTimesheetService().getTimesheetDocument(tsaf.getDocumentId());
71  
72          // Switched to grab the target (chain, resolution: target -> backdoor -> actual) user.
73          // Approvals still using backdoor > actual
74          if (StringUtils.equals(tsaf.getAction(), TkConstants.DOCUMENT_ACTIONS.ROUTE)) {
75              if (DocumentStatus.INITIATED.getCode().equals(document.getDocumentHeader().getDocumentStatus())
76                      || DocumentStatus.SAVED.getCode().equals(document.getDocumentHeader().getDocumentStatus())) {
77              	boolean nonExemptLE = TkServiceLocator.getLeaveApprovalService().isActiveAssignmentFoundOnJobFlsaStatus(document.getPrincipalId(),
78              				TkConstants.FLSA_STATUS_NON_EXEMPT, true);
79              	if(nonExemptLE) {
80              		//TODO: MaxBalanceService.getMaxBalanceViolations()
81              		Map<String,ArrayList<String>> eligibilities = TkServiceLocator.getBalanceTransferService().getEligibleTransfers(document.getCalendarEntry(),document.getPrincipalId());
82              		List<String> eligibleTransfers = new ArrayList<String>();
83              		eligibleTransfers.addAll(eligibilities.get(LMConstants.MAX_BAL_ACTION_FREQ.LEAVE_APPROVE));
84              		eligibleTransfers.addAll(eligibilities.get(LMConstants.MAX_BAL_ACTION_FREQ.YEAR_END));
85          			if(!eligibleTransfers.isEmpty()) {
86          				int categoryCounter = 0;
87                  		StringBuilder sb = new StringBuilder();
88                  		ActionRedirect redirect = new ActionRedirect();
89                  		for(String accrualRuleId : eligibleTransfers) {
90                  			sb.append("&accrualCategory"+categoryCounter+"="+accrualRuleId);
91                  		}
92                  		redirect.setPath("/BalanceTransfer.do?"+request.getQueryString()+sb.toString());
93                  		return redirect;
94          			}
95              		eligibilities = TkServiceLocator.getLeavePayoutService().getEligiblePayouts(document.getCalendarEntry(),document.getPrincipalId());
96              		List<String> eligiblePayouts = new ArrayList<String>();
97              		eligiblePayouts.addAll(eligibilities.get(LMConstants.MAX_BAL_ACTION_FREQ.LEAVE_APPROVE));
98              		eligiblePayouts.addAll(eligibilities.get(LMConstants.MAX_BAL_ACTION_FREQ.YEAR_END));
99          			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 }