View Javadoc
1   package org.kuali.ole.batch.ingest;
2   
3   import org.apache.log4j.Logger;
4   import org.kuali.ole.OLEConstants;
5   import org.kuali.ole.batch.impl.AbstractBatchProcess;
6   import org.kuali.ole.ingest.OleLocationXMLSchemaValidator;
7   import org.kuali.ole.service.OleLocationConverterService;
8   import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
9   import java.io.ByteArrayInputStream;
10  import java.io.InputStream;
11  
12  public class BatchProcessLocationIngest extends AbstractBatchProcess {
13      private static final Logger LOG = Logger.getLogger(BatchProcessLocationIngest.class);
14      OleLocationConverterService oleLocationRecordService;
15      OleLocationXMLSchemaValidator oleLocationXMLSchemaValidator;
16      private String fileContent;
17      private String failedRecords;
18      private Integer totalCount = 0;
19      private Integer successCount = 0;
20      private Integer failureCount = 0;
21  
22      @Override
23      public void prepareForRead() throws Exception {
24          //To change body of implemented methods use File | Settings | File Templates.
25          oleLocationRecordService = GlobalResourceLoader.getService("oleLocationConverterService");
26          oleLocationXMLSchemaValidator = new OleLocationXMLSchemaValidator();
27          fileContent = getBatchProcessFileContent();
28          job.setNoOfSuccessRecords("0");
29          job.setNoOfFailureRecords("0");
30          job.setNoOfRecordsProcessed("0");
31          job.setTotalNoOfRecords("0");
32      }
33  
34      @Override
35      public void prepareForWrite() throws Exception {
36          try {
37              InputStream inputStream = new ByteArrayInputStream(fileContent.getBytes());
38              boolean schemaFlag = oleLocationXMLSchemaValidator.validateContentsAgainstSchema(inputStream);
39              if (schemaFlag) {
40                  String summary = oleLocationRecordService.persistLocationFromFileContent(fileContent, job.getUploadFileName());
41                  totalCount = oleLocationRecordService.getSuccessCount() + oleLocationRecordService.getFailureCount();
42                  successCount = oleLocationRecordService.getSuccessCount();
43                  failureCount = oleLocationRecordService.getFailureCount();
44                  job.setTotalNoOfRecords(totalCount.toString());
45                  job.setNoOfRecordsProcessed(totalCount.toString());
46                  job.setNoOfSuccessRecords(successCount.toString());
47                  job.setNoOfFailureRecords(failureCount.toString());
48                  job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
49                  //job.setStatusDesc(OLEConstants.LOCATION_RECORD_SUCCESS);
50                  processDef.setMessage(OLEConstants.LOCATION_RECORD_SUCCESS + " " + summary);
51                  deleteBatchFile();
52                  if (failureCount > 0) {
53                      failedRecords = oleLocationRecordService.getIngestFailureRecord();
54                      if (!"".equals(failedRecords) && failedRecords != null) {
55                          createBatchFailureFile(failedRecords);
56                      }
57                  }
58              } else {
59                  job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
60                  job.setStatusDesc(OLEConstants.LOCATION_RECORD_INVALID_SCHEMA);
61                  deleteBatchFile();
62                  createBatchFailureFile(fileContent);
63              }
64  
65          } catch (Exception locationIngestException) {
66              job.setStatusDesc(OLEConstants.LOCATION_RECORD_FAILURE);
67              LOG.error("Failed to upload location.", locationIngestException);
68              throw locationIngestException;
69          }
70      }
71  
72      @Override
73      public void getNextBatch() {
74          //To change body of implemented methods use File | Settings | File Templates.
75      }
76  
77      @Override
78      public void processBatch() {
79          //To change body of implemented methods use File | Settings | File Templates.
80      }
81  
82  }