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.Comparator;
20  import java.util.HashSet;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Map.Entry;
24  import java.util.Set;
25  
26  import org.apache.commons.lang.StringUtils;
27  import org.apache.commons.lang.time.DateUtils;
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.accrual.AccrualCategory;
35  import org.kuali.hr.lm.accrual.AccrualCategoryRule;
36  import org.kuali.hr.lm.leaveblock.LeaveBlock;
37  import org.kuali.hr.lm.leavecalendar.LeaveCalendarDocument;
38  import org.kuali.hr.lm.leaveplan.LeavePlan;
39  import org.kuali.hr.lm.workflow.service.LeaveCalendarDocumentHeaderService;
40  import org.kuali.hr.time.base.web.TkAction;
41  import org.kuali.hr.time.calendar.Calendar;
42  import org.kuali.hr.time.principal.PrincipalHRAttributes;
43  import org.kuali.hr.time.roles.TkUserRoles;
44  import org.kuali.hr.time.roles.UserRoles;
45  import org.kuali.hr.time.service.base.TkServiceLocator;
46  import org.kuali.hr.time.util.TKContext;
47  import org.kuali.hr.time.util.TKUser;
48  import org.kuali.hr.time.util.TKUtils;
49  import org.kuali.hr.time.util.TkConstants;
50  import org.kuali.rice.kew.api.document.DocumentStatus;
51  import org.kuali.rice.krad.exception.AuthorizationException;
52  import org.kuali.rice.krad.util.GlobalVariables;
53  import org.kuali.rice.krad.util.ObjectUtils;
54  
55  import edu.emory.mathcs.backport.java.util.Collections;
56  
57  import javax.servlet.http.HttpServletRequest;
58  import javax.servlet.http.HttpServletResponse;
59  
60  
61  public class LeaveCalendarSubmitAction extends TkAction {
62      @Override
63      protected void checkTKAuthorization(ActionForm form, String methodToCall) throws AuthorizationException {
64          LeaveCalendarSubmitForm lcf = (LeaveCalendarSubmitForm)form;
65  
66          String principal = TKContext.getPrincipalId();
67          UserRoles roles = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId());
68  
69          LeaveCalendarDocument document = TkServiceLocator.getLeaveCalendarService().getLeaveCalendarDocument(lcf.getDocumentId());
70          if (!roles.isDocumentWritable(document)) {
71              throw new AuthorizationException(principal, "LeaveCalendarSubmitAction", "");
72          }
73      }
74      
75      public ActionForward approveLeaveCalendar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
76      	String documentId = request.getParameter("documentId");
77      	String action = request.getParameter("action");
78          LeaveCalendarDocument document = TkServiceLocator.getLeaveCalendarService().getLeaveCalendarDocument(documentId);
79  
80          // Switched to grab the target (chain, resolution: target -> backdoor -> actual) user.
81          // Approvals still using backdoor > actual
82          if (StringUtils.equals(action, TkConstants.DOCUMENT_ACTIONS.ROUTE)) {
83              if (DocumentStatus.INITIATED.getCode().equals(document.getDocumentHeader().getDocumentStatus())
84                      || DocumentStatus.SAVED.getCode().equals(document.getDocumentHeader().getDocumentStatus())) {
85              	
86          		Map<String,Set<LeaveBlock>> eligibilities = TkServiceLocator.getAccrualCategoryMaxBalanceService().getMaxBalanceViolations(document.getCalendarEntry(), document.getPrincipalId());
87          		
88          		ActionRedirect transferRedirect = new ActionRedirect();
89          		ActionRedirect payoutRedirect = new ActionRedirect();
90  				
91  				List<LeaveBlock> eligibleTransfers = new ArrayList<LeaveBlock>();
92  				List<LeaveBlock> eligiblePayouts = new ArrayList<LeaveBlock>();
93          		Interval interval = new Interval(document.getCalendarEntry().getBeginPeriodDate().getTime(), document.getCalendarEntry().getEndPeriodDate().getTime());
94          		for(Entry<String,Set<LeaveBlock>> entry : eligibilities.entrySet()) {
95          			
96              		for(LeaveBlock lb : entry.getValue()) {
97              			if(interval.contains(lb.getLeaveDate().getTime())) {
98  	            			//maxBalanceViolations should, if a violation exists, return a leave block with leave date either current date, or the end period date - 1 days.
99                      		PrincipalHRAttributes pha = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(document.getPrincipalId(), lb.getLeaveDate());
100 	        				AccrualCategoryRule aRule = TkServiceLocator.getAccrualCategoryRuleService().getAccrualCategoryRule(lb.getAccrualCategoryRuleId());
101 	
102 	            			if(ObjectUtils.isNotNull(aRule)
103 	            					&& !StringUtils.equals(aRule.getMaxBalanceActionFrequency(),LMConstants.MAX_BAL_ACTION_FREQ.ON_DEMAND)) {
104 	            				if(StringUtils.equals(aRule.getMaxBalanceActionFrequency(),LMConstants.MAX_BAL_ACTION_FREQ.LEAVE_APPROVE)
105 	            						|| (StringUtils.equals(aRule.getMaxBalanceActionFrequency(),LMConstants.MAX_BAL_ACTION_FREQ.YEAR_END)
106 	            								&& TkServiceLocator.getLeavePlanService().isLastCalendarPeriodOfLeavePlan(document.getCalendarEntry(),pha.getLeavePlan(),lb.getLeaveDate())))
107 			            			if(StringUtils.equals(aRule.getActionAtMaxBalance(),LMConstants.ACTION_AT_MAX_BAL.PAYOUT)) {
108 			            				eligiblePayouts.add(lb);
109 			            			}
110 			            			else if(StringUtils.equals(aRule.getActionAtMaxBalance(), LMConstants.ACTION_AT_MAX_BAL.TRANSFER)
111 			            					|| StringUtils.equals(aRule.getActionAtMaxBalance(), LMConstants.ACTION_AT_MAX_BAL.LOSE)) {
112 			            				eligibleTransfers.add(lb);
113 			            			}
114 	            			}
115             			}
116             		}
117         		}
118         		if(!eligibleTransfers.isEmpty()) {
119             		transferRedirect.setPath("/BalanceTransfer.do?"+request.getQueryString());
120             		request.getSession().setAttribute("eligibilities", eligibleTransfers);
121             		return transferRedirect;
122         		}
123         		if(!eligiblePayouts.isEmpty()) {
124             		payoutRedirect.setPath("/LeavePayout.do?"+request.getQueryString());
125             		request.getSession().setAttribute("eligibilities", eligiblePayouts);
126             		return payoutRedirect;           			
127         		}
128                 TkServiceLocator.getLeaveCalendarService().routeLeaveCalendar(TKContext.getTargetPrincipalId(), document);
129             }
130         } else if (StringUtils.equals(action, TkConstants.DOCUMENT_ACTIONS.APPROVE)) {
131             if (TkServiceLocator.getLeaveCalendarService().isReadyToApprove(document)) {
132                 if (document.getDocumentHeader().getDocumentStatus().equals(DocumentStatus.ENROUTE.getCode())) {
133                     TkServiceLocator.getLeaveCalendarService().approveLeaveCalendar(TKContext.getPrincipalId(), document);
134                 }
135             } else {
136                 //ERROR!!!!
137             }
138         } else if (StringUtils.equals(action, TkConstants.DOCUMENT_ACTIONS.DISAPPROVE)) {
139             if (document.getDocumentHeader().getDocumentStatus().equals(DocumentStatus.ENROUTE.getCode())) {
140                 TkServiceLocator.getLeaveCalendarService().disapproveLeaveCalendar(TKContext.getPrincipalId(), document);
141             }
142         }
143         ActionRedirect rd = new ActionRedirect(mapping.findForward("leaveCalendarRedirect"));
144         TkServiceLocator.getTkSearchableAttributeService().updateSearchableAttribute(document, document.getAsOfDate());
145         rd.addParameter("documentId", documentId);
146 
147         return rd;
148     }
149 
150     public ActionForward approveApprovalTab(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
151         LeaveCalendarSubmitForm lcf = (LeaveCalendarSubmitForm)form;
152         LeaveCalendarDocument document = TkServiceLocator.getLeaveCalendarService().getLeaveCalendarDocument(lcf.getDocumentId());
153 
154         // Switched to grab the target (chain, resolution: target -> backdoor -> actual) user.
155         // Approvals still using backdoor > actual
156         if (StringUtils.equals(lcf.getAction(), TkConstants.DOCUMENT_ACTIONS.ROUTE)) {
157             if (document.getDocumentHeader().getDocumentStatus().equals(DocumentStatus.INITIATED.getCode())) {
158                 TkServiceLocator.getLeaveCalendarService().routeLeaveCalendar(TKContext.getTargetPrincipalId(), document);
159             }
160         } else if (StringUtils.equals(lcf.getAction(), TkConstants.DOCUMENT_ACTIONS.APPROVE)) {
161             //Todo:  check for unfinalized BalanceTransfer on current leave calendar.
162             if (TkServiceLocator.getLeaveCalendarService().isReadyToApprove(document)) {
163                 if (document.getDocumentHeader().getDocumentStatus().equals(DocumentStatus.ENROUTE.getCode())) {
164                     TkServiceLocator.getLeaveCalendarService().approveLeaveCalendar(TKContext.getPrincipalId(), document);
165                 }
166             } else {
167                 //ERROR!!!!
168             }
169 
170         } else if (StringUtils.equals(lcf.getAction(), TkConstants.DOCUMENT_ACTIONS.DISAPPROVE)) {
171             if (document.getDocumentHeader().getDocumentStatus().equals(DocumentStatus.ENROUTE.getCode())) {
172                 TkServiceLocator.getLeaveCalendarService().disapproveLeaveCalendar(TKContext.getPrincipalId(), document);
173             }
174         }
175 
176         TKUser.clearTargetUser();
177         return new ActionRedirect(mapping.findForward("approverRedirect"));
178 
179     }
180 
181 }