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.batch;
017    
018    import org.apache.log4j.Logger;
019    import org.joda.time.DateTime;
020    import org.kuali.hr.lm.accrual.service.AccrualService;
021    import org.kuali.hr.lm.leavecalendar.service.LeaveCalendarServiceImpl;
022    import org.kuali.hr.lm.leaveplan.LeavePlan;
023    import org.kuali.hr.lm.leaveplan.service.LeavePlanService;
024    import org.kuali.hr.time.assignment.Assignment;
025    import org.kuali.hr.time.assignment.service.AssignmentService;
026    import org.kuali.hr.time.principal.PrincipalHRAttributes;
027    import org.kuali.hr.time.principal.service.PrincipalHRAttributesService;
028    import org.kuali.hr.time.util.TKUtils;
029    import org.kuali.hr.time.util.TkConstants;
030    import org.kuali.rice.core.api.config.property.ConfigContext;
031    import org.kuali.rice.kim.api.identity.principal.Principal;
032    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
033    import org.quartz.Job;
034    import org.quartz.JobExecutionContext;
035    import org.quartz.JobExecutionException;
036    
037    import java.sql.Date;
038    import java.util.List;
039    
040    public class AccrualJob implements Job {
041    
042            private static final Logger LOG = Logger.getLogger(LeaveCalendarServiceImpl.class);
043    
044            private static AccrualService ACCRUAL_SERVICE;
045            private static AssignmentService ASSIGNMENT_SERVICE;
046            private static LeavePlanService LEAVE_PLAN_SERVICE;
047            private static PrincipalHRAttributesService PRINCIPAL_HR_ATTRIBUTES_SERVICE;
048            
049            @Override
050            public void execute(JobExecutionContext context) throws JobExecutionException {
051            String batchUserPrincipalId = getBatchUserPrincipalId();
052            
053            if (batchUserPrincipalId != null) {
054                    Date asOfDate = TKUtils.getCurrentDate();
055                            List<Assignment> assignments = getAssignmentService().getActiveAssignments(asOfDate);
056                            
057                            for (Assignment assignment : assignments) {
058                                    if (assignment.getJob().isEligibleForLeave()) {
059                                            String principalId = assignment.getPrincipalId();
060                                            
061                                            PrincipalHRAttributes principalHRAttributes = getPrincipalHRAttributesService().getPrincipalCalendar(principalId, asOfDate);
062                                            if (principalHRAttributes != null) {
063                                                    LeavePlan leavePlan = getLeavePlanService().getLeavePlan(principalHRAttributes.getLeavePlan(), principalHRAttributes.getEffectiveDate());
064                                                    if (leavePlan != null) {
065                                                            DateTime endDate = new DateTime(asOfDate).plusMonths(Integer.parseInt(leavePlan.getPlanningMonths()));
066                                                            getAccrualService().runAccrual(principalId, new java.sql.Date(asOfDate.getTime()), new java.sql.Date(endDate.toDate().getTime()), true, batchUserPrincipalId);
067                                                    }
068                                            }
069                                    }
070                            }
071            } else {
072                    String principalName = ConfigContext.getCurrentContextConfig().getProperty(TkConstants.BATCH_USER_PRINCIPAL_NAME);
073                    LOG.error("Could not run batch jobs due to missing batch user " + principalName);
074            }
075            }
076    
077            public static AccrualService getAccrualService() {
078                    return ACCRUAL_SERVICE;
079            }
080    
081            public static void setAccrualService(AccrualService accrualService) {
082                    ACCRUAL_SERVICE = accrualService;
083            }
084    
085            public static AssignmentService getAssignmentService() {
086                    return ASSIGNMENT_SERVICE;
087            }
088    
089            public static void setAssignmentService(AssignmentService assignmentService) {
090                    ASSIGNMENT_SERVICE = assignmentService;
091            }
092    
093            public static LeavePlanService getLeavePlanService() {
094                    return LEAVE_PLAN_SERVICE;
095            }
096    
097            public static void setLeavePlanService(LeavePlanService leavePlanService) {
098                    LEAVE_PLAN_SERVICE = leavePlanService;
099            }
100    
101            public static PrincipalHRAttributesService getPrincipalHRAttributesService() {
102                    return PRINCIPAL_HR_ATTRIBUTES_SERVICE;
103            }
104    
105            public static void setPrincipalHRAttributesService(PrincipalHRAttributesService principalHRAttributesService) {
106                    PRINCIPAL_HR_ATTRIBUTES_SERVICE = principalHRAttributesService;
107            }
108    
109        private String getBatchUserPrincipalId() {
110            String principalName = ConfigContext.getCurrentContextConfig().getProperty(TkConstants.BATCH_USER_PRINCIPAL_NAME);
111            Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalName);
112            return principal == null ? null : principal.getPrincipalId();
113        }
114    
115    }