View Javadoc
1   package org.kuali.ole.describe.bo;
2   
3   import org.apache.commons.lang.StringEscapeUtils;
4   import org.apache.commons.lang.StringUtils;
5   import org.kuali.ole.describe.bo.marc.structuralfields.ControlFields;
6   import org.kuali.ole.describe.bo.marc.structuralfields.controlfield006.ControlField006Text;
7   import org.kuali.ole.describe.bo.marc.structuralfields.controlfield007.ControlField007Text;
8   import org.kuali.ole.describe.bo.marc.structuralfields.controlfield008.ControlField008;
9   import org.kuali.ole.describe.form.MarcEditorForm;
10  import org.kuali.ole.describe.form.WorkBibMarcForm;
11  import org.kuali.ole.docstore.common.document.content.bib.marc.*;
12  import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
13  import org.kuali.ole.docstore.indexer.solr.DocumentLocalId;
14  import org.kuali.rice.krad.web.form.UifFormBase;
15  
16  import java.util.*;
17  
18  /**
19   * MarcEditorFormDataHandler is the data handler class for Marc Editor
20   */
21  public class MarcEditorFormDataHandler {
22      /**
23       * This method will generate the responseXml,
24       * This method builds the bibliographicRecord,And BibliographicRecordHandler will generate the responseXml.,
25       *
26       * @param uifFormBase
27       * @return String
28       */
29      public String buildBibRecordForDocStore(UifFormBase uifFormBase) {
30          MarcEditorForm marcEditorForm = (MarcEditorForm) uifFormBase;
31          BibMarcRecord bibMarcRecord = new BibMarcRecord();
32          bibMarcRecord.setLeader(marcEditorForm.getLeader());
33          bibMarcRecord.setControlFields(buildBibliographicContrlFields(marcEditorForm.getControlFields()));
34          bibMarcRecord.setDataFields(buildBibliographicDataFields(marcEditorForm.getDataFields()));
35          String content = new BibMarcRecordProcessor().generateXML(bibMarcRecord);
36          return content;
37      }
38  
39      /**
40       * This method will generate the responseXml,
41       * This method builds the bibliographicRecord,And BibliographicRecordHandler will generate the responseXml.,
42       *
43       * @param workBibMarcForm
44       * @return String
45       */
46      public String buildBibRecordForDocStore(WorkBibMarcForm workBibMarcForm) {
47  
48          BibMarcRecord bibMarcRecord = new BibMarcRecord();
49          bibMarcRecord.setLeader(workBibMarcForm.getLeader());
50          bibMarcRecord.setControlFields(buildBibliographicContrlFields(workBibMarcForm.getControlFields()));
51          bibMarcRecord.setDataFields(buildBibliographicDataFields(workBibMarcForm.getDataFields()));
52          String content = new BibMarcRecordProcessor().generateXML(bibMarcRecord);
53  
54          return content;
55      }
56  
57      /**
58       * This method build the dataFields for bibliographicRecord and it will return List of marcEditorControlField ,
59       * each marcEditorControlField sets the tag and value get from controlFields
60       *
61       * @param dataFields
62       * @return List<DataField>
63       */
64      private List<DataField> buildBibliographicDataFields(List<MarcEditorDataField> dataFields) {
65          String marcEditorDataFieldValue;
66          List<DataField> marcDataFieldList = new ArrayList<DataField>();
67          for (int i = 0; i < dataFields.size(); i++) {
68              DataField marcDataField = new DataField();
69              List<SubField> marcSubFieldList = new ArrayList<SubField>();
70              marcEditorDataFieldValue = dataFields.get(i).getValue();
71              StringTokenizer str = new StringTokenizer(marcEditorDataFieldValue, "|");
72              while (str.hasMoreTokens()) {
73                  SubField marcSubField = new SubField();
74                  String dataFieldTokenizedVal = str.nextToken();
75                  String code = Character.toString(dataFieldTokenizedVal.charAt(0));
76                  marcSubField.setCode(code);
77                  String value = dataFieldTokenizedVal.substring(1, dataFieldTokenizedVal.length());
78                  marcSubField.setValue(value.trim());
79                  marcSubFieldList.add(marcSubField);
80              }
81              marcDataField.setSubFields(marcSubFieldList);
82              marcDataField.setTag(dataFields.get(i).getTag());
83  
84              if (dataFields.get(i).getInd1().equals("") || dataFields.get(i).getInd1().equals("#") || dataFields.get(i).getInd1() == null) {
85                  marcDataField.setInd1(" ");
86              } else {
87                  marcDataField.setInd1(dataFields.get(i).getInd1());
88              }
89  
90              if (dataFields.get(i).getInd2().equals("") || dataFields.get(i).getInd2().equals("#") || dataFields.get(i).getInd2() == null) {
91                  marcDataField.setInd2(" ");
92              } else {
93                  marcDataField.setInd2(dataFields.get(i).getInd2());
94              }
95  
96              marcDataFieldList.add(marcDataField);
97          }
98          return marcDataFieldList;
99      }
100 
101     /**
102      * This method build the controlFields for bibliographicRecord and it will return List of marcEditorControlField ,
103      * each marcEditorControlField sets the tag and value get from controlFields
104      *
105      * @param controlFields
106      * @return List<ControlField>
107      */
108     private List<ControlField> buildBibliographicContrlFields(List<MarcEditorControlField> controlFields) {
109         List<ControlField> marcControlFieldList = new ArrayList<ControlField>();
110         for (int i = 0; i < controlFields.size(); i++) {
111             ControlField marcControlField = new ControlField();
112             marcControlField.setTag(controlFields.get(i).getTag());
113             marcControlField.setValue(controlFields.get(i).getValue());
114             marcControlFieldList.add(marcControlField);
115         }
116         return marcControlFieldList;
117     }
118 
119     /**
120      * This method build the controlFields and it will return List of marcEditorControlField ,
121      * each marcEditorControlField sets the tag and value get from controlFields
122      *
123      * @param controlFields
124      * @return List<MarcEditorControlField>
125      */
126     public List<MarcEditorControlField> buildMarcEditorControlFields(WorkBibMarcForm workBibMarcForm,
127                                                                      List<ControlField> controlFields) {
128         List<MarcEditorControlField> marcControlFieldList = new ArrayList<MarcEditorControlField>();
129         ControlFields marcControlFields = new ControlFields();
130 
131         ControlField008 controlField008 = new ControlField008();
132         List<ControlField006Text> controlFields006List = new ArrayList<ControlField006Text>();
133         List<ControlField007Text> controlFields007List = new ArrayList<ControlField007Text>();
134 
135         for (int i = 0; i < controlFields.size(); i++) {
136             MarcEditorControlField marcEditorControlField = new MarcEditorControlField();
137             marcEditorControlField.setTag(controlFields.get(i).getTag());
138             marcEditorControlField.setValue(controlFields.get(i).getValue());
139             if (marcEditorControlField.getTag().equals(ControlFields.CONTROL_FIELD_001)) {
140                 if (marcEditorControlField.getValue() != null && marcEditorControlField.getValue().length() > 0) {
141                     marcControlFields.setLocalId(DocumentLocalId.getDocumentIdDisplay(marcEditorControlField.getValue()));
142                 }
143                 marcControlFields.setControlField001(marcEditorControlField.getValue());
144             } else if (marcEditorControlField.getTag().equals(ControlFields.CONTROL_FIELD_003)) {
145                 marcControlFields.setControlField003(marcEditorControlField.getValue());
146             } else if (marcEditorControlField.getTag().equals(ControlFields.CONTROL_FIELD_005)) {
147                 marcControlFields.setControlField005(marcEditorControlField.getValue());
148             } else if (marcEditorControlField.getTag().equals(ControlFields.CONTROL_FIELD_006)) {
149                 ControlField006Text controlField006Text = new ControlField006Text();
150                 controlField006Text.setRawText(marcEditorControlField.getValue());
151                 controlFields006List.add(controlField006Text);
152                 marcControlFields.setControlFields006List(controlFields006List);
153             } else if (marcEditorControlField.getTag().equals(ControlFields.CONTROL_FIELD_007)) {
154                 ControlField007Text controlField007Text = new ControlField007Text();
155                 controlField007Text.setRawText(marcEditorControlField.getValue());
156                 controlFields007List.add(controlField007Text);
157                 marcControlFields.setControlFields007List(controlFields007List);
158             } else if (marcEditorControlField.getTag().equals(ControlFields.CONTROL_FIELD_008)) {
159                 controlField008.setRawText(marcEditorControlField.getValue());
160                 controlField008.setDate008(controlField008.getRawText().substring(0,6));
161                 marcControlFields.setControlField008(controlField008);
162             }
163             workBibMarcForm.setMarcControlFields(marcControlFields);
164             marcControlFieldList.add(marcEditorControlField);
165         }
166         return marcControlFieldList;
167     }
168 
169     /**
170      * This method build the dataFields and it will return List of marcEditorDataField ,
171      * each marcEditorDataField sets the value, and each value has code and value.
172      *
173      * @param dataFields
174      * @return List<MarcEditorDataField>
175      */
176     public List<MarcEditorDataField> buildMarcEditorDataFields(List<DataField> dataFields) {
177         List<MarcEditorDataField> marcEditorDataFields = new ArrayList<MarcEditorDataField>();
178         for (int i = 0; i < dataFields.size(); i++) {
179             MarcEditorDataField marcEditorDataField = new MarcEditorDataField();
180             marcEditorDataField.setTag(dataFields.get(i).getTag());
181             if (dataFields.get(i).getInd1() == null || dataFields.get(i).getInd1().equals(" ") || dataFields.get(i).getInd1().equals("")) {
182                 marcEditorDataField.setInd1("#");
183             } else {
184                 marcEditorDataField.setInd1(dataFields.get(i).getInd1());
185             }
186             if (dataFields.get(i).getInd2() == null || dataFields.get(i).getInd2().equals(" ") || dataFields.get(i).getInd2().equals("")) {
187                 marcEditorDataField.setInd2("#");
188             } else {
189                 marcEditorDataField.setInd2(dataFields.get(i).getInd2());
190             }
191             List<SubField> subFields = dataFields.get(i).getSubFields();
192             String subFieldVal = null;
193             for (int j = 0; j < subFields.size(); j++) {
194                 String code = subFields.get(j).getCode();
195                 String value = subFields.get(j).getValue();
196                 String modifiedVal = value.replace("\"", "&quot;");
197                 // modified code for jira - 4989
198                 /*if (modifiedVal != null) {
199                     modifiedVal = modifiedVal.replace(" ", "&nbsp;");
200                 }*/
201 
202                 if (subFieldVal != null) {
203                     subFieldVal = subFieldVal + "|" + code + " "+modifiedVal+" ";
204                 } else {
205                     subFieldVal = "|" + code + " "+modifiedVal+" ";
206                 }
207             }
208             marcEditorDataField.setValue(subFieldVal);
209             marcEditorDataFields.add(marcEditorDataField);
210         }
211         return marcEditorDataFields;
212     }
213 
214     /**
215      * This method will get the title and Author fields from the marc data fields.
216      *
217      * @param dataFields
218      * @return
219      */
220     public String buildMarcEditorTitleField(List<DataField> dataFields) {
221         StringBuilder titleBuilder = new StringBuilder();
222         String title = null;
223         String author = null;
224         for (DataField dataField : dataFields) {
225             if (dataField.getTag().equalsIgnoreCase("245")) {
226                 List<SubField> subFields = dataField.getSubFields();
227                 for (SubField subField : subFields) {
228                     if (subField.getCode().equalsIgnoreCase("a"))
229                         if (subField.getValue() != null)
230                             title = subField.getValue();
231                 }
232 
233             }
234 
235             if ((dataField.getTag().equalsIgnoreCase("100")) || (dataField.getTag().equalsIgnoreCase("110"))) {
236                 List<SubField> subFields = dataField.getSubFields();
237                 for (SubField subField : subFields) {
238                     if (subField.getCode().equalsIgnoreCase("a")) {
239                         if (subField.getValue() != null) {
240                             author = subField.getValue();
241                         }
242 
243                     }
244                 }
245 
246             }
247         }
248         if (!StringUtils.isBlank(title)) {
249             titleBuilder.append(title);
250 
251         }
252         titleBuilder.append(" / ");
253         if (!StringUtils.isBlank(author)) {
254             titleBuilder.append(author);
255         }
256         return titleBuilder.toString();
257     }
258 
259     /**
260      * This method will get the uuid from the marcEditorForm,
261      * uifFormBase has type casted in to MarcEditorForm and from the marcEditorForm we will get the controlFields,
262      * iterating the controlFields will return the uuid if tag value equal 001
263      *
264      * @param uifFormBase
265      * @return Returns uuid
266      */
267     public String getUUIDFromForm(UifFormBase uifFormBase) {
268         MarcEditorForm form = (MarcEditorForm) uifFormBase;
269         List<MarcEditorControlField> controlFields = form.getControlFields();
270         for (Iterator<MarcEditorControlField> iterator = controlFields.iterator(); iterator.hasNext(); ) {
271             MarcEditorControlField controlField = iterator.next();
272             if (controlField.getTag().equals("001")) {
273                 return controlField.getValue();
274             }
275         }
276         return null;
277     }
278 
279     /**
280      * This method will get the uuid from the marcEditorForm,
281      * uifFormBase has type casted in to MarcEditorForm and from the marcEditorForm we will get the controlFields,
282      * iterating the controlFields will return the uuid if tag value equal 001
283      *
284      * @param workBibMarcForm
285      * @return Returns uuid
286      */
287     public String getUUIDFromForm(WorkBibMarcForm workBibMarcForm) {
288         List<MarcEditorControlField> controlFields = workBibMarcForm.getControlFields();
289         for (Iterator<MarcEditorControlField> iterator = controlFields.iterator(); iterator.hasNext(); ) {
290             MarcEditorControlField controlField = iterator.next();
291             if (controlField.getTag().equals("001")) {
292                 return controlField.getValue();
293             }
294         }
295         return null;
296     }
297 
298 }