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.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.principal.Principal;
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
40 public class EndPayPeriodJob implements Job {
41
42 private static final Logger LOG = Logger.getLogger(EndPayPeriodJob.class);
43
44 public void execute(JobExecutionContext context) throws JobExecutionException {
45 LOG.info("Starting of EndPayPeriod Job!!!");
46 String batchUserPrincipalId = getBatchUserPrincipalId();
47
48 if (batchUserPrincipalId != null) {
49 JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
50
51 String hrCalendarEntryId = jobDataMap.getString("hrCalendarEntryId");
52 String tkClockLogId = jobDataMap.getString("tkClockLogId");
53 CalendarEntries calendarEntry = TkServiceLocator.getCalendarEntriesService().getCalendarEntries(hrCalendarEntryId);
54 Calendar calendar = TkServiceLocator.getCalendarService().getCalendar(calendarEntry.getHrCalendarId());
55 calendarEntry.setCalendarObj(calendar);
56
57 Date endPeriodDateTime = calendarEntry.getEndPeriodDateTime();
58 CalendarEntries nextCalendarEntry = TkServiceLocator.getCalendarEntriesService().getNextCalendarEntriesByCalendarId(calendarEntry.getHrCalendarId(), calendarEntry);
59 Date beginNextPeriodDateTime = nextCalendarEntry.getBeginPeriodDateTime();
60
61 ClockLog openClockLog = TkServiceLocator.getClockLogService().getClockLog(tkClockLogId);
62 String ipAddress = openClockLog.getIpAddress();
63 String principalId = openClockLog.getPrincipalId();
64
65 TimesheetDocumentHeader timesheetDocumentHeader = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeader(principalId, calendarEntry.getBeginPeriodDateTime(), endPeriodDateTime);
66 if (timesheetDocumentHeader != null) {
67 LOG.info("Current timesheet document id is " + timesheetDocumentHeader.getDocumentId());
68 TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(timesheetDocumentHeader.getDocumentId());
69 String assignmentKey = new AssignmentDescriptionKey(openClockLog.getJobNumber(), openClockLog.getWorkArea(), openClockLog.getTask()).toAssignmentKeyString();
70 Assignment assignment = TkServiceLocator.getAssignmentService().getAssignment(timesheetDocument, assignmentKey);
71
72 LOG.info("Before creating clock OUT log!");
73 ClockLog clockOutLog = TkServiceLocator.getClockLogService().processClockLog(new java.sql.Timestamp(endPeriodDateTime.getTime()), assignment, calendarEntry, ipAddress,
74 new java.sql.Date(endPeriodDateTime.getTime()), timesheetDocument, TkConstants.CLOCK_OUT, false, principalId, batchUserPrincipalId);
75 LOG.info("Clock OUT log created, the id is " + clockOutLog.getTkClockLogId() + ", timestamp is " + clockOutLog.getTimestamp().toString());
76
77 TimesheetDocumentHeader nextTdh = TkServiceLocator.getTimesheetDocumentHeaderService()
78 .getDocumentHeader(principalId, nextCalendarEntry.getBeginPeriodDateTime(), nextCalendarEntry.getEndPeriodDateTime());
79 TimesheetDocument nextTimeDoc = null;
80 if(nextTdh != null) {
81 nextTimeDoc = TkServiceLocator.getTimesheetService().getTimesheetDocument(nextTdh.getDocumentId());
82 LOG.info("Next Time document is not null, the document id is " + nextTdh.getDocumentId());
83 }
84 LOG.info("Before creating clock IN log!");
85 ClockLog clockInLog = TkServiceLocator.getClockLogService().processClockLog(new java.sql.Timestamp(beginNextPeriodDateTime.getTime()), assignment, nextCalendarEntry, ipAddress,
86 new java.sql.Date(beginNextPeriodDateTime.getTime()), nextTimeDoc, TkConstants.CLOCK_IN, false, principalId, batchUserPrincipalId);
87 LOG.info("Clock IN log created, the id is " + clockInLog.getTkClockLogId() + ", timestamp is " + clockInLog.getTimestamp().toString());
88
89
90 Timestamp ts= clockInLog.getTimestamp();
91 java.util.Calendar cal = java.util.Calendar.getInstance();
92 cal.setTimeInMillis(ts.getTime());
93 cal.add(java.util.Calendar.SECOND, 5);
94 Timestamp later = new Timestamp(cal.getTime().getTime());
95 clockInLog.setTimestamp(later);
96 TkServiceLocator.getClockLogService().saveClockLog(clockInLog);
97 LOG.info("After adding 5 seconds to ClockInLog, the timestamp is " + clockInLog.getTimestamp().toString());
98 }
99 } 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 }