View Javadoc
1   package org.kuali.ole.docstore.common.document;
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.HashMap;
14  
15  
16  /**
17   * <p>Java class for items complex type.
18   * <p/>
19   * <p>The following schema fragment specifies the expected content contained within this class.
20   * <p/>
21   * <pre>
22   * &lt;complexType name="items">
23   *   &lt;complexContent>
24   *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
25   *       &lt;sequence>
26   *         &lt;element name="itemMap" type="{}item" maxOccurs="unbounded" minOccurs="0"/>
27   *       &lt;/sequence>
28   *     &lt;/restriction>
29   *   &lt;/complexContent>
30   * &lt;/complexType>
31   * </pre>
32   */
33  @XmlAccessorType(XmlAccessType.FIELD)
34  @XmlType(name = "itemMap", propOrder = {
35          "itemMap"
36  })
37  @XmlRootElement(name="itemsDocs")
38  public class ItemMap {
39  
40      private static final Logger LOG = Logger.getLogger(ItemMap.class);
41      @XmlElement(name = "itemsDoc")
42      protected HashMap<String, Item> itemMap;
43  
44      public static String serialize(Object object) {
45          String result = null;
46          StringWriter sw = new StringWriter();
47          ItemMap items = (ItemMap) object;
48          try {
49              JAXBContext jaxbContext = JAXBContext.newInstance(ItemMap.class);
50              Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
51              jaxbMarshaller.marshal(items, sw);
52              result = sw.toString();
53          } catch (Exception e) {
54              LOG.error("Exception ", e);
55          }
56          return result;
57      }
58  
59      public static Object deserialize(String itemsXml) {
60  
61          JAXBElement<ItemMap> itemsElement = null;
62          try {
63              JAXBContext jaxbContext = JAXBContext.newInstance(ItemMap.class);
64              Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
65              ByteArrayInputStream input = new ByteArrayInputStream(itemsXml.getBytes("UTF-8"));
66              itemsElement = jaxbUnmarshaller.unmarshal(new StreamSource(input), ItemMap.class);
67          } catch (Exception e) {
68              LOG.error("Exception ", e);
69          }
70          return itemsElement.getValue();
71      }
72  
73      /**
74       * Gets the value of the items property.
75       * <p/>
76       * <p/>
77       * This accessor method returns a reference to the live list,
78       * not a snapshot. Therefore any modification you make to the
79       * returned list will be present inside the JAXB object.
80       * This is why there is not a <CODE>set</CODE> method for the items property.
81       * <p/>
82       * <p/>
83       * For example, to add a new item, do as follows:
84       * <pre>
85       *    getItems().add(newItem);
86       * </pre>
87       * <p/>
88       * <p/>
89       * <p/>
90       * Objects of the following type(s) are allowed in the list
91       * {@link Item }
92       */
93      public HashMap<String, Item> getItemMap() {
94          if (itemMap == null) {
95              itemMap = new HashMap<>();
96          }
97          return this.itemMap;
98      }
99  }