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.web; 017 018 import java.util.HashMap; 019 import java.util.List; 020 import java.util.Map; 021 022 import javax.servlet.http.HttpServletRequest; 023 import javax.servlet.http.HttpServletResponse; 024 025 import org.apache.commons.lang.StringUtils; 026 import org.apache.log4j.Logger; 027 import org.apache.struts.action.ActionForm; 028 import org.apache.struts.action.ActionForward; 029 import org.apache.struts.action.ActionMapping; 030 import org.kuali.hr.time.base.web.TkAction; 031 import org.kuali.hr.time.batch.BatchApproveMissedPunchJob; 032 import org.kuali.hr.time.batch.BatchApproveMissedPunchJobRunnable; 033 import org.kuali.hr.time.batch.BatchJob; 034 import org.kuali.hr.time.batch.BatchJobEntry; 035 import org.kuali.hr.time.batch.EmployeeApprovalBatchJob; 036 import org.kuali.hr.time.batch.EmployeeApprovalBatchJobRunnable; 037 import org.kuali.hr.time.batch.InitiateBatchJob; 038 import org.kuali.hr.time.batch.InitiateBatchJobRunnable; 039 import org.kuali.hr.time.batch.PayPeriodEndBatchJob; 040 import org.kuali.hr.time.batch.PayPeriodEndBatchJobRunnable; 041 import org.kuali.hr.time.batch.SupervisorApprovalBatchJob; 042 import org.kuali.hr.time.batch.SupervisorApprovalBatchJobRunnable; 043 import org.kuali.hr.time.calendar.CalendarEntries; 044 import org.kuali.hr.time.service.base.TkServiceLocator; 045 import org.kuali.hr.time.util.TkConstants; 046 047 public class BatchJobAction extends TkAction { 048 049 private static final Logger LOG = Logger.getLogger(BatchJobAction.class); 050 051 public ActionForward getBatchJobEntryStatus(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 052 BatchJobActionForm bjaf = (BatchJobActionForm) form; 053 Map<String, Object> searchCrit = new HashMap<String, Object>(); 054 searchCrit.put("tkBatchJobId", bjaf.getBatchJobId()); 055 searchCrit.put("batchJobName", bjaf.getBatchJobName()); 056 searchCrit.put("batchJobEntryStatus", bjaf.getBatchJobEntryStatus()); 057 searchCrit.put("hrPyCalendarEntryId", bjaf.getHrPyCalendarEntryId()); 058 searchCrit.put("ipAddress", bjaf.getIpAddress()); 059 searchCrit.put("documentId", bjaf.getDocumentId()); 060 searchCrit.put("principalId", bjaf.getPrincipalId()); 061 062 bjaf.setBatchJobEntries(TkServiceLocator.getBatchJobEntryService().getBatchJobEntries(searchCrit)); 063 064 return mapping.findForward("basic"); 065 } 066 067 public ActionForward changeIpAddress(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 068 BatchJobActionForm bjaf = (BatchJobActionForm) form; 069 070 BatchJobEntry batchJobEntry = TkServiceLocator.getBatchJobEntryService().getBatchJobEntry(Long.valueOf(bjaf.getTkBatchJobEntryId())); 071 batchJobEntry.setIpAddress(bjaf.getIpToChange()); 072 TkServiceLocator.getBatchJobEntryService().saveBatchJobEntry(batchJobEntry); 073 074 return mapping.findForward("basic"); 075 } 076 077 public ActionForward runBatchJob(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 078 BatchJobActionForm bjaf = (BatchJobActionForm) form; 079 080 createBatchJobEntry(bjaf.getSelectedBatchJob(), bjaf.getHrPyCalendarEntryId()); 081 082 runBatchJobEntry(bjaf.getSelectedBatchJob(), bjaf.getHrPyCalendarEntryId()); 083 084 return mapping.findForward("basic"); 085 } 086 087 private void createBatchJobEntry(String selectedBatchJob, String hrPyCalendarEntryId) { 088 Map<String, Object> searchCrit = new HashMap<String, Object>(); 089 searchCrit.put("batchJobName", selectedBatchJob); 090 searchCrit.put("hrPyCalendarEntryId", hrPyCalendarEntryId); 091 searchCrit.put("batchJobEntryStatus", TkConstants.BATCH_JOB_ENTRY_STATUS.SCHEDULED); 092 093 List<BatchJobEntry> batchJobEntries = TkServiceLocator.getBatchJobEntryService().getBatchJobEntries(searchCrit); 094 095 if (batchJobEntries.isEmpty()) { 096 CalendarEntries calendarEntry = TkServiceLocator.getCalendarEntriesService().getCalendarEntries(hrPyCalendarEntryId); 097 098 if (StringUtils.equals(selectedBatchJob, TkConstants.BATCH_JOB_NAMES.APPROVE)) { 099 BatchJob job = new EmployeeApprovalBatchJob(calendarEntry); 100 TkServiceLocator.getBatchJobService().saveBatchJob(job); 101 job.runJob(); 102 } else if (StringUtils.equals(selectedBatchJob, TkConstants.BATCH_JOB_NAMES.PAY_PERIOD_END)) { 103 BatchJob job = new PayPeriodEndBatchJob(calendarEntry); 104 TkServiceLocator.getBatchJobService().saveBatchJob(job); 105 job.runJob(); 106 } else if (StringUtils.equals(selectedBatchJob, TkConstants.BATCH_JOB_NAMES.SUPERVISOR_APPROVAL)) { 107 BatchJob job = new SupervisorApprovalBatchJob(calendarEntry); 108 TkServiceLocator.getBatchJobService().saveBatchJob(job); 109 job.runJob(); 110 } else if (StringUtils.equals(selectedBatchJob, TkConstants.BATCH_JOB_NAMES.INITIATE)) { 111 BatchJob job = new InitiateBatchJob(calendarEntry); 112 TkServiceLocator.getBatchJobService().saveBatchJob(job); 113 job.runJob(); 114 } else if (StringUtils.equals(selectedBatchJob, TkConstants.BATCH_JOB_NAMES.BATCH_APPROVE_MISSED_PUNCH)) { 115 BatchJob job = new BatchApproveMissedPunchJob(calendarEntry); 116 TkServiceLocator.getBatchJobService().saveBatchJob(job); 117 job.runJob(); 118 } 119 } 120 } 121 122 private void runBatchJobEntry(String selectedBatchJob, String hrPyCalendarEntryId) throws Exception { 123 Map<String, Object> searchCrit = new HashMap<String, Object>(); 124 searchCrit.put("batchJobName", selectedBatchJob); 125 searchCrit.put("hrPyCalendarEntryId", hrPyCalendarEntryId); 126 searchCrit.put("batchJobEntryStatus", TkConstants.BATCH_JOB_ENTRY_STATUS.SCHEDULED); 127 128 List<BatchJobEntry> batchJobEntries = TkServiceLocator.getBatchJobEntryService().getBatchJobEntries(searchCrit); 129 130 for (BatchJobEntry entry : batchJobEntries) { 131 long startTime = System.currentTimeMillis(); 132 LOG.debug("Before run."); 133 entry.setBatchJobEntryStatus(TkConstants.BATCH_JOB_ENTRY_STATUS.RUNNING); 134 TkServiceLocator.getBatchJobEntryService().saveBatchJobEntry(entry); 135 136 if (StringUtils.equals(entry.getBatchJobName(), TkConstants.BATCH_JOB_NAMES.APPROVE)) { 137 LOG.debug("Creating EmployeeApprovalBatchJobRunnable."); 138 new EmployeeApprovalBatchJobRunnable(entry).doWork(); 139 } else if (StringUtils.equals(entry.getBatchJobName(), TkConstants.BATCH_JOB_NAMES.PAY_PERIOD_END)) { 140 LOG.debug("Creating PayPeriodEndBatchJobRunnable."); 141 new PayPeriodEndBatchJobRunnable(entry).doWork(); 142 } else if (StringUtils.equals(entry.getBatchJobName(), TkConstants.BATCH_JOB_NAMES.SUPERVISOR_APPROVAL)) { 143 LOG.debug("Creating SupervisorApprovalBatchJobRunnabble."); 144 new SupervisorApprovalBatchJobRunnable(entry).doWork(); 145 } else if (StringUtils.equals(entry.getBatchJobName(), TkConstants.BATCH_JOB_NAMES.INITIATE)) { 146 LOG.debug("Creating InitiateBatchJobRunnable."); 147 new InitiateBatchJobRunnable(entry).doWork(); 148 } else if (StringUtils.equals(entry.getBatchJobName(), TkConstants.BATCH_JOB_NAMES.BATCH_APPROVE_MISSED_PUNCH)) { 149 LOG.debug("Creating BatchApproveMissedPunchJobRunnable."); 150 new BatchApproveMissedPunchJobRunnable(entry).doWork(); 151 } else { 152 LOG.warn("Unknown BatchJobEntryRunnable found in BatchJobEntry table. Unable to create Runnable."); 153 } 154 155 long endTime = System.currentTimeMillis(); 156 long runtime = endTime - startTime; 157 runtime = (runtime > 0) ? runtime : 1; // hack around 0 length job... just in case. 158 LOG.debug("Job finished in " + runtime / 1000 + " seconds."); 159 160 if (StringUtils.isEmpty(entry.getBatchJobException())) { 161 entry.setBatchJobEntryStatus(TkConstants.BATCH_JOB_ENTRY_STATUS.FINISHED); 162 } else { 163 entry.setBatchJobEntryStatus(TkConstants.BATCH_JOB_ENTRY_STATUS.EXCEPTION); 164 } 165 TkServiceLocator.getBatchJobEntryService().saveBatchJobEntry(entry); 166 } 167 } 168 169 170 }