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.content.instance.OleHoldings;
008import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
009import org.kuali.ole.docstore.common.document.factory.JAXBContextFactory;
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:32 PM
023 * To change this template use File | Settings | File Templates.
024 */
025@XmlRootElement(name = "holdingsDoc")
026public class PHoldingsOleml extends PHoldings {
027
028    private static final Logger LOG = Logger.getLogger(PHoldingsOleml.class);
029
030    public PHoldingsOleml() {
031        holdingsType = "print";
032        category=DocCategory.WORK.getCode();
033        type=DocType.HOLDINGS.getCode();
034        format=DocFormat.OLEML.getCode();
035    }
036
037    @Override
038    public String serialize(Object object) {
039        String result = null;
040        PHoldingsOleml holdings = (PHoldingsOleml) object;
041        try {
042            StringWriter sw = new StringWriter();
043            Marshaller jaxbMarshaller = JAXBContextFactory.getInstance().getMarshaller(PHoldingsOleml.class);
044            synchronized (jaxbMarshaller) {
045                jaxbMarshaller.marshal(holdings, sw);
046            }
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        PHoldingsOleml holdings = new PHoldingsOleml();
057        try {
058            Unmarshaller unmarshaller = JAXBContextFactory.getInstance().getUnMarshaller(PHoldingsOleml.class);
059            ByteArrayInputStream input = new ByteArrayInputStream(content.getBytes("UTF-8"));
060            synchronized (unmarshaller) {
061                holdings = unmarshaller.unmarshal(new StreamSource(input), PHoldingsOleml.class).getValue();
062            }
063        } catch (Exception e) {
064            LOG.error("Exception ", e);
065        }
066        return holdings;
067    }
068
069
070    @Override
071    public Object deserializeContent(String content) {
072        HoldingOlemlRecordProcessor recordProcessor = new HoldingOlemlRecordProcessor();
073        OleHoldings oleHoldings = recordProcessor.fromXML(content);
074        return oleHoldings;
075    }
076
077    @Override
078    public String serializeContent(Object object) {
079        OleHoldings oleHoldings = (OleHoldings) object;
080        HoldingOlemlRecordProcessor recordProcessor = new HoldingOlemlRecordProcessor();
081        String content = recordProcessor.toXML(oleHoldings);
082        return content;
083    }
084
085}