001 /** 002 * Copyright 2004-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.hr.time.batch; 017 018 import java.sql.Timestamp; 019 import java.util.Date; 020 021 import org.apache.log4j.Logger; 022 import org.kuali.hr.time.assignment.Assignment; 023 import org.kuali.hr.time.assignment.AssignmentDescriptionKey; 024 import org.kuali.hr.time.calendar.Calendar; 025 import org.kuali.hr.time.calendar.CalendarEntries; 026 import org.kuali.hr.time.clocklog.ClockLog; 027 import org.kuali.hr.time.service.base.TkServiceLocator; 028 import org.kuali.hr.time.timesheet.TimesheetDocument; 029 import org.kuali.hr.time.util.TkConstants; 030 import org.kuali.hr.time.workflow.TimesheetDocumentHeader; 031 import org.kuali.rice.core.api.config.property.ConfigContext; 032 import org.kuali.rice.kim.api.identity.principal.Principal; 033 import org.kuali.rice.kim.api.services.KimApiServiceLocator; 034 import org.quartz.Job; 035 import org.quartz.JobDataMap; 036 import org.quartz.JobExecutionContext; 037 import org.quartz.JobExecutionException; 038 039 040 public class EndPayPeriodJob implements Job { 041 042 private static final Logger LOG = Logger.getLogger(EndPayPeriodJob.class); 043 044 public void execute(JobExecutionContext context) throws JobExecutionException { 045 LOG.info("Starting of EndPayPeriod Job!!!"); 046 String batchUserPrincipalId = getBatchUserPrincipalId(); 047 048 if (batchUserPrincipalId != null) { 049 JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); 050 051 String hrCalendarEntryId = jobDataMap.getString("hrCalendarEntryId"); 052 String tkClockLogId = jobDataMap.getString("tkClockLogId"); 053 CalendarEntries calendarEntry = TkServiceLocator.getCalendarEntriesService().getCalendarEntries(hrCalendarEntryId); 054 Calendar calendar = TkServiceLocator.getCalendarService().getCalendar(calendarEntry.getHrCalendarId()); 055 calendarEntry.setCalendarObj(calendar); 056 057 Date endPeriodDateTime = calendarEntry.getEndPeriodDateTime(); 058 CalendarEntries nextCalendarEntry = TkServiceLocator.getCalendarEntriesService().getNextCalendarEntriesByCalendarId(calendarEntry.getHrCalendarId(), calendarEntry); 059 Date beginNextPeriodDateTime = nextCalendarEntry.getBeginPeriodDateTime(); 060 061 ClockLog openClockLog = TkServiceLocator.getClockLogService().getClockLog(tkClockLogId); 062 String ipAddress = openClockLog.getIpAddress(); 063 String principalId = openClockLog.getPrincipalId(); 064 065 TimesheetDocumentHeader timesheetDocumentHeader = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeader(principalId, calendarEntry.getBeginPeriodDateTime(), endPeriodDateTime); 066 if (timesheetDocumentHeader != null) { 067 LOG.info("Current timesheet document id is " + timesheetDocumentHeader.getDocumentId()); 068 TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(timesheetDocumentHeader.getDocumentId()); 069 String assignmentKey = new AssignmentDescriptionKey(openClockLog.getJobNumber(), openClockLog.getWorkArea(), openClockLog.getTask()).toAssignmentKeyString(); 070 Assignment assignment = TkServiceLocator.getAssignmentService().getAssignment(timesheetDocument, assignmentKey); 071 072 LOG.info("Before creating clock OUT log!"); 073 ClockLog clockOutLog = TkServiceLocator.getClockLogService().processClockLog(new java.sql.Timestamp(endPeriodDateTime.getTime()), assignment, calendarEntry, ipAddress, 074 new java.sql.Date(endPeriodDateTime.getTime()), timesheetDocument, TkConstants.CLOCK_OUT, false, principalId, batchUserPrincipalId); 075 LOG.info("Clock OUT log created, the id is " + clockOutLog.getTkClockLogId() + ", timestamp is " + clockOutLog.getTimestamp().toString()); 076 077 TimesheetDocumentHeader nextTdh = TkServiceLocator.getTimesheetDocumentHeaderService() 078 .getDocumentHeader(principalId, nextCalendarEntry.getBeginPeriodDateTime(), nextCalendarEntry.getEndPeriodDateTime()); 079 TimesheetDocument nextTimeDoc = null; 080 if(nextTdh != null) { 081 nextTimeDoc = TkServiceLocator.getTimesheetService().getTimesheetDocument(nextTdh.getDocumentId()); 082 LOG.info("Next Time document is not null, the document id is " + nextTdh.getDocumentId()); 083 } 084 LOG.info("Before creating clock IN log!"); 085 ClockLog clockInLog = TkServiceLocator.getClockLogService().processClockLog(new java.sql.Timestamp(beginNextPeriodDateTime.getTime()), assignment, nextCalendarEntry, ipAddress, 086 new java.sql.Date(beginNextPeriodDateTime.getTime()), nextTimeDoc, TkConstants.CLOCK_IN, false, principalId, batchUserPrincipalId); 087 LOG.info("Clock IN log created, the id is " + clockInLog.getTkClockLogId() + ", timestamp is " + clockInLog.getTimestamp().toString()); 088 089 // add 5 seconds to clock in log's timestamp so it will be found as the latest clock action 090 Timestamp ts= clockInLog.getTimestamp(); 091 java.util.Calendar cal = java.util.Calendar.getInstance(); 092 cal.setTimeInMillis(ts.getTime()); 093 cal.add(java.util.Calendar.SECOND, 5); 094 Timestamp later = new Timestamp(cal.getTime().getTime()); 095 clockInLog.setTimestamp(later); 096 TkServiceLocator.getClockLogService().saveClockLog(clockInLog); 097 LOG.info("After adding 5 seconds to ClockInLog, the timestamp is " + clockInLog.getTimestamp().toString()); 098 } 099 } else { 100 String principalName = ConfigContext.getCurrentContextConfig().getProperty(TkConstants.BATCH_USER_PRINCIPAL_NAME); 101 LOG.error("Could not run batch jobs due to missing batch user " + principalName); 102 } 103 } 104 105 private String getBatchUserPrincipalId() { 106 String principalName = ConfigContext.getCurrentContextConfig().getProperty(TkConstants.BATCH_USER_PRINCIPAL_NAME); 107 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalName); 108 return principal == null ? null : principal.getPrincipalId(); 109 } 110 111 }