1   package org.kuali.ole.docstore.common.document;
2   
3   import org.apache.log4j.Logger;
4   import org.kuali.ole.docstore.common.document.factory.JAXBContextFactory;
5   
6   import javax.xml.bind.Marshaller;
7   import javax.xml.bind.Unmarshaller;
8   import javax.xml.bind.annotation.*;
9   import javax.xml.stream.XMLStreamReader;
10  import javax.xml.transform.stream.StreamSource;
11  import java.io.ByteArrayInputStream;
12  import java.io.StringWriter;
13  import java.util.ArrayList;
14  import java.util.List;
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  @XmlAccessorType(XmlAccessType.FIELD)
36  @XmlType(name = "holdingsTree", propOrder = {
37          "items",
38          "holdings"
39  })
40  
41  @XmlRootElement(name = "holdingsDocTree")
42  public class HoldingsTree
43          implements Comparable<HoldingsTree> {
44  
45      private static final Logger LOG = Logger.getLogger(HoldingsTree.class);
46      @XmlElementWrapper(name = "itemsDocs")
47      @XmlElement(name = "itemsDoc")
48      protected List<Item> items;
49      @XmlElement(name = "holdingsDoc")
50      protected Holdings holdings;
51  
52      
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72      public List<Item> getItems() {
73          if (items == null) {
74              items = new ArrayList<Item>();
75          }
76          return this.items;
77      }
78  
79      
80  
81  
82  
83  
84  
85      public Holdings getHoldings() {
86          return holdings;
87      }
88  
89      
90  
91  
92  
93  
94  
95      public void setHoldings(Holdings value) {
96          this.holdings = value;
97      }
98  
99      public String serialize(Object object) {
100         String result = null;
101         HoldingsTree holdingsTree = (HoldingsTree) object;
102         try {
103             StringWriter sw = new StringWriter();
104             Marshaller jaxbMarshaller = JAXBContextFactory.getInstance().getMarshaller(HoldingsTree.class);
105             synchronized (jaxbMarshaller) {
106                 jaxbMarshaller.marshal(holdingsTree, sw);
107             }
108             result = sw.toString();
109         } catch (Exception e) {
110             LOG.error("Exception ", e);
111         }
112         return result;
113     }
114 
115     public Object deserialize(String holdingsTreeXml) {
116         HoldingsTree holdingsTree = new HoldingsTree();
117         try {
118             ByteArrayInputStream bibTreeInputStream = new ByteArrayInputStream(holdingsTreeXml.getBytes("UTF-8"));
119             StreamSource streamSource = new StreamSource(bibTreeInputStream);
120             XMLStreamReader xmlStreamReader = JAXBContextFactory.getInstance().getXmlInputFactory().createXMLStreamReader(streamSource);
121             Unmarshaller unmarshaller = JAXBContextFactory.getInstance().getUnMarshaller(HoldingsTree.class);
122             synchronized (unmarshaller) {
123                 holdingsTree = unmarshaller.unmarshal(xmlStreamReader, HoldingsTree.class).getValue();
124             }
125         } catch (Exception e) {
126             LOG.error("Exception ", e);
127         }
128         return holdingsTree;
129     }
130 
131     public Object deserializeContent(Object object) {
132         return null;  
133     }
134 
135     @Override
136     public int compareTo(HoldingsTree o) {
137         return this.getHoldings().compareTo(o.getHoldings());
138     }
139 }