1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.tklm.time.batch;
17
18 import java.util.Collection;
19 import java.util.List;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.apache.log4j.Logger;
23 import org.joda.time.DateTime;
24 import org.kuali.kpme.core.batch.BatchJob;
25 import org.kuali.kpme.core.batch.BatchJobUtil;
26 import org.kuali.kpme.core.calendar.Calendar;
27 import org.kuali.kpme.core.calendar.entry.CalendarEntry;
28 import org.kuali.kpme.core.service.HrServiceLocator;
29 import org.kuali.kpme.core.util.HrConstants;
30 import org.kuali.kpme.tklm.common.TkConstants;
31 import org.kuali.kpme.tklm.leave.calendar.LeaveCalendarDocument;
32 import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
33 import org.kuali.kpme.tklm.leave.workflow.LeaveCalendarDocumentHeader;
34 import org.kuali.kpme.tklm.time.missedpunch.MissedPunchDocument;
35 import org.kuali.kpme.tklm.time.service.TkServiceLocator;
36 import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
37 import org.kuali.kpme.tklm.time.workflow.TimesheetDocumentHeader;
38 import org.kuali.rice.core.api.config.property.ConfigContext;
39 import org.kuali.rice.kew.actionitem.ActionItemActionListExtension;
40 import org.kuali.rice.kew.api.KewApiServiceLocator;
41 import org.kuali.rice.kew.api.document.DocumentStatus;
42 import org.kuali.rice.kew.service.KEWServiceLocator;
43 import org.kuali.rice.kim.api.identity.principal.Principal;
44 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
45 import org.quartz.Job;
46 import org.quartz.JobDataMap;
47 import org.quartz.JobExecutionContext;
48 import org.quartz.JobExecutionException;
49 import org.quartz.Scheduler;
50 import org.quartz.SchedulerException;
51 import org.quartz.SimpleTrigger;
52 import org.quartz.Trigger;
53
54 public class SupervisorApprovalJob extends BatchJob {
55
56 private static final Logger LOG = Logger.getLogger(SupervisorApprovalJob.class);
57
58 public void execute(JobExecutionContext context) throws JobExecutionException {
59 String batchUserPrincipalId = getBatchUserPrincipalId();
60
61 if (batchUserPrincipalId != null) {
62 JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
63
64 String hrCalendarEntryId = jobDataMap.getString("hrCalendarEntryId");
65 String documentId = jobDataMap.getString("documentId");
66
67 CalendarEntry calendarEntry = HrServiceLocator.getCalendarEntryService().getCalendarEntry(hrCalendarEntryId);
68 Calendar calendar = HrServiceLocator.getCalendarService().getCalendar(calendarEntry.getHrCalendarId());
69
70 if (StringUtils.equals(calendar.getCalendarTypes(), "Pay")) {
71 TimesheetDocumentHeader timesheetDocumentHeader = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeader(documentId);
72 if (timesheetDocumentHeader != null) {
73 if (missedPunchDocumentsEnroute(documentId) || documentNotEnroute(documentId)) {
74 rescheduleJob(context);
75 } else {
76 TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(documentId);
77 TkServiceLocator.getTimesheetService().approveTimesheet(batchUserPrincipalId, timesheetDocument, HrConstants.BATCH_JOB_ACTIONS.BATCH_JOB_APPROVE);
78 }
79 }
80 } else if (StringUtils.equals(calendar.getCalendarTypes(), "Leave")) {
81 LeaveCalendarDocumentHeader leaveCalendarDocumentHeader = LmServiceLocator.getLeaveCalendarDocumentHeaderService().getDocumentHeader(documentId);
82 if (leaveCalendarDocumentHeader != null) {
83 if (documentNotEnroute(documentId)) {
84 rescheduleJob(context);
85 } else {
86 LeaveCalendarDocument leaveCalendarDocument = LmServiceLocator.getLeaveCalendarService().getLeaveCalendarDocument(documentId);
87 LmServiceLocator.getLeaveCalendarService().approveLeaveCalendar(batchUserPrincipalId, leaveCalendarDocument, HrConstants.BATCH_JOB_ACTIONS.BATCH_JOB_APPROVE);
88 }
89 }
90 }
91 } else {
92 String principalName = getBatchUserPrincipalName();
93 LOG.error("Could not run batch jobs due to missing batch user " + principalName);
94 }
95 }
96
97 private boolean missedPunchDocumentsEnroute(String documentId) {
98 boolean missedPunchDocumentsEnroute = false;
99
100 List<MissedPunchDocument> missedPunchDocuments = TkServiceLocator.getMissedPunchService().getMissedPunchDocumentsByTimesheetDocumentId(documentId);
101 for (MissedPunchDocument missedPunchDocument : missedPunchDocuments) {
102 DocumentStatus documentStatus = KewApiServiceLocator.getWorkflowDocumentService().getDocumentStatus(missedPunchDocument.getDocumentNumber());
103 if (DocumentStatus.ENROUTE.equals(documentStatus)) {
104 missedPunchDocumentsEnroute = true;
105 break;
106 }
107 }
108
109 return missedPunchDocumentsEnroute;
110 }
111
112 private boolean documentNotEnroute(String documentId) {
113 boolean documentNotInAStateToBeApproved = false;
114
115 Collection<ActionItemActionListExtension> actionItems = KEWServiceLocator.getActionListService().getActionListForSingleDocument(documentId);
116 for (ActionItemActionListExtension actionItem : actionItems) {
117 if (!actionItem.getRouteHeader().isEnroute()) {
118 documentNotInAStateToBeApproved = true;
119 break;
120 }
121 }
122
123 return documentNotInAStateToBeApproved;
124 }
125
126 private void rescheduleJob(JobExecutionContext context) throws JobExecutionException {
127 try {
128 Scheduler scheduler = context.getScheduler();
129 Trigger oldTrigger = context.getTrigger();
130
131 DateTime newStartTime = new DateTime().plusMinutes(5);
132 String newTriggerName = BatchJobUtil.getTriggerName(SupervisorApprovalJob.class, newStartTime);
133 Trigger newTrigger = new SimpleTrigger(newTriggerName, oldTrigger.getGroup(), newStartTime.toDate());
134 newTrigger.setJobName(oldTrigger.getJobName());
135 newTrigger.setJobGroup(oldTrigger.getJobGroup());
136
137 LOG.info("Rescheduing " + newTrigger.getFullJobName() + " to be run on " + newTrigger.getStartTime());
138
139 scheduler.rescheduleJob(oldTrigger.getName(), oldTrigger.getGroup(), newTrigger);
140 } catch (SchedulerException se) {
141 LOG.error("Failure to execute job due to SchedulerException", se);
142
143 }
144 }
145
146 }