001package org.kuali.ole.describe.bo;
002
003import org.apache.commons.lang.StringEscapeUtils;
004import org.apache.commons.lang.StringUtils;
005import org.kuali.ole.describe.bo.marc.structuralfields.ControlFields;
006import org.kuali.ole.describe.bo.marc.structuralfields.controlfield006.ControlField006Text;
007import org.kuali.ole.describe.bo.marc.structuralfields.controlfield007.ControlField007Text;
008import org.kuali.ole.describe.bo.marc.structuralfields.controlfield008.ControlField008;
009import org.kuali.ole.describe.form.MarcEditorForm;
010import org.kuali.ole.describe.form.WorkBibMarcForm;
011import org.kuali.ole.docstore.common.document.content.bib.marc.*;
012import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
013import org.kuali.ole.docstore.indexer.solr.DocumentLocalId;
014import org.kuali.rice.krad.web.form.UifFormBase;
015
016import java.util.*;
017
018/**
019 * MarcEditorFormDataHandler is the data handler class for Marc Editor
020 */
021public class MarcEditorFormDataHandler {
022    /**
023     * This method will generate the responseXml,
024     * This method builds the bibliographicRecord,And BibliographicRecordHandler will generate the responseXml.,
025     *
026     * @param uifFormBase
027     * @return String
028     */
029    public String buildBibRecordForDocStore(UifFormBase uifFormBase) {
030        MarcEditorForm marcEditorForm = (MarcEditorForm) uifFormBase;
031        BibMarcRecord bibMarcRecord = new BibMarcRecord();
032        bibMarcRecord.setLeader(marcEditorForm.getLeader());
033        bibMarcRecord.setControlFields(buildBibliographicContrlFields(marcEditorForm.getControlFields()));
034        bibMarcRecord.setDataFields(buildBibliographicDataFields(marcEditorForm.getDataFields()));
035        String content = new BibMarcRecordProcessor().generateXML(bibMarcRecord);
036        return content;
037    }
038
039    /**
040     * This method will generate the responseXml,
041     * This method builds the bibliographicRecord,And BibliographicRecordHandler will generate the responseXml.,
042     *
043     * @param workBibMarcForm
044     * @return String
045     */
046    public String buildBibRecordForDocStore(WorkBibMarcForm workBibMarcForm) {
047
048        BibMarcRecord bibMarcRecord = new BibMarcRecord();
049        bibMarcRecord.setLeader(workBibMarcForm.getLeader());
050        bibMarcRecord.setControlFields(buildBibliographicContrlFields(workBibMarcForm.getControlFields()));
051        bibMarcRecord.setDataFields(buildBibliographicDataFields(workBibMarcForm.getDataFields()));
052        String content = new BibMarcRecordProcessor().generateXML(bibMarcRecord);
053
054        return content;
055    }
056
057    /**
058     * This method build the dataFields for bibliographicRecord and it will return List of marcEditorControlField ,
059     * each marcEditorControlField sets the tag and value get from controlFields
060     *
061     * @param dataFields
062     * @return List<DataField>
063     */
064    private List<DataField> buildBibliographicDataFields(List<MarcEditorDataField> dataFields) {
065        String marcEditorDataFieldValue;
066        List<DataField> marcDataFieldList = new ArrayList<DataField>();
067        for (int i = 0; i < dataFields.size(); i++) {
068            DataField marcDataField = new DataField();
069            List<SubField> marcSubFieldList = new ArrayList<SubField>();
070            marcEditorDataFieldValue = dataFields.get(i).getValue();
071            StringTokenizer str = new StringTokenizer(marcEditorDataFieldValue, "|");
072            while (str.hasMoreTokens()) {
073                SubField marcSubField = new SubField();
074                String dataFieldTokenizedVal = str.nextToken();
075                String code = Character.toString(dataFieldTokenizedVal.charAt(0));
076                marcSubField.setCode(code);
077                String value = dataFieldTokenizedVal.substring(1, dataFieldTokenizedVal.length());
078                marcSubField.setValue(value.trim());
079                marcSubFieldList.add(marcSubField);
080            }
081            marcDataField.setSubFields(marcSubFieldList);
082            marcDataField.setTag(dataFields.get(i).getTag());
083
084            if (dataFields.get(i).getInd1().equals("") || dataFields.get(i).getInd1().equals("#") || dataFields.get(i).getInd1() == null) {
085                marcDataField.setInd1(" ");
086            } else {
087                marcDataField.setInd1(dataFields.get(i).getInd1());
088            }
089
090            if (dataFields.get(i).getInd2().equals("") || dataFields.get(i).getInd2().equals("#") || dataFields.get(i).getInd2() == null) {
091                marcDataField.setInd2(" ");
092            } else {
093                marcDataField.setInd2(dataFields.get(i).getInd2());
094            }
095
096            marcDataFieldList.add(marcDataField);
097        }
098        return marcDataFieldList;
099    }
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}