View Javadoc

1   /**
2    * Copyright 2004-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.batch.web;
17  
18  import java.util.HashMap;
19  import java.util.List;
20  import java.util.Map;
21  
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.apache.log4j.Logger;
27  import org.apache.struts.action.ActionForm;
28  import org.apache.struts.action.ActionForward;
29  import org.apache.struts.action.ActionMapping;
30  import org.kuali.hr.time.base.web.TkAction;
31  import org.kuali.hr.time.batch.BatchApproveMissedPunchJobRunnable;
32  import org.kuali.hr.time.batch.BatchJobEntry;
33  import org.kuali.hr.time.batch.EmployeeApprovalBatchJobRunnable;
34  import org.kuali.hr.time.batch.InitiateBatchJobRunnable;
35  import org.kuali.hr.time.batch.PayPeriodEndBatchJobRunnable;
36  import org.kuali.hr.time.batch.SupervisorApprovalBatchJobRunnable;
37  import org.kuali.hr.time.service.base.TkServiceLocator;
38  import org.kuali.hr.time.util.TkConstants;
39  
40  public class BatchJobAction extends TkAction {
41  
42      private static final Logger LOG = Logger.getLogger(BatchJobAction.class);
43  
44      public ActionForward getBatchJobEntryStatus(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
45          BatchJobActionForm bjaf = (BatchJobActionForm) form;
46          Map<String, Object> searchCrit = new HashMap<String, Object>();
47          searchCrit.put("tkBatchJobId", bjaf.getBatchJobId());
48          searchCrit.put("batchJobName", bjaf.getBatchJobName());
49          searchCrit.put("batchJobEntryStatus", bjaf.getBatchJobEntryStatus());
50          searchCrit.put("hrPyCalendarEntryId", bjaf.getHrPyCalendarEntryId());
51          searchCrit.put("ipAddress", bjaf.getIpAddress());
52          searchCrit.put("documentId", bjaf.getDocumentId());
53          searchCrit.put("principalId", bjaf.getPrincipalId());
54  
55          bjaf.setBatchJobEntries(TkServiceLocator.getBatchJobEntryService().getBatchJobEntries(searchCrit));
56  
57          return mapping.findForward("basic");
58      }
59  
60      public ActionForward changeIpAddress(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
61          BatchJobActionForm bjaf = (BatchJobActionForm) form;
62  
63          BatchJobEntry batchJobEntry = TkServiceLocator.getBatchJobEntryService().getBatchJobEntry(Long.valueOf(bjaf.getTkBatchJobEntryId()));
64          batchJobEntry.setIpAddress(bjaf.getIpToChange());
65          TkServiceLocator.getBatchJobEntryService().saveBatchJobEntry(batchJobEntry);
66  
67          return mapping.findForward("basic");
68      }
69  
70      public ActionForward runBatchJob(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
71          BatchJobActionForm bjaf = (BatchJobActionForm) form;
72  
73          Map<String, Object> searchCrit = new HashMap<String, Object>();
74          searchCrit.put("batchJobName", bjaf.getSelectedBatchJob());
75          searchCrit.put("hrPyCalendarEntryId", bjaf.getHrPyCalendarEntryId());
76          searchCrit.put("batchJobEntryStatus", TkConstants.BATCH_JOB_ENTRY_STATUS.SCHEDULED);
77  
78          List<BatchJobEntry> batchJobEntries = TkServiceLocator.getBatchJobEntryService().getBatchJobEntries(searchCrit);
79  
80          if (batchJobEntries.size() > 0) {
81  
82              for (BatchJobEntry entry : batchJobEntries) {
83  
84                  long startTime = System.currentTimeMillis();
85                  LOG.debug("Before run.");
86                  entry.setBatchJobEntryStatus(TkConstants.BATCH_JOB_ENTRY_STATUS.RUNNING);
87                  TkServiceLocator.getBatchJobEntryService().saveBatchJobEntry(entry);
88  
89                  if (StringUtils.equals(entry.getBatchJobName(), TkConstants.BATCH_JOB_NAMES.APPROVE)) {
90                      LOG.debug("Creating EmployeeApprovalBatchJobRunnable.");
91                      new EmployeeApprovalBatchJobRunnable(entry).doWork();
92                  } else if (StringUtils.equals(entry.getBatchJobName(), TkConstants.BATCH_JOB_NAMES.PAY_PERIOD_END)) {
93                      LOG.debug("Creating PayPeriodEndBatchJobRunnable.");
94                      new PayPeriodEndBatchJobRunnable(entry).doWork();
95                  } else if (StringUtils.equals(entry.getBatchJobName(), TkConstants.BATCH_JOB_NAMES.SUPERVISOR_APPROVAL)) {
96                      LOG.debug("Creating SupervisorApprovalBatchJobRunnabble.");
97                      new SupervisorApprovalBatchJobRunnable(entry).doWork();
98                  } else if (StringUtils.equals(entry.getBatchJobName(), TkConstants.BATCH_JOB_NAMES.INITIATE)) {
99                      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 }