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.List;
020    
021    import org.apache.log4j.Logger;
022    import org.joda.time.DateTime;
023    import org.joda.time.DateTimeZone;
024    import org.kuali.hr.time.assignment.Assignment;
025    import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
026    import org.kuali.hr.time.calendar.CalendarEntries;
027    import org.kuali.hr.time.clocklog.ClockLog;
028    import org.kuali.hr.time.service.base.TkServiceLocator;
029    import org.kuali.hr.time.timesheet.TimesheetDocument;
030    import org.kuali.hr.time.util.TkConstants;
031    import org.kuali.hr.time.workflow.TimesheetDocumentHeader;
032    
033    public class PayPeriodEndBatchJobRunnable extends BatchJobEntryRunnable {
034    
035        private static final Logger LOG = Logger.getLogger(PayPeriodEndBatchJobRunnable.class);
036    
037        public PayPeriodEndBatchJobRunnable(BatchJobEntry entry) {
038            super(entry);
039    
040        }
041    
042        @Override
043        public void doWork() throws Exception {
044            // For each batch job entry, grab the pay calendar entry id
045            BatchJobEntry payPeriodEndBatchEntry = TkServiceLocator.getBatchJobEntryService().getBatchJobEntry(getTkBatchJobEntryId());
046            // get pay calendar entry object by using id
047    //        TkServiceLocator.getPayCalendarEntriesService().getCurrentPayCalendarEntriesByPayCalendarId()
048            CalendarEntries pe = TkServiceLocator.getCalendarEntriesService().getCalendarEntries(payPeriodEndBatchEntry.getHrPyCalendarEntryId());
049            // get open clock logs by pay calendar entry id
050            List<ClockLog> openClockLogs = TkServiceLocator.getClockLogService().getOpenClockLogs(pe);
051            // clock out at midnight based on user's timezone
052            if (openClockLogs.size() > 0) {
053                for (ClockLog cl : openClockLogs) {
054                    // clock out for each open clock log:
055                    // create a end period DateTime object at midnight based on user's timezone
056    
057                    DateTime endPeriodDateTime = new DateTime(pe.getEndPeriodDateTime().getTime(), DateTimeZone.forID(cl.getClockTimestampTimezone()));
058                    TimesheetDocumentHeader tdh = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeader(cl.getPrincipalId(), pe.getBeginPeriodDate(), pe.getEndPeriodDate());
059                    TimesheetDocument td = TkServiceLocator.getTimesheetService().getTimesheetDocument(tdh.getDocumentId());
060                    String assignmentKey = new AssignmentDescriptionKey(cl.getJobNumber(), cl.getWorkArea(), cl.getTask()).toAssignmentKeyString();
061                    Assignment assignment = TkServiceLocator.getAssignmentService().getAssignment(td, assignmentKey);
062    
063                    TkServiceLocator.getClockLogService().processClockLog(new Timestamp(endPeriodDateTime.getMillis()), assignment, pe, cl.getIpAddress(),
064                            new java.sql.Date(endPeriodDateTime.getMillis()), td, TkConstants.CLOCK_OUT, cl.getPrincipalId(), TkConstants.BATCH_JOB_USER_PRINCIPAL_ID);
065    
066                    // clock in
067                    TkServiceLocator.getClockLogService().processClockLog(new Timestamp(endPeriodDateTime.getMillis()), assignment, pe, cl.getIpAddress(),
068                            new java.sql.Date(endPeriodDateTime.getMillis()), td, TkConstants.CLOCK_IN, cl.getPrincipalId(), TkConstants.BATCH_JOB_USER_PRINCIPAL_ID);
069                }
070    
071            }
072    
073        }
074    }