View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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.BatchJobUtil;
25  import org.kuali.kpme.core.calendar.Calendar;
26  import org.kuali.kpme.core.calendar.entry.CalendarEntry;
27  import org.kuali.kpme.core.service.HrServiceLocator;
28  import org.kuali.kpme.core.util.HrConstants;
29  import org.kuali.kpme.tklm.common.TkConstants;
30  import org.kuali.kpme.tklm.leave.calendar.LeaveCalendarDocument;
31  import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
32  import org.kuali.kpme.tklm.leave.workflow.LeaveCalendarDocumentHeader;
33  import org.kuali.kpme.tklm.time.missedpunch.MissedPunchDocument;
34  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
35  import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
36  import org.kuali.kpme.tklm.time.workflow.TimesheetDocumentHeader;
37  import org.kuali.rice.core.api.config.property.ConfigContext;
38  import org.kuali.rice.kew.actionitem.ActionItemActionListExtension;
39  import org.kuali.rice.kew.api.KewApiServiceLocator;
40  import org.kuali.rice.kew.api.document.DocumentStatus;
41  import org.kuali.rice.kew.service.KEWServiceLocator;
42  import org.kuali.rice.kim.api.identity.principal.Principal;
43  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
44  import org.quartz.Job;
45  import org.quartz.JobDataMap;
46  import org.quartz.JobExecutionContext;
47  import org.quartz.JobExecutionException;
48  import org.quartz.Scheduler;
49  import org.quartz.SchedulerException;
50  import org.quartz.SimpleTrigger;
51  import org.quartz.Trigger;
52  
53  public class SupervisorApprovalJob implements Job {
54  	
55  	private static final Logger LOG = Logger.getLogger(SupervisorApprovalJob.class);
56  	
57  	public void execute(JobExecutionContext context) throws JobExecutionException {
58  		String batchUserPrincipalId = getBatchUserPrincipalId();
59          
60  		if (batchUserPrincipalId != null) {
61  			JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
62  	
63  			String hrCalendarEntryId = jobDataMap.getString("hrCalendarEntryId");
64  			String documentId = jobDataMap.getString("documentId");
65  	
66  			CalendarEntry calendarEntry = HrServiceLocator.getCalendarEntryService().getCalendarEntry(hrCalendarEntryId);
67  			Calendar calendar = HrServiceLocator.getCalendarService().getCalendar(calendarEntry.getHrCalendarId());
68  					
69  			if (StringUtils.equals(calendar.getCalendarTypes(), "Pay")) {
70  				TimesheetDocumentHeader timesheetDocumentHeader = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeader(documentId);
71  				if (timesheetDocumentHeader != null) {
72  					if (missedPunchDocumentsEnroute(documentId) || documentNotEnroute(documentId)) {
73  						rescheduleJob(context);
74  					} else {
75  						TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(documentId);
76  						TkServiceLocator.getTimesheetService().approveTimesheet(batchUserPrincipalId, timesheetDocument, HrConstants.BATCH_JOB_ACTIONS.BATCH_JOB_APPROVE);
77  					}
78  				}
79  			} else if (StringUtils.equals(calendar.getCalendarTypes(), "Leave")) {
80  				LeaveCalendarDocumentHeader leaveCalendarDocumentHeader = LmServiceLocator.getLeaveCalendarDocumentHeaderService().getDocumentHeader(documentId);
81  				if (leaveCalendarDocumentHeader != null) {
82  					if (documentNotEnroute(documentId)) {
83  						rescheduleJob(context);
84  					} else {
85  						LeaveCalendarDocument leaveCalendarDocument = LmServiceLocator.getLeaveCalendarService().getLeaveCalendarDocument(documentId);
86  						LmServiceLocator.getLeaveCalendarService().approveLeaveCalendar(batchUserPrincipalId, leaveCalendarDocument, HrConstants.BATCH_JOB_ACTIONS.BATCH_JOB_APPROVE);
87  					}
88  				}
89  			}
90          } else {
91          	String principalName = ConfigContext.getCurrentContextConfig().getProperty(TkConstants.BATCH_USER_PRINCIPAL_NAME);
92          	LOG.error("Could not run batch jobs due to missing batch user " + principalName);
93          }
94  	}
95  	
96      private String getBatchUserPrincipalId() {
97      	String principalName = ConfigContext.getCurrentContextConfig().getProperty(TkConstants.BATCH_USER_PRINCIPAL_NAME);
98          Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalName);
99          return principal == null ? null : principal.getPrincipalId();
100     }
101 	
102 	private boolean missedPunchDocumentsEnroute(String documentId) {
103 		boolean missedPunchDocumentsEnroute = false;
104 		
105 		List<MissedPunchDocument> missedPunchDocuments = TkServiceLocator.getMissedPunchService().getMissedPunchDocumentsByTimesheetDocumentId(documentId);
106 		for (MissedPunchDocument missedPunchDocument : missedPunchDocuments) {
107 			DocumentStatus documentStatus = KewApiServiceLocator.getWorkflowDocumentService().getDocumentStatus(missedPunchDocument.getDocumentNumber());
108 			if (DocumentStatus.ENROUTE.equals(documentStatus)) {
109 				missedPunchDocumentsEnroute = true;
110 				break;
111 			}
112 		}
113 		
114 		return missedPunchDocumentsEnroute;
115 	}
116 	
117 	private boolean documentNotEnroute(String documentId) {
118 		boolean documentNotInAStateToBeApproved = false;
119 		
120 		Collection<ActionItemActionListExtension> actionItems = KEWServiceLocator.getActionListService().getActionListForSingleDocument(documentId);
121 		for (ActionItemActionListExtension actionItem : actionItems) {
122 			if (!actionItem.getRouteHeader().isEnroute()) {
123 				documentNotInAStateToBeApproved = true;
124 				break;
125 			}
126 		}
127 		
128 		return documentNotInAStateToBeApproved;
129 	}
130 	
131 	private void rescheduleJob(JobExecutionContext context) throws JobExecutionException {
132 		try {
133 			Scheduler scheduler = context.getScheduler();
134 			Trigger oldTrigger = context.getTrigger();
135 			
136 			DateTime newStartTime = new DateTime().plusMinutes(5);
137 			String newTriggerName = BatchJobUtil.getTriggerName(SupervisorApprovalJob.class, newStartTime);
138 			Trigger newTrigger = new SimpleTrigger(newTriggerName, oldTrigger.getGroup(), newStartTime.toDate());
139 			newTrigger.setJobName(oldTrigger.getJobName());
140 			newTrigger.setJobGroup(oldTrigger.getJobGroup());
141 			
142 			LOG.info("Rescheduing " + newTrigger.getFullJobName() + " to be run on " + newTrigger.getStartTime());
143 			
144 			scheduler.rescheduleJob(oldTrigger.getName(), oldTrigger.getGroup(), newTrigger);
145 		} catch (SchedulerException se) {
146 			LOG.error("Failure to execute job due to SchedulerException", se);
147 //			throw new JobExecutionException(se);
148 		}
149 	}
150 	
151 }