001package org.kuali.ole.describe.bo;
002
003import org.kuali.ole.BibliographicRecordHandler;
004import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.ControlField;
005import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.DataField;
006import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.SubField;
007import org.kuali.ole.describe.form.MarcEditorForm;
008import org.kuali.ole.pojo.bib.BibliographicRecord;
009import org.kuali.rice.krad.web.form.UifFormBase;
010
011import java.util.ArrayList;
012import java.util.Iterator;
013import java.util.List;
014import java.util.StringTokenizer;
015
016/**
017 * MarcEditorFormDataHandler is the data handler class for Marc Editor
018 */
019public class WorkMarcEditorFormDataHandler {
020    /**
021     * This method will generate the responseXml,
022     * This method builds the bibliographicRecord,And BibliographicRecordHandler will generate the responseXml.,
023     *
024     * @param uifFormBase
025     * @return String
026     */
027    public String buildBibRecordForDocStore(UifFormBase uifFormBase) {
028        MarcEditorForm marcEditorForm = (MarcEditorForm) uifFormBase;
029        BibliographicRecord bibliographicRecord = new BibliographicRecord();
030        bibliographicRecord.setLeader(marcEditorForm.getLeader());
031        bibliographicRecord.setControlfields(buldBibliographicContrlFields(marcEditorForm.getControlFields()));
032        bibliographicRecord.setDatafields(buildBibliographicDataFields(marcEditorForm.getDataFields()));
033        String content = new BibliographicRecordHandler().generateXML(bibliographicRecord);
034        return content;
035    }
036
037    /**
038     * This method build the dataFields for bibliographicRecord and it will return List of marcEditorControlField ,
039     * each marcEditorControlField sets the tag and value get from controlFields
040     *
041     * @param dataFields
042     * @return List<DataField>
043     */
044    private List<DataField> buildBibliographicDataFields(List<MarcEditorDataField> dataFields) {
045        String marcEditorDataFieldValue;
046        List<DataField> marcDataFieldList = new ArrayList<DataField>();
047        for (int i = 0; i < dataFields.size(); i++) {
048            DataField marcDataField = new DataField();
049            List<SubField> marcSubFieldList = new ArrayList<SubField>();
050            marcEditorDataFieldValue = dataFields.get(i).getValue();
051            StringTokenizer str = new StringTokenizer(marcEditorDataFieldValue, "|");
052            while (str.hasMoreTokens()) {
053                SubField marcSubField = new SubField();
054                String dataFieldTokenizedVal = str.nextToken();
055                String code = Character.toString(dataFieldTokenizedVal.charAt(0));
056                marcSubField.setCode(code);
057                String value = dataFieldTokenizedVal.substring(1, dataFieldTokenizedVal.length());
058                marcSubField.setValue(value);
059                marcSubFieldList.add(marcSubField);
060            }
061            marcDataField.setSubFields(marcSubFieldList);
062            marcDataField.setTag(dataFields.get(i).getTag());
063            marcDataField.setInd1(dataFields.get(i).getInd1());
064            marcDataField.setInd2(dataFields.get(i).getInd2());
065            marcDataFieldList.add(marcDataField);
066        }
067        return marcDataFieldList;
068    }
069
070    /**
071     * This method build the controlFields for bibliographicRecord and it will return List of marcEditorControlField ,
072     * each marcEditorControlField sets the tag and value get from controlFields
073     *
074     * @param controlFields
075     * @return List<ControlField>
076     */
077    private List<ControlField> buldBibliographicContrlFields(List<MarcEditorControlField> controlFields) {
078        List<ControlField> marcControlFieldList = new ArrayList<ControlField>();
079        for (int i = 0; i < controlFields.size(); i++) {
080            ControlField marcControlField = new ControlField();
081            marcControlField.setTag(controlFields.get(i).getTag());
082            marcControlField.setValue(controlFields.get(i).getValue());
083            marcControlFieldList.add(marcControlField);
084        }
085        return marcControlFieldList;
086    }
087
088    /**
089     * This method build the controlFields and it will return List of marcEditorControlField ,
090     * each marcEditorControlField sets the tag and value get from controlFields
091     *
092     * @param controlFields
093     * @return List<MarcEditorControlField>
094     */
095    public List<MarcEditorControlField> buildMarcEditorControlFields(List<ControlField> controlFields) {
096        List<MarcEditorControlField> marcControlFieldList = new ArrayList<MarcEditorControlField>();
097        for (int i = 0; i < controlFields.size(); i++) {
098            MarcEditorControlField marcEditorControlField = new MarcEditorControlField();
099            marcEditorControlField.setTag(controlFields.get(i).getTag());
100            marcEditorControlField.setValue(controlFields.get(i).getValue());
101            marcControlFieldList.add(marcEditorControlField);
102        }
103        return marcControlFieldList;
104    }
105
106    /**
107     * This method build the dataFields and it will return List of marcEditorDataField ,
108     * each marcEditorDataField sets the value, and each value has code and value.
109     *
110     * @param dataFields
111     * @return List<MarcEditorDataField>
112     */
113    public List<MarcEditorDataField> buildMarcEditorDataFields(List<DataField> dataFields) {
114        List<MarcEditorDataField> marcEditorDataFields = new ArrayList<MarcEditorDataField>();
115        for (int i = 0; i < dataFields.size(); i++) {
116            MarcEditorDataField marcEditorDataField = new MarcEditorDataField();
117            marcEditorDataField.setTag(dataFields.get(i).getTag());
118            marcEditorDataField.setInd1(dataFields.get(i).getInd1());
119            marcEditorDataField.setInd2(dataFields.get(i).getInd2());
120            List<SubField> subFields = dataFields.get(i).getSubFields();
121            String subFieldVal = null;
122            for (int j = 0; j < subFields.size(); j++) {
123                String code = subFields.get(j).getCode();
124                String value = subFields.get(j).getValue();
125                if (subFieldVal != null)
126                    subFieldVal = subFieldVal + "|" + code + value;
127                else
128                    subFieldVal = "|" + code + value;
129            }
130            marcEditorDataField.setValue(subFieldVal);
131            marcEditorDataFields.add(marcEditorDataField);
132        }
133        return marcEditorDataFields;
134    }
135
136    /**
137     * This method will get the uuid from the marcEditorForm,
138     * uifFormBase has type casted in to MarcEditorForm and from the marcEditorForm we will get the controlFields,
139     * iterating the controlFields will return the uuid if tag value equal 001
140     *
141     * @param uifFormBase
142     * @return Returns uuid
143     */
144    public String getUUIDFromForm(UifFormBase uifFormBase) {
145        MarcEditorForm form = (MarcEditorForm) uifFormBase;
146        List<MarcEditorControlField> controlFields = form.getControlFields();
147        for (Iterator<MarcEditorControlField> iterator = controlFields.iterator(); iterator.hasNext(); ) {
148            MarcEditorControlField controlField = iterator.next();
149            if (controlField.getTag().equals("001")) {
150                return controlField.getValue();
151            }
152        }
153        return null;
154    }
155}