001 /**
002 * Copyright 2004-2012 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.BatchApproveMissedPunchJobRunnable;
032 import org.kuali.hr.time.batch.BatchJobEntry;
033 import org.kuali.hr.time.batch.EmployeeApprovalBatchJobRunnable;
034 import org.kuali.hr.time.batch.InitiateBatchJobRunnable;
035 import org.kuali.hr.time.batch.PayPeriodEndBatchJobRunnable;
036 import org.kuali.hr.time.batch.SupervisorApprovalBatchJobRunnable;
037 import org.kuali.hr.time.service.base.TkServiceLocator;
038 import org.kuali.hr.time.util.TkConstants;
039
040 public class BatchJobAction extends TkAction {
041
042 private static final Logger LOG = Logger.getLogger(BatchJobAction.class);
043
044 public ActionForward getBatchJobEntryStatus(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
045 BatchJobActionForm bjaf = (BatchJobActionForm) form;
046 Map<String, Object> searchCrit = new HashMap<String, Object>();
047 searchCrit.put("tkBatchJobId", bjaf.getBatchJobId());
048 searchCrit.put("batchJobName", bjaf.getBatchJobName());
049 searchCrit.put("batchJobEntryStatus", bjaf.getBatchJobEntryStatus());
050 searchCrit.put("hrPyCalendarEntryId", bjaf.getHrPyCalendarEntryId());
051 searchCrit.put("ipAddress", bjaf.getIpAddress());
052 searchCrit.put("documentId", bjaf.getDocumentId());
053 searchCrit.put("principalId", bjaf.getPrincipalId());
054
055 bjaf.setBatchJobEntries(TkServiceLocator.getBatchJobEntryService().getBatchJobEntries(searchCrit));
056
057 return mapping.findForward("basic");
058 }
059
060 public ActionForward changeIpAddress(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
061 BatchJobActionForm bjaf = (BatchJobActionForm) form;
062
063 BatchJobEntry batchJobEntry = TkServiceLocator.getBatchJobEntryService().getBatchJobEntry(Long.valueOf(bjaf.getTkBatchJobEntryId()));
064 batchJobEntry.setIpAddress(bjaf.getIpToChange());
065 TkServiceLocator.getBatchJobEntryService().saveBatchJobEntry(batchJobEntry);
066
067 return mapping.findForward("basic");
068 }
069
070 public ActionForward runBatchJob(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
071 BatchJobActionForm bjaf = (BatchJobActionForm) form;
072
073 Map<String, Object> searchCrit = new HashMap<String, Object>();
074 searchCrit.put("batchJobName", bjaf.getSelectedBatchJob());
075 searchCrit.put("hrPyCalendarEntryId", bjaf.getHrPyCalendarEntryId());
076 searchCrit.put("batchJobEntryStatus", TkConstants.BATCH_JOB_ENTRY_STATUS.SCHEDULED);
077
078 List<BatchJobEntry> batchJobEntries = TkServiceLocator.getBatchJobEntryService().getBatchJobEntries(searchCrit);
079
080 if (batchJobEntries.size() > 0) {
081
082 for (BatchJobEntry entry : batchJobEntries) {
083
084 long startTime = System.currentTimeMillis();
085 LOG.debug("Before run.");
086 entry.setBatchJobEntryStatus(TkConstants.BATCH_JOB_ENTRY_STATUS.RUNNING);
087 TkServiceLocator.getBatchJobEntryService().saveBatchJobEntry(entry);
088
089 if (StringUtils.equals(entry.getBatchJobName(), TkConstants.BATCH_JOB_NAMES.APPROVE)) {
090 LOG.debug("Creating EmployeeApprovalBatchJobRunnable.");
091 new EmployeeApprovalBatchJobRunnable(entry).doWork();
092 } else if (StringUtils.equals(entry.getBatchJobName(), TkConstants.BATCH_JOB_NAMES.PAY_PERIOD_END)) {
093 LOG.debug("Creating PayPeriodEndBatchJobRunnable.");
094 new PayPeriodEndBatchJobRunnable(entry).doWork();
095 } else if (StringUtils.equals(entry.getBatchJobName(), TkConstants.BATCH_JOB_NAMES.SUPERVISOR_APPROVAL)) {
096 LOG.debug("Creating SupervisorApprovalBatchJobRunnabble.");
097 new SupervisorApprovalBatchJobRunnable(entry).doWork();
098 } else if (StringUtils.equals(entry.getBatchJobName(), TkConstants.BATCH_JOB_NAMES.INITIATE)) {
099 LOG.debug("Creating InitiateBatchJobRunnable.");
100 new InitiateBatchJobRunnable(entry).doWork();
101 } else if (StringUtils.equals(entry.getBatchJobName(), TkConstants.BATCH_JOB_NAMES.BATCH_APPROVE_MISSED_PUNCH)) {
102 LOG.debug("Creating BatchApproveMissedPunchJobRunnable.");
103 new BatchApproveMissedPunchJobRunnable(entry).doWork();
104 } else {
105 LOG.warn("Unknown BatchJobEntryRunnable found in BatchJobEntry table. Unable to create Runnable.");
106 }
107
108 long endTime = System.currentTimeMillis();
109 long runtime = endTime - startTime;
110 runtime = (runtime > 0) ? runtime : 1; // hack around 0 length job... just in case.
111 LOG.debug("Job finished in " + runtime / 1000 + " seconds.");
112
113 if (StringUtils.isEmpty(entry.getBatchJobException())) {
114 entry.setBatchJobEntryStatus(TkConstants.BATCH_JOB_ENTRY_STATUS.FINISHED);
115 } else {
116 entry.setBatchJobEntryStatus(TkConstants.BATCH_JOB_ENTRY_STATUS.EXCEPTION);
117 }
118 TkServiceLocator.getBatchJobEntryService().saveBatchJobEntry(entry);
119 }
120
121 }
122 return mapping.findForward("basic");
123 }
124
125
126 }