001package org.kuali.ole.docstore.common.document;
002
003import org.apache.log4j.Logger;
004import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecords;
005import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
006import org.kuali.ole.docstore.common.document.content.enums.DocCategory;
007import org.kuali.ole.docstore.common.document.content.enums.DocFormat;
008import org.kuali.ole.docstore.common.document.content.enums.DocType;
009import org.kuali.ole.docstore.common.document.factory.JAXBContextFactory;
010import org.kuali.ole.docstore.common.exception.DocstoreDeserializeException;
011import org.kuali.ole.docstore.common.exception.DocstoreResources;
012
013import javax.xml.bind.Marshaller;
014import javax.xml.bind.Unmarshaller;
015import javax.xml.bind.annotation.XmlRootElement;
016import javax.xml.transform.stream.StreamSource;
017import java.io.ByteArrayInputStream;
018import java.io.StringWriter;
019
020/**
021 * Created with IntelliJ IDEA.
022 * User: sambasivam
023 * Date: 12/13/13
024 * Time: 3:31 PM
025 * To change this template use File | Settings | File Templates.
026 */
027
028@XmlRootElement(name = "bibDoc")
029public class BibMarc extends Bib {
030
031    private static final Logger LOG = Logger.getLogger(BibMarc.class);
032
033    public BibMarc() {
034        category=DocCategory.WORK.getCode();
035        type=DocType.BIB.getCode();
036        format=DocFormat.MARC.getCode();
037    }
038
039    @Override
040    public String serialize(Object object) {
041        String result = null;
042        BibMarc bib = (BibMarc) object;
043        try {
044            StringWriter sw = new StringWriter();
045            Marshaller jaxbMarshaller = JAXBContextFactory.getInstance().getMarshaller(BibMarc.class);
046            synchronized (jaxbMarshaller) {
047                jaxbMarshaller.marshal(bib, sw);
048            }
049            result = sw.toString();
050        } catch (Exception e) {
051            LOG.error("Exception :", e);
052        }
053        return result;
054    }
055
056    @Override
057    public Object deserialize(String content) {
058        BibMarc bib = new BibMarc();
059        try {
060            Unmarshaller unmarshaller = JAXBContextFactory.getInstance().getUnMarshaller(BibMarc.class);
061            ByteArrayInputStream input = new ByteArrayInputStream(content.getBytes("UTF-8"));
062            synchronized (unmarshaller) {
063                bib = unmarshaller.unmarshal(new StreamSource(input), BibMarc.class).getValue();
064            }
065        } catch (Exception e) {
066            LOG.error("Exception :", e);
067            throw new DocstoreDeserializeException(DocstoreResources.BIB_CREATION_FAILED, DocstoreResources.BIB_CREATION_FAILED);
068        }
069        return bib;
070    }
071
072
073    @Override
074    public Object deserializeContent(Object object) {
075        Bib bib = (Bib) object;
076        BibMarcMapping bibMarcMapping = new BibMarcMapping();
077        return bibMarcMapping.getDocument(bib);
078    }
079
080    @Override
081    public Object deserializeContent(String content) {
082        BibMarcRecordProcessor recordProcessor = new BibMarcRecordProcessor();
083        BibMarcRecords bibMarcRecords = recordProcessor.fromXML(content);
084        return bibMarcRecords;
085    }
086
087    @Override
088    public String serializeContent(Object object) {
089        BibMarcRecordProcessor recordProcessor = new BibMarcRecordProcessor();
090        BibMarcRecords bibMarc = (BibMarcRecords) object;
091        String content = recordProcessor.toXml(bibMarc);
092        return content;
093    }
094
095}