View Javadoc
1   package org.kuali.ole.ingest;
2   
3   import org.kuali.ole.OLEConstants;
4   import org.kuali.ole.OleOrderRecordHandler;
5   import org.kuali.ole.OleOrderRecords;
6   import org.kuali.ole.converter.MarcXMLConverter;
7   import org.kuali.ole.converter.OLEEDIConverter;
8   import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
9   import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
10  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
11  import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
12  import org.kuali.ole.pojo.OleBibRecord;
13  import org.kuali.ole.pojo.OleOrderRecord;
14  import org.kuali.ole.sys.context.SpringContext;
15  import org.kuali.rice.core.api.config.property.ConfigContext;
16  import org.kuali.rice.krms.api.engine.EngineResults;
17  import org.xml.sax.SAXException;
18  
19  import java.io.IOException;
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  /**
26   * Created with IntelliJ IDEA.
27   * User: ?
28   * Date: 12/27/12
29   * Time: 2:44 PM
30   * To change this template use File | Settings | File Templates.
31   */
32  public class MockIngestProcessor 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  
45      @Override
46      public String preProcessMarc(String marcFileContent) {
47          String marcXMLContent = null;
48          MarcXMLConverter marcXMLConverter = new MarcXMLConverter();
49          marcXMLContent = marcXMLConverter.convert(marcFileContent);
50  
51          //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.
52          //TODO: the duplicate entry does not get genereated if its run directly in the ole-docstore-utilty project.
53          String modifiedXMLContent =
54                  marcXMLContent.
55                          replace("collection xmlns=\"http://www.loc.gov/MARC21/slim\" xmlns=\"http://www.loc.gov/MARC21/slim",
56                                  "collection xmlns=\"http://www.loc.gov/MARC21/slim");
57          return modifiedXMLContent;
58      }
59  
60      @Override
61      public String preProcessEDI(String ediFileContent) {
62          String ediXMLContent = null;
63          OLEEDIConverter oleEDIConverter = new OLEEDIConverter();
64          try {
65              ediXMLContent = oleEDIConverter.convertToXML(ediFileContent);
66          } catch (IOException e) {
67              System.out.println(e.getMessage());
68          } catch (SAXException e) {
69              System.out.println(e.getMessage());
70          }
71          return ediXMLContent;
72      }
73  
74      @Override
75      public void postProcess() {
76          OleOrderRecords oleOrderRecords = null;
77          try {
78              oleOrderRecords = new OleOrderRecords();
79              List<EngineResults> engineResults = getEngineResults();
80              List<OleOrderRecord> oleOrderRecordList = new ArrayList();
81              for (Iterator<EngineResults> iterator = engineResults.iterator(); iterator.hasNext(); ) {
82                  EngineResults results = iterator.next();
83                  OleOrderRecord oleOrderRecord = (OleOrderRecord) results.getAttribute(OLEConstants.OLE_ORDER_RECORD);
84                  oleOrderRecordList.add(oleOrderRecord);
85              }
86              oleOrderRecords.setRecords(oleOrderRecordList);
87              OleOrderRecordHandler oleEditorResponseHandler = new OleOrderRecordHandler();
88              String oleOrderRecordXml = oleEditorResponseHandler.toXML(oleOrderRecords);
89              /*OleWebServiceProvider oleWebServiceProvider = GlobalResourceLoader.getService(OLEConstants.OLE_WEB_SERVICE_PROVIDER);
90              OleExposedWebService oleExposedWebService =
91                      (OleExposedWebService) oleWebServiceProvider.
92                              getService("org.kuali.ole.describe.service.OleExposedWebService", "oleExposedWebService", getURL());
93              oleExposedWebService.createReqAndPO(oleOrderRecordXml);*/
94          } catch (Exception e) {
95              System.out.println(e.getMessage());
96              //String xmlForRollback = buildRequestForRollback(oleOrderRecords);
97              /*List<String> bibIds = new ArrayList<>();
98              for (Iterator<OleOrderRecord> iterator = oleOrderRecords.getRecords().iterator(); iterator.hasNext(); ) {
99                  OleOrderRecord oleOrderRecord = iterator.next();
100                 OleBibRecord oleBibRecord = oleOrderRecord.getOleBibRecord();
101                 bibIds.add(oleBibRecord.getBibUUID());
102 
103             }*/
104             //MockDocstoreHelperService docstoreHelperService = GlobalResourceLoader.getService(OLEConstants.DOCSTORE_HELPER_SERVICE);
105             try {
106                /* if (bibIds.size() > 0) {
107                     getDocstoreClientLocator().getDocstoreClient().deleteBibs(bibIds);
108                 }*/
109                 //docstoreHelperService.rollbackData(xmlForRollback);
110             } catch (Exception e1) {
111                 System.out.println(e1.getMessage());
112             }
113         }
114     }
115 
116     /**
117      * This method builds the request to Rollback the LinkedDocs for failure transactions.
118      *
119      * @param oleOrderRecords
120      * @return xml
121      */
122     private String buildRequestForRollback(OleOrderRecords oleOrderRecords) {
123         String xml = null;
124         for (Iterator<OleOrderRecord> iterator = oleOrderRecords.getRecords().iterator(); iterator.hasNext(); ) {
125             OleOrderRecord oleOrderRecord = iterator.next();
126             /*OleBibRecord oleBibRecord = oleOrderRecord.getOleBibRecord();
127             String bibUUID = oleBibRecord.getBibUUID();*/
128 
129             RequestHandler requestHandler = new RequestHandler();
130             Request request = new Request();
131             request.setOperation("deleteWithLinkedDocs");
132             RequestDocument requestDocument = new RequestDocument();
133             //requestDocument.setId(bibUUID);
134             request.setRequestDocuments(Arrays.asList(requestDocument));
135             xml = requestHandler.toXML(request);
136         }
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 }