View Javadoc
1   package org.kuali.ole.ingest;
2   
3   import org.kuali.ole.DataCarrierService;
4   import org.kuali.ole.OLEConstants;
5   import org.kuali.ole.OleOrderRecordHandler;
6   import org.kuali.ole.OleOrderRecords;
7   import org.kuali.ole.batch.bo.OLEBatchProcessJobDetailsBo;
8   import org.kuali.ole.batch.bo.OrderImportHelperBo;
9   import org.kuali.ole.converter.MarcXMLConverter;
10  import org.kuali.ole.converter.OLEEDIConverter;
11  import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
12  import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
13  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
14  import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
15  import org.kuali.ole.pojo.OleBibRecord;
16  import org.kuali.ole.pojo.OleOrderRecord;
17  import org.kuali.ole.select.businessobject.OleRequisitionItem;
18  import org.kuali.ole.select.service.OleExposedWebService;
19  import org.kuali.ole.select.service.impl.OleExposedWebServiceImpl;
20  import org.kuali.ole.sys.context.SpringContext;
21  import org.kuali.rice.core.api.config.property.ConfigContext;
22  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
23  import org.xml.sax.SAXException;
24  
25  import java.io.IOException;
26  import java.util.*;
27  
28  /**
29   * IngestProcessor converts the marcFileContent in to marcXmlContent,ediFileContent in to ediXMLContent and also
30   * creates Requisition and Purchase Order based on oleOrderRecordXml
31   */
32  public class IngestProcessor extends AbstractIngestProcessor {
33  
34      private DocstoreClientLocator docstoreClientLocator;
35  
36      public DocstoreClientLocator getDocstoreClientLocator() {
37  
38          if (docstoreClientLocator == null) {
39              docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
40  
41          }
42          return docstoreClientLocator;
43      }
44      //DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
45  
46      /**
47       * This method modify the marcFileContent in to marcXmlContent.
48       *
49       * @param marcFileContent
50       * @return modifiedXMLContent.
51       */
52      @Override
53      public String preProcessMarc(String marcFileContent, OLEBatchProcessJobDetailsBo job) {
54          OrderImportHelperBo orderImportHelperBo = job.getOrderImportHelperBo();
55          String marcXMLContent = null;
56          MarcXMLConverter marcXMLConverter = new MarcXMLConverter();
57          try {
58              marcXMLContent = marcXMLConverter.convert(marcFileContent);
59          }
60          catch(Exception ex){
61              ex.getMessage();
62              List<String> reasonForFailure = new ArrayList<>();
63              reasonForFailure.add("Unable to parse the marc file. Allowed format is UTF-8");
64              reasonForFailure.add("======================================================");
65              reasonForFailure.add(ex.getMessage());
66              //dataCarrierService.addData(OLEConstants.FAILURE_REASON,reasonForFailure);
67              orderImportHelperBo.setFailureReason(reasonForFailure);
68          }
69          //TODO: hack to get rid of the extra xmlns entry. Not sure why the second entry gets generated when calling marc4J in ole-docstore-utility.
70          //TODO: the duplicate entry does not get genereated if its run directly in the ole-docstore-utilty project.
71          String modifiedXMLContent =
72                  marcXMLContent.
73                          replace("collection xmlns=\"http://www.loc.gov/MARC21/slim\" xmlns=\"http://www.loc.gov/MARC21/slim",
74                                  "collection xmlns=\"http://www.loc.gov/MARC21/slim");
75          return modifiedXMLContent;
76      }
77  
78      /**
79       * This method converts the ediFileContent in to ediXMLContent.
80       *
81       * @param ediFileContent
82       * @return ediXMLContent.
83       */
84      @Override
85      public String preProcessEDI(String ediFileContent) {
86          String ediXMLContent = null;
87          OLEEDIConverter oleEDIConverter = new OLEEDIConverter();
88          try {
89              ediXMLContent = oleEDIConverter.convertToXML(ediFileContent);
90          } catch (IOException e) {
91              System.out.println(e.getMessage());
92          } catch (SAXException e) {
93              System.out.println(e.getMessage());
94          }
95          return ediXMLContent;
96      }
97  
98      /**
99       * This method creates Requisition and Purchase Order based on oleOrderRecordXml.
100      * The oleExposedWebService will call the createReqAndPO method in oleRice1 and creates the requisition and PO.
101      */
102     @Override
103     public void postProcess(OLEBatchProcessJobDetailsBo job) {
104         OleOrderRecords oleOrderRecords = new OleOrderRecords();
105         try {
106             List<OleOrderRecord> oleOrderRecordList = getOleOrderRecordList();
107             oleOrderRecords.setRecords(oleOrderRecordList);
108             OleOrderRecordHandler oleEditorResponseHandler = new OleOrderRecordHandler();
109             String oleOrderRecordXml = oleEditorResponseHandler.toXML(oleOrderRecords);
110             OleExposedWebServiceImpl oleExposedWebService = (OleExposedWebServiceImpl) SpringContext.getBean(OleExposedWebService.class);
111             oleExposedWebService.createReqAndPO(oleOrderRecordXml, job);
112         } catch (Exception e) {
113             e.printStackTrace();
114         }
115     }
116 
117     /**
118      * This method builds the request to Rollback the LinkedDocs for failure transactions.
119      *
120      * @param oleOrderRecords
121      * @return xml
122      */
123     private String buildRequestForRollback(OleOrderRecords oleOrderRecords) {
124         String xml = null;
125         for (Iterator<OleOrderRecord> iterator = oleOrderRecords.getRecords().iterator(); iterator.hasNext(); ) {
126             OleOrderRecord oleOrderRecord = iterator.next();
127             OleBibRecord oleBibRecord = oleOrderRecord.getOleBibRecord();
128             String bibUUID = oleBibRecord.getBibUUID();
129 
130             RequestHandler requestHandler = new RequestHandler();
131             Request request = new Request();
132             request.setOperation("deleteWithLinkedDocs");
133             RequestDocument requestDocument = new RequestDocument();
134             requestDocument.setId(bibUUID);
135             request.setRequestDocuments(Arrays.asList(requestDocument));
136             xml = requestHandler.toXML(request);
137         }
138         return xml;
139     }
140 
141     /**
142      * Gets the oleExposedWebService url from PropertyUtil.
143      *
144      * @return url.
145      */
146     public String getURL() {
147         String url = ConfigContext.getCurrentContextConfig().getProperty("oleExposedWebService.url");
148         return url;
149     }
150 
151 }