View Javadoc
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.dc.unqualified.*;
5   import org.kuali.ole.docstore.common.document.content.bib.dc.unqualified.xstream.BibDublinUnQualifiedRecordProcessor;
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   * Created with IntelliJ IDEA.
22   * User: sambasivam
23   * Date: 1/21/14
24   * Time: 3:39 PM
25   * To change this template use File | Settings | File Templates.
26   */
27  @XmlRootElement(name = "bibDoc")
28  
29  public class BibDcUnqualified extends Bib {
30  
31      private static final Logger LOG = Logger.getLogger(BibDcUnqualified.class);
32      public BibDcUnqualified () {
33          category=DocCategory.WORK.getCode();
34          type=DocType.BIB.getCode();
35          format=DocFormat.DUBLIN_UNQUALIFIED.getCode();
36      }
37  
38      @Override
39      public String serialize(Object object) {
40          String result = null;
41          BibDcUnqualified bib = (BibDcUnqualified) object;
42          try {
43              StringWriter sw = new StringWriter();
44              Marshaller jaxbMarshaller = JAXBContextFactory.getInstance().getMarshaller(BibDcUnqualified.class);
45              synchronized (jaxbMarshaller) {
46                  jaxbMarshaller.marshal(bib, sw);
47              }
48              result = sw.toString();
49          } catch (Exception e) {
50              LOG.error("Exception :", e);
51          }
52          return result;
53      }
54  
55      @Override
56      public Object deserialize(String content) {
57          BibDcUnqualified bib = new BibDcUnqualified();
58          try {
59              Unmarshaller unmarshaller = JAXBContextFactory.getInstance().getUnMarshaller(BibDcUnqualified.class);
60              ByteArrayInputStream input = new ByteArrayInputStream(content.getBytes("UTF-8"));
61              synchronized (unmarshaller) {
62                  bib = unmarshaller.unmarshal(new StreamSource(input), BibDcUnqualified.class).getValue();
63              }
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          BibDcUnqualified bibDcUnqualified= (BibDcUnqualified)object;
75          BibDublinUnQualifiedRecordProcessor bibDublinUnQualifiedRecordProcessor=new BibDublinUnQualifiedRecordProcessor();
76          BibDublinUnQualifiedRecord bibDcUnqualifiedRecord=bibDublinUnQualifiedRecordProcessor.fromXML(bibDcUnqualified.getContent());
77          ListRecords listRecords=bibDcUnqualifiedRecord.getListRecords();
78          for(Record recordlist:listRecords.getRecords()){
79              MetaData metaData=recordlist.getMetadata();
80              for(OaiDcDoc oaiDcDoc:metaData.getOaiDcDocs()){
81                  for (Tag tag:oaiDcDoc.getAllTags()){
82                     if(tag.getName().equalsIgnoreCase("dc:title")){
83                         bibDcUnqualified.setTitle(tag.getValue());
84                     }
85                  }
86              }
87          }
88          return bibDcUnqualified;
89      }
90  }