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.util.ArrayList; 019 import java.util.Date; 020 import java.util.List; 021 022 import org.apache.commons.lang.StringUtils; 023 import org.apache.log4j.Logger; 024 import org.kuali.hr.time.calendar.CalendarEntries; 025 import org.kuali.hr.time.service.base.TkServiceLocator; 026 import org.kuali.hr.time.util.TKUtils; 027 import org.kuali.hr.time.util.TkConstants; 028 029 public class BatchJobManagerThread extends Thread { 030 private static final Logger LOG = Logger.getLogger(BatchJobManagerThread.class); 031 032 int startupSleep = 120; 033 //This represents a number of days on both sides of today 034 int numOfDaysToPoll = 30; 035 int secondsToSleep = 120; 036 037 public BatchJobManagerThread(int secondsToSleep, int numberOfDaysToPoll, int startupSleep) { 038 this.numOfDaysToPoll = numberOfDaysToPoll; 039 this.secondsToSleep = secondsToSleep; 040 this.startupSleep = startupSleep; 041 } 042 043 @Override 044 public void run() { 045 046 try { 047 Thread.sleep(1000 * startupSleep); 048 } catch (Exception e) { 049 LOG.error(e); 050 } 051 052 while (true) { 053 Date asOfDate = TKUtils.getCurrentDate(); 054 List<CalendarEntries> calendarEntries = TkServiceLocator.getCalendarEntriesService().getCurrentCalendarEntryNeedsScheduled(numOfDaysToPoll, asOfDate); 055 056 LOG.info("Scanning for batch jobs to run: (" + asOfDate.toString() + ")"); 057 058 List<BatchJob> batchJobs = new ArrayList<BatchJob>(); 059 for (CalendarEntries calendarEntry : calendarEntries) { 060 List<BatchJob> existingBatchJobs = TkServiceLocator.getBatchJobService().getBatchJobs(calendarEntry.getHrCalendarEntriesId()); 061 062 if (calendarEntry.getBeginPeriodDateTime() != null && calendarEntry.getEndPeriodDateTime() != null && !jobExists(existingBatchJobs, TkConstants.BATCH_JOB_NAMES.BATCH_APPROVE_MISSED_PUNCH)) { 063 BatchJob job = new BatchApproveMissedPunchJob(calendarEntry); 064 TkServiceLocator.getBatchJobService().saveBatchJob(job); 065 batchJobs.add(job); 066 } 067 068 if (calendarEntry.getBatchInitiateDate() != null && !jobExists(existingBatchJobs, TkConstants.BATCH_JOB_NAMES.INITIATE)) { 069 BatchJob job = new InitiateBatchJob(calendarEntry); 070 TkServiceLocator.getBatchJobService().saveBatchJob(job); 071 batchJobs.add(job); 072 } 073 074 if (calendarEntry.getBatchEndPayPeriodDate() != null && !jobExists(existingBatchJobs, TkConstants.BATCH_JOB_NAMES.PAY_PERIOD_END)) { 075 BatchJob job = new PayPeriodEndBatchJob(calendarEntry); 076 TkServiceLocator.getBatchJobService().saveBatchJob(job); 077 batchJobs.add(job); 078 } 079 080 // TODO: Batch jobs going to exception status 081 // if (calendarEntry.getBatchEmployeeApprovalDate() != null && !jobExists(existingBatchJobs, TkConstants.BATCH_JOB_NAMES.APPROVE)) { 082 // BatchJob job = new EmployeeApprovalBatchJob(calendarEntry); 083 // TkServiceLocator.getBatchJobService().saveBatchJob(job); 084 // batchJobs.add(job); 085 // } 086 // 087 // if (calendarEntry.getBatchSupervisorApprovalDate() != null && !jobExists(existingBatchJobs, TkConstants.BATCH_JOB_NAMES.SUPERVISOR_APPROVAL)) { 088 // BatchJob job = new SupervisorApprovalBatchJob(calendarEntry); 089 // TkServiceLocator.getBatchJobService().saveBatchJob(job); 090 // batchJobs.add(job); 091 // } 092 } 093 094 for (BatchJob batchJob : batchJobs) { 095 batchJob.runJob(); 096 } 097 098 try { 099 Thread.sleep(1000 * secondsToSleep); 100 } catch (Exception e) { 101 LOG.error(e); 102 } 103 } 104 } 105 106 private boolean jobExists(List<BatchJob> batchJobs, String batchJobName) { 107 for (BatchJob batchJob : batchJobs) { 108 if (StringUtils.equals(batchJob.getBatchJobName(), batchJobName)) { 109 return true; 110 } 111 } 112 113 return false; 114 } 115 116 117 118 }