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
20
21
22
23
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
38
39
40
41
42
43
44
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
61 }
62 return true;
63 }
64
65
66
67
68
69
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
87
88
89
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
101
102
103
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 }
118 mrcFileContent = getOLEBatchProcessDataHelper().getBatchProcessFileContent(batchProcessType, job.getOleBatchPrcsScheduleId() + OLEConstants.OLEBatchProcess.PROFILE_SCHEDULE + "_" + mrcFileName, job.getOleBatchPrcsScheduleId());
119 ediFileContent = getOLEBatchProcessDataHelper().getBatchProcessFileContent(batchProcessType, job.getOleBatchPrcsScheduleId() + OLEConstants.OLEBatchProcess.PROFILE_SCHEDULE + "_" + ediFileName, job.getOleBatchPrcsScheduleId());
120 createBatchProcessJobFile(batchProcessType, job.getJobId() + OLEConstants.OLEBatchProcess.PROFILE_JOB + "_" + mrcFileName, job.getJobId() + OLEConstants.OLEBatchProcess.PROFILE_JOB + "_" + ediFileName, mrcFileContent, ediFileContent , job.getJobId());
121
122 } else {
123 ingestFileContent = getOLEBatchProcessDataHelper().getBatchProcessFileContent(batchProcessType, job.getOleBatchPrcsScheduleId() + OLEConstants.OLEBatchProcess.PROFILE_SCHEDULE + "_" + job.getUploadFileName(), job.getOleBatchPrcsScheduleId());
124 createBatchProcessJobFile(batchProcessType, job.getJobId() + OLEConstants.OLEBatchProcess.PROFILE_JOB + "_" + job.getUploadFileName(), ingestFileContent, job.getJobId());
125 }
126
127
128 }
129 }
130
131 private void createBatchProcessJobFile(String batchProcessType, String mrcFileName, String ediFileName, String mrcFileContent, String ediFileContent , String jobId) throws Exception {
132 getOLEBatchProcessDataHelper().createBatchProcessFile(batchProcessType, mrcFileName, ediFileName, mrcFileContent, ediFileContent , jobId);
133 }
134
135 private void createBatchProcessJobFile(String batchProcessType, String fileName, String fileContent , String jobId) throws Exception {
136 getOLEBatchProcessDataHelper().createBatchProcessFile(batchProcessType, fileName, fileContent , jobId);
137 }
138
139 private OLEBatchProcessDataHelper getOLEBatchProcessDataHelper() {
140
141 if (oleBatchProcessDataHelper == null) {
142 oleBatchProcessDataHelper = OLEBatchProcessDataHelper.getInstance();
143 }
144 return oleBatchProcessDataHelper;
145 }
146 }