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.OlePatronXMLSchemaValidator;
7 import org.kuali.ole.deliver.bo.OlePatronIngestSummaryRecord;
8 import org.kuali.ole.service.OlePatronConverterService;
9 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
10 import org.kuali.rice.krad.util.GlobalVariables;
11
12 import java.io.ByteArrayInputStream;
13 import java.io.InputStream;
14 import java.util.Map;
15
16
17
18
19
20
21
22
23 public class BatchProcessPatronIngest extends AbstractBatchProcess {
24 private static final Logger LOG = Logger.getLogger(BatchProcessPatronIngest.class);
25 private OlePatronConverterService olePatronRecordService;
26 private String principalName = GlobalVariables.getUserSession().getPrincipalName();
27 private OlePatronIngestSummaryRecord olePatronIngestSummaryRecord;
28 private OlePatronXMLSchemaValidator olePatronXMLSchemaValidator;
29 private String fileContent = null;
30
31 @Override
32 public void prepareForRead() throws Exception {
33 olePatronRecordService = GlobalResourceLoader.getService(OLEConstants.PATRON_CONVERTER_SERVICE);
34 olePatronIngestSummaryRecord = new OlePatronIngestSummaryRecord();
35 olePatronXMLSchemaValidator = new OlePatronXMLSchemaValidator();
36 fileContent = getBatchProcessFileContent();
37 job.setNoOfSuccessRecords("0");
38 job.setNoOfFailureRecords("0");
39 job.setNoOfRecordsProcessed("0");
40 }
41
42
43 @Override
44 public void prepareForWrite() throws Exception {
45 InputStream inputStream = new ByteArrayInputStream(fileContent.getBytes());
46
47 Map validateResultMap = olePatronXMLSchemaValidator.validateContentsAgainstSchema(inputStream);
48 boolean schemaFlag = (boolean)validateResultMap.get(OLEConstants.OlePatron.PATRON_XML_ISVALID);
49 if (!schemaFlag) {
50 job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
51 deleteBatchFile();
52 createBatchFailureFile(fileContent);
53 throw new Exception("patron ingest schema Failed");
54 }
55
56 olePatronRecordService.persistPatronFromFileContent(fileContent, processDef.isAddUnmatchedPatron(), job.getUploadFileName(), olePatronIngestSummaryRecord, "", principalName);
57 String failedRecords = olePatronIngestSummaryRecord.getFailureRecords();
58 if (!"".equals(failedRecords) && failedRecords != null) {
59 createBatchFailureFile(failedRecords);
60 }
61 deleteBatchFile();
62 int totalCount = olePatronIngestSummaryRecord.getPatronCreateCount() + olePatronIngestSummaryRecord.getPatronUpdateCount() + olePatronIngestSummaryRecord.getPatronFailedCount();
63 int faiCount = olePatronIngestSummaryRecord.getPatronFailedCount();
64 job.setTotalNoOfRecords(totalCount + "");
65 job.setNoOfRecordsProcessed(totalCount + "");
66 job.setNoOfSuccessRecords(totalCount - faiCount + "");
67 job.setNoOfFailureRecords(faiCount + "");
68 job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
69
70 }
71
72 @Override
73 public void getNextBatch() {
74
75 }
76
77 @Override
78 public void processBatch() {
79
80 }
81 }