View Javadoc
1   /**
2    * Copyright 2004-2014 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 org.apache.commons.lang.StringUtils;
19  import org.joda.time.DateTime;
20  import org.kuali.kpme.core.api.calendar.Calendar;
21  import org.kuali.kpme.core.api.calendar.entry.CalendarEntry;
22  import org.kuali.kpme.core.api.principal.PrincipalHRAttributes;
23  import org.kuali.kpme.core.batch.BatchJob;
24  import org.kuali.kpme.core.service.HrServiceLocator;
25  import org.kuali.kpme.core.util.HrConstants;
26  import org.kuali.kpme.tklm.api.common.TkConstants;
27  import org.kuali.kpme.tklm.common.BatchJobService;
28  import org.kuali.kpme.tklm.leave.calendar.LeaveCalendarDocument;
29  import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
30  import org.kuali.kpme.tklm.leave.workflow.LeaveCalendarDocumentHeader;
31  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
32  import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
33  import org.kuali.kpme.tklm.time.workflow.TimesheetDocumentHeader;
34  import org.kuali.rice.kew.service.KEWServiceLocator;
35  import org.quartz.JobDataMap;
36  import org.quartz.JobExecutionContext;
37  import org.quartz.JobExecutionException;
38  
39  import java.util.List;
40  
41  public class EmployeeApprovalJob extends BatchJob {
42  
43  	public void execute(JobExecutionContext context) throws JobExecutionException {
44          try {
45              JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
46              String batchUserPrincipalId = getBatchUserPrincipalId();
47              String hrCalendarEntryId = jobDataMap.getString("hrCalendarEntryId");
48  
49              CalendarEntry calendarEntry = HrServiceLocator.getCalendarEntryService().getCalendarEntry(hrCalendarEntryId);
50              Calendar calendar = HrServiceLocator.getCalendarService().getCalendar(calendarEntry.getHrCalendarId());
51  
52              DateTime beginDate = calendarEntry.getBeginPeriodFullDateTime();
53              DateTime endDate = calendarEntry.getEndPeriodFullDateTime();
54  
55              if (StringUtils.equals(calendar.getCalendarTypes(), "Pay")) {
56                  List<TimesheetDocumentHeader> timesheetDocumentHeaders = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeaders(beginDate, endDate);
57  
58                  for (TimesheetDocumentHeader timesheetDocumentHeader : timesheetDocumentHeaders) {
59                      if(timesheetDocumentHeader != null) {
60                          String docId = timesheetDocumentHeader.getDocumentId();
61                          TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(docId);
62                          if(timesheetDocument != null) {
63                              if (TkConstants.EMPLOYEE_APPROVAL_DOC_STATUS.contains(KEWServiceLocator.getRouteHeaderService().getDocumentStatus(docId))) {
64                                  // use the range of the calendar entry to retrieve the correct PrincipalHrAtrribute record for the employee
65                                  // then check if the calendar name/id matches the one from the calendar entry
66                                  String principalId = timesheetDocument.getPrincipalId();
67                                  PrincipalHRAttributes phraRecord = HrServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, endDate.toLocalDate());
68                                  if(phraRecord != null && phraRecord.getPayCalendar().equals(calendar.getCalendarName())) {
69                                      TkServiceLocator.getTimesheetService().routeTimesheet(batchUserPrincipalId, timesheetDocument, HrConstants.BATCH_JOB_ACTIONS.BATCH_JOB_ROUTE);
70                                  }
71                              }
72                          }
73                      }
74                  }
75              } else if (StringUtils.equals(calendar.getCalendarTypes(), "Leave")) {
76                  List<LeaveCalendarDocumentHeader> leaveCalendarDocumentHeaders = LmServiceLocator.getLeaveCalendarDocumentHeaderService().getDocumentHeaders(beginDate, endDate);
77                  for (LeaveCalendarDocumentHeader leaveCalendarDocumentHeader : leaveCalendarDocumentHeaders) {
78                      if (leaveCalendarDocumentHeader != null) {
79                          String docId = leaveCalendarDocumentHeader.getDocumentId();
80                          LeaveCalendarDocument leaveCalendarDocument = LmServiceLocator.getLeaveCalendarService().getLeaveCalendarDocument(docId);
81                          if (TkConstants.EMPLOYEE_APPROVAL_DOC_STATUS.contains(KEWServiceLocator.getRouteHeaderService().getDocumentStatus(docId))) {
82                              String principalId = leaveCalendarDocument.getPrincipalId();
83                              PrincipalHRAttributes phraRecord = HrServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, endDate.toLocalDate());
84                              if (phraRecord != null && phraRecord.getLeaveCalendar().equals(calendar.getCalendarName())) {
85                                  LmServiceLocator.getLeaveCalendarService().routeLeaveCalendar(batchUserPrincipalId, leaveCalendarDocument, HrConstants.BATCH_JOB_ACTIONS.BATCH_JOB_ROUTE);
86                              }
87                          }
88                      }
89                  }
90              }
91  		}catch(Exception ex) {
92  			TkServiceLocator.getBatchJobService().updateStatus(context.getJobDetail(), BatchJobService.FAILED_JOB_STATUS_CODE);
93  			ex.printStackTrace();
94  		}
95  		TkServiceLocator.getBatchJobService().updateStatus(context.getJobDetail(), BatchJobService.SUCCEEDED_JOB_STATUS_CODE);
96  	} 
97  	
98  }