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