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 org.apache.log4j.Logger;
019    import org.kuali.hr.time.assignment.Assignment;
020    import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
021    import org.kuali.hr.time.calendar.Calendar;
022    import org.kuali.hr.time.calendar.CalendarEntries;
023    import org.kuali.hr.time.clocklog.ClockLog;
024    import org.kuali.hr.time.service.base.TkServiceLocator;
025    import org.kuali.hr.time.timesheet.TimesheetDocument;
026    import org.kuali.hr.time.util.TkConstants;
027    import org.kuali.hr.time.workflow.TimesheetDocumentHeader;
028    import org.kuali.rice.core.api.config.property.ConfigContext;
029    import org.kuali.rice.kim.api.identity.principal.Principal;
030    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
031    import org.quartz.Job;
032    import org.quartz.JobDataMap;
033    import org.quartz.JobExecutionContext;
034    import org.quartz.JobExecutionException;
035    
036    import java.sql.Timestamp;
037    import java.util.Date;
038    
039    public class EndPayPeriodJob implements Job {
040            
041            private static final Logger LOG = Logger.getLogger(EndPayPeriodJob.class);
042            
043            public void execute(JobExecutionContext context) throws JobExecutionException {
044                    String batchUserPrincipalId = getBatchUserPrincipalId();
045            
046                    if (batchUserPrincipalId != null) {
047                            JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
048            
049                            String hrCalendarEntriesId = jobDataMap.getString("hrCalendarEntriesId");
050                            String tkClockLogId = jobDataMap.getString("tkClockLogId");
051                            
052                    CalendarEntries calendarEntry = TkServiceLocator.getCalendarEntriesService().getCalendarEntries(hrCalendarEntriesId);
053                    Calendar calendar = TkServiceLocator.getCalendarService().getCalendar(calendarEntry.getHrCalendarId());
054                    calendarEntry.setCalendarObj(calendar);
055                    
056                    Date beginPeriodDateTime = calendarEntry.getBeginPeriodDateTime();
057                    Date endPeriodDateTime = calendarEntry.getEndPeriodDateTime();
058                    ClockLog openClockLog = TkServiceLocator.getClockLogService().getClockLog(tkClockLogId);
059                    String ipAddress = openClockLog.getIpAddress();
060                    String principalId = openClockLog.getPrincipalId();
061            
062                    TimesheetDocumentHeader timesheetDocumentHeader = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeader(principalId, beginPeriodDateTime, endPeriodDateTime);
063                    if (timesheetDocumentHeader != null) {
064                        TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(timesheetDocumentHeader.getDocumentId());
065                        String assignmentKey = new AssignmentDescriptionKey(openClockLog.getJobNumber(), openClockLog.getWorkArea(), openClockLog.getTask()).toAssignmentKeyString();
066                        Assignment assignment = TkServiceLocator.getAssignmentService().getAssignment(timesheetDocument, assignmentKey);
067                            
068                        TkServiceLocator.getClockLogService().processClockLog(new Timestamp(endPeriodDateTime.getTime()), assignment, calendarEntry, ipAddress, 
069                                    new java.sql.Date(endPeriodDateTime.getTime()), timesheetDocument, TkConstants.CLOCK_OUT, false, principalId, batchUserPrincipalId);
070                        TkServiceLocator.getClockLogService().processClockLog(new Timestamp(beginPeriodDateTime.getTime()), assignment, calendarEntry, ipAddress, 
071                                    new java.sql.Date(beginPeriodDateTime.getTime()), timesheetDocument, TkConstants.CLOCK_IN, false, principalId, batchUserPrincipalId);
072                    }
073            } else {
074                    String principalName = ConfigContext.getCurrentContextConfig().getProperty(TkConstants.BATCH_USER_PRINCIPAL_NAME);
075                    LOG.error("Could not run batch jobs due to missing batch user " + principalName);
076            }
077            }
078            
079        private String getBatchUserPrincipalId() {
080            String principalName = ConfigContext.getCurrentContextConfig().getProperty(TkConstants.BATCH_USER_PRINCIPAL_NAME);
081            Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalName);
082            return principal == null ? null : principal.getPrincipalId();
083        }
084    
085    }