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.time.batch;
17  
18  import org.apache.log4j.Logger;
19  import org.joda.time.DateTime;
20  import org.kuali.hr.lm.accrual.service.AccrualService;
21  import org.kuali.hr.lm.leavecalendar.service.LeaveCalendarServiceImpl;
22  import org.kuali.hr.lm.leaveplan.LeavePlan;
23  import org.kuali.hr.lm.leaveplan.service.LeavePlanService;
24  import org.kuali.hr.time.assignment.Assignment;
25  import org.kuali.hr.time.assignment.service.AssignmentService;
26  import org.kuali.hr.time.principal.PrincipalHRAttributes;
27  import org.kuali.hr.time.principal.service.PrincipalHRAttributesService;
28  import org.kuali.hr.time.util.TKUtils;
29  import org.kuali.hr.time.util.TkConstants;
30  import org.kuali.rice.core.api.config.property.ConfigContext;
31  import org.kuali.rice.kim.api.identity.principal.Principal;
32  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
33  import org.quartz.Job;
34  import org.quartz.JobExecutionContext;
35  import org.quartz.JobExecutionException;
36  
37  import java.sql.Date;
38  import java.util.List;
39  
40  public class AccrualJob implements Job {
41  
42  	private static final Logger LOG = Logger.getLogger(LeaveCalendarServiceImpl.class);
43  
44  	private static AccrualService ACCRUAL_SERVICE;
45  	private static AssignmentService ASSIGNMENT_SERVICE;
46  	private static LeavePlanService LEAVE_PLAN_SERVICE;
47  	private static PrincipalHRAttributesService PRINCIPAL_HR_ATTRIBUTES_SERVICE;
48  	
49  	@Override
50  	public void execute(JobExecutionContext context) throws JobExecutionException {
51          String batchUserPrincipalId = getBatchUserPrincipalId();
52          
53          if (batchUserPrincipalId != null) {
54      		Date asOfDate = TKUtils.getCurrentDate();
55  			List<Assignment> assignments = getAssignmentService().getActiveAssignments(asOfDate);
56  			
57  			for (Assignment assignment : assignments) {
58  				if (assignment.getJob().isEligibleForLeave()) {
59  					String principalId = assignment.getPrincipalId();
60  					
61  					PrincipalHRAttributes principalHRAttributes = getPrincipalHRAttributesService().getPrincipalCalendar(principalId, asOfDate);
62  					if (principalHRAttributes != null) {
63  						LeavePlan leavePlan = getLeavePlanService().getLeavePlan(principalHRAttributes.getLeavePlan(), principalHRAttributes.getEffectiveDate());
64  						if (leavePlan != null) {
65  							DateTime endDate = new DateTime(asOfDate).plusMonths(Integer.parseInt(leavePlan.getPlanningMonths()));
66  							getAccrualService().runAccrual(principalId, new java.sql.Date(asOfDate.getTime()), new java.sql.Date(endDate.toDate().getTime()), true, batchUserPrincipalId);
67  						}
68  					}
69  				}
70  			}
71          } else {
72          	String principalName = ConfigContext.getCurrentContextConfig().getProperty(TkConstants.BATCH_USER_PRINCIPAL_NAME);
73          	LOG.error("Could not run batch jobs due to missing batch user " + principalName);
74          }
75  	}
76  
77  	public static AccrualService getAccrualService() {
78  		return ACCRUAL_SERVICE;
79  	}
80  
81  	public static void setAccrualService(AccrualService accrualService) {
82  		ACCRUAL_SERVICE = accrualService;
83  	}
84  
85  	public static AssignmentService getAssignmentService() {
86  		return ASSIGNMENT_SERVICE;
87  	}
88  
89  	public static void setAssignmentService(AssignmentService assignmentService) {
90  		ASSIGNMENT_SERVICE = assignmentService;
91  	}
92  
93  	public static LeavePlanService getLeavePlanService() {
94  		return LEAVE_PLAN_SERVICE;
95  	}
96  
97  	public static void setLeavePlanService(LeavePlanService leavePlanService) {
98  		LEAVE_PLAN_SERVICE = leavePlanService;
99  	}
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 }