1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.batch;
17
18 import java.sql.Date;
19 import java.util.List;
20
21 import org.apache.log4j.Logger;
22 import org.joda.time.DateTime;
23 import org.kuali.hr.lm.accrual.service.AccrualService;
24 import org.kuali.hr.lm.leavecalendar.service.LeaveCalendarServiceImpl;
25 import org.kuali.hr.lm.leaveplan.LeavePlan;
26 import org.kuali.hr.lm.leaveplan.service.LeavePlanService;
27 import org.kuali.hr.time.assignment.Assignment;
28 import org.kuali.hr.time.assignment.service.AssignmentService;
29 import org.kuali.hr.time.principal.PrincipalHRAttributes;
30 import org.kuali.hr.time.principal.service.PrincipalHRAttributesService;
31 import org.kuali.hr.time.util.TKUtils;
32 import org.kuali.hr.time.util.TkConstants;
33 import org.kuali.rice.core.api.config.property.ConfigContext;
34 import org.kuali.rice.kim.api.identity.Person;
35 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
36 import org.quartz.Job;
37 import org.quartz.JobExecutionContext;
38 import org.quartz.JobExecutionException;
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 principalId = null;
111
112 String principalName = ConfigContext.getCurrentContextConfig().getProperty(TkConstants.BATCH_USER_PRINCIPAL_NAME);
113 Person person = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(principalName);
114 if (person != null) {
115 principalId = person.getPrincipalId();
116 }
117
118 return principalId;
119 }
120
121 }