001package org.kuali.ole.docstore.common.document;
002
003import org.apache.log4j.Logger;
004
005import javax.xml.bind.JAXBContext;
006import javax.xml.bind.JAXBElement;
007import javax.xml.bind.Marshaller;
008import javax.xml.bind.Unmarshaller;
009import javax.xml.bind.annotation.*;
010import javax.xml.transform.stream.StreamSource;
011import java.io.ByteArrayInputStream;
012import java.io.StringWriter;
013import java.util.HashMap;
014
015
016/**
017 * <p>Java class for items complex type.
018 * <p/>
019 * <p>The following schema fragment specifies the expected content contained within this class.
020 * <p/>
021 * <pre>
022 * &lt;complexType name="items">
023 *   &lt;complexContent>
024 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
025 *       &lt;sequence>
026 *         &lt;element name="itemMap" type="{}item" maxOccurs="unbounded" minOccurs="0"/>
027 *       &lt;/sequence>
028 *     &lt;/restriction>
029 *   &lt;/complexContent>
030 * &lt;/complexType>
031 * </pre>
032 */
033@XmlAccessorType(XmlAccessType.FIELD)
034@XmlType(name = "itemMap", propOrder = {
035        "itemMap"
036})
037@XmlRootElement(name="itemsDocs")
038public class ItemMap {
039
040    private static final Logger LOG = Logger.getLogger(ItemMap.class);
041    @XmlElement(name = "itemsDoc")
042    protected HashMap<String, Item> itemMap;
043
044    public static String serialize(Object object) {
045        String result = null;
046        StringWriter sw = new StringWriter();
047        ItemMap items = (ItemMap) object;
048        try {
049            JAXBContext jaxbContext = JAXBContext.newInstance(ItemMap.class);
050            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
051            jaxbMarshaller.marshal(items, sw);
052            result = sw.toString();
053        } catch (Exception e) {
054            LOG.error("Exception ", e);
055        }
056        return result;
057    }
058
059    public static Object deserialize(String itemsXml) {
060
061        JAXBElement<ItemMap> itemsElement = null;
062        try {
063            JAXBContext jaxbContext = JAXBContext.newInstance(ItemMap.class);
064            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
065            ByteArrayInputStream input = new ByteArrayInputStream(itemsXml.getBytes("UTF-8"));
066            itemsElement = jaxbUnmarshaller.unmarshal(new StreamSource(input), ItemMap.class);
067        } catch (Exception e) {
068            LOG.error("Exception ", e);
069        }
070        return itemsElement.getValue();
071    }
072
073    /**
074     * Gets the value of the items property.
075     * <p/>
076     * <p/>
077     * This accessor method returns a reference to the live list,
078     * not a snapshot. Therefore any modification you make to the
079     * returned list will be present inside the JAXB object.
080     * This is why there is not a <CODE>set</CODE> method for the items property.
081     * <p/>
082     * <p/>
083     * For example, to add a new item, do as follows:
084     * <pre>
085     *    getItems().add(newItem);
086     * </pre>
087     * <p/>
088     * <p/>
089     * <p/>
090     * Objects of the following type(s) are allowed in the list
091     * {@link Item }
092     */
093    public HashMap<String, Item> getItemMap() {
094        if (itemMap == null) {
095            itemMap = new HashMap<>();
096        }
097        return this.itemMap;
098    }
099}