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
20
21 public class MarcEditorFormDataHandler {
22
23
24
25
26
27
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
41
42
43
44
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
59
60
61
62
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
103
104
105
106
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
121
122
123
124
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
171
172
173
174
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("\"", """);
197
198
199
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
216
217
218
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
261
262
263
264
265
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
281
282
283
284
285
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 }