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.hr.time.batch;
17  
18  import java.sql.Timestamp;
19  import java.util.Date;
20  
21  import org.apache.log4j.Logger;
22  import org.kuali.hr.time.assignment.Assignment;
23  import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
24  import org.kuali.hr.time.calendar.Calendar;
25  import org.kuali.hr.time.calendar.CalendarEntries;
26  import org.kuali.hr.time.clocklog.ClockLog;
27  import org.kuali.hr.time.service.base.TkServiceLocator;
28  import org.kuali.hr.time.timesheet.TimesheetDocument;
29  import org.kuali.hr.time.util.TkConstants;
30  import org.kuali.hr.time.workflow.TimesheetDocumentHeader;
31  import org.kuali.rice.core.api.config.property.ConfigContext;
32  import org.kuali.rice.kim.api.identity.Person;
33  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
34  import org.quartz.Job;
35  import org.quartz.JobDataMap;
36  import org.quartz.JobExecutionContext;
37  import org.quartz.JobExecutionException;
38  
39  public class EndPayPeriodJob implements Job {
40  	
41  	private static final Logger LOG = Logger.getLogger(EndPayPeriodJob.class);
42  	
43  	public void execute(JobExecutionContext context) throws JobExecutionException {
44  		String batchUserPrincipalId = getBatchUserPrincipalId();
45          
46  		if (batchUserPrincipalId != null) {
47  			JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
48  	
49  			String hrCalendarEntriesId = jobDataMap.getString("hrCalendarEntriesId");
50  			String tkClockLogId = jobDataMap.getString("tkClockLogId");
51  			
52  	        CalendarEntries calendarEntry = TkServiceLocator.getCalendarEntriesService().getCalendarEntries(hrCalendarEntriesId);
53  	        Calendar calendar = TkServiceLocator.getCalendarService().getCalendar(calendarEntry.getHrCalendarId());
54  	        calendarEntry.setCalendarObj(calendar);
55  	        
56  	        Date beginPeriodDateTime = calendarEntry.getBeginPeriodDateTime();
57  	        Date endPeriodDateTime = calendarEntry.getEndPeriodDateTime();
58  	        ClockLog openClockLog = TkServiceLocator.getClockLogService().getClockLog(tkClockLogId);
59  	        String ipAddress = openClockLog.getIpAddress();
60  	        String principalId = openClockLog.getPrincipalId();
61  	
62  	        TimesheetDocumentHeader timesheetDocumentHeader = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeader(principalId, beginPeriodDateTime, endPeriodDateTime);
63  	        if (timesheetDocumentHeader != null) {
64  	            TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(timesheetDocumentHeader.getDocumentId());
65  	            String assignmentKey = new AssignmentDescriptionKey(openClockLog.getJobNumber(), openClockLog.getWorkArea(), openClockLog.getTask()).toAssignmentKeyString();
66  	            Assignment assignment = TkServiceLocator.getAssignmentService().getAssignment(timesheetDocument, assignmentKey);
67  	    		
68  	            TkServiceLocator.getClockLogService().processClockLog(new Timestamp(endPeriodDateTime.getTime()), assignment, calendarEntry, ipAddress, 
69  	            		new java.sql.Date(endPeriodDateTime.getTime()), timesheetDocument, TkConstants.CLOCK_OUT, principalId, batchUserPrincipalId);
70  	            TkServiceLocator.getClockLogService().processClockLog(new Timestamp(beginPeriodDateTime.getTime()), assignment, calendarEntry, ipAddress, 
71  	            		new java.sql.Date(beginPeriodDateTime.getTime()), timesheetDocument, TkConstants.CLOCK_IN, principalId, batchUserPrincipalId);
72  	        }
73          } else {
74          	String principalName = ConfigContext.getCurrentContextConfig().getProperty(TkConstants.BATCH_USER_PRINCIPAL_NAME);
75          	LOG.error("Could not run batch jobs due to missing batch user " + principalName);
76          }
77  	}
78  	
79      private String getBatchUserPrincipalId() {
80      	String principalId = null;
81      	
82      	String principalName = ConfigContext.getCurrentContextConfig().getProperty(TkConstants.BATCH_USER_PRINCIPAL_NAME);
83          Person person = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(principalName);
84          if (person != null) {
85          	principalId = person.getPrincipalId();
86          }
87          
88          return principalId;
89      }
90  
91  }