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 org.apache.commons.lang.StringUtils;
19 import org.apache.log4j.Logger;
20 import org.joda.time.DateTime;
21 import org.kuali.hr.lm.leavecalendar.LeaveCalendarDocument;
22 import org.kuali.hr.lm.workflow.LeaveCalendarDocumentHeader;
23 import org.kuali.hr.time.calendar.Calendar;
24 import org.kuali.hr.time.calendar.CalendarEntries;
25 import org.kuali.hr.time.missedpunch.MissedPunchDocument;
26 import org.kuali.hr.time.service.base.TkServiceLocator;
27 import org.kuali.hr.time.timesheet.TimesheetDocument;
28 import org.kuali.hr.time.util.TkConstants;
29 import org.kuali.hr.time.workflow.TimesheetDocumentHeader;
30 import org.kuali.rice.core.api.config.property.ConfigContext;
31 import org.kuali.rice.kew.actionitem.ActionItemActionListExtension;
32 import org.kuali.rice.kew.service.KEWServiceLocator;
33 import org.kuali.rice.kim.api.identity.principal.Principal;
34 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
35 import org.quartz.*;
36
37 import java.util.Collection;
38 import java.util.Date;
39 import java.util.List;
40
41 public class SupervisorApprovalJob implements Job {
42
43 private static final Logger LOG = Logger.getLogger(SupervisorApprovalJob.class);
44
45 public void execute(JobExecutionContext context) throws JobExecutionException {
46 String batchUserPrincipalId = getBatchUserPrincipalId();
47
48 if (batchUserPrincipalId != null) {
49 JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
50
51 String hrCalendarEntriesId = jobDataMap.getString("hrCalendarEntriesId");
52 String documentId = jobDataMap.getString("documentId");
53
54 CalendarEntries calendarEntry = TkServiceLocator.getCalendarEntriesService().getCalendarEntries(hrCalendarEntriesId);
55 Calendar calendar = TkServiceLocator.getCalendarService().getCalendar(calendarEntry.getHrCalendarId());
56
57 if (StringUtils.equals(calendar.getCalendarTypes(), "Pay")) {
58 TimesheetDocumentHeader timesheetDocumentHeader = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeader(documentId);
59 if (timesheetDocumentHeader != null) {
60 if (missedPunchDocumentsNotFinal(documentId) || documentNotEnroute(documentId)) {
61 rescheduleJob(context);
62 } else {
63 TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(documentId);
64 TkServiceLocator.getTimesheetService().approveTimesheet(batchUserPrincipalId, timesheetDocument, TkConstants.BATCH_JOB_ACTIONS.BATCH_JOB_APPROVE);
65 }
66 }
67 } else if (StringUtils.equals(calendar.getCalendarTypes(), "Leave")) {
68 LeaveCalendarDocumentHeader leaveCalendarDocumentHeader = TkServiceLocator.getLeaveCalendarDocumentHeaderService().getDocumentHeader(documentId);
69 if (leaveCalendarDocumentHeader != null) {
70 if (documentNotEnroute(documentId)) {
71 rescheduleJob(context);
72 } else {
73 LeaveCalendarDocument leaveCalendarDocument = TkServiceLocator.getLeaveCalendarService().getLeaveCalendarDocument(documentId);
74 TkServiceLocator.getLeaveCalendarService().approveLeaveCalendar(batchUserPrincipalId, leaveCalendarDocument, TkConstants.BATCH_JOB_ACTIONS.BATCH_JOB_APPROVE);
75 }
76 }
77 }
78 } else {
79 String principalName = ConfigContext.getCurrentContextConfig().getProperty(TkConstants.BATCH_USER_PRINCIPAL_NAME);
80 LOG.error("Could not run batch jobs due to missing batch user " + principalName);
81 }
82 }
83
84 private String getBatchUserPrincipalId() {
85 String principalName = ConfigContext.getCurrentContextConfig().getProperty(TkConstants.BATCH_USER_PRINCIPAL_NAME);
86 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalName);
87 return principal == null ? null : principal.getPrincipalId();
88 }
89
90 private boolean missedPunchDocumentsNotFinal(String documentId) {
91 boolean missedPunchDocumentsNotFinal = false;
92
93 List<MissedPunchDocument> missedPunchDocuments = TkServiceLocator.getMissedPunchService().getMissedPunchDocsByTimesheetDocumentId(documentId);
94 for (MissedPunchDocument missedPunchDocument : missedPunchDocuments) {
95 Collection<ActionItemActionListExtension> actionItems = KEWServiceLocator.getActionListService().getActionListForSingleDocument(missedPunchDocument.getDocumentNumber());
96 for (ActionItemActionListExtension actionItem : actionItems) {
97 if (!actionItem.getRouteHeader().isFinal()) {
98 missedPunchDocumentsNotFinal = true;
99 break;
100 }
101 }
102 }
103
104 return missedPunchDocumentsNotFinal;
105 }
106
107 private boolean documentNotEnroute(String documentId) {
108 boolean documentNotInAStateToBeApproved = false;
109
110 Collection<ActionItemActionListExtension> actionItems = KEWServiceLocator.getActionListService().getActionListForSingleDocument(documentId);
111 for (ActionItemActionListExtension actionItem : actionItems) {
112 if (!actionItem.getRouteHeader().isEnroute()) {
113 documentNotInAStateToBeApproved = true;
114 break;
115 }
116 }
117
118 return documentNotInAStateToBeApproved;
119 }
120
121 private void rescheduleJob(JobExecutionContext context) throws JobExecutionException {
122 try {
123 Scheduler scheduler = context.getScheduler();
124 Trigger oldTrigger = context.getTrigger();
125
126 Date newStartTime = new DateTime().plusMinutes(5).toDate();
127 String newTriggerName = BatchJobUtil.getTriggerName(SupervisorApprovalJob.class, newStartTime);
128 Trigger newTrigger = new SimpleTrigger(newTriggerName, oldTrigger.getGroup(), newStartTime);
129 newTrigger.setJobName(oldTrigger.getJobName());
130 newTrigger.setJobGroup(oldTrigger.getJobGroup());
131
132 LOG.info("Rescheduing " + newTrigger.getFullJobName() + " to be run on " + newTrigger.getStartTime());
133
134 scheduler.rescheduleJob(oldTrigger.getName(), oldTrigger.getGroup(), newTrigger);
135 } catch (SchedulerException se) {
136 throw new JobExecutionException(se);
137 }
138 }
139
140 }