1 package org.kuali.ole.docstore.common.document;
2
3 import org.apache.log4j.Logger;
4 import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecords;
5 import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
6 import org.kuali.ole.docstore.common.document.content.enums.DocCategory;
7 import org.kuali.ole.docstore.common.document.content.enums.DocFormat;
8 import org.kuali.ole.docstore.common.document.content.enums.DocType;
9 import org.kuali.ole.docstore.common.document.factory.JAXBContextFactory;
10 import org.kuali.ole.docstore.common.exception.DocstoreDeserializeException;
11 import org.kuali.ole.docstore.common.exception.DocstoreResources;
12
13 import javax.xml.bind.JAXBElement;
14 import javax.xml.bind.Marshaller;
15 import javax.xml.bind.Unmarshaller;
16 import javax.xml.bind.annotation.XmlRootElement;
17 import javax.xml.transform.stream.StreamSource;
18 import java.io.ByteArrayInputStream;
19 import java.io.StringWriter;
20
21
22
23
24
25
26
27
28
29 @XmlRootElement(name = "bibDoc")
30 public class BibMarc extends Bib {
31
32 private static final Logger LOG = Logger.getLogger(BibMarc.class);
33
34 public BibMarc() {
35 category=DocCategory.WORK.getCode();
36 type=DocType.BIB.getCode();
37 format=DocFormat.MARC.getCode();
38 }
39
40
41 @Override
42 public String serialize(Object object) {
43 String result = null;
44 BibMarc bib = (BibMarc) object;
45 try {
46 StringWriter sw = new StringWriter();
47 Marshaller jaxbMarshaller = JAXBContextFactory.getInstance().getMarshaller(BibMarc.class);
48 jaxbMarshaller.marshal(bib, sw);
49 result = sw.toString();
50 } catch (Exception e) {
51 LOG.error("Exception :", e);
52 }
53 return result;
54 }
55
56 @Override
57 public Object deserialize(String content) {
58 BibMarc bib = new BibMarc();
59 try {
60 Unmarshaller unmarshaller = JAXBContextFactory.getInstance().getUnMarshaller(BibMarc.class);
61 ByteArrayInputStream input = new ByteArrayInputStream(content.getBytes("UTF-8"));
62 JAXBElement<BibMarc> bibElement = unmarshaller.unmarshal(new StreamSource(input), BibMarc.class);
63 bib = bibElement.getValue();
64 } catch (Exception e) {
65 LOG.error("Exception :", e);
66 throw new DocstoreDeserializeException(DocstoreResources.BIB_CREATION_FAILED,DocstoreResources.BIB_CREATION_FAILED);
67 }
68 return bib;
69 }
70
71
72 @Override
73 public Object deserializeContent(Object object) {
74 Bib bib = (Bib) object;
75 BibMarcMapping bibMarcMapping = new BibMarcMapping();
76 return bibMarcMapping.getDocument(bib);
77 }
78
79 @Override
80 public Object deserializeContent(String content) {
81 BibMarcRecordProcessor recordProcessor = new BibMarcRecordProcessor();
82 BibMarcRecords bibMarcRecords = recordProcessor.fromXML(content);
83 return bibMarcRecords;
84 }
85
86 @Override
87 public String serializeContent(Object object) {
88 BibMarcRecordProcessor recordProcessor = new BibMarcRecordProcessor();
89 BibMarcRecords bibMarc = (BibMarcRecords) object;
90 String content = recordProcessor.toXml(bibMarc);
91 return content;
92 }
93
94 }