View Javadoc
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   * <p>Java class for holdingsTree complex type.
27   * <p/>
28   * <p>The following schema fragment specifies the expected content contained within this class.
29   * <p/>
30   * <pre>
31   * &lt;complexType name="holdingsTree">
32   *   &lt;complexContent>
33   *     &lt;extension base="{}docstoreDocument">
34   *       &lt;sequence>
35   *         &lt;element name="items" type="{}item" maxOccurs="unbounded" minOccurs="0"/>
36   *         &lt;element name="holdings" type="{}holdings" minOccurs="0"/>
37   *       &lt;/sequence>
38   *     &lt;/extension>
39   *   &lt;/complexContent>
40   * &lt;/complexType>
41   * </pre>
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       * Gets the value of the items property.
62       * <p/>
63       * <p/>
64       * This accessor method returns a reference to the live list,
65       * not a snapshot. Therefore any modification you make to the
66       * returned list will be present inside the JAXB object.
67       * This is why there is not a <CODE>set</CODE> method for the items property.
68       * <p/>
69       * <p/>
70       * For example, to add a new item, do as follows:
71       * <pre>
72       *    getItems().add(newItem);
73       * </pre>
74       * <p/>
75       * <p/>
76       * <p/>
77       * Objects of the following type(s) are allowed in the list
78       * {@link org.kuali.ole.docstore.common.document.Item }
79       */
80      public List<Item> getItems() {
81          if (items == null) {
82              items = new ArrayList<Item>();
83          }
84          return this.items;
85      }
86  
87      /**
88       * Gets the value of the holdings property.
89       *
90       * @return possible object is
91       *         {@link org.kuali.ole.docstore.common.document.Holdings }
92       */
93      public Holdings getHoldings() {
94          return holdings;
95      }
96  
97      /**
98       * Sets the value of the holdings property.
99       *
100      * @param value allowed object is
101      *              {@link org.kuali.ole.docstore.common.document.Holdings }
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;  //To change body of implemented methods use File | Settings | File Templates.
175     }
176 
177     @Override
178     public Object deserializeContent(String content) {
179         return null;  //To change body of implemented methods use File | Settings | File Templates.
180     }
181 
182     @Override
183     public String serializeContent(Object object) {
184         return null;  //To change body of implemented methods use File | Settings | File Templates.
185     }
186 
187     @Override
188     public int compareTo(HoldingsTree o) {
189         return this.getHoldings().compareTo(o.getHoldings());
190     }
191 }