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.bo.OLEBatchProcessScheduleBo;
8   import org.kuali.ole.batch.document.OLEBatchProcessDefinitionDocument;
9   import org.kuali.ole.batch.helper.OLEBatchProcessDataHelper;
10  import org.kuali.ole.sys.batch.AbstractStep;
11  import org.kuali.ole.sys.batch.service.SchedulerService;
12  import org.kuali.rice.krad.service.KRADServiceLocator;
13  
14  import java.util.Date;
15  import java.util.HashMap;
16  import java.util.Map;
17  
18  /**
19   * Created with IntelliJ IDEA.
20   * User: meenrajd
21   * Date: 7/25/13
22   * Time: 4:02 PM
23   * To change this template use File | Settings | File Templates.
24   */
25  public class OLEBatchProcessStep extends AbstractStep {
26      private static final Logger LOG = Logger.getLogger(OLEBatchProcessStep.class);
27      private OLEBatchProcessDataHelper oleBatchProcessDataHelper;
28  
29      @Override
30      public boolean execute(String jobName, Date jobRunDate) throws InterruptedException {
31          String scheduleId = StringUtils.substringAfter(jobName, SchedulerService.SCHEDULED_GROUP + "." + OLEConstants.OLEBatchProcess.BATCH_JOB);
32          if (StringUtils.isEmpty(scheduleId)) return true;
33          return executeBatch(scheduleId);
34      }
35  
36      /**
37       * performs job operation
38       * 1. reads the scheduled record in using the scheduleId which is in the job name
39       * 2. Reads the process definition from the process def table
40       * 3. Creates a job record for the given schedule
41       * 4. Creates the process based on process type and runs the batch process
42       *
43       * @param scheduleId
44       * @return
45       */
46      private boolean executeBatch(String scheduleId) {
47          try {
48              OLEBatchProcessScheduleBo scheduleBo = readScheduleRecord(scheduleId);
49              String prcsId = scheduleBo.getBatchProcessId();
50              Map<String, String> prcsMap = new HashMap<String, String>();
51              prcsMap.put("bat_prcs_id", prcsId);
52              OLEBatchProcessDefinitionDocument processDef = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OLEBatchProcessDefinitionDocument.class, prcsMap);
53              OLEBatchProcessJobDetailsBo jobBo = createJobRecord(processDef, scheduleBo);
54              createBatchProcessJobFile(processDef.getBatchProcessType(), jobBo);
55              LOG.info("Executing Batch process type :: " + jobBo.getBatchProcessType());
56              OLEBatchProcess process = BatchProcessFactory.createProcess(jobBo.getBatchProcessType());
57              process.process(processDef, jobBo);
58          } catch (Exception e) {
59              LOG.error("Error while running Batch Process Step:: OLEBatchProcessStep", e);
60              //  return false;
61          }
62          return true;
63      }
64  
65      /**
66       * Creates the job record for the given process def
67       *
68       * @param processDef
69       * @return
70       */
71      private OLEBatchProcessJobDetailsBo createJobRecord(OLEBatchProcessDefinitionDocument processDef, OLEBatchProcessScheduleBo scheduleBo) {
72          OLEBatchProcessJobDetailsBo jobBo = new OLEBatchProcessJobDetailsBo();
73          jobBo.setBatchProcessId(processDef.getBatchProcessId());
74          jobBo.setBatchProcessType(processDef.getBatchProcessType());
75          jobBo.setBatchProfileName(processDef.getBatchProcessProfileName());
76          jobBo.setUserName(processDef.getUser());
77          OLEBatchProcessJobDetailsBo jobDetailsBo = KRADServiceLocator.getBusinessObjectService().save(jobBo);
78          jobDetailsBo.setJobName(processDef.getBatchProcessName());
79          jobDetailsBo.setUserName(processDef.getUser());
80          jobDetailsBo.setUploadFileName(scheduleBo.getUploadFileName());
81          jobDetailsBo.setOleBatchPrcsScheduleId(scheduleBo.getScheduleId());
82          return KRADServiceLocator.getBusinessObjectService().save(jobDetailsBo);
83      }
84  
85      /**
86       * Reads the schedule record for the given schedule id
87       *
88       * @param scheduleId
89       * @return
90       */
91      private OLEBatchProcessScheduleBo readScheduleRecord(String scheduleId) {
92          Map<String, String> schMap = new HashMap<String, String>();
93          schMap.put("ole_bat_prcs_schdule_id", scheduleId);
94          OLEBatchProcessScheduleBo scheduleBo = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OLEBatchProcessScheduleBo.class, schMap);
95          return scheduleBo;
96      }
97  
98  
99      /**
100      * This method is using for creating the job file for each schedule job running time
101      * @param batchProcessType
102      * @param job
103      * @throws Exception
104      */
105     private void createBatchProcessJobFile(String batchProcessType, OLEBatchProcessJobDetailsBo job) throws Exception {
106         if (!batchProcessType.equals(OLEConstants.OLEBatchProcess.BATCH_EXPORT)) {
107             String mrcFileContent = null;
108             String ediFileContent = null;
109             String ingestFileContent = null;
110             String mrcFileName = null;
111             String ediFileName = null;
112             if (batchProcessType.equals(OLEConstants.OLEBatchProcess.ORDER_RECORD_IMPORT)) {
113                 String[] fileNames = job.getUploadFileName().split(",");
114                 if (fileNames.length == 2) {
115                     mrcFileName = fileNames[0];
116                     ediFileName = fileNames[1];
117                     mrcFileContent = getOLEBatchProcessDataHelper().getBatchProcessFileContent(batchProcessType, job.getOleBatchPrcsScheduleId() + OLEConstants.OLEBatchProcess.PROFILE_SCHEDULE + "_" + mrcFileName, job.getOleBatchPrcsScheduleId());
118                     ediFileContent = getOLEBatchProcessDataHelper().getBatchProcessFileContent(batchProcessType, job.getOleBatchPrcsScheduleId() + OLEConstants.OLEBatchProcess.PROFILE_SCHEDULE + "_" + ediFileName, job.getOleBatchPrcsScheduleId());
119                     createBatchProcessJobFile(batchProcessType, job.getJobId() + OLEConstants.OLEBatchProcess.PROFILE_JOB + "_" + mrcFileName, job.getJobId() + OLEConstants.OLEBatchProcess.PROFILE_JOB + "_" + ediFileName, mrcFileContent, ediFileContent , job.getJobId());
120                 }
121                 else {
122                     ingestFileContent = getOLEBatchProcessDataHelper().getBatchProcessFileContent(batchProcessType, job.getOleBatchPrcsScheduleId() + OLEConstants.OLEBatchProcess.PROFILE_SCHEDULE + "_" + job.getUploadFileName(), job.getOleBatchPrcsScheduleId());
123                     createBatchProcessJobFile(batchProcessType, job.getJobId() + OLEConstants.OLEBatchProcess.PROFILE_JOB + "_" + job.getUploadFileName(), ingestFileContent, job.getJobId());
124                 }
125             } else {
126                 ingestFileContent = getOLEBatchProcessDataHelper().getBatchProcessFileContent(batchProcessType, job.getOleBatchPrcsScheduleId() + OLEConstants.OLEBatchProcess.PROFILE_SCHEDULE + "_" + job.getUploadFileName(), job.getOleBatchPrcsScheduleId());
127                 createBatchProcessJobFile(batchProcessType, job.getJobId() + OLEConstants.OLEBatchProcess.PROFILE_JOB + "_" + job.getUploadFileName(), ingestFileContent, job.getJobId());
128             }
129 
130 
131         }
132     }
133 
134     private void createBatchProcessJobFile(String batchProcessType, String mrcFileName, String ediFileName, String mrcFileContent, String ediFileContent , String jobId) throws Exception {
135         getOLEBatchProcessDataHelper().createBatchProcessFile(batchProcessType, mrcFileName, ediFileName, mrcFileContent, ediFileContent , jobId);
136     }
137 
138     private void createBatchProcessJobFile(String batchProcessType, String fileName, String fileContent , String jobId) throws Exception {
139         getOLEBatchProcessDataHelper().createBatchProcessFile(batchProcessType, fileName, fileContent , jobId);
140     }
141 
142     private OLEBatchProcessDataHelper getOLEBatchProcessDataHelper() {
143 
144         if (oleBatchProcessDataHelper == null) {
145             oleBatchProcessDataHelper = OLEBatchProcessDataHelper.getInstance();
146         }
147         return oleBatchProcessDataHelper;
148     }
149 }