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.model.xmlpojo.ingest.Request;
9 import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
10 import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
11 import org.kuali.ole.describe.service.DocstoreHelperService;
12 import org.kuali.ole.pojo.OleBibRecord;
13 import org.kuali.ole.pojo.OleOrderRecord;
14 import org.kuali.ole.select.service.impl.OleExposedWebServiceImpl;
15 import org.kuali.ole.sys.context.SpringContext;
16 import org.kuali.rice.core.api.config.property.ConfigContext;
17 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
18 import org.kuali.rice.krms.api.engine.EngineResults;
19 import org.xml.sax.SAXException;
20
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Iterator;
25 import java.util.List;
26
27
28
29
30
31 public class IngestProcessor extends AbstractIngestProcessor {
32
33
34
35
36
37 @Override
38 public String preProcessMarc(String marcFileContent) {
39 String marcXMLContent = null;
40 MarcXMLConverter marcXMLConverter = new MarcXMLConverter();
41 marcXMLContent = marcXMLConverter.convert(marcFileContent);
42
43
44
45 String modifiedXMLContent =
46 marcXMLContent.
47 replace("collection xmlns=\"http://www.loc.gov/MARC21/slim\" xmlns=\"http://www.loc.gov/MARC21/slim",
48 "collection xmlns=\"http://www.loc.gov/MARC21/slim");
49 return modifiedXMLContent;
50 }
51
52
53
54
55
56
57 @Override
58 public String preProcessEDI(String ediFileContent) {
59 String ediXMLContent = null;
60 OLEEDIConverter oleEDIConverter = new OLEEDIConverter();
61 try {
62 ediXMLContent = oleEDIConverter.convertToXML(ediFileContent);
63 } catch (IOException e) {
64 System.out.println(e.getMessage());
65 } catch (SAXException e) {
66 System.out.println(e.getMessage());
67 }
68 return ediXMLContent;
69 }
70
71
72
73
74
75 @Override
76 public void postProcess() {
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 OleExposedWebServiceImpl oleExposedWebService = (OleExposedWebServiceImpl)SpringContext.getBean("oleExposedWebService");
91 oleExposedWebService.createReqAndPO(oleOrderRecordXml);
92 } catch (Exception e) {
93 System.out.println(e.getMessage());
94 String xmlForRollback = buildRequestForRollback(oleOrderRecords);
95 DocstoreHelperService docstoreHelperService = GlobalResourceLoader.getService(OLEConstants.DOCSTORE_HELPER_SERVICE);
96 try {
97 docstoreHelperService.rollbackData(xmlForRollback);
98 } catch (Exception e1) {
99 System.out.println(e1.getMessage());
100 }
101 }
102 }
103
104
105
106
107
108
109 private String buildRequestForRollback(OleOrderRecords oleOrderRecords) {
110 String xml = null;
111 for (Iterator<OleOrderRecord> iterator = oleOrderRecords.getRecords().iterator(); iterator.hasNext(); ) {
112 OleOrderRecord oleOrderRecord = iterator.next();
113 OleBibRecord oleBibRecord = oleOrderRecord.getOleBibRecord();
114 String bibUUID = oleBibRecord.getBibUUID();
115
116 RequestHandler requestHandler = new RequestHandler();
117 Request request = new Request();
118 request.setOperation("deleteWithLinkedDocs");
119 RequestDocument requestDocument = new RequestDocument();
120 requestDocument.setId(bibUUID);
121 request.setRequestDocuments(Arrays.asList(requestDocument));
122 xml = requestHandler.toXML(request);
123 }
124
125 return xml;
126 }
127
128
129
130
131
132 public String getURL() {
133 String url = ConfigContext.getCurrentContextConfig().getProperty("oleExposedWebService.url");
134 return url;
135 }
136 }