1 package org.kuali.ole.handler;
2
3 import com.thoughtworks.xstream.XStream;
4 import com.thoughtworks.xstream.io.xml.DomDriver;
5 import org.apache.commons.collections.CollectionUtils;
6 import org.apache.commons.lang.StringEscapeUtils;
7 import org.apache.commons.lang3.StringUtils;
8 import org.kuali.ole.OleSRUConstants;
9 import org.kuali.ole.bo.diagnostics.OleSRUDiagnostic;
10 import org.kuali.ole.bo.diagnostics.OleSRUDiagnostics;
11 import org.kuali.ole.bo.serachRetrieve.*;
12 import org.kuali.ole.converters.OleSRUCirculationDocumentConverter;
13 import org.kuali.ole.docstore.common.document.content.bib.marc.*;
14 import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.ControlFieldConverter;
15 import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.DataFieldConverter;
16
17 import java.io.IOException;
18 import java.lang.reflect.Method;
19 import java.lang.reflect.Modifier;
20 import java.net.URISyntaxException;
21 import java.util.ArrayList;
22 import java.util.List;
23
24
25
26
27
28
29
30
31 public class OleSRUOpacXMLResponseHandler {
32 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleSRUOpacXMLResponseHandler.class);
33
34
35
36
37
38
39
40
41
42 public OleSRUResponseDocuments fromXML(String fileContent) throws URISyntaxException, IOException {
43
44 XStream xStream = new XStream();
45 xStream.alias("opacRecords", OleSRUResponseDocuments.class);
46 xStream.alias("opacRecord", OleSRUResponseDocument.class);
47 xStream.alias("holding", OleSRUInstanceDocument.class);
48 xStream.alias("circulation", OleSRUCirculationDocument.class);
49 xStream.alias("volume", OleSRUInstanceVolume.class);
50 xStream.addImplicitCollection(OleSRUResponseDocuments.class, "opacRecords");
51 Object object = xStream.fromXML(fileContent);
52 return (OleSRUResponseDocuments) object;
53 }
54
55
56
57
58
59
60
61 public String toXML(OleSRUSearchRetrieveResponse oleSRUSearchRetrieveResponse, String recordSchema) {
62
63 List<OleSRUResponseRecord> oleSRUResponseRecords = new ArrayList<>();
64 if(oleSRUSearchRetrieveResponse.getOleSRUResponseRecords()!=null && CollectionUtils.isEmpty(oleSRUSearchRetrieveResponse.getOleSRUResponseRecords().getOleSRUResponseRecordList())){
65 oleSRUResponseRecords=oleSRUSearchRetrieveResponse.getOleSRUResponseRecords().getOleSRUResponseRecordList();
66 }
67 if (CollectionUtils.isNotEmpty(oleSRUResponseRecords)) {
68 for (OleSRUResponseRecord oleSRUResponseRecord : oleSRUResponseRecords) {
69 OleSRUResponseDocument oleSRUResponseDocument = oleSRUResponseRecord.getOleSRUResponseDocument();
70 OleSRUResponseRecordData oleSRUResponseRecordData = oleSRUResponseDocument.getOleSRUResponseRecordData();
71 List<OleSRUInstanceDocument> oleSRUInstanceDocuments = oleSRUResponseRecordData.getHoldings();
72 if (CollectionUtils.isNotEmpty(oleSRUInstanceDocuments)) {
73 for (OleSRUInstanceDocument oleSRUInstanceDocument : oleSRUInstanceDocuments) {
74 convertToEscape(oleSRUInstanceDocument);
75 if (CollectionUtils.isNotEmpty(oleSRUInstanceDocument.getVolumes())) {
76 for (OleSRUInstanceVolume oleSRUInstanceVolume : oleSRUInstanceDocument.getVolumes()) {
77 convertToEscape(oleSRUInstanceVolume);
78 }
79 }
80 if (CollectionUtils.isNotEmpty(oleSRUInstanceDocument.getCirculations())) {
81 for (OleSRUCirculationDocument oleSRUCirculationDocument : oleSRUInstanceDocument.getCirculations()) {
82 convertToEscape(oleSRUCirculationDocument);
83 }
84 }
85
86 }
87 }
88 }
89 }
90 StringBuffer stringBuffer = new StringBuffer();
91 stringBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
92 XStream xStream = new XStream();
93 xStream.registerConverter(new OleSRUCirculationDocumentConverter());
94
95 xStream.alias("zs:searchRetrieveResponse", OleSRUSearchRetrieveResponse.class);
96 xStream.aliasField("zs:version", OleSRUSearchRetrieveResponse.class, "version");
97 xStream.aliasField("zs:numberOfRecords", OleSRUSearchRetrieveResponse.class, "numberOfRecords");
98 xStream.aliasField("zs:records", OleSRUSearchRetrieveResponse.class, "oleSRUResponseRecords");
99 xStream.aliasField("zs:diagnostics", OleSRUSearchRetrieveResponse.class, "oleSRUDiagnostics");
100 xStream.addImplicitCollection(OleSRUResponseRecords.class, "oleSRUResponseRecordList");
101 xStream.addImplicitCollection(OleSRUDiagnostics.class, "oleSRUDiagnosticList");
102 xStream.alias("diagnostic", OleSRUDiagnostic.class);
103 xStream.alias("zs:record", OleSRUResponseRecord.class);
104 xStream.aliasField("zs:recordSchema", OleSRUResponseRecord.class, "recordSchema");
105 xStream.aliasField("zs:recordPacking", OleSRUResponseRecord.class, "recordPacking");
106 xStream.aliasField("zs:recordPosition", OleSRUResponseRecord.class, "recordPosition");
107 xStream.alias("holding", OleSRUInstanceDocument.class);
108 xStream.alias("circulation", OleSRUCirculationDocument.class);
109 xStream.alias("volume", OleSRUInstanceVolume.class);
110 xStream.aliasField("bibliographicRecord",OleSRUResponseRecordData.class,"bibMarcRecords");
111 xStream.alias("record", BibMarcRecord.class);
112 xStream.alias("controlfield", ControlField.class);
113 xStream.alias("datafield", DataField.class);
114 xStream.alias("subfield", SubField.class);
115 xStream.addImplicitCollection(BibMarcRecord.class, "dataFields", DataField.class);
116 xStream.addImplicitCollection(BibMarcRecord.class, "controlFields", ControlField.class);
117 xStream.addImplicitCollection(BibMarcRecords.class, "records");
118 xStream.registerConverter(new DataFieldConverter());
119 xStream.registerConverter(new ControlFieldConverter());
120 xStream.omitField(OleSRUResponseRecordData.class,"bibliographicRecord");
121
122 if (recordSchema != null && recordSchema.equalsIgnoreCase("OPAC")) {
123 xStream.aliasField("opacRecord", OleSRUResponseDocument.class, "oleSRUResponseRecordData");
124 String xml = xStream.toXML(oleSRUSearchRetrieveResponse);
125 xml = xml.replace("<zs:searchRetrieveResponse>", "<zs:searchRetrieveResponse xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:zs=\"http://www.loc.gov/zing/srw/\">");
126 xml = xml.replace("<diagnostic>", "<diagnostic xmlns=\"http://www.loc.gov/zing/srw/diagnostic/\">");
127 xml = xml.replace("<oleSRUResponseDocument>", "<zs:recordData>");
128 xml = xml.replace("</oleSRUResponseDocument>", "</zs:recordData>");
129 stringBuffer.append(xml);
130 } else {
131 xStream.alias("zs:searchRetrieveResponse", OleSRUSearchRetrieveResponse.class);
132 xStream.aliasField("zs:recordData", OleSRUResponseDocument.class, "oleSRUResponseRecordData");
133 String xml = xStream.toXML(oleSRUSearchRetrieveResponse);
134 xml = xml.replace("<zs:searchRetrieveResponse>", "<zs:searchRetrieveResponse xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:zs=\"http://www.loc.gov/zing/srw/\">");
135 xml = xml.replace("<diagnostic>", "<diagnostic xmlns=\"http://www.loc.gov/zing/srw/diagnostic/\">");
136 xml = xml.replace("<oleSRUResponseDocument>", "");
137 xml = xml.replace("<bibliographicRecord>", "");
138 xml = xml.replace("</oleSRUResponseDocument>", "");
139 xml = xml.replace("</bibliographicRecord>", "");
140 stringBuffer.append(xml);
141 }
142 return stringBuffer.toString();
143 }
144
145 public void convertToEscape(Object obj) {
146 Class clazz=obj.getClass();
147 Method[] methods = clazz.getDeclaredMethods();
148 for (Method method : methods) {
149 boolean isSetter= Modifier.isPublic(method.getModifiers()) &&
150 method.getReturnType()!=null && StringUtils.isNotEmpty(method.getReturnType().getName()) && method.getReturnType().getName().equalsIgnoreCase("void") &&
151 method.getName().matches("^set[A-Z].*");
152 Class<?>[] parameterTypes=method.getParameterTypes();
153
154 if (isSetter) {
155 if (parameterTypes.length == 1 && parameterTypes[0].getName().equalsIgnoreCase("java.lang.String")) {
156 try {
157 String value = (String) method.getDefaultValue();
158 if(StringUtils.isNotBlank(value)){
159 value = StringEscapeUtils.unescapeXml(value);
160 method.invoke(obj, value);
161 }
162
163 } catch (Exception e) {
164 LOG.error("Error :while converting data to unescapeXml "+e);
165 }
166 }
167 }
168
169 }
170 }
171 }