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.batch.bo.OLEBatchProcessJobDetailsBo;
7   import org.kuali.ole.converter.MarcXMLConverter;
8   import org.kuali.ole.converter.OLEEDIConverter;
9   import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
10  import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
11  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
12  import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
13  import org.kuali.ole.pojo.OleBibRecord;
14  import org.kuali.ole.pojo.OleOrderRecord;
15  import org.kuali.ole.sys.context.SpringContext;
16  import org.kuali.rice.core.api.config.property.ConfigContext;
17  import org.kuali.rice.krms.api.engine.EngineResults;
18  import org.xml.sax.SAXException;
19  
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  /**
27   * Created with IntelliJ IDEA.
28   * User: ?
29   * Date: 12/27/12
30   * Time: 2:44 PM
31   * To change this template use File | Settings | File Templates.
32   */
33  public class MockIngestProcessor extends AbstractIngestProcessor {
34  
35      private DocstoreClientLocator docstoreClientLocator;
36  
37      public DocstoreClientLocator getDocstoreClientLocator() {
38  
39          if (docstoreClientLocator == null) {
40              docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
41  
42          }
43          return docstoreClientLocator;
44      }
45  
46      @Override
47      public String preProcessMarc(String marcFileContent, OLEBatchProcessJobDetailsBo job) {
48          String marcXMLContent = null;
49          MarcXMLConverter marcXMLConverter = new MarcXMLConverter();
50          marcXMLContent = marcXMLConverter.convert(marcFileContent);
51  
52          //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.
53          //TODO: the duplicate entry does not get genereated if its run directly in the ole-docstore-utilty project.
54          String modifiedXMLContent =
55                  marcXMLContent.
56                          replace("collection xmlns=\"http://www.loc.gov/MARC21/slim\" xmlns=\"http://www.loc.gov/MARC21/slim",
57                                  "collection xmlns=\"http://www.loc.gov/MARC21/slim");
58          return modifiedXMLContent;
59      }
60  
61      @Override
62      public String preProcessEDI(String ediFileContent) {
63          String ediXMLContent = null;
64          OLEEDIConverter oleEDIConverter = new OLEEDIConverter();
65          try {
66              ediXMLContent = oleEDIConverter.convertToXML(ediFileContent);
67          } catch (IOException e) {
68              System.out.println(e.getMessage());
69          } catch (SAXException e) {
70              System.out.println(e.getMessage());
71          }
72          return ediXMLContent;
73      }
74  
75      @Override
76      public void postProcess(OLEBatchProcessJobDetailsBo job) {
77          OleOrderRecords oleOrderRecords = null;
78          try {
79              oleOrderRecords = new OleOrderRecords();
80              List<EngineResults> engineResults = getEngineResults();
81              List<OleOrderRecord> oleOrderRecordList = new ArrayList();
82              for (Iterator<EngineResults> iterator = engineResults.iterator(); iterator.hasNext(); ) {
83                  EngineResults results = iterator.next();
84                  OleOrderRecord oleOrderRecord = (OleOrderRecord) results.getAttribute(OLEConstants.OLE_ORDER_RECORD);
85                  oleOrderRecordList.add(oleOrderRecord);
86              }
87              oleOrderRecords.setRecords(oleOrderRecordList);
88              OleOrderRecordHandler oleEditorResponseHandler = new OleOrderRecordHandler();
89              String oleOrderRecordXml = oleEditorResponseHandler.toXML(oleOrderRecords);
90              /*OleWebServiceProvider oleWebServiceProvider = GlobalResourceLoader.getService(OLEConstants.OLE_WEB_SERVICE_PROVIDER);
91              OleExposedWebService oleExposedWebService =
92                      (OleExposedWebService) oleWebServiceProvider.
93                              getService("org.kuali.ole.describe.service.OleExposedWebService", "oleExposedWebService", getURL());
94              oleExposedWebService.createReqAndPO(oleOrderRecordXml);*/
95          } catch (Exception e) {
96              System.out.println(e.getMessage());
97              //String xmlForRollback = buildRequestForRollback(oleOrderRecords);
98              /*List<String> bibIds = new ArrayList<>();
99              for (Iterator<OleOrderRecord> iterator = oleOrderRecords.getRecords().iterator(); iterator.hasNext(); ) {
100                 OleOrderRecord oleOrderRecord = iterator.next();
101                 OleBibRecord oleBibRecord = oleOrderRecord.getOleBibRecord();
102                 bibIds.add(oleBibRecord.getBibUUID());
103 
104             }*/
105             //MockDocstoreHelperService docstoreHelperService = GlobalResourceLoader.getService(OLEConstants.DOCSTORE_HELPER_SERVICE);
106             try {
107                /* if (bibIds.size() > 0) {
108                     getDocstoreClientLocator().getDocstoreClient().deleteBibs(bibIds);
109                 }*/
110                 //docstoreHelperService.rollbackData(xmlForRollback);
111             } catch (Exception e1) {
112                 System.out.println(e1.getMessage());
113             }
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 
139         return xml;
140     }
141 
142     /**
143      * Gets the oleExposedWebService url from PropertyUtil.
144      *
145      * @return url.
146      */
147     public String getURL() {
148         String url = ConfigContext.getCurrentContextConfig().getProperty("oleExposedWebService.url");
149         return url;
150     }
151 
152 }