1 package org.kuali.ole.docstore.common.document;
2
3 import org.apache.log4j.Logger;
4 import org.kuali.ole.docstore.common.util.ParseXml;
5 import org.w3c.dom.Document;
6 import org.w3c.dom.Node;
7 import org.w3c.dom.NodeList;
8 import org.xml.sax.InputSource;
9 import org.xml.sax.SAXException;
10
11 import javax.xml.bind.*;
12 import javax.xml.bind.annotation.*;
13 import javax.xml.parsers.DocumentBuilder;
14 import javax.xml.parsers.DocumentBuilderFactory;
15 import javax.xml.parsers.ParserConfigurationException;
16 import javax.xml.transform.stream.StreamSource;
17 import java.io.ByteArrayInputStream;
18 import java.io.IOException;
19 import java.io.StringReader;
20 import java.io.StringWriter;
21 import java.util.ArrayList;
22 import java.util.List;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 @XmlAccessorType(XmlAccessType.FIELD)
44 @XmlType(name = "holdingsTree", propOrder = {
45 "items",
46 "holdings"
47 })
48
49 @XmlRootElement(name = "holdingsDocTree")
50 public class HoldingsTree
51 extends DocstoreDocument implements Comparable<HoldingsTree>{
52
53 private static final Logger LOG = Logger.getLogger(HoldingsTree.class);
54 @XmlElementWrapper(name = "itemsDocs")
55 @XmlElement(name = "itemsDoc")
56 protected List<Item> items;
57 @XmlElement(name = "holdingsDoc")
58 protected Holdings holdings;
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 public List<Item> getItems() {
81 if (items == null) {
82 items = new ArrayList<Item>();
83 }
84 return this.items;
85 }
86
87
88
89
90
91
92
93 public Holdings getHoldings() {
94 return holdings;
95 }
96
97
98
99
100
101
102
103 public void setHoldings(Holdings value) {
104 this.holdings = value;
105 }
106
107 @Override
108 public String serialize(Object object) {
109 String result = null;
110 StringWriter sw = new StringWriter();
111 HoldingsTree holdingsTree = (HoldingsTree) object;
112 try {
113 JAXBContext jaxbContext = JAXBContext.newInstance(HoldingsTree.class);
114 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
115 jaxbMarshaller.marshal(holdingsTree, sw);
116 result = sw.toString();
117 } catch (Exception e) {
118 LOG.error("Exception ", e);
119 }
120 return result;
121 }
122
123 @Override
124 public Object deserialize(String holdingsTreeXml) {
125 JAXBElement<HoldingsTree> holdingsTreeElement = null;
126 try {
127 JAXBContext jaxbContext = JAXBContext.newInstance(HoldingsTree.class);
128 Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
129 ByteArrayInputStream input = new ByteArrayInputStream(holdingsTreeXml.getBytes("UTF-8"));
130 holdingsTreeElement = jaxbUnmarshaller.unmarshal(new StreamSource(input), HoldingsTree.class);
131 } catch (Exception e) {
132 LOG.error("Exception ", e);
133 }
134 HoldingsTree holdingsTrees = holdingsTreeElement.getValue();
135
136 try {
137 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
138 DocumentBuilder builder = factory.newDocumentBuilder();
139 Document doc = builder.parse(new InputSource(new StringReader(holdingsTreeXml)));
140 NodeList root = doc.getChildNodes();
141 Node holdingsTree = ParseXml.getNode("holdingsDocTree", root);
142 Node holdings = ParseXml.getNode("holdingsDoc", holdingsTree.getChildNodes());
143 NodeList nodes = holdings.getChildNodes();
144 String holdingsXml = ParseXml.nodeToString(holdings);
145 if (ParseXml.getNodeValue("holdingsType", nodes).equals("print")) {
146 JAXBContext jc = JAXBContext.newInstance(PHoldings.class);
147 Unmarshaller unmarshaller1 = jc.createUnmarshaller();
148 StreamSource xmlSource = new StreamSource(new StringReader(holdingsXml));
149 JAXBElement<PHoldings> je1 = unmarshaller1.unmarshal(xmlSource, PHoldings.class);
150 PHoldings pHoldings = je1.getValue();
151 holdingsTrees.setHoldings(pHoldings);
152 } else {
153 JAXBContext jc = JAXBContext.newInstance(EHoldings.class);
154 Unmarshaller unmarshaller = jc.createUnmarshaller();
155 StreamSource xmlSource1 = new StreamSource(new StringReader(holdingsXml));
156 JAXBElement<EHoldings> je = unmarshaller.unmarshal(xmlSource1, EHoldings.class);
157 EHoldings eHoldings = je.getValue();
158 holdingsTrees.setHoldings(eHoldings);
159 }
160 } catch (SAXException e) {
161 LOG.error("Exception ", e);
162 } catch (IOException e) {
163 LOG.error("Exception ", e);
164 } catch (ParserConfigurationException e) {
165 LOG.error("Exception ", e);
166 } catch (JAXBException e) {
167 LOG.error("Exception ", e);
168 }
169
170 return holdingsTrees;
171 }
172
173 public Object deserializeContent(Object object) {
174 return null;
175 }
176
177 @Override
178 public Object deserializeContent(String content) {
179 return null;
180 }
181
182 @Override
183 public String serializeContent(Object object) {
184 return null;
185 }
186
187 @Override
188 public int compareTo(HoldingsTree o) {
189 return this.getHoldings().compareTo(o.getHoldings());
190 }
191 }