View Javadoc
1   package org.kuali.ole.docstore.common.document;
2   
3   import org.apache.log4j.Logger;
4   import org.kuali.ole.docstore.common.document.content.enums.DocCategory;
5   import org.kuali.ole.docstore.common.document.content.enums.DocFormat;
6   import org.kuali.ole.docstore.common.document.content.enums.DocType;
7   import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
8   import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
9   import org.kuali.ole.docstore.common.document.factory.JAXBContextFactory;
10  
11  import javax.xml.bind.Marshaller;
12  import javax.xml.bind.Unmarshaller;
13  import javax.xml.bind.annotation.XmlRootElement;
14  import javax.xml.transform.stream.StreamSource;
15  import java.io.ByteArrayInputStream;
16  import java.io.StringWriter;
17  
18  /**
19   * Created with IntelliJ IDEA.
20   * User: sambasivam
21   * Date: 12/13/13
22   * Time: 3:32 PM
23   * To change this template use File | Settings | File Templates.
24   */
25  @XmlRootElement(name = "holdingsDoc")
26  public class PHoldingsOleml extends PHoldings {
27  
28      private static final Logger LOG = Logger.getLogger(PHoldingsOleml.class);
29  
30      public PHoldingsOleml() {
31          holdingsType = "print";
32          category=DocCategory.WORK.getCode();
33          type=DocType.HOLDINGS.getCode();
34          format=DocFormat.OLEML.getCode();
35      }
36  
37      @Override
38      public String serialize(Object object) {
39          String result = null;
40          PHoldingsOleml holdings = (PHoldingsOleml) object;
41          try {
42              StringWriter sw = new StringWriter();
43              Marshaller jaxbMarshaller = JAXBContextFactory.getInstance().getMarshaller(PHoldingsOleml.class);
44              synchronized (jaxbMarshaller) {
45                  jaxbMarshaller.marshal(holdings, sw);
46              }
47              result = sw.toString();
48          } catch (Exception e) {
49              LOG.error("Exception ", e);
50          }
51          return result;
52      }
53  
54      @Override
55      public Object deserialize(String content) {
56          PHoldingsOleml holdings = new PHoldingsOleml();
57          try {
58              Unmarshaller unmarshaller = JAXBContextFactory.getInstance().getUnMarshaller(PHoldingsOleml.class);
59              ByteArrayInputStream input = new ByteArrayInputStream(content.getBytes("UTF-8"));
60              synchronized (unmarshaller) {
61                  holdings = unmarshaller.unmarshal(new StreamSource(input), PHoldingsOleml.class).getValue();
62              }
63          } catch (Exception e) {
64              LOG.error("Exception ", e);
65          }
66          return holdings;
67      }
68  
69  
70      @Override
71      public Object deserializeContent(String content) {
72          HoldingOlemlRecordProcessor recordProcessor = new HoldingOlemlRecordProcessor();
73          OleHoldings oleHoldings = recordProcessor.fromXML(content);
74          return oleHoldings;
75      }
76  
77      @Override
78      public String serializeContent(Object object) {
79          OleHoldings oleHoldings = (OleHoldings) object;
80          HoldingOlemlRecordProcessor recordProcessor = new HoldingOlemlRecordProcessor();
81          String content = recordProcessor.toXML(oleHoldings);
82          return content;
83      }
84  
85  }