View Javadoc

1   package org.kuali.ole;
2   
3   import com.thoughtworks.xstream.XStream;
4   import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.ControlField;
5   import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.DataField;
6   import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.SubField;
7   import org.kuali.ole.docstore.model.xstream.work.bib.marc.ControlFieldConverter;
8   import org.kuali.ole.docstore.model.xstream.work.bib.marc.DataFieldConverter;
9   import org.kuali.ole.docstore.model.xstream.work.bib.marc.SubFieldConverter;
10  import org.kuali.ole.pojo.bib.BibliographicRecord;
11  import org.kuali.ole.pojo.bib.Collection;
12  
13  import java.util.*;
14  
15  /**
16   * Created by IntelliJ IDEA.
17   * User: pvsubrah
18   * Date: 4/2/12
19   * Time: 3:57 PM
20   * To change this template use File | Settings | File Templates.
21   */
22  public class BibliographicRecordHandler {
23      private Map<String, String> valuesMap = new HashMap<String, String>();
24  
25      public String generateXML(BibliographicRecord bibliographicRecord) {
26          StringBuffer stringBuffer = new StringBuffer();
27          //stringBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><collection xmlns=\"http://www.loc.gov/MARC21/slim\">");
28          stringBuffer.append("<collection xmlns=\"http://www.loc.gov/MARC21/slim\">");
29          stringBuffer.append("\n");
30  
31          XStream xStream = new XStream();
32          xStream.alias("record", BibliographicRecord.class);
33          xStream.alias("controlfield", ControlField.class);
34          xStream.alias("datafield", DataField.class);
35          xStream.alias("subfield", SubField.class);
36          xStream.addImplicitCollection(BibliographicRecord.class, "datafields");
37          xStream.addImplicitCollection(DataField.class, "subFields");
38          xStream.addImplicitCollection(BibliographicRecord.class, "controlfields");
39          xStream.registerConverter(new DataFieldConverter());
40          xStream.registerConverter(new ControlFieldConverter());
41          String xml = xStream.toXML(bibliographicRecord);
42          stringBuffer.append(xml);
43          stringBuffer.append("\n");
44          stringBuffer.append("</collection>");
45          return stringBuffer.toString();
46      }
47  
48      public BibliographicRecord getModifiedBibWithout8xxFields(BibliographicRecord bibliographicRecord) {
49          List<DataField> dataFields = new ArrayList<DataField>();
50          BibliographicRecord newBibliographicRecord = new BibliographicRecord();
51          newBibliographicRecord.setLeader(bibliographicRecord.getLeader());
52          newBibliographicRecord.setControlfields(bibliographicRecord.getControlfields());
53          List<DataField> datafields = bibliographicRecord.getDatafields();
54          for (Iterator<DataField> iterator1 = datafields.iterator(); iterator1.hasNext(); ) {
55              DataField dataField = iterator1.next();
56              if (!dataField.getTag().contains("8")) {
57                  dataFields.add(dataField);
58              }
59          }
60          newBibliographicRecord.setDatafields(dataFields);
61  
62          return newBibliographicRecord;
63      }
64  
65      public Collection fromXML(String marcXMLContent) {
66          XStream xStream = new XStream();
67          xStream.alias("collection", Collection.class);
68          xStream.alias("record", BibliographicRecord.class);
69          xStream.alias("controlfield", ControlField.class);
70          xStream.alias("datafield", DataField.class);
71          xStream.alias("subfield", SubField.class);
72          xStream.addImplicitCollection(BibliographicRecord.class, "datafields", DataField.class);
73          xStream.addImplicitCollection(BibliographicRecord.class, "controlfields", ControlField.class);
74          xStream.addImplicitCollection(Collection.class, "collection");
75          xStream.registerConverter(new DataFieldConverter());
76          xStream.registerConverter(new SubFieldConverter());
77          xStream.registerConverter(new ControlFieldConverter());
78          return (Collection) xStream.fromXML(marcXMLContent);
79      }
80  
81      public Map<String, String> valuesMapFromXML(String marcXMLContent) {
82          Collection bibliographicRecordCollection = fromXML(marcXMLContent);
83          List<BibliographicRecord> records = bibliographicRecordCollection.getRecords();
84          for (Iterator<BibliographicRecord> iterator = records.iterator(); iterator.hasNext(); ) {
85              BibliographicRecord record = iterator.next();
86              valuesMap.put("leader", record.getLeader());
87              List<ControlField> controlFields = record.getControlfields();
88              for (Iterator<ControlField> controlFieldIterator = controlFields.iterator(); controlFieldIterator.hasNext(); ) {
89                  ControlField controlField = controlFieldIterator.next();
90                  valuesMap.put(controlField.getTag(), controlField.getValue());
91              }
92              List<DataField> datafields = record.getDatafields();
93              for (Iterator<DataField> dataFieldIterator = datafields.iterator(); dataFieldIterator.hasNext(); ) {
94                  DataField dataField = dataFieldIterator.next();
95                  String tag = dataField.getTag();
96                  List<SubField> subfields = dataField.getSubFields();
97                  for (Iterator<SubField> marcSubFieldIterator = subfields.iterator(); marcSubFieldIterator.hasNext(); ) {
98                      SubField subField = marcSubFieldIterator.next();
99                      valuesMap.put(tag + subField.getCode(), subField.getValue());
100                 }
101             }
102         }
103         return valuesMap;
104     }
105 
106 
107     public String generateXML(Collection bibliographicRecords) {
108         StringBuffer stringBuffer = new StringBuffer();
109         stringBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
110         stringBuffer.append("\n");
111 
112         XStream xStream = new XStream();
113         xStream.alias("collection", Collection.class);
114         xStream.alias("record", BibliographicRecord.class);
115         xStream.alias("controlfield", ControlField.class);
116         xStream.alias("datafield", DataField.class);
117         xStream.alias("subfield", SubField.class);
118         xStream.addImplicitCollection(BibliographicRecord.class, "datafields", DataField.class);
119         xStream.addImplicitCollection(BibliographicRecord.class, "controlfields", ControlField.class);
120         xStream.addImplicitCollection(Collection.class, "collection");
121         xStream.registerConverter(new DataFieldConverter());
122         xStream.registerConverter(new SubFieldConverter());
123         xStream.registerConverter(new ControlFieldConverter());
124         String xml = xStream.toXML(bibliographicRecords);
125         stringBuffer.append(xml);
126         stringBuffer.append("\n");
127         return stringBuffer.toString();
128     }
129     
130 
131 }