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