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