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         /*DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
119         Integer createBibCount = (Integer) dataCarrierService.getData(OLEConstants.ORDER_IMPORT_SUCCESS_COUNT);
120         Integer updateBibCount = (Integer) dataCarrierService.getData(OLEConstants.UPDATE_BIB_CNT);
121         Integer createHoldingsCount = (Integer) dataCarrierService.getData(OLEConstants.CREATE_HLD_CNT);
122         Integer orderImportSuccessCount = (Integer) dataCarrierService.getData(OLEConstants.ORDER_IMPORT_SUCCESS_COUNT);
123         Integer orderImportFailureCount = (Integer) dataCarrierService.getData(OLEConstants.ORDER_IMPORT_FAILURE_COUNT);
124         List<String> reasonForFailure = (List<String>) dataCarrierService.getData(OLEConstants.FAILURE_REASON);*/
125 
126         OrderImportHelperBo orderImportHelperBo = job.getOrderImportHelperBo();
127         Integer createBibCount = orderImportHelperBo.getOrderImportSuccessCount();
128         Integer updateBibCount = orderImportHelperBo.getUpdateBibCount();
129         Integer createHoldingsCount = orderImportHelperBo.getCreatHoldingCount();
130         Integer orderImportSuccessCount = orderImportHelperBo.getOrderImportSuccessCount();
131         Integer orderImportFailureCount = orderImportHelperBo.getOrderImportFailureCount();
132         List<String> reasonForFailure = orderImportHelperBo.getFailureReason();
133 
134         job.setCreateBibCount(createBibCount);
135         job.setUpdateBibCount(updateBibCount);
136         job.setCreateHoldingsCount(createHoldingsCount);
137         job.setOrderImportSuccessCount(orderImportSuccessCount);
138         job.setOrderImportFailureCount(orderImportFailureCount);
139         StringBuffer failureBuffer = new StringBuffer();
140         if(reasonForFailure != null && reasonForFailure.size() > 0){
141             for(int failureCount = 0;failureCount < reasonForFailure.size();failureCount++){
142                 failureBuffer.append(reasonForFailure.get(failureCount) + "\n");
143             }
144             createBatchErrorAttachmentFile(failureBuffer.toString());
145         }
146         /*dataCarrierService.addData(OLEConstants.ORDER_IMPORT_SUCCESS_COUNT,0);
147         dataCarrierService.addData(OLEConstants.ORDER_IMPORT_FAILURE_COUNT,0);
148         dataCarrierService.addData(OLEConstants.FAILURE_REASON,new ArrayList<>());*/
149     }
150 
151 }