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  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
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  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93      public HashMap<String, Item> getItemMap() {
94          if (itemMap == null) {
95              itemMap = new HashMap<>();
96          }
97          return this.itemMap;
98      }
99  }