View Javadoc
1   package org.kuali.ole.handler;
2   
3   import com.thoughtworks.xstream.XStream;
4   import org.kuali.ole.OleSRUConstants;
5   import org.kuali.ole.bo.diagnostics.OleSRUDiagnostic;
6   import org.kuali.ole.bo.diagnostics.OleSRUDiagnostics;
7   import org.kuali.ole.bo.serachRetrieve.*;
8   import org.kuali.ole.converters.OleSRUCirculationDocumentConverter;
9   
10  import java.io.IOException;
11  import java.net.URISyntaxException;
12  
13  /**
14   * Created with IntelliJ IDEA.
15   * User: Srinivasan
16   * Date: 7/16/12
17   * Time: 7:06 PM
18   * To change this template use File | Settings | File Templates.
19   */
20  public class OleSRUOpacXMLResponseHandler {
21  
22      /**
23       * this method converts xml to  OleSRUResponseDocuments object
24       *
25       * @param fileContent
26       * @return OleSRUResponseDocuments object
27       * @throws URISyntaxException
28       * @throws IOException
29       */
30      public OleSRUResponseDocuments fromXML(String fileContent) throws URISyntaxException, IOException {
31  
32          XStream xStream = new XStream();
33          xStream.alias("opacRecords", OleSRUResponseDocuments.class);
34          xStream.alias("opacRecord", OleSRUResponseDocument.class);
35          xStream.alias("holding", OleSRUInstanceDocument.class);
36          xStream.alias("circulation", OleSRUCirculationDocument.class);
37          xStream.alias("volume", OleSRUInstanceVolume.class);
38          xStream.addImplicitCollection(OleSRUResponseDocuments.class, "opacRecords");
39          Object object = xStream.fromXML(fileContent);
40          return (OleSRUResponseDocuments) object;
41      }
42  
43      /**
44       * this method converts OleSRUResponseDocuments object to xml string   </>
45       *
46       * @param oleSRUSearchRetrieveResponse object
47       * @return xml as a String
48       */
49      public String toXML(OleSRUSearchRetrieveResponse oleSRUSearchRetrieveResponse,String recordSchema) {
50          StringBuffer stringBuffer = new StringBuffer();
51          stringBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
52          XStream xStream = new XStream();
53          xStream.registerConverter(new OleSRUCirculationDocumentConverter());
54          if(recordSchema!=null && recordSchema.equalsIgnoreCase("OPAC")){
55          xStream.alias("zs:searchRetrieveResponse", OleSRUSearchRetrieveResponse.class);
56          xStream.aliasField("zs:version", OleSRUSearchRetrieveResponse.class, "version");
57          xStream.aliasField("zs:numberOfRecords", OleSRUSearchRetrieveResponse.class, "numberOfRecords");
58          xStream.aliasField("zs:records", OleSRUSearchRetrieveResponse.class, "oleSRUResponseRecords");
59          xStream.aliasField("zs:diagnostics", OleSRUSearchRetrieveResponse.class, "oleSRUDiagnostics");
60          xStream.addImplicitCollection(OleSRUResponseRecords.class, "oleSRUResponseRecordList");
61          xStream.addImplicitCollection(OleSRUDiagnostics.class, "oleSRUDiagnosticList");
62          xStream.alias("diagnostic", OleSRUDiagnostic.class);
63          xStream.alias("zs:record", OleSRUResponseRecord.class);
64          xStream.aliasField("zs:recordSchema", OleSRUResponseRecord.class, "recordSchema");
65          xStream.aliasField("zs:recordPacking", OleSRUResponseRecord.class, "recordPacking");
66          xStream.aliasField("opacRecord", OleSRUResponseDocument.class, "oleSRUResponseRecordData");
67          xStream.aliasField("zs:recordPosition", OleSRUResponseRecord.class, "recordPosition");
68          xStream.alias("holding", OleSRUInstanceDocument.class);
69          xStream.alias("circulation", OleSRUCirculationDocument.class);
70          xStream.alias("volume", OleSRUInstanceVolume.class);
71          String xml = xStream.toXML(oleSRUSearchRetrieveResponse);
72          xml = xml.replace("<zs:searchRetrieveResponse>", "<zs:searchRetrieveResponse xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:zs=\"http://www.loc.gov/zing/srw/\">");
73          xml = xml.replace("<diagnostic>","<diagnostic xmlns=\"http://www.loc.gov/zing/srw/diagnostic/\">");
74          xml = xml.replace("<oleSRUResponseDocument>", "<zs:recordData>");
75          xml = xml.replace("</oleSRUResponseDocument>", "</zs:recordData>");
76              stringBuffer.append(xml);
77          }else {
78              xStream.alias("zs:searchRetrieveResponse", OleSRUSearchRetrieveResponse.class);
79              xStream.aliasField("zs:version", OleSRUSearchRetrieveResponse.class, "version");
80              xStream.aliasField("zs:numberOfRecords", OleSRUSearchRetrieveResponse.class, "numberOfRecords");
81              xStream.aliasField("zs:records", OleSRUSearchRetrieveResponse.class, "oleSRUResponseRecords");
82              xStream.aliasField("zs:diagnostics", OleSRUSearchRetrieveResponse.class, "oleSRUDiagnostics");
83              xStream.addImplicitCollection(OleSRUResponseRecords.class, "oleSRUResponseRecordList");
84              xStream.addImplicitCollection(OleSRUDiagnostics.class, "oleSRUDiagnosticList");
85              xStream.alias("diagnostic", OleSRUDiagnostic.class);
86              xStream.alias("zs:record", OleSRUResponseRecord.class);
87              xStream.aliasField("zs:recordSchema", OleSRUResponseRecord.class, "recordSchema");
88              xStream.aliasField("zs:recordPacking", OleSRUResponseRecord.class, "recordPacking");
89              xStream.aliasField("zs:recordData", OleSRUResponseDocument.class, "oleSRUResponseRecordData");
90              xStream.aliasField("zs:recordPosition", OleSRUResponseRecord.class, "recordPosition");
91              xStream.alias("holding", OleSRUInstanceDocument.class);
92              xStream.alias("circulation", OleSRUCirculationDocument.class);
93              xStream.alias("volume", OleSRUInstanceVolume.class);
94              String xml = xStream.toXML(oleSRUSearchRetrieveResponse);
95              xml = xml.replace("<zs:searchRetrieveResponse>", "<zs:searchRetrieveResponse xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:zs=\"http://www.loc.gov/zing/srw/\">");
96              xml = xml.replace("<diagnostic>","<diagnostic xmlns=\"http://www.loc.gov/zing/srw/diagnostic/\">");
97              xml = xml.replace("<oleSRUResponseDocument>", "");
98              xml = xml.replace("<bibliographicRecord>", "");
99              xml = xml.replace("</oleSRUResponseDocument>", "");
100             xml = xml.replace("</bibliographicRecord>", "");
101             stringBuffer.append(xml);
102         }
103 
104         return stringBuffer.toString();
105     }
106 }