View Javadoc
1   package org.kuali.ole.batch.impl;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.log4j.Logger;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.batch.bo.OLEBatchProcessJobDetailsBo;
7   import org.kuali.ole.batch.document.OLEBatchProcessDefinitionDocument;
8   import org.kuali.ole.sys.batch.AbstractStep;
9   import org.kuali.ole.sys.batch.service.SchedulerService;
10  import org.kuali.rice.krad.service.KRADServiceLocator;
11  
12  import java.util.Date;
13  import java.util.HashMap;
14  import java.util.Map;
15  
16  /**
17   * Created with IntelliJ IDEA.
18   * User: meenrajd
19   * Date: 7/25/13
20   * Time: 4:02 PM
21   * To change this template use File | Settings | File Templates.
22   */
23  public class OLEBatchProcessAdhocStep extends AbstractStep {
24      private static final Logger LOG = Logger.getLogger(OLEBatchProcessAdhocStep.class);
25  
26      @Override
27      public boolean execute(String jobName, Date jobRunDate) throws InterruptedException {
28          String jobId = StringUtils.substringAfter(jobName, SchedulerService.SCHEDULED_GROUP + "." + OLEConstants.OLEBatchProcess.ADHOC_BATCH_JOB);
29          if (StringUtils.isEmpty(jobId)) return true;
30          return executeBatch(jobId);
31      }
32  
33      /**
34       * performs the  job operation
35       * 1. Reads the job record
36       * 2. gets the process type from the job record, creates the specific process and call the process batch
37       * jobName will be of the fomat scheduled.ADHOC_BATCH_JOB{1} where 1 is the job record id
38       *
39       * @param jobName
40       * @return
41       */
42      private boolean executeBatch(String jobName) {
43          try {
44              OLEBatchProcessJobDetailsBo jobBo = readJobRecord(jobName);
45              Map<String, String> prcsMap = new HashMap<String, String>();
46              prcsMap.put("bat_prcs_id", jobBo.getBatchProcessId());
47              OLEBatchProcessDefinitionDocument processDef = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OLEBatchProcessDefinitionDocument.class, prcsMap);
48              LOG.info("Executing Batch process type :: " + jobBo.getBatchProcessType());
49              OLEBatchProcess process = BatchProcessFactory.createProcess(jobBo.getBatchProcessType());
50              process.process(processDef, jobBo);
51          } catch (Exception e) {
52              LOG.error("Error while running Batch Process Step::OLEBatchProcessAdhocStep", e);
53              // return false;
54          }
55          return true;
56      }
57  
58      /**
59       * reads the job record which is being run
60       *
61       * @param jobId
62       * @return
63       */
64      private OLEBatchProcessJobDetailsBo readJobRecord(String jobId) {
65          try {
66              Thread.sleep(2000);
67          } catch (InterruptedException e) {
68              LOG.error("Error while running Batch Process Step::", e);
69          }
70          Map<String, String> jobMap = new HashMap<String, String>();
71          jobMap.put("job_id", jobId);
72          OLEBatchProcessJobDetailsBo jobDetailsBo = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OLEBatchProcessJobDetailsBo.class, jobMap);
73          return jobDetailsBo;
74          //jobDetailsBo.setJobName(jobDetailsBo.getBatchProfileName() + "_" + jobDetailsBo.getJobId());
75          //return KRADServiceLocator.getBusinessObjectService().save(jobDetailsBo);
76  
77      }
78  }