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(jobDetailsBo.getBatchProfileName() + "_" + jobDetailsBo.getJobId());
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      private void createBatchProcessJobFile(String batchProcessType, OLEBatchProcessJobDetailsBo job) throws Exception {
99          if (!batchProcessType.equals(OLEConstants.OLEBatchProcess.BATCH_EXPORT)) {
100             String mrcFileContent = null;
101             String ediFileContent = null;
102             String ingestFileContent = null;
103             String mrcFileName = null;
104             String ediFileName = null;
105             if (batchProcessType.equals(OLEConstants.OLEBatchProcess.ORDER_RECORD_IMPORT)) {
106                 String[] fileNames = job.getUploadFileName().split(",");
107                 if (fileNames.length == 2) {
108                     mrcFileName = fileNames[0];
109                     ediFileName = fileNames[1];
110                 }
111                 mrcFileContent = getOLEBatchProcessDataHelper().getBatchProcessFileContent(batchProcessType, job.getOleBatchPrcsScheduleId() + OLEConstants.OLEBatchProcess.PROFILE_SCHEDULE + "_" + mrcFileName);
112                 ediFileContent = getOLEBatchProcessDataHelper().getBatchProcessFileContent(batchProcessType, job.getOleBatchPrcsScheduleId() + OLEConstants.OLEBatchProcess.PROFILE_SCHEDULE + "_" + ediFileName);
113                 createBatchProcessJobFile(batchProcessType, job.getJobId() + OLEConstants.OLEBatchProcess.PROFILE_JOB + "_" + mrcFileName, job.getJobId() + OLEConstants.OLEBatchProcess.PROFILE_JOB + "_" + ediFileName, mrcFileContent, ediFileContent);
114 
115             } else {
116                 ingestFileContent = getOLEBatchProcessDataHelper().getBatchProcessFileContent(batchProcessType, job.getOleBatchPrcsScheduleId() + OLEConstants.OLEBatchProcess.PROFILE_SCHEDULE + "_" + job.getUploadFileName());
117                 createBatchProcessJobFile(batchProcessType, job.getJobId() + OLEConstants.OLEBatchProcess.PROFILE_JOB + "_" + job.getUploadFileName(), ingestFileContent);
118             }
119 
120 
121         }
122     }
123 
124     private void createBatchProcessJobFile(String batchProcessType, String mrcFileName, String ediFileName, String mrcFileContent, String ediFileContent) throws Exception {
125         getOLEBatchProcessDataHelper().createBatchProcessFile(batchProcessType, mrcFileName, ediFileName, mrcFileContent, ediFileContent);
126     }
127 
128     private void createBatchProcessJobFile(String batchProcessType, String fileName, String fileContent) throws Exception {
129         getOLEBatchProcessDataHelper().createBatchProcessFile(batchProcessType, fileName, fileContent);
130     }
131 
132     private OLEBatchProcessDataHelper getOLEBatchProcessDataHelper() {
133 
134         if (oleBatchProcessDataHelper == null) {
135             oleBatchProcessDataHelper = OLEBatchProcessDataHelper.getInstance();
136         }
137         return oleBatchProcessDataHelper;
138     }
139 }