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.LeaderTag;
7   import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.SubField;
8   import org.kuali.ole.docstore.model.xstream.work.bib.marc.ControlFieldConverter;
9   import org.kuali.ole.docstore.model.xstream.work.bib.marc.DataFieldConverter;
10  import org.kuali.ole.docstore.model.xstream.work.bib.marc.LeaderConverter;
11  import org.kuali.ole.docstore.model.xstream.work.bib.marc.SubFieldConverter;
12  import org.kuali.ole.pojo.bib.BibliographicRecord;
13  import org.kuali.ole.pojo.bib.Collection;
14  
15  
16  import java.util.*;
17  
18  
19  /**
20   * Created by IntelliJ IDEA.
21   * User: pvsubrah
22   * Date: 4/2/12
23   * Time: 3:57 PM
24   * To change this template use File | Settings | File Templates.
25   */
26  public class BibliographicRecordHandler {
27      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BibliographicRecordHandler.class);
28      private Map<String, String> valuesMap = new HashMap<String, String>();
29      private StringBuilder errBuilder;
30      private static final String ERR_BIB = "Error BIB:: ";
31      private static final String TIME_STAMP = " ::TIME STAMP:: ";
32      private static final String ERR_CAUSE = " ::Error Caused:: ";
33      private static final String lineSeparator = System.getProperty("line.separator");
34      private int errCnt;
35      private int successCnt;
36      private final static String RECORD = "record";
37      private final static String CONTROLFIELD = "controlfield";
38      private final static String DATAFIELD = "datafield";
39      private final static String SUBFIELD = "subfield";
40      private final static String CONTROLFIELDS = "controlfields";
41      private final static String DATAFIELDS = "datafields";
42      private final static String SUBFIELDS = "subFields";
43      private final static String LEADER = "leader";
44  
45      public BibliographicRecordHandler() {
46      }// default Constructor
47  
48      public BibliographicRecordHandler(StringBuilder errBuilder) {
49          this.errBuilder = errBuilder;
50      }
51  
52      /**
53       * Converts given bib record to xml returns null if any error occured
54       * @param bibliographicRecord
55       * @return
56       */
57      public String generateXML(BibliographicRecord bibliographicRecord) {
58          StringBuffer stringBuffer = new StringBuffer();
59          try{
60              stringBuffer.append("<collection xmlns=\"http://www.loc.gov/MARC21/slim\">");
61              stringBuffer.append("\n");
62  
63              XStream xStream = new XStream();
64              xStream.alias(LEADER, LeaderTag.class);
65              xStream.alias(RECORD, BibliographicRecord.class);
66              xStream.alias(CONTROLFIELD, ControlField.class);
67              xStream.alias(DATAFIELD, DataField.class);
68              xStream.alias(SUBFIELD, SubField.class);
69              xStream.addImplicitCollection(BibliographicRecord.class, DATAFIELDS, DataField.class);
70              xStream.addImplicitCollection(DataField.class, SUBFIELDS,SubField.class);
71              xStream.addImplicitCollection(BibliographicRecord.class, CONTROLFIELDS,ControlField.class);
72              xStream.registerConverter(new DataFieldConverter());
73              xStream.registerConverter(new ControlFieldConverter());
74              xStream.registerConverter(new LeaderConverter());
75              String xml = xStream.toXML(bibliographicRecord);
76              stringBuffer.append(xml);
77              stringBuffer.append("\n");
78              stringBuffer.append("</collection>");
79              successCnt++;
80          } catch (Exception ex) {
81              LOG.error("Error while converting BibliographicRecord to string for Bib Record id:: " + bibliographicRecord.getRecordId(), ex);
82              errBuilder.append(ERR_BIB).append(bibliographicRecord.getRecordId()).append(TIME_STAMP)
83                      .append(new Date()).append(ERR_CAUSE).append(ex.getMessage()).append(" ::At:: ").append("generateXML() for mrc").append(lineSeparator);
84              errCnt++;
85              return null;
86          }
87          return stringBuffer.toString();
88      }
89  
90      /**
91       * generates the xml string with all the records under one collection tag
92       * @param bibliographicRecord
93       * @return
94       */
95      public String generateXML(List<BibliographicRecord> bibliographicRecord) {
96          StringBuffer stringBuffer = new StringBuffer();
97          //stringBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><collection xmlns=\"http://www.loc.gov/MARC21/slim\">");
98          stringBuffer.append("<collection xmlns=\"http://www.loc.gov/MARC21/slim\">");
99          for (BibliographicRecord record : bibliographicRecord) {
100             try {
101                 if (record != null) {
102                     stringBuffer.append(lineSeparator);
103 
104                     XStream xStream = new XStream();
105                     xStream.alias(LEADER, LeaderTag.class);
106                     xStream.alias(RECORD, BibliographicRecord.class);
107                     xStream.alias(CONTROLFIELD, ControlField.class);
108                     xStream.alias(DATAFIELD, DataField.class);
109                     xStream.alias(SUBFIELD, SubField.class);
110                     xStream.addImplicitCollection(BibliographicRecord.class, DATAFIELDS, DataField.class);
111                     xStream.addImplicitCollection(DataField.class, SUBFIELDS, SubField.class);
112                     xStream.addImplicitCollection(BibliographicRecord.class, CONTROLFIELDS, ControlField.class);
113                     xStream.registerConverter(new DataFieldConverter());
114                     xStream.registerConverter(new ControlFieldConverter());
115                     xStream.registerConverter(new LeaderConverter());
116                     String xml = xStream.toXML(record);
117                     stringBuffer.append(xml);
118                     successCnt++;
119                 }
120             } catch (Exception ex) {
121                 LOG.error("Error while converting BibliographicRecord to string for Bib Record id:: " + record.getRecordId(), ex);
122                 errBuilder.append(ERR_BIB).append(record.getRecordId()).append(TIME_STAMP)
123                         .append(new Date()).append(ERR_CAUSE).append(ex.getMessage()).append(" ::At:: ").append("generateXML()").append(lineSeparator);
124                 errCnt++;
125             }
126         }
127         stringBuffer.append(lineSeparator);
128         stringBuffer.append("</collection>");
129         return stringBuffer.toString();
130     }
131 
132     public BibliographicRecord getModifiedBibWithout8xxFields(BibliographicRecord bibliographicRecord) {
133         List<DataField> dataFields = new ArrayList<DataField>();
134         BibliographicRecord newBibliographicRecord = new BibliographicRecord();
135         newBibliographicRecord.setLeader(bibliographicRecord.getLeader());
136         newBibliographicRecord.setControlfields(bibliographicRecord.getControlfields());
137         List<DataField> datafields = bibliographicRecord.getDatafields();
138         for (Iterator<DataField> iterator1 = datafields.iterator(); iterator1.hasNext(); ) {
139             DataField dataField = iterator1.next();
140             if (!dataField.getTag().contains("8")) {
141                 dataFields.add(dataField);
142             }
143         }
144         newBibliographicRecord.setDatafields(dataFields);
145 
146         return newBibliographicRecord;
147     }
148 
149     public Collection fromXML(String marcXMLContent) {
150         XStream xStream = new XStream();
151         xStream.alias("collection", Collection.class);
152         xStream.alias(LEADER, LeaderTag.class);
153         xStream.alias("record", BibliographicRecord.class);
154         xStream.alias("controlfield", ControlField.class);
155         xStream.alias("datafield", DataField.class);
156         xStream.alias("subfield", SubField.class);
157         xStream.addImplicitCollection(BibliographicRecord.class, "datafields", DataField.class);
158         xStream.addImplicitCollection(BibliographicRecord.class, "controlfields", ControlField.class);
159         xStream.addImplicitCollection(Collection.class, "collection");
160         xStream.registerConverter(new DataFieldConverter());
161         xStream.registerConverter(new SubFieldConverter());
162         xStream.registerConverter(new ControlFieldConverter());
163         xStream.registerConverter(new LeaderConverter());
164         return (Collection) xStream.fromXML(marcXMLContent);
165     }
166 
167    /* public Map<String, String> valuesMapFromXML(String marcXMLContent) {
168         Collection bibliographicRecordCollection = fromXML(marcXMLContent);
169         List<BibliographicRecord> records = bibliographicRecordCollection.getRecords();
170         for (Iterator<BibliographicRecord> iterator = records.iterator(); iterator.hasNext(); ) {
171             BibliographicRecord record = iterator.next();
172             valuesMap.put("leader", record.getLeader());
173             List<ControlField> controlFields = record.getControlfields();
174             for (Iterator<ControlField> controlFieldIterator = controlFields.iterator(); controlFieldIterator.hasNext(); ) {
175                 ControlField controlField = controlFieldIterator.next();
176                 valuesMap.put(controlField.getTag(), controlField.getValue());
177             }
178             List<DataField> datafields = record.getDatafields();
179             for (Iterator<DataField> dataFieldIterator = datafields.iterator(); dataFieldIterator.hasNext(); ) {
180                 DataField dataField = dataFieldIterator.next();
181                 String tag = dataField.getTag();
182                 List<SubField> subfields = dataField.getSubFields();
183                 for (Iterator<SubField> marcSubFieldIterator = subfields.iterator(); marcSubFieldIterator.hasNext(); ) {
184                     SubField subField = marcSubFieldIterator.next();
185                     valuesMap.put(tag + subField.getCode(), subField.getValue());
186                 }
187             }
188         }
189         return valuesMap;
190     }*/
191 
192 
193     public String generateXML(Collection bibliographicRecords) {
194         StringBuffer stringBuffer = new StringBuffer();
195         stringBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
196         stringBuffer.append("\n");
197 
198         XStream xStream = new XStream();
199         xStream.alias("collection", Collection.class);
200         xStream.alias(LEADER, LeaderTag.class);
201         xStream.alias("record", BibliographicRecord.class);
202         xStream.alias("controlfield", ControlField.class);
203         xStream.alias("datafield", DataField.class);
204         xStream.alias("subfield", SubField.class);
205         xStream.addImplicitCollection(BibliographicRecord.class, "datafields", DataField.class);
206         xStream.addImplicitCollection(BibliographicRecord.class, "controlfields", ControlField.class);
207         xStream.addImplicitCollection(Collection.class, "collection");
208         xStream.registerConverter(new DataFieldConverter());
209         xStream.registerConverter(new SubFieldConverter());
210         xStream.registerConverter(new ControlFieldConverter());
211         xStream.registerConverter(new LeaderConverter());
212         String xml = xStream.toXML(bibliographicRecords);
213         stringBuffer.append(xml);
214         stringBuffer.append("\n");
215         return stringBuffer.toString();
216     }
217 
218     public String generateXMLWithoutCollectionTag(BibliographicRecord bibliographicRecord) {
219         StringBuffer stringBuffer = new StringBuffer();
220         //stringBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><collection xmlns=\"http://www.loc.gov/MARC21/slim\">");
221         //stringBuffer.append("<collection xmlns=\"http://www.loc.gov/MARC21/slim\">");
222         //stringBuffer.append("\n");
223 
224         XStream xStream = new XStream();
225         xStream.alias(RECORD, BibliographicRecord.class);
226         xStream.alias(CONTROLFIELD, ControlField.class);
227         xStream.alias(DATAFIELD, DataField.class);
228         xStream.alias(SUBFIELD, SubField.class);
229         xStream.alias(LEADER, LeaderTag.class);
230         xStream.addImplicitCollection(BibliographicRecord.class, DATAFIELDS,DataField.class);
231         xStream.addImplicitCollection(DataField.class, SUBFIELDS,SubField.class);
232         xStream.addImplicitCollection(BibliographicRecord.class, CONTROLFIELDS,ControlField.class);
233         xStream.registerConverter(new DataFieldConverter());
234         xStream.registerConverter(new ControlFieldConverter());
235         xStream.registerConverter(new LeaderConverter());
236         String xml = xStream.toXML(bibliographicRecord);
237         stringBuffer.append(xml);
238         stringBuffer.append("\n");
239        // stringBuffer.append("</collection>");
240         return stringBuffer.toString();
241     }
242 
243     /**
244      * gets the errBuilder value
245      *
246      * @return
247      */
248     public String getErrBuilder() {
249         return errBuilder.toString();
250     }
251 
252     public int getErrCnt() {
253         return errCnt;
254     }
255 
256     public int getSuccessCnt() {
257         return successCnt;
258     }
259 
260 
261 }