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.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   * <p>Java class for holdingsTree complex type.
19   * <p/>
20   * <p>The following schema fragment specifies the expected content contained within this class.
21   * <p/>
22   * <pre>
23   * &lt;complexType name="holdingsTree">
24   *   &lt;complexContent>
25   *     &lt;extension base="{}docstoreDocument">
26   *       &lt;sequence>
27   *         &lt;element name="items" type="{}item" maxOccurs="unbounded" minOccurs="0"/>
28   *         &lt;element name="holdings" type="{}holdings" minOccurs="0"/>
29   *       &lt;/sequence>
30   *     &lt;/extension>
31   *   &lt;/complexContent>
32   * &lt;/complexType>
33   * </pre>
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       * Gets the value of the items property.
54       * <p/>
55       * <p/>
56       * This accessor method returns a reference to the live list,
57       * not a snapshot. Therefore any modification you make to the
58       * returned list will be present inside the JAXB object.
59       * This is why there is not a <CODE>set</CODE> method for the items property.
60       * <p/>
61       * <p/>
62       * For example, to add a new item, do as follows:
63       * <pre>
64       *    getItems().add(newItem);
65       * </pre>
66       * <p/>
67       * <p/>
68       * <p/>
69       * Objects of the following type(s) are allowed in the list
70       * {@link org.kuali.ole.docstore.common.document.Item }
71       */
72      public List<Item> getItems() {
73          if (items == null) {
74              items = new ArrayList<Item>();
75          }
76          return this.items;
77      }
78  
79      /**
80       * Gets the value of the holdings property.
81       *
82       * @return possible object is
83       *         {@link org.kuali.ole.docstore.common.document.Holdings }
84       */
85      public Holdings getHoldings() {
86          return holdings;
87      }
88  
89      /**
90       * Sets the value of the holdings property.
91       *
92       * @param value allowed object is
93       *              {@link org.kuali.ole.docstore.common.document.Holdings }
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());
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;  //To change body of implemented methods use File | Settings | File Templates.
133     }
134 
135     @Override
136     public int compareTo(HoldingsTree o) {
137         return this.getHoldings().compareTo(o.getHoldings());
138     }
139 }