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