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.Marshaller;
14 import javax.xml.bind.Unmarshaller;
15 import javax.xml.bind.annotation.XmlRootElement;
16 import javax.xml.transform.stream.StreamSource;
17 import java.io.ByteArrayInputStream;
18 import java.io.StringWriter;
19
20
21
22
23
24
25
26
27
28 @XmlRootElement(name = "bibDoc")
29 public class BibMarc extends Bib {
30
31 private static final Logger LOG = Logger.getLogger(BibMarc.class);
32
33 public BibMarc() {
34 category=DocCategory.WORK.getCode();
35 type=DocType.BIB.getCode();
36 format=DocFormat.MARC.getCode();
37 }
38
39 @Override
40 public String serialize(Object object) {
41 String result = null;
42 BibMarc bib = (BibMarc) object;
43 try {
44 StringWriter sw = new StringWriter();
45 Marshaller jaxbMarshaller = JAXBContextFactory.getInstance().getMarshaller(BibMarc.class);
46 synchronized (jaxbMarshaller) {
47 jaxbMarshaller.marshal(bib, sw);
48 }
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 synchronized (unmarshaller) {
63 bib = unmarshaller.unmarshal(new StreamSource(input), BibMarc.class).getValue();
64 }
65 } catch (Exception e) {
66 LOG.error("Exception :", e);
67 throw new DocstoreDeserializeException(DocstoreResources.BIB_CREATION_FAILED, DocstoreResources.BIB_CREATION_FAILED);
68 }
69 return bib;
70 }
71
72
73 @Override
74 public Object deserializeContent(Object object) {
75 Bib bib = (Bib) object;
76 BibMarcMapping bibMarcMapping = new BibMarcMapping();
77 return bibMarcMapping.getDocument(bib);
78 }
79
80 @Override
81 public Object deserializeContent(String content) {
82 BibMarcRecordProcessor recordProcessor = new BibMarcRecordProcessor();
83 BibMarcRecords bibMarcRecords = recordProcessor.fromXML(content);
84 return bibMarcRecords;
85 }
86
87 @Override
88 public String serializeContent(Object object) {
89 BibMarcRecordProcessor recordProcessor = new BibMarcRecordProcessor();
90 BibMarcRecords bibMarc = (BibMarcRecords) object;
91 String content = recordProcessor.toXml(bibMarc);
92 return content;
93 }
94
95 }