1 package org.kuali.ole.describe.controller; 2 3 import org.apache.commons.lang.StringUtils; 4 import org.apache.log4j.Logger; 5 import org.kuali.ole.BibliographicRecordHandler; 6 import org.kuali.ole.describe.bo.EditorFormDataHandler; 7 import org.kuali.ole.describe.form.EditorForm; 8 import org.kuali.ole.describe.form.WorkBibMarcForm; 9 import org.kuali.ole.docstore.model.bo.WorkBibDocument; 10 import org.kuali.ole.editor.bo.MarcEditorControlField; 11 import org.kuali.ole.editor.bo.MarcEditorDataField; 12 import org.kuali.ole.editor.bo.MarcEditorFormDataHandler; 13 import org.kuali.ole.editor.service.DocstoreHelperService; 14 import org.kuali.ole.pojo.bib.BibliographicRecord; 15 import org.kuali.ole.pojo.bib.Collection; 16 import org.kuali.rice.krad.uif.UifParameters; 17 import org.kuali.rice.krad.web.form.UifFormBase; 18 import org.springframework.stereotype.Controller; 19 import org.springframework.validation.BindingResult; 20 import org.springframework.web.bind.annotation.ModelAttribute; 21 import org.springframework.web.bind.annotation.RequestMapping; 22 import org.springframework.web.servlet.ModelAndView; 23 24 import javax.servlet.http.HttpServletRequest; 25 import javax.servlet.http.HttpServletResponse; 26 import java.util.ArrayList; 27 import java.util.HashMap; 28 import java.util.List; 29 import java.util.Map; 30 31 /** 32 * Class for handling all editor operations for WorkBibMarc documents. 33 */ 34 public class WorkBibMarcEditor implements 35 DocumentEditor { 36 37 private static final Logger LOG = Logger.getLogger(WorkBibMarcEditor.class); 38 39 private DocstoreHelperService docstoreHelperService; 40 private MarcEditorFormDataHandler marcEditorFormDataHandler; 41 42 private static WorkBibMarcEditor workBibMarcEditor = new WorkBibMarcEditor(); 43 44 public static WorkBibMarcEditor getInstance() { 45 return workBibMarcEditor; 46 } 47 48 private WorkBibMarcEditor() { 49 } 50 51 @Override 52 public EditorForm loadDocument(EditorForm editorForm) { 53 WorkBibMarcForm workBibMarcForm = new WorkBibMarcForm(); 54 String docId = editorForm.getDocId(); 55 if (null != docId) { 56 try { 57 // Read the document details from docstore. 58 String docStoreData = getDocstoreHelperService().getDocstoreData(docId); 59 Collection bibliographicRecords = new BibliographicRecordHandler().fromXML(docStoreData); 60 List<BibliographicRecord> bibliographicRecordList = bibliographicRecords.getRecords(); 61 BibliographicRecord bibliographicRecord = bibliographicRecordList.get(0); 62 workBibMarcForm.setLeader(bibliographicRecord.getLeader()); 63 workBibMarcForm.setControlFields( 64 getMarcFormDataHandler().buildMarcEditorControlFields(bibliographicRecord.getControlfields())); 65 workBibMarcForm.setDataFields( 66 getMarcFormDataHandler().buildMarcEditorDataFields(bibliographicRecord.getDatafields())); 67 68 workBibMarcForm.setMessage("Please edit details for the Bib record."); 69 } 70 catch (Exception e) { 71 String message = "Error: Could not load Bib record for id:" + docId; 72 LOG.error(message, e); 73 workBibMarcForm.setMessage(message + "Exception:" + e.getMessage()); 74 } 75 } 76 else { 77 workBibMarcForm.setMessage("Please enter details for new Bib record."); 78 } 79 // Add a node for this document to the left pane tree. 80 addDocumentToTree(editorForm); 81 82 workBibMarcForm.setViewId("WorkBibEditorViewPage"); 83 return workBibMarcForm; 84 } 85 86 @Override 87 public EditorForm saveDocument(EditorForm editorForm) { 88 return new WorkBibMarcForm(); 89 90 } 91 92 @Override 93 public EditorForm deleteDocument(EditorForm editorForm) { 94 return new WorkBibMarcForm(); 95 } 96 97 private void addDocumentToTree(EditorForm editorForm) { 98 // TODO: Make sure the same document is not added more than once. 99 List<WorkBibDocument> workBibDocumentList = editorForm.getWorkBibDocumentList(); 100 if (null == workBibDocumentList) { 101 workBibDocumentList = new ArrayList<WorkBibDocument>(); 102 } 103 if (null == editorForm.getDocId()) { 104 WorkBibDocument workBibDocument = new WorkBibDocument(); 105 workBibDocument.setTitle("New Bib1"); 106 workBibDocumentList.add(workBibDocument); 107 } 108 else { 109 WorkBibDocument workBibDocument = new WorkBibDocument(); 110 workBibDocument.setId(editorForm.getDocId()); 111 DocstoreHelperService docstoreHelperService = new DocstoreHelperService(); 112 workBibDocument = docstoreHelperService.getInfoForBibTree(workBibDocument); 113 workBibDocumentList.add(workBibDocument); 114 } 115 editorForm.setWorkBibDocumentList(workBibDocumentList); 116 } 117 118 /** 119 * This method will return instance of MarcEditorForm 120 * @param request 121 * @return MarcEditorForm 122 */ 123 124 /* 125 @Override 126 protected WorkBibMarcForm createInitialForm(HttpServletRequest request) { 127 return new WorkBibMarcForm(); 128 } 129 */ 130 131 /** 132 * This method takes the initial request when click on MarcEditor Screen. 133 * @param form 134 * @param result 135 * @param request 136 * @param response 137 * @return ModelAndView 138 */ 139 /* 140 @Override 141 @RequestMapping(params = "methodToCall=start") 142 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, 143 HttpServletRequest request, HttpServletResponse response) { 144 WorkBibMarcForm marcEditorForm = (WorkBibMarcForm) form; 145 ModelAndView modelAndView = super.start(form,result,request,response); 146 return getUIFModelAndView(marcEditorForm, "WorkBibEditorViewPage"); 147 } 148 */ 149 150 /** 151 * This method will add the controlField record based on the selected Line index and updates the component. 152 * @param form 153 * @param result 154 * @param request 155 * @param response 156 * @return ModelAndView 157 */ 158 /* 159 @RequestMapping(params = "methodToCall=addControlField") 160 public ModelAndView addControlField(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, 161 HttpServletRequest request, HttpServletResponse response) { 162 //WorkBibForm marcEditorForm = (WorkBibForm) form; 163 WorkBibMarcForm marcEditorForm = (WorkBibMarcForm) form; 164 int index = Integer.parseInt(form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX)); 165 index++; 166 List<MarcEditorControlField> marcEditorControlFieldList = marcEditorForm.getControlFields(); 167 marcEditorControlFieldList.add(index, new MarcEditorControlField()); 168 return super.updateComponent(marcEditorForm, result, request, response); 169 } 170 */ 171 172 /** 173 * This method will remove the controlField based on the index position and updates the component. 174 * @param form 175 * @param result 176 * @param request 177 * @param response 178 * @return ModelAndView 179 */ 180 /* 181 @RequestMapping(params = "methodToCall=removeControlField") 182 public ModelAndView removeControlField(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, 183 HttpServletRequest request, HttpServletResponse response) { 184 // WorkBibForm marcEditorForm = (WorkBibForm) form; 185 WorkBibMarcForm marcEditorForm = (WorkBibMarcForm) form; 186 int index = Integer.parseInt(form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX)); 187 List<MarcEditorControlField> editorControlFieldList = marcEditorForm.getControlFields(); 188 if (editorControlFieldList.size() > 1) 189 editorControlFieldList.remove(index); 190 return super.updateComponent(marcEditorForm, result, request, response); 191 } 192 */ 193 194 /** 195 * This method will add the datField record based on the selected Line index and updates the component. 196 * @param form 197 * @param result 198 * @param request 199 * @param response 200 * @return ModelAndView 201 */ 202 /* 203 @RequestMapping(params = "methodToCall=addDataField") 204 public ModelAndView addDataField(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, 205 HttpServletRequest request, HttpServletResponse response) { 206 // WorkBibForm marcEditorForm = (WorkBibForm) form; 207 WorkBibMarcForm marcEditorForm = (WorkBibMarcForm) form; 208 int index = Integer.parseInt(form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX)); 209 index++; 210 List<MarcEditorDataField> editorDataFieldList = marcEditorForm.getDataFields(); 211 editorDataFieldList.add(index, new MarcEditorDataField()); 212 return super.updateComponent(marcEditorForm, result, request, response); 213 } 214 */ 215 216 /** 217 * This method will remove the dataField based on the index position and updates the component. 218 * @param form 219 * @param result 220 * @param request 221 * @param response 222 * @return ModelAndView 223 */ 224 /* 225 @RequestMapping(params = "methodToCall=removeDataField") 226 public ModelAndView removeDataField(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, 227 HttpServletRequest request, HttpServletResponse response) { 228 // WorkBibForm marcEditorForm = (WorkBibForm) form; 229 WorkBibMarcForm marcEditorForm = (WorkBibMarcForm) form; 230 int index = Integer.parseInt(form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX)); 231 List<MarcEditorDataField> marcEditorDataFieldList = marcEditorForm.getDataFields(); 232 if (marcEditorDataFieldList.size() > 1) 233 marcEditorDataFieldList.remove(index); 234 return super.updateComponent(marcEditorForm, result, request, response); 235 } 236 */ 237 238 /** 239 * This method load the MarcEditor records based on uuid. 240 * 241 * @param form 242 * @param result 243 * @param request 244 * @param response 245 * @return ModelAndView 246 */ 247 /* 248 @RequestMapping(params = "methodToCall=loadMarcRecord") 249 public ModelAndView load(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, 250 HttpServletRequest request, HttpServletResponse response) { 251 WorkBibMarcForm marcEditorForm = (WorkBibMarcForm) form; 252 String existingUUID = request.getParameter("uuid"); 253 if (null == existingUUID || StringUtils.isEmpty(existingUUID)) { 254 existingUUID = marcEditorForm.getUuid(); 255 } 256 try { 257 marcEditorForm.setExisting("true"); 258 String docStoreData = getDocstoreHelperService().getDocstoreData(existingUUID); 259 Collection bibliographicRecords = new BibliographicRecordHandler().fromXML(docStoreData); 260 List<BibliographicRecord> bibliographicRecordList = bibliographicRecords.getRecords(); 261 BibliographicRecord bibliographicRecord = bibliographicRecordList.get(0); 262 marcEditorForm.setLeader(bibliographicRecord.getLeader()); 263 marcEditorForm.setControlFields(getMarcFormDataHandler().buildMarcEditorControlFields(bibliographicRecord.getControlfields())); 264 marcEditorForm.setDataFields(getMarcFormDataHandler().buildMarcEditorDataFields(bibliographicRecord.getDatafields())); 265 } catch (Exception e) { 266 e.printStackTrace(); 267 } 268 return getUIFModelAndView(marcEditorForm, "WorkBibEditorViewPage"); 269 } 270 */ 271 272 /** 273 * Gets the docstoreHelperService attribute. 274 * @return DocstoreHelperService 275 */ 276 private DocstoreHelperService getDocstoreHelperService() { 277 if (null == docstoreHelperService) { 278 return new DocstoreHelperService(); 279 } 280 return docstoreHelperService; 281 } 282 283 /** 284 * Gets the MarcEditorFormDataHandler attribute. 285 * @return MarcEditorFormDataHandler 286 */ 287 private MarcEditorFormDataHandler getMarcFormDataHandler() { 288 if (null == marcEditorFormDataHandler) { 289 marcEditorFormDataHandler = new MarcEditorFormDataHandler(); 290 } 291 return marcEditorFormDataHandler; 292 } 293 }