View Javadoc
1   package org.kuali.ole.batch.ingest;
2   
3   import org.apache.commons.lang3.StringUtils;
4   import org.apache.log4j.Logger;
5   import org.kuali.ole.DataCarrierService;
6   import org.kuali.ole.OLEConstants;
7   import org.kuali.ole.batch.bo.OrderImportHelperBo;
8   import org.kuali.ole.batch.impl.AbstractBatchProcess;
9   import org.kuali.ole.ingest.IngestProcessor;
10  import org.kuali.ole.ingest.pojo.IngestRecord;
11  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
12  import org.kuali.rice.krad.UserSession;
13  import org.kuali.rice.krad.util.GlobalVariables;
14  import java.util.ArrayList;
15  import java.util.List;
16  
17  /**
18   * Created with IntelliJ IDEA.
19   * User: aurojyotit
20   * Date: 7/15/13
21   * Time: 4:10 PM
22   * To change this template use File | Settings | File Templates.
23   */
24  public class BatchProcessOrderIngest extends AbstractBatchProcess {
25      private static final Logger LOG = Logger.getLogger(BatchProcessOrderIngest.class);
26      private String marcFileContent;
27      private String ediFileContent;
28      private String mrcFileName;
29      private String ediFileName;
30      private IngestProcessor ingestProcessor;
31  
32      @Override
33      public void prepareForRead() throws Exception {
34          String[] fileNames = job.getUploadFileName().split(",");
35          if(fileNames.length == 2) {
36              mrcFileName = fileNames[0];
37              ediFileName = fileNames[1];
38              ediFileContent=getBatchProcessFileContent(ediFileName);
39          }
40          else {
41              mrcFileName = job.getUploadFileName();
42          }
43          marcFileContent=getBatchProcessFileContent(mrcFileName);
44  
45      }
46  
47      @Override
48      public void prepareForWrite() throws Exception {
49  
50          if(!StringUtils.isBlank(processDef.getUser())){
51              GlobalVariables.setUserSession(new UserSession(processDef.getUser()));
52          }
53          String agendaName = OLEConstants.PROFILE_AGENDA_NM;
54          String agendaDescription = processDef.getBatchProcessName();
55          int recordCount=0;
56          if (null != agendaName && !agendaName.equals("")) {
57              IngestRecord ingestRecord = new IngestRecord();
58              ingestRecord.setOriginalMarcFileName(mrcFileName);
59              ingestRecord.setMarcFileContent(marcFileContent);
60              ingestRecord.setAgendaName(agendaName);
61              ingestRecord.setAgendaDescription(agendaDescription);
62              ingestRecord.setByPassPreProcessing(false);
63              //ingestRecord.setUser(principalName);
64              ingestRecord.setOriginalEdiFileName(ediFileName);
65              ingestRecord.setEdiFileContent(ediFileContent);
66              ingestRecord.setByPassPreProcessing(isPreProcessingRequired(mrcFileName, ediFileName));
67              try {
68                  boolean failure_flag=true;
69                  recordCount = getIngestProcessor().start(ingestRecord, failure_flag, processDef, job);
70                  if (recordCount!=0) {
71                      job.setStatusDesc(OLEConstants.BATCH_ORDER_IMPORT_SUCCESS);
72                  } else if (ingestRecord.isUpdate()) {
73                      job.setStatusDesc(OLEConstants.BATCH_ORDER_IMPORT_SUCCESS);
74                  } else {
75                      job.setStatusDesc(OLEConstants.BATCH_ORDER_IMPORT_FAILURE);
76                  }
77              } catch (Exception orderRecordException) {
78                  LOG.error("Failed to perform Staff Upload:", orderRecordException);
79              }
80          } else {
81              job.setStatusDesc(OLEConstants.ERROR_AGENDA_NAME);
82          }
83          job.setTotalNoOfRecords(recordCount+"");
84          job.setNoOfRecordsProcessed(recordCount+"");
85          job.setNoOfSuccessRecords("N/A");
86          job.setNoOfFailureRecords("N/A");
87          job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
88          checkForFailureReport();
89      }
90  
91      @Override
92      public void getNextBatch() {
93  
94      }
95  
96      @Override
97      public void processBatch() {
98  
99      }
100 
101     private IngestProcessor getIngestProcessor() {
102         if(ingestProcessor == null)
103          ingestProcessor = new IngestProcessor();
104         return ingestProcessor;
105     }
106 
107     private boolean isPreProcessingRequired(String marcFile, String ediFile) {
108         if(ediFile != null) {
109             return (marcFile.contains(".mrc") && ediFile.contains(".edi") ? true : false);
110         }
111         else {
112             return (marcFile.contains(".mrc")? true : false);
113         }
114     }
115 
116 
117     private void checkForFailureReport() throws Exception {
118         OrderImportHelperBo orderImportHelperBo = job.getOrderImportHelperBo();
119         Integer createBibCount = orderImportHelperBo.getCreateBibCount();
120         Integer updateBibCount = orderImportHelperBo.getUpdateBibCount();
121         Integer createHoldingsCount = orderImportHelperBo.getCreateHoldingsCount();
122         Integer updateHoldingsCount = orderImportHelperBo.getUpdateHoldingsCount();
123         Integer orderImportSuccessCount = orderImportHelperBo.getOrderImportSuccessCount();
124         Integer orderImportFailureCount = orderImportHelperBo.getOrderImportFailureCount();
125         List<String> reasonForFailure = orderImportHelperBo.getFailureReason();
126         job.setCreateBibCount(createBibCount);
127         job.setUpdateBibCount(updateBibCount);
128         job.setCreateHoldingsCount(createHoldingsCount);
129         job.setUpdateHoldingsCount(updateHoldingsCount);
130         job.setOrderImportSuccessCount(orderImportSuccessCount);
131         job.setOrderImportFailureCount(orderImportFailureCount);
132         StringBuffer failureBuffer = new StringBuffer();
133         if(reasonForFailure != null && reasonForFailure.size() > 0){
134             for(int failureCount = 0;failureCount < reasonForFailure.size();failureCount++){
135                 failureBuffer.append(reasonForFailure.get(failureCount) + "\n");
136             }
137             createBatchErrorAttachmentFile(failureBuffer.toString());
138         }
139     }
140 
141 }