View Javadoc
1   package org.kuali.ole.describe.bo;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.jfree.util.Log;
5   import org.kuali.ole.UnQualifiedDublinRecordHandler;
6   import org.kuali.ole.describe.form.EditorForm;
7   import org.kuali.ole.docstore.model.enums.DocFormat;
8   import org.kuali.ole.docstore.model.xmlpojo.work.bib.dublin.DCValue;
9   import org.kuali.ole.docstore.model.xmlpojo.work.bib.dublin.WorkBibDublinRecord;
10  import org.kuali.ole.docstore.model.xmlpojo.work.bib.dublin.unqualified.*;
11  import org.kuali.ole.pojo.dublin.unqualified.UnQualifiedDublinRecord;
12  import org.kuali.rice.krad.web.form.UifFormBase;
13  
14  import java.util.ArrayList;
15  import java.util.List;
16  
17  /**
18   * DublinEditorFormDataHandler is the data handler class for Dublin Editor
19   */
20  public class WorkDublinEditorFormDataHandler {
21      /**
22       * This method returns response Xml from the docStore for Duplin Record, This method will ingest new record to docstore if uuid is null, else it will update existing record.
23       * the toXml method in UnQualifiedDublinRecordHandler will convert the unQualifiedDublinRecord in to responseXml.
24       *
25       * @param uifFormBase
26       * @param uuid
27       * @return content
28       */
29      public String buildDublinRecordForDocStore(UifFormBase uifFormBase, String uuid) {
30  
31          String content = "";
32          EditorForm dublinEditorForm = (EditorForm) uifFormBase;
33          if (dublinEditorForm.getDocFormat().equals(DocFormat.DUBLIN_UNQUALIFIED.getCode())) {
34              UnQualifiedDublinRecord unQualifiedDublinRecord = new UnQualifiedDublinRecord();
35              if (uuid != null && !uuid.trim().equals("")) {
36                  unQualifiedDublinRecord.setListRecords(buildDublinDCValuesForEdit(dublinEditorForm));
37              } else {
38                  unQualifiedDublinRecord.setListRecords(buildDublinDCValues(dublinEditorForm.getDublinFieldList()));
39              }
40              dublinEditorForm.setTitle(buildDublinUnqTitleField(dublinEditorForm, unQualifiedDublinRecord));
41              content = new UnQualifiedDublinRecordHandler().toXml(unQualifiedDublinRecord);
42          } else if (dublinEditorForm.getDocFormat().equals(DocFormat.DUBLIN_CORE.getCode())) {
43              //System.out.println("Enter Dublin Save");
44              Log.info("Enter Dublin Save");
45              //TODO: code for building content.
46              //            WorkBibDublinRecord workBibDublinRecord = new WorkBibDublinRecord();
47              //            List<DCValue> dcValues = new ArrayList<DCValue>();
48              //            DCValue dcValue = new DCValue();
49              //            dcValue.setQualifier("contributor");
50              //            dcValue.setElement("contributor");
51              //            dcValue.setValue("contributor");
52              //            dcValues.add(dcValue);
53              //            workBibDublinRecord.setDcValues(dcValues);
54              //            content = new QualifiedDublinRecordHandler().toXml(workBibDublinRecord);
55          }
56          return content;
57      }
58  
59      /**
60       * This method returns List of DublinDcValue records,
61       * In this method each record contains a metadata with oaiDcDocList,
62       * each oaiDcDocList contains list of oaiDcDoc with tagName and value.
63       *
64       * @param dublinFields
65       * @return listRecords
66       */
67      private ListRecords buildDublinDCValues(List<WorkDublinEditorField> dublinFields) {
68          ListRecords listRecords = new ListRecords();
69          Record record = new Record();
70          MetaData metaData = new MetaData();
71          OaiDcDoc oaiDcDoc = new OaiDcDoc();
72          List<OaiDcDoc> oaiDcDocList = new ArrayList<OaiDcDoc>();
73          for (int i = 0; i < dublinFields.size(); i++) {
74              if (!dublinFields.get(i).getElement().equalsIgnoreCase("default")) {
75                  oaiDcDoc.put(dublinFields.get(i).getElement(), dublinFields.get(i).getValue());
76              }
77          }
78          oaiDcDocList.add(oaiDcDoc);
79          metaData.setOaiDcDocs(oaiDcDocList);
80          record.setMetadata(metaData);
81          listRecords.addRecord(record);
82          return listRecords;
83      }
84  
85      private ListRecords buildDublinDCValues1(List<WorkDublinEditorField> dublinFields) {
86          ListRecords listRecords = new ListRecords();
87          Record record = new Record();
88          MetaData metaData = new MetaData();
89          OaiDcDoc oaiDcDoc = new OaiDcDoc();
90          List<OaiDcDoc> oaiDcDocList = new ArrayList<OaiDcDoc>();
91          for (int i = 0; i < dublinFields.size(); i++) {
92              if (!dublinFields.get(i).getElement().equalsIgnoreCase("default")) {
93                  oaiDcDoc.put(dublinFields.get(i).getElement(), dublinFields.get(i).getValue());
94              }
95          }
96          oaiDcDocList.add(oaiDcDoc);
97          metaData.setOaiDcDocs(oaiDcDocList);
98          record.setMetadata(metaData);
99          listRecords.addRecord(record);
100         return listRecords;
101     }
102 
103     /**
104      * This method returns List of  DublinEditorField,
105      * each dublinEditorField object sets the tagName and value got from unQualifiedDublinRecord.
106      *
107      * @param unQualifiedDublinRecord
108      * @return dublinEditorFieldList
109      */
110 
111     public List<WorkDublinEditorField> buildDublinUnqEditorFields(EditorForm editorForm, UnQualifiedDublinRecord unQualifiedDublinRecord) {
112         ListRecords listRecords = unQualifiedDublinRecord.getListRecords();
113         List<Tag> tagList = listRecords.getRecords().get(0).getMetadata().getOaiDcDocs().get(0).getAllTags();
114         //   List<WorkDublinEditorField> dublinEditorFieldList = editorForm.getDublinFieldList();
115         List<WorkDublinEditorField> dublinEditorFieldList = new ArrayList<WorkDublinEditorField>();
116         for (int i = 0; i < tagList.size(); i++) {
117             WorkDublinEditorField dublinEditorField = new WorkDublinEditorField();
118             dublinEditorField.setElement(tagList.get(i).getName());
119             dublinEditorField.setValue(tagList.get(i).getValue());
120             dublinEditorFieldList.add(dublinEditorField);
121         }
122 
123         return dublinEditorFieldList;
124     }
125 
126     public String buildDublinUnqTitleField(EditorForm editorForm, UnQualifiedDublinRecord unQualifiedDublinRecord) {
127 
128         String title = "";
129         String author = "";
130         ListRecords listRecords = unQualifiedDublinRecord.getListRecords();
131         List<Tag> tagList = listRecords.getRecords().get(0).getMetadata().getOaiDcDocs().get(0).getAllTags();
132         List<WorkDublinEditorField> dublinEditorFieldList = new ArrayList<WorkDublinEditorField>();
133         for (Tag tag : tagList) {
134             if (tag.getName().contains("title")) {
135                 if (!(StringUtils.isBlank(tag.getValue()))) {
136                     title = tag.getValue();
137                 }
138             }
139 
140             if (tag.getName().contains("creator")) {
141                 if (!StringUtils.isBlank(tag.getValue())) {
142                     author = tag.getValue();
143                 }
144             }
145         }
146         return title + " / " + author;
147 
148     }
149 
150     public List<WorkDublinEditorField> buildDublinEditorFields(EditorForm editorForm, WorkBibDublinRecord workBibDublinRecord) {
151         List<DCValue> listRecords = workBibDublinRecord.getDcValues();
152         List<WorkDublinEditorField> dublinEditorFieldList = editorForm.getDublinFieldList();
153         for (int i = 0; i < listRecords.size(); i++) {
154             WorkDublinEditorField dublinEditorField = new WorkDublinEditorField();
155             dublinEditorField.setElement(listRecords.get(i).getElement());
156             dublinEditorField.setValue(listRecords.get(i).getValue());
157             dublinEditorFieldList.add(dublinEditorField);
158         }
159         return dublinEditorFieldList;
160     }
161 
162     /**
163      * This method returns updated ListRecords to be set in UnQualifiedDublinRecord  to update the existing docStore records.
164      * In this method each record contains updated metadata with oaiDcDocList,
165      * each oaiDcDocList contains updated list of oaiDcDoc with tagName and value.
166      *
167      * @param dublinEditorForm
168      * @return listRecords
169      */
170     private ListRecords buildDublinDCValuesForEdit(EditorForm dublinEditorForm) {
171         ListRecords listRecords = new ListRecords();
172         String element = "";
173         String value = "";
174         Record record = new Record();
175         List<String> elementList = new ArrayList<String>();
176         MetaData metaData = new MetaData();
177         OaiDcDoc oaiDcDoc = new OaiDcDoc();
178         List<OaiDcDoc> oaiDcDocList = new ArrayList<OaiDcDoc>();
179         List<WorkDublinEditorField> newElementList = dublinEditorForm.getDublinFieldList();
180         List<WorkDublinEditorField> existingElementList = dublinEditorForm.getExistingDublinFieldList();
181         for (int i = 0; i < newElementList.size(); i++) {
182             if (!newElementList.get(i).getElement().equalsIgnoreCase("default")) {
183                 element = newElementList.get(i).getElement();
184                 value = newElementList.get(i).getValue();
185                 if (!elementList.contains(element)) {
186                     elementList.add(element);
187                     oaiDcDoc.put(element, value);
188                 }
189 
190             }
191         }
192 //        for (int i = 0; i < existingElementList.size(); i++) {
193 //            if (!existingElementList.get(i).getElement().equalsIgnoreCase("default")) {
194 //                oaiDcDoc.put(existingElementList.get(i).getElement(), existingElementList.get(i).getValue());
195 //            }
196 //        }
197         oaiDcDocList.add(oaiDcDoc);
198         metaData.setOaiDcDocs(oaiDcDocList);
199         record.setMetadata(metaData);
200         listRecords.addRecord(record);
201         return listRecords;
202     }
203 
204 }