1 package org.kuali.ole.docstore.common.document.ids;
2
3 import org.apache.log4j.Logger;
4
5 import javax.xml.bind.JAXBContext;
6 import javax.xml.bind.JAXBElement;
7 import javax.xml.bind.Marshaller;
8 import javax.xml.bind.Unmarshaller;
9 import javax.xml.bind.annotation.*;
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 @XmlAccessorType(XmlAccessType.FIELD)
25 @XmlType(name = "holdings", propOrder = {
26 "id",
27 "itemIds"
28 })
29 @XmlRootElement(name = "holdings")
30
31 public class HoldingsId {
32
33 private static final Logger LOG = Logger.getLogger(HoldingsId.class);
34 @XmlElement(name = "id")
35 String id;
36 @XmlElementWrapper(name = "itemIds")
37 @XmlElement(name = "itemId")
38 List<String> itemIds;
39
40 public String getId() {
41 return id;
42 }
43
44 public void setId(String id) {
45 this.id = id;
46 }
47
48 public List<String> getItems() {
49 if(itemIds == null) {
50 itemIds = new ArrayList<>();
51 }
52 return itemIds;
53 }
54
55
56 public String serialize(Object object) {
57 String result = null;
58 StringWriter sw = new StringWriter();
59 HoldingsId holdingsId = (HoldingsId) object;
60 try {
61 JAXBContext jaxbContext = JAXBContext.newInstance(HoldingsId.class);
62 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
63 jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
64 jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
65 jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
66 jaxbMarshaller.marshal(holdingsId, sw);
67 result = sw.toString();
68 } catch (Exception e) {
69 LOG.error("Exception :", e);
70 }
71 return result;
72 }
73
74 public Object deserialize(String content) {
75
76 JAXBElement<HoldingsId> holdingsIdJAXBElement = null;
77 try {
78 JAXBContext jaxbContext = JAXBContext.newInstance(HoldingsId.class);
79 Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
80 ByteArrayInputStream input = new ByteArrayInputStream(content.getBytes("UTF-8"));
81 holdingsIdJAXBElement = jaxbUnmarshaller.unmarshal(new StreamSource(input), HoldingsId.class);
82 } catch (Exception e) {
83 LOG.error("Exception :", e);
84 }
85 return holdingsIdJAXBElement.getValue();
86 }
87
88
89
90
91 }