1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.tklm.leave.batch;
17
18 import org.apache.log4j.Logger;
19 import org.joda.time.DateTime;
20 import org.joda.time.LocalDate;
21 import org.kuali.kpme.core.api.assignment.Assignment;
22 import org.kuali.kpme.core.api.assignment.service.AssignmentService;
23 import org.kuali.kpme.core.api.leaveplan.LeavePlanContract;
24 import org.kuali.kpme.core.api.leaveplan.LeavePlanService;
25 import org.kuali.kpme.core.api.principal.PrincipalHRAttributes;
26 import org.kuali.kpme.core.api.principal.service.PrincipalHRAttributesService;
27 import org.kuali.kpme.core.batch.BatchJob;
28 import org.kuali.kpme.tklm.api.leave.accrual.AccrualService;
29 import org.kuali.kpme.tklm.leave.calendar.service.LeaveCalendarServiceImpl;
30 import org.quartz.JobExecutionContext;
31 import org.quartz.JobExecutionException;
32
33 import java.util.List;
34
35 public class AccrualJob extends BatchJob {
36
37 private static final Logger LOG = Logger.getLogger(LeaveCalendarServiceImpl.class);
38
39 private static AccrualService ACCRUAL_SERVICE;
40 private static AssignmentService ASSIGNMENT_SERVICE;
41 private static LeavePlanService LEAVE_PLAN_SERVICE;
42 private static PrincipalHRAttributesService PRINCIPAL_HR_ATTRIBUTES_SERVICE;
43
44 @Override
45 public void execute(JobExecutionContext context) throws JobExecutionException {
46 String batchUserPrincipalId = getBatchUserPrincipalId();
47
48 if (batchUserPrincipalId != null) {
49 LocalDate asOfDate = LocalDate.now();
50 List<Assignment> assignments = getAssignmentService().getActiveAssignments(asOfDate);
51
52 for (Assignment assignment : assignments) {
53 if (assignment.getJob().isEligibleForLeave()) {
54 String principalId = assignment.getPrincipalId();
55
56 PrincipalHRAttributes principalHRAttributes = getPrincipalHRAttributesService().getPrincipalCalendar(principalId, asOfDate);
57 if (principalHRAttributes != null) {
58 LeavePlanContract leavePlan = getLeavePlanService().getLeavePlan(principalHRAttributes.getLeavePlan(), principalHRAttributes.getEffectiveLocalDate());
59 if (leavePlan != null) {
60 DateTime endDate = asOfDate.toDateTimeAtStartOfDay().plusMonths(Integer.parseInt(leavePlan.getPlanningMonths()));
61 getAccrualService().runAccrual(principalId, asOfDate.toDateTimeAtStartOfDay(), endDate, true, batchUserPrincipalId);
62 }
63 }
64 }
65 }
66 } else {
67 String principalName = getBatchUserPrincipalName();
68 LOG.error("Could not run batch jobs due to missing batch user " + principalName);
69 }
70 }
71
72 public static AccrualService getAccrualService() {
73 return ACCRUAL_SERVICE;
74 }
75
76 public static void setAccrualService(AccrualService accrualService) {
77 ACCRUAL_SERVICE = accrualService;
78 }
79
80 public static AssignmentService getAssignmentService() {
81 return ASSIGNMENT_SERVICE;
82 }
83
84 public static void setAssignmentService(AssignmentService assignmentService) {
85 ASSIGNMENT_SERVICE = assignmentService;
86 }
87
88 public static LeavePlanService getLeavePlanService() {
89 return LEAVE_PLAN_SERVICE;
90 }
91
92 public static void setLeavePlanService(LeavePlanService leavePlanService) {
93 LEAVE_PLAN_SERVICE = leavePlanService;
94 }
95
96 public static PrincipalHRAttributesService getPrincipalHRAttributesService() {
97 return PRINCIPAL_HR_ATTRIBUTES_SERVICE;
98 }
99
100 public static void setPrincipalHRAttributesService(PrincipalHRAttributesService principalHRAttributesService) {
101 PRINCIPAL_HR_ATTRIBUTES_SERVICE = principalHRAttributesService;
102 }
103
104 }