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