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;
007import org.kuali.ole.docstore.common.document.factory.JAXBContextFactory;
008import org.kuali.ole.docstore.common.exception.DocstoreDeserializeException;
009import org.kuali.ole.docstore.common.exception.DocstoreResources;
010
011import javax.xml.bind.Marshaller;
012import javax.xml.bind.Unmarshaller;
013import javax.xml.bind.annotation.XmlRootElement;
014import javax.xml.transform.stream.StreamSource;
015import java.io.ByteArrayInputStream;
016import java.io.StringWriter;
017
018/**
019 * Created with IntelliJ IDEA.
020 * User: sambasivam
021 * Date: 12/13/13
022 * Time: 3:31 PM
023 * To change this template use File | Settings | File Templates.
024 */
025@XmlRootElement(name = "bibDoc")
026public class BibDc extends Bib {
027
028    private static final Logger LOG = Logger.getLogger(BibDc.class);
029    public BibDc() {
030        category=DocCategory.WORK.getCode();
031        type=DocType.BIB.getCode();
032        format=DocFormat.DUBLIN_CORE.getCode();
033    }
034
035    @Override
036    public String serialize(Object object) {
037        String result = null;
038        BibDc bib = (BibDc) object;
039        try {
040            StringWriter sw = new StringWriter();
041            Marshaller jaxbMarshaller = JAXBContextFactory.getInstance().getMarshaller(BibDc.class);
042            synchronized (jaxbMarshaller) {
043            jaxbMarshaller.marshal(bib, sw);
044            }
045            result = sw.toString();
046        } catch (Exception e) {
047            LOG.error("Exception :", e);
048        }
049        return result;
050    }
051
052    @Override
053    public Object deserialize(String content) {
054        BibDc bib = new BibDc();
055        try {
056            Unmarshaller unmarshaller = JAXBContextFactory.getInstance().getUnMarshaller(BibDc.class);
057            ByteArrayInputStream input = new ByteArrayInputStream(content.getBytes("UTF-8"));
058            synchronized (unmarshaller) {
059                bib = unmarshaller.unmarshal(new StreamSource(input), BibDc.class).getValue();
060            }
061        } catch (Exception e) {
062            LOG.error("Exception :", e);
063            throw new DocstoreDeserializeException(DocstoreResources.BIB_CREATION_FAILED, DocstoreResources.BIB_CREATION_FAILED);
064        }
065        return bib;
066    }
067
068}