001package org.kuali.ole.docstore.common.document.ids;
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.ArrayList;
014import java.util.List;
015
016/**
017 * Created with IntelliJ IDEA.
018 * User: sambasivam
019 * Date: 6/6/14
020 * Time: 12:54 PM
021 * To change this template use File | Settings | File Templates.
022 */
023
024@XmlAccessorType(XmlAccessType.FIELD)
025@XmlType(name = "bib", propOrder = {
026        "id",
027        "holdingsIds"
028})
029@XmlRootElement(name = "bib")
030
031public class BibId {
032    private static final Logger LOG = Logger.getLogger(BibId.class);
033    @XmlElement(name = "id")
034    protected String id;
035
036    @XmlElementWrapper(name = "holdingsIds")
037    @XmlElement(name = "holdingsId")
038    protected List<HoldingsId> holdingsIds;
039
040    public String getId() {
041        return id;
042    }
043
044    public void setId(String id) {
045        this.id = id;
046    }
047
048    public List<HoldingsId> getHoldingsIds() {
049        if(holdingsIds == null) {
050            holdingsIds = new ArrayList<>();
051        }
052        return holdingsIds;
053    }
054
055    public static String serialize(Object object) {
056        String result = null;
057        StringWriter sw = new StringWriter();
058        BibId bibId = (BibId) object;
059        try {
060            JAXBContext jaxbContext = JAXBContext.newInstance(BibId.class);
061            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
062            jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
063            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
064            jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
065            jaxbMarshaller.marshal(bibId, sw);
066            result = sw.toString();
067        } catch (Exception e) {
068            LOG.error("Exception :", e);
069        }
070        return result;
071    }
072
073    public static Object deserialize(String content) {
074
075        JAXBElement<BibId> bibIdJAXBElement = null;
076        try {
077            JAXBContext jaxbContext = JAXBContext.newInstance(BibId.class);
078            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
079            ByteArrayInputStream input = new ByteArrayInputStream(content.getBytes("UTF-8"));
080            bibIdJAXBElement = jaxbUnmarshaller.unmarshal(new StreamSource(input), BibId.class);
081        } catch (Exception e) {
082            LOG.error("Exception :", e);
083        }
084        return bibIdJAXBElement.getValue();
085    }
086
087}