001package org.kuali.ole.handler; 002 003import com.thoughtworks.xstream.XStream; 004import com.thoughtworks.xstream.io.xml.DomDriver; 005import org.apache.commons.collections.CollectionUtils; 006import org.apache.commons.lang.StringEscapeUtils; 007import org.apache.commons.lang3.StringUtils; 008import org.kuali.ole.OleSRUConstants; 009import org.kuali.ole.bo.diagnostics.OleSRUDiagnostic; 010import org.kuali.ole.bo.diagnostics.OleSRUDiagnostics; 011import org.kuali.ole.bo.serachRetrieve.*; 012import org.kuali.ole.converters.OleSRUCirculationDocumentConverter; 013import org.kuali.ole.docstore.common.document.content.bib.marc.*; 014import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.ControlFieldConverter; 015import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.DataFieldConverter; 016 017import java.io.IOException; 018import java.lang.reflect.Method; 019import java.lang.reflect.Modifier; 020import java.net.URISyntaxException; 021import java.util.ArrayList; 022import java.util.List; 023 024/** 025 * Created with IntelliJ IDEA. 026 * User: Srinivasan 027 * Date: 7/16/12 028 * Time: 7:06 PM 029 * To change this template use File | Settings | File Templates. 030 */ 031public class OleSRUOpacXMLResponseHandler { 032 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleSRUOpacXMLResponseHandler.class); 033 034 /** 035 * this method converts xml to OleSRUResponseDocuments object 036 * 037 * @param fileContent 038 * @return OleSRUResponseDocuments object 039 * @throws URISyntaxException 040 * @throws IOException 041 */ 042 public OleSRUResponseDocuments fromXML(String fileContent) throws URISyntaxException, IOException { 043 044 XStream xStream = new XStream(); 045 xStream.alias("opacRecords", OleSRUResponseDocuments.class); 046 xStream.alias("opacRecord", OleSRUResponseDocument.class); 047 xStream.alias("holding", OleSRUInstanceDocument.class); 048 xStream.alias("circulation", OleSRUCirculationDocument.class); 049 xStream.alias("volume", OleSRUInstanceVolume.class); 050 xStream.addImplicitCollection(OleSRUResponseDocuments.class, "opacRecords"); 051 Object object = xStream.fromXML(fileContent); 052 return (OleSRUResponseDocuments) object; 053 } 054 055 /** 056 * this method converts OleSRUResponseDocuments object to xml string </> 057 * 058 * @param oleSRUSearchRetrieveResponse object 059 * @return xml as a String 060 */ 061 public String toXML(OleSRUSearchRetrieveResponse oleSRUSearchRetrieveResponse, String recordSchema) { 062 063 List<OleSRUResponseRecord> oleSRUResponseRecords = new ArrayList<>(); 064 if(oleSRUSearchRetrieveResponse.getOleSRUResponseRecords()!=null && CollectionUtils.isEmpty(oleSRUSearchRetrieveResponse.getOleSRUResponseRecords().getOleSRUResponseRecordList())){ 065 oleSRUResponseRecords=oleSRUSearchRetrieveResponse.getOleSRUResponseRecords().getOleSRUResponseRecordList(); 066 } 067 if (CollectionUtils.isNotEmpty(oleSRUResponseRecords)) { 068 for (OleSRUResponseRecord oleSRUResponseRecord : oleSRUResponseRecords) { 069 OleSRUResponseDocument oleSRUResponseDocument = oleSRUResponseRecord.getOleSRUResponseDocument(); 070 OleSRUResponseRecordData oleSRUResponseRecordData = oleSRUResponseDocument.getOleSRUResponseRecordData(); 071 List<OleSRUInstanceDocument> oleSRUInstanceDocuments = oleSRUResponseRecordData.getHoldings(); 072 if (CollectionUtils.isNotEmpty(oleSRUInstanceDocuments)) { 073 for (OleSRUInstanceDocument oleSRUInstanceDocument : oleSRUInstanceDocuments) { 074 convertToEscape(oleSRUInstanceDocument); 075 if (CollectionUtils.isNotEmpty(oleSRUInstanceDocument.getVolumes())) { 076 for (OleSRUInstanceVolume oleSRUInstanceVolume : oleSRUInstanceDocument.getVolumes()) { 077 convertToEscape(oleSRUInstanceVolume); 078 } 079 } 080 if (CollectionUtils.isNotEmpty(oleSRUInstanceDocument.getCirculations())) { 081 for (OleSRUCirculationDocument oleSRUCirculationDocument : oleSRUInstanceDocument.getCirculations()) { 082 convertToEscape(oleSRUCirculationDocument); 083 } 084 } 085 086 } 087 } 088 } 089 } 090 StringBuffer stringBuffer = new StringBuffer(); 091 stringBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); 092 XStream xStream = new XStream(); 093 xStream.registerConverter(new OleSRUCirculationDocumentConverter()); 094 //xStream.registerConverter(new OleSRUResponseRecordDataConverter()); 095 xStream.alias("zs:searchRetrieveResponse", OleSRUSearchRetrieveResponse.class); 096 xStream.aliasField("zs:version", OleSRUSearchRetrieveResponse.class, "version"); 097 xStream.aliasField("zs:numberOfRecords", OleSRUSearchRetrieveResponse.class, "numberOfRecords"); 098 xStream.aliasField("zs:records", OleSRUSearchRetrieveResponse.class, "oleSRUResponseRecords"); 099 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}