001package org.kuali.ole.docstore.common.document;
002
003import org.apache.log4j.Logger;
004import org.kuali.ole.docstore.common.document.content.enums.DocCategory;
005import org.kuali.ole.docstore.common.document.content.enums.DocFormat;
006import org.kuali.ole.docstore.common.document.content.enums.DocType;
007
008import javax.xml.bind.JAXBContext;
009import javax.xml.bind.JAXBElement;
010import javax.xml.bind.Marshaller;
011import javax.xml.bind.Unmarshaller;
012import javax.xml.bind.annotation.XmlRootElement;
013import javax.xml.transform.stream.StreamSource;
014import java.io.ByteArrayInputStream;
015import java.io.StringWriter;
016
017/**
018 * Created with IntelliJ IDEA.
019 * User: chenchulakshmig
020 * Date: 2/25/14
021 * Time: 2:57 PM
022 * To change this template use File | Settings | File Templates.
023 */
024@XmlRootElement(name = "license")
025public class LicenseOnixpl extends License {
026
027    private static final Logger LOG = Logger.getLogger(LicenseOnixpl.class);
028
029    public LicenseOnixpl() {
030        category = DocCategory.WORK.getCode();
031        type = DocType.LICENSE.getCode();
032        format = DocFormat.ONIXPL.getCode();
033    }
034
035    @Override
036    public String serialize(Object object) {
037        String result = null;
038        StringWriter sw = new StringWriter();
039        LicenseOnixpl licenseOnixpl = (LicenseOnixpl) object;
040        try {
041            JAXBContext jaxbContext = JAXBContext.newInstance(LicenseOnixpl.class);
042            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
043            jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
044            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
045            jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
046            jaxbMarshaller.marshal(licenseOnixpl, sw);
047            result = sw.toString();
048        } catch (Exception e) {
049            LOG.error("Exception :", e);
050        }
051        return result;
052    }
053
054    @Override
055    public Object deserialize(String content) {
056        JAXBElement<LicenseOnixpl> licenseOnixplElement = null;
057        try {
058            JAXBContext jaxbContext = JAXBContext.newInstance(LicenseOnixpl.class);
059            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
060            ByteArrayInputStream input = new ByteArrayInputStream(content.getBytes("UTF-8"));
061            licenseOnixplElement = jaxbUnmarshaller.unmarshal(new StreamSource(input), LicenseOnixpl.class);
062        } catch (Exception e) {
063            LOG.error("Exception :", e);
064        }
065        return licenseOnixplElement.getValue();
066    }
067}