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.List;
20  
21  import org.apache.log4j.Logger;
22  import org.joda.time.DateTime;
23  import org.joda.time.DateTimeZone;
24  import org.kuali.hr.time.assignment.Assignment;
25  import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
26  import org.kuali.hr.time.calendar.CalendarEntries;
27  import org.kuali.hr.time.clocklog.ClockLog;
28  import org.kuali.hr.time.service.base.TkServiceLocator;
29  import org.kuali.hr.time.timesheet.TimesheetDocument;
30  import org.kuali.hr.time.util.TkConstants;
31  import org.kuali.hr.time.workflow.TimesheetDocumentHeader;
32  
33  public class PayPeriodEndBatchJobRunnable extends BatchJobEntryRunnable {
34  
35      private static final Logger LOG = Logger.getLogger(PayPeriodEndBatchJobRunnable.class);
36  
37      public PayPeriodEndBatchJobRunnable(BatchJobEntry entry) {
38          super(entry);
39  
40      }
41  
42      @Override
43      public void doWork() throws Exception {
44          // For each batch job entry, grab the pay calendar entry id
45          BatchJobEntry payPeriodEndBatchEntry = TkServiceLocator.getBatchJobEntryService().getBatchJobEntry(getTkBatchJobEntryId());
46          // get pay calendar entry object by using id
47  //        TkServiceLocator.getPayCalendarEntriesService().getCurrentPayCalendarEntriesByPayCalendarId()
48          CalendarEntries pe = TkServiceLocator.getCalendarEntriesService().getCalendarEntries(payPeriodEndBatchEntry.getHrPyCalendarEntryId());
49          // get open clock logs by pay calendar entry id
50          List<ClockLog> openClockLogs = TkServiceLocator.getClockLogService().getOpenClockLogs(pe);
51          // clock out at midnight based on user's timezone
52          if (openClockLogs.size() > 0) {
53              for (ClockLog cl : openClockLogs) {
54                  // clock out for each open clock log:
55                  // create a end period DateTime object at midnight based on user's timezone
56  
57                  DateTime endPeriodDateTime = new DateTime(pe.getEndPeriodDateTime().getTime(), DateTimeZone.forID(cl.getClockTimestampTimezone()));
58                  TimesheetDocumentHeader tdh = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeader(cl.getPrincipalId(), pe.getBeginPeriodDate(), pe.getEndPeriodDate());
59                  TimesheetDocument td = TkServiceLocator.getTimesheetService().getTimesheetDocument(tdh.getDocumentId());
60                  String assignmentKey = new AssignmentDescriptionKey(cl.getJobNumber(), cl.getWorkArea(), cl.getTask()).toAssignmentKeyString();
61                  Assignment assignment = TkServiceLocator.getAssignmentService().getAssignment(td, assignmentKey);
62  
63                  TkServiceLocator.getClockLogService().processClockLog(new Timestamp(endPeriodDateTime.getMillis()), assignment, pe, cl.getIpAddress(),
64                          new java.sql.Date(endPeriodDateTime.getMillis()), td, TkConstants.CLOCK_OUT, cl.getPrincipalId(), TkConstants.BATCH_JOB_USER_PRINCIPAL_ID);
65  
66                  // clock in
67                  TkServiceLocator.getClockLogService().processClockLog(new Timestamp(endPeriodDateTime.getMillis()), assignment, pe, cl.getIpAddress(),
68                          new java.sql.Date(endPeriodDateTime.getMillis()), td, TkConstants.CLOCK_IN, cl.getPrincipalId(), TkConstants.BATCH_JOB_USER_PRINCIPAL_ID);
69              }
70  
71          }
72  
73      }
74  }