View Javadoc
1   /**
2    * Copyright 2004-2015 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.kpme.tklm.common;
17  
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.http.HttpServletResponse;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.apache.log4j.Logger;
23  import org.apache.struts.action.ActionForm;
24  import org.apache.struts.action.ActionForward;
25  import org.apache.struts.action.ActionMapping;
26  import org.joda.time.DateTime;
27  import org.kuali.kpme.core.api.leaveplan.LeavePlan;
28  import org.kuali.kpme.core.leaveplan.LeavePlanBo;
29  import org.kuali.kpme.core.service.HrServiceLocator;
30  import org.kuali.kpme.core.util.TKUtils;
31  import org.kuali.kpme.core.web.KPMEAction;
32  import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
33  import org.kuali.rice.kim.api.identity.principal.Principal;
34  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
35  
36  public class CalculateLeaveAccrualsAction extends KPMEAction {
37  	
38  	private static final Logger LOG = Logger.getLogger(ChangeTargetPersonAction.class);
39  	
40      public ActionForward runAccruals(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
41      	CalculateLeaveAccrualsForm calculateLeaveAccrualsForm = (CalculateLeaveAccrualsForm) form;
42      	String messagePrefix = "AccrualServiceImpl.runAccrual() called with ";
43      	String messageParameters = "";
44      	if(StringUtils.isNotBlank(calculateLeaveAccrualsForm.getPrincipalName())) {
45      		Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(calculateLeaveAccrualsForm.getPrincipalName());
46      		if (principal != null) {
47          		if (StringUtils.isNotBlank(calculateLeaveAccrualsForm.getStartDate()) && StringUtils.isNotBlank(calculateLeaveAccrualsForm.getEndDate())) {
48          			DateTime startDate = TKUtils.formatDateTimeString(calculateLeaveAccrualsForm.getStartDate());
49          			DateTime endDate = TKUtils.formatDateTimeString(calculateLeaveAccrualsForm.getEndDate());
50          			messageParameters = "Principal: " + principal.getPrincipalName() + " Start Date: " + TKUtils.formatDateTimeShort(startDate) + " End Date: " + TKUtils.formatDateTimeShort(endDate);
51          			LOG.debug(messagePrefix + messageParameters);
52          			LmServiceLocator.getLeaveAccrualService().runAccrual(principal.getPrincipalId(), startDate, endDate, true);
53          		} else {
54          			messageParameters = "Principal: " + principal.getPrincipalName();
55          			LOG.debug(messagePrefix + messageParameters);
56          			LmServiceLocator.getLeaveAccrualService().runAccrual(principal.getPrincipalId());
57          		}
58      		}	
59      	} else if(StringUtils.isNotBlank(calculateLeaveAccrualsForm.getLeavePlanId())) {
60      		LeavePlan aLeavePlan = HrServiceLocator.getLeavePlanService().getLeavePlan(calculateLeaveAccrualsForm.getLeavePlanId());
61      		if (aLeavePlan != null) {
62          		if (StringUtils.isNotBlank(calculateLeaveAccrualsForm.getStartDate()) && StringUtils.isNotBlank(calculateLeaveAccrualsForm.getEndDate())) {
63          			DateTime startDate = TKUtils.formatDateTimeString(calculateLeaveAccrualsForm.getStartDate());
64          			DateTime endDate = TKUtils.formatDateTimeString(calculateLeaveAccrualsForm.getEndDate());
65          			messageParameters = "Leave Plan: " + aLeavePlan.getLeavePlan() + " Start Date: " + TKUtils.formatDateTimeShort(startDate) + " End Date: " + TKUtils.formatDateTimeShort(endDate);
66          			LOG.debug(messagePrefix + messageParameters);
67          			LmServiceLocator.getLeaveAccrualService().runAccrualForLeavePlan(aLeavePlan, startDate, endDate, true);
68          		} else {
69          			messageParameters = "Leave Plan: " + aLeavePlan.getLeavePlan();
70          			LOG.debug(messagePrefix + messageParameters);
71          			LmServiceLocator.getLeaveAccrualService().runAccrualForLeavePlan(aLeavePlan, null, null, true);
72          		}
73      		}	
74      		
75      	}
76      	
77  		calculateLeaveAccrualsForm.setMessage("Leave accrual calculation has been submitted with " + messageParameters);
78      	return mapping.findForward("basic");
79      }
80      
81      public ActionForward clearAccruals(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
82      	CalculateLeaveAccrualsForm adminForm = (CalculateLeaveAccrualsForm) form;
83      	
84      	if (StringUtils.isNotBlank(adminForm.getPrincipalName())) {
85      		LOG.debug("AccrualServiceImpl.clearAccrual() called with Principal: " + adminForm.getPrincipalName());
86      		adminForm.setPrincipalName("");
87      	} 
88      	if (StringUtils.isNotBlank(adminForm.getLeavePlanId())) {
89      		LOG.debug("AccrualServiceImpl.clearAccrual() called with LeavePlanId: " + adminForm.getLeavePlanId());
90      		adminForm.setLeavePlanId("");
91      	} 
92      	if (StringUtils.isNotBlank(adminForm.getStartDate())) {
93      		LOG.debug("AccrualServiceImpl.clearAccrual() called with Start Date: " + adminForm.getStartDate());
94      		adminForm.setStartDate("");
95      	} 
96      	if (StringUtils.isNotBlank(adminForm.getEndDate())) {
97      		LOG.debug("AccrualServiceImpl.clearAccrual() called with End Date: " + adminForm.getEndDate());
98      		adminForm.setEndDate("");
99      	} 
100     	
101     	return mapping.findForward("basic");
102     }
103 
104 }