001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.time.admin.web;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.apache.log4j.Logger;
020    import org.apache.struts.action.ActionForm;
021    import org.apache.struts.action.ActionForward;
022    import org.apache.struts.action.ActionMapping;
023    import org.kuali.hr.time.base.web.TkAction;
024    import org.kuali.hr.time.service.base.TkServiceLocator;
025    import org.kuali.rice.kim.api.identity.principal.Principal;
026    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
027    
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.http.HttpServletResponse;
030    import java.text.DateFormat;
031    import java.text.SimpleDateFormat;
032    
033    public class CalculateLeaveAccrualsAction extends TkAction {
034            
035            private static final Logger LOG = Logger.getLogger(ChangeTargetPersonAction.class);
036            
037        public ActionForward runAccruals(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
038            CalculateLeaveAccrualsForm calculateLeaveAccrualsForm = (CalculateLeaveAccrualsForm) form;
039            
040            DateFormat formater = new SimpleDateFormat("MM/dd/yyyy");
041            Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(calculateLeaveAccrualsForm.getPrincipalName());
042                    if (principal != null) {
043                    if (StringUtils.isNotBlank(calculateLeaveAccrualsForm.getStartDate()) && StringUtils.isNotBlank(calculateLeaveAccrualsForm.getEndDate())) {
044                            java.util.Date parsedStartDate = formater.parse(calculateLeaveAccrualsForm.getStartDate());
045                            java.sql.Date startDate= new java.sql.Date(parsedStartDate.getTime());
046    
047                            java.util.Date parsedEndDate = formater.parse(calculateLeaveAccrualsForm.getEndDate());
048                            java.sql.Date endDate= new java.sql.Date(parsedEndDate.getTime());
049            
050                            LOG.debug("AccrualServiceImpl.runAccrual() called with Principal: " + principal.getPrincipalName() + " Start: " + startDate.toString() + " End: " + endDate.toString());
051                            TkServiceLocator.getLeaveAccrualService().runAccrual(principal.getPrincipalId(), startDate, endDate, true);
052                    } else {
053                            LOG.debug("AccrualServiceImpl.runAccrual() called with Principal: " + principal.getPrincipalName());
054                            TkServiceLocator.getLeaveAccrualService().runAccrual(principal.getPrincipalId());
055                    }
056                    }
057            return mapping.findForward("basic");
058        }
059        
060        public ActionForward clearAccruals(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
061            CalculateLeaveAccrualsForm adminForm = (CalculateLeaveAccrualsForm) form;
062            
063            if (StringUtils.isNotBlank(adminForm.getPrincipalName())) {
064                    LOG.debug("AccrualServiceImpl.clearAccrual() called with Principal: " + adminForm.getPrincipalName());
065                    adminForm.setPrincipalName("");
066            } 
067            if (StringUtils.isNotBlank(adminForm.getStartDate())) {
068                    LOG.debug("AccrualServiceImpl.clearAccrual() called with Start Date: " + adminForm.getStartDate());
069                    adminForm.setStartDate("");
070            } 
071            if (StringUtils.isNotBlank(adminForm.getEndDate())) {
072                    LOG.debug("AccrualServiceImpl.clearAccrual() called with End Date: " + adminForm.getEndDate());
073                    adminForm.setEndDate("");
074            } 
075            
076            return mapping.findForward("basic");
077        }
078    
079    }