View Javadoc

1   package org.kuali.ole.describe.controller;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.cxf.helpers.IOUtils;
5   import org.apache.log4j.Logger;
6   import org.apache.solr.client.solrj.SolrServerException;
7   import org.kuali.ole.OLEConstants;
8   import org.kuali.ole.OleEditorResponseHandler;
9   import org.kuali.ole.describe.bo.*;
10  import org.kuali.ole.describe.form.*;
11  import org.kuali.ole.describe.service.DiscoveryHelperService;
12  import org.kuali.ole.describe.service.DocstoreHelperService;
13  import org.kuali.ole.docstore.discovery.service.QueryService;
14  import org.kuali.ole.docstore.discovery.service.QueryServiceImpl;
15  import org.kuali.ole.docstore.model.bo.*;
16  import org.kuali.ole.docstore.model.enums.DocCategory;
17  import org.kuali.ole.docstore.model.enums.DocFormat;
18  import org.kuali.ole.docstore.model.enums.DocType;
19  import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
20  import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
21  import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
22  import org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.*;
23  import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.Extension;
24  import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
25  import org.kuali.ole.docstore.model.xstream.ingest.ResponseHandler;
26  import org.kuali.ole.describe.bo.InstanceEditorFormDataHandler;
27  import org.kuali.ole.describe.form.InstanceEditorForm;
28  import org.kuali.ole.describe.service.DiscoveryHelperService;
29  import org.kuali.ole.describe.service.DocstoreHelperService;
30  import org.kuali.ole.docstore.model.xstream.work.oleml.WorkEHoldingOlemlRecordProcessor;
31  import org.kuali.ole.docstore.model.xstream.work.oleml.WorkEInstanceOlemlRecordProcessor;
32  import org.kuali.ole.pojo.OleBibRecord;
33  import org.kuali.ole.pojo.OleEditorResponse;
34  import org.kuali.ole.select.businessobject.OleCopy;
35  import org.kuali.ole.select.document.OLEEResourceRecordDocument;
36  import org.kuali.ole.select.service.impl.OleExposedWebServiceImpl;
37  import org.kuali.ole.service.OLEEResourceSearchService;
38  import org.kuali.ole.service.impl.OLEEResourceSearchServiceImpl;
39  import org.kuali.ole.sys.context.SpringContext;
40  import org.kuali.rice.core.api.exception.RiceRuntimeException;
41  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
42  import org.kuali.rice.kim.api.identity.Person;
43  import org.kuali.rice.kim.api.identity.PersonService;
44  import org.kuali.rice.krad.service.*;
45  import org.kuali.rice.krad.util.GlobalVariables;
46  import org.kuali.rice.krad.util.KRADConstants;
47  import org.kuali.rice.krad.web.form.UifFormBase;
48  import org.springframework.validation.BindingResult;
49  import org.springframework.web.bind.annotation.ModelAttribute;
50  import org.springframework.web.servlet.ModelAndView;
51  
52  import javax.servlet.http.HttpServletRequest;
53  import javax.servlet.http.HttpServletResponse;
54  import java.sql.Date;
55  import java.util.ArrayList;
56  import java.util.HashMap;
57  import java.util.List;
58  import java.util.Map;
59  
60  /**
61   * Created with IntelliJ IDEA.
62   * User: pp7788
63   * Date: 12/11/12
64   * Time: 2:38 PM
65   * To change this template use File | Settings | File Templates.
66   */
67  
68  /**
69   * InstanceEditorController is the controller class for Instance Editor
70   */
71  //@Controller
72  //@RequestMapping(value = "/instanceeditor")
73  public class WorkEInstanceOlemlEditor
74          extends AbstractEditor
75          implements DocumentEditor {
76  
77      private static final Logger LOG = Logger.getLogger(WorkEInstanceOlemlEditor.class);
78  
79      private DocstoreHelperService docstoreHelperService;
80      private InstanceEditorFormDataHandler instanceEditorFormDataHandler;
81      private EInstanceFormDataHandler eInstanceFormDataHandler;
82      private DiscoveryHelperService discoveryHelperService;
83      private OLEEResourceSearchService oleEResourceSearchService;
84      private DocumentService documentService;
85  
86  
87      private static WorkEInstanceOlemlEditor workEInstanceOlemlEditor = new WorkEInstanceOlemlEditor();
88  
89      public static WorkEInstanceOlemlEditor getInstance() {
90          return workEInstanceOlemlEditor;
91      }
92  
93      public OLEEResourceSearchService getOleEResourceSearchService() {
94          if (oleEResourceSearchService == null) {
95              oleEResourceSearchService = GlobalResourceLoader.getService(OLEConstants.OLEEResourceRecord.ERESOURSE_SEARCH_SERVICE);
96          }
97          return oleEResourceSearchService;
98      }
99  
100     public DocumentService getDocumentService() {
101         if (this.documentService == null) {
102             this.documentService = KRADServiceLocatorWeb.getDocumentService();
103         }
104         return this.documentService;
105     }
106 
107     public void setDocumentService(DocumentService documentService) {
108         this.documentService = documentService;
109     }
110 
111     private WorkEInstanceOlemlEditor() {
112     }
113 
114     @Override
115     public EditorForm copy(EditorForm editorForm) {
116         loadDocument(editorForm);
117         editorForm.setDocId(null);
118         editorForm.setShowEditorFooter(false);
119         editorForm.setFromSearch(false);
120 
121         //editorForm.setMessage("Record copied successfully");
122         GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO, "info.einstance.copy.success");
123         return editorForm;
124     }
125 
126     @Override
127     public EditorForm loadDocument(EditorForm editorForm) {
128         WorkEInstanceOlemlForm workEInstanceOlemlForm = new WorkEInstanceOlemlForm();
129         WorkEInstanceOlemlRecordProcessor workEInstanceOlemlRecordProcessor = new WorkEInstanceOlemlRecordProcessor();
130         WorkEHoldingOlemlRecordProcessor workEHoldingOlemlRecordProcessor = new WorkEHoldingOlemlRecordProcessor();
131         String docId = editorForm.getDocId();
132         String bibId = editorForm.getBibId();
133         String tokenId = editorForm.getTokenId();
134         workEInstanceOlemlForm.setTokenId(tokenId);
135         workEInstanceOlemlForm.setBibId(bibId);
136         String docType = editorForm.getDocType();
137         editorForm.setHasLink(true);
138         if ((StringUtils.isBlank(editorForm.getTitle()))) {
139             getTitleInfo(editorForm);
140         }
141         if (editorForm.isFromDublin()) {
142             editorForm.setHasLink(false);
143         }
144         if (editorForm.getTitle() != null && editorForm.getTitle().contains("<")) {
145             String title = org.apache.commons.lang.StringEscapeUtils.escapeHtml((editorForm.getTitle()));
146             editorForm.setTitle(title);
147         }
148         editorForm.setShowEditorFooter(false);
149         editorForm.setFromSearch(false);
150         String eResId = null;
151         workEInstanceOlemlForm.seteResourceId(editorForm.geteResourceId());
152         if (docId != null && docId.length() > 0) {
153             if (docType.equals(DocType.EINSTANCE.getCode())) {
154                 try {
155                     //String eResId = editorForm.geteResourceId();
156                     String docStoreData = getDocstoreHelperService().getDocstoreData(docId);
157                     InstanceCollection instanceCollection = workEInstanceOlemlRecordProcessor.fromXML(docStoreData);
158                     EInstance eInstance = instanceCollection.getEInstance().get(0);
159                     EHoldings eHoldings = eInstance.getEHoldings();
160                     if (eResId == null || eResId.length() == 0) {
161                         eResId = eHoldings.getEResourceId();
162                     }
163                     /*Map copyMap = new HashMap();
164                     copyMap.put(OLEConstants.INSTANCE_ID, eInstance.getInstanceIdentifier());
165                     List<OleCopy> copyList = (List<OleCopy>) KRADServiceLocator.getBusinessObjectService().findMatching(OleCopy.class, copyMap);
166                     if (copyList.size() > 0) {
167                         for (OleCopy oleCopy : copyList) {
168                             eResId=oleCopy.getOleERSIdentifier();
169                         }
170                     }*/
171                     getOleEResourceSearchService().getAccessLocationFromEInstance(eHoldings, workEInstanceOlemlForm);
172                     getOleEResourceSearchService().getEResourcesFields(eResId, eHoldings, workEInstanceOlemlForm);
173                     getOleEResourceSearchService().getEResourcesLicenseFields(eResId, workEInstanceOlemlForm);
174                     workEInstanceOlemlForm.setSelectedEInstance(eInstance);
175                     editorForm.setDocId(eInstance.getEHoldings().getHoldingsIdentifier());
176                     editorForm.setDocType(DocType.EHOLDINGS.getCode());
177                     workEInstanceOlemlForm.setInstanceId(docId);
178                     workEInstanceOlemlForm.setSelectedEHoldings(eHoldings);
179                     workEInstanceOlemlForm.getSelectedEHoldings().setStatusDate(new Date(System.currentTimeMillis()).toString());
180                 } catch (Exception e) {
181                     //e.printStackTrace();
182                     LOG.error("loadDocument EInstance Exception:" + e);
183                     //workEInstanceOlemlForm.setMessage("Fail to load Record");
184                     GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO, "record.load.fail.message ");
185                     return workEInstanceOlemlForm;
186                 }
187             } else {
188                 try {
189                     String docStoreData = getDocstoreHelperService().getDocstoreData(docId);
190                     EHoldings eHoldings = workEHoldingOlemlRecordProcessor.fromXML(docStoreData);
191                     if (eResId == null || eResId.length() == 0) {
192                         eResId = eHoldings.getEResourceId();
193                     }
194                     /*Map copyMap = new HashMap();
195                     copyMap.put(OLEConstants.INSTANCE_ID, workEInstanceOlemlForm.getInstanceId());
196                     List<OleCopy> copyList = (List<OleCopy>) KRADServiceLocator.getBusinessObjectService().findMatching(OleCopy.class, copyMap);
197                     if (copyList.size() > 0) {
198                         for (OleCopy oleCopy : copyList) {
199                             eResId=oleCopy.getOleERSIdentifier();
200                         }
201                     }*/
202                     getOleEResourceSearchService().getAccessLocationFromEInstance(eHoldings, workEInstanceOlemlForm);
203                     getOleEResourceSearchService().getEResourcesFields(eResId, eHoldings, workEInstanceOlemlForm);
204                     getOleEResourceSearchService().getEResourcesLicenseFields(eResId, workEInstanceOlemlForm);
205                     workEInstanceOlemlForm.setSelectedEHoldings(eHoldings);
206                     workEInstanceOlemlForm.getSelectedEHoldings().setStatusDate(new Date(System.currentTimeMillis()).toString());
207                 } catch (Exception e) {
208                     //e.printStackTrace();
209                     LOG.error("loadDocument Instance Exception:" + e);
210                     //workEInstanceOlemlForm.setMessage("Fail to load Record");
211                     GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO, "record.load.fail.message ");
212                     return workEInstanceOlemlForm;
213                 }
214 
215             }
216             getEInstanceFormDataHandler().setLocationDetails(workEInstanceOlemlForm);
217             //workEInstanceOlemlForm.setMessage("Record loaded successfully.");
218             GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO, "record.load.message");
219         } else {
220             EHoldings eHoldings = new EHoldings();
221             eHoldings.setStatusDate(new Date(System.currentTimeMillis()).toString());
222             getOleEResourceSearchService().getAccessLocationFromEInstance(eHoldings, workEInstanceOlemlForm);
223             getOleEResourceSearchService().getEResourcesFields(editorForm.geteResourceId(), eHoldings, workEInstanceOlemlForm);
224             getOleEResourceSearchService().getEResourcesLicenseFields(editorForm.geteResourceId(), workEInstanceOlemlForm);
225             workEInstanceOlemlForm.setSelectedEHoldings(eHoldings);
226             workEInstanceOlemlForm.setViewId("WorkEInstanceViewPage");
227             //workEInstanceOlemlForm.setMessage("Please enter details for new Holding record.");
228             GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO, "eholdings.record.new.load.message");
229         }
230         workEInstanceOlemlForm.setViewId("WorkEInstanceViewPage");
231         List<WorkBibDocument> workBibDocumentList = new ArrayList<>();
232         DocstoreHelperService docstoreHelperService = new DocstoreHelperService();
233         WorkBibDocument workBibDocument = new WorkBibDocument();
234         workBibDocument.setId(editorForm.getBibId());
235         workBibDocument = docstoreHelperService.getInfoForBibTree(workBibDocument);
236         workBibDocumentList.add(workBibDocument);
237         editorForm.setWorkBibDocumentList(workBibDocumentList);
238         workEInstanceOlemlForm.setIssn(workBibDocument.getIssn());
239         editorForm.setHeaderText("EInstance Editor (EHoldings) - OLEML Format");
240         if (workEInstanceOlemlForm.getTokenId() != null && !workEInstanceOlemlForm.getTokenId().isEmpty()) {
241             editorForm.setTokenId(workEInstanceOlemlForm.getTokenId());
242         }
243         return workEInstanceOlemlForm;
244     }
245 
246     private void getTitleInfo(EditorForm editorForm) {
247         WorkBibDocument workBibDocument = new WorkBibDocument();
248         workBibDocument.setId(editorForm.getBibId());
249         QueryService queryService = QueryServiceImpl.getInstance();
250         WorkBibDocument document = null;
251         try {
252             document = queryService.buildBibDocumentInfo(workBibDocument);
253         } catch (Exception e) {
254             LOG.error("Failed to get Title / Author details : " + e.getMessage());
255         }
256         StringBuilder titleBuilder = new StringBuilder();
257         if (document != null) {
258             titleBuilder.append(document.getTitle());
259             titleBuilder.append("/");
260             if (document.getAuthor() != null) {
261 
262                 titleBuilder.append(document.getAuthor());
263             }
264             if (workBibDocument.getDocFormat().equalsIgnoreCase("dublinunq")) {
265                 editorForm.setFromDublin(true);
266             }
267             editorForm.setTitle(org.apache.commons.lang.StringEscapeUtils.escapeHtml(titleBuilder.toString()));
268         }
269     }
270 
271     @Override
272     public EditorForm saveDocument(EditorForm editorForm) {
273         //        WorkInstanceOlemlForm workInstanceOlemlForm = (WorkInstanceOlemlForm)loadDocument(editorForm);
274 
275         WorkEInstanceOlemlForm workEInstanceOlemlForm = (WorkEInstanceOlemlForm) editorForm.getDocumentForm();
276         String responseFromDocstore = null;
277         String docId = editorForm.getDocId();
278         String docType = editorForm.getDocType();
279         String editorStatusMessage = "";
280         // Boolean staffOnlyFlag=editorForm.isStaffOnlyFlag();
281         AdditionalAttributes additionalAttributes = new AdditionalAttributes();
282         //boolean valid = validateRequiredFields(workEInstanceOlemlForm);
283         if (docId != null && docId.length() > 0) {
284             EHoldings eHoldings = workEInstanceOlemlForm.getSelectedEHoldings();
285             getOleEResourceSearchService().getAccessLocationFromEInstance(eHoldings, workEInstanceOlemlForm);
286             String content = getEInstanceFormDataHandler().buildHoldingContent(eHoldings);
287             String responseXml = "";
288             try {
289                 getOleEResourceSearchService().getEResourcesFields(editorForm.geteResourceId(), eHoldings, workEInstanceOlemlForm);
290                 getOleEResourceSearchService().getEResourcesLicenseFields(editorForm.geteResourceId(), workEInstanceOlemlForm);
291                 responseXml = getDocstoreHelperService().updateInstanceRecord(docId, docType, content, additionalAttributes);
292                 Response response = new ResponseHandler().toObject(responseXml);
293                 if (response.getStatus().equalsIgnoreCase("success")) {
294                     workEInstanceOlemlForm.setDocId(response.getDocuments().get(0).getUuid());
295                     //workEInstanceOlemlForm.setMessage("Record updated successfully");
296                     editorStatusMessage = "Record updated successfully";
297                 } else {
298                     editorStatusMessage = response.getStatusMessage();
299                     //workEInstanceOlemlForm.setMessage(response.getStatusMessage());
300                 }
301             } catch (Exception e) {
302                 //e.printStackTrace();
303                 LOG.error("saveDocument(docId != null) Exception:" + e);
304                 //workEInstanceOlemlForm.setMessage("Fail to save Record");
305                 return workEInstanceOlemlForm;
306 
307             }
308         } else {
309             EInstance eInstance = new EInstance();
310             workEInstanceOlemlForm.setSelectedEInstance(eInstance);
311             eInstance.getResourceIdentifier().add(editorForm.getBibId());
312             EHoldings eHoldings = workEInstanceOlemlForm.getSelectedEHoldings();
313             getOleEResourceSearchService().getAccessLocationFromEInstance(eHoldings, workEInstanceOlemlForm);
314             /*if (valid) {*/
315             //getOleEResourceSearchService().getEResourcesFields(editorForm.geteResourceId(), eHoldings, workEInstanceOlemlForm);
316             //getOleEResourceSearchService().getEResourcesLicenseFields(editorForm.geteResourceId(), workEInstanceOlemlForm);
317             String content = getEInstanceFormDataHandler().buildEInstanceRecordForDocStore(workEInstanceOlemlForm);
318             String reqXml = getDocstoreHelperService().buildRequestForIngest(DocCategory.WORK.getCode(), DocType.EINSTANCE.getCode(), DocFormat.OLEML.getCode(), content, null);
319             String responseXml = null;
320             try {
321                 responseXml = getDocstoreHelperService().persistDocstore(reqXml, null);
322             } catch (Exception e) {
323                 // e.printStackTrace();
324                 LOG.error("saveDocument(docId == null) Exception:" + e);
325                 // workEInstanceOlemlForm.setMessage("Fail to save Record");
326                 return workEInstanceOlemlForm;
327             }
328             Response response = new ResponseHandler().toObject(responseXml);
329             if (response.getStatus().equalsIgnoreCase("success")) {
330                 editorForm.setInstanceId(response.getDocuments().get(0).getUuid());
331                 workEInstanceOlemlForm.setInstanceId(response.getDocuments().get(0).getUuid());
332                 editorForm.setDocId(response.getDocuments().get(0).getLinkedDocuments().get(0).getUuid());
333                 editorForm.setDocType(DocType.EHOLDINGS.getCode());
334                 //workEInstanceOlemlForm.setMessage("Record created successfully");
335                 editorStatusMessage = "Record created successfully";
336                 processResponse(workEInstanceOlemlForm, editorForm.getInstanceId(), editorForm.getTokenId());
337             } else {
338                 editorStatusMessage = response.getStatusMessage();
339 //                workEInstanceOlemlForm.setMessage(response.getStatusMessage());
340             }
341             /*} else {
342                 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, "einstance.editor.required.message");
343             }*/
344         }
345         getEInstanceFormDataHandler().setLocationDetails(workEInstanceOlemlForm);
346         /*if (valid) {*/
347         String eResId = editorForm.geteResourceId();
348         editorForm.seteResourceId(null);
349         WorkEInstanceOlemlForm newworkEInstanceOlemlForm = (WorkEInstanceOlemlForm) loadDocument(editorForm);
350         workEInstanceOlemlForm.setViewId("WorkEInstanceViewPage");
351         GlobalVariables.getMessageMap().getInfoMessages().clear();
352         GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO, "docstore.response", editorStatusMessage);
353         if (eResId != null && !eResId.isEmpty()) {
354             Map<String, String> tempId = new HashMap<String, String>();
355             tempId.put(OLEConstants.OLEEResourceRecord.ERESOURCE_IDENTIFIER, eResId);
356             OLEEResourceRecordDocument tempDocument = (OLEEResourceRecordDocument) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OLEEResourceRecordDocument.class, tempId);
357             try {
358                 Person principalPerson = SpringContext.getBean(PersonService.class).getPerson(GlobalVariables.getUserSession().getPerson().getPrincipalId());
359                 tempDocument.setDocumentHeader(SpringContext.getBean(DocumentHeaderService.class).getDocumentHeaderById(tempDocument.getDocumentNumber()));
360                 tempDocument.getDocumentHeader().setWorkflowDocument(KRADServiceLocatorWeb.getWorkflowDocumentService().loadWorkflowDocument(tempDocument.getDocumentNumber(), principalPerson));
361                 if (tempDocument != null) {
362                     try {
363                         tempDocument.setSelectInstance(OLEConstants.OLEEResourceRecord.CREATE_NEW_INSTANCE);
364                         tempDocument.seteInstanceFlag(true);
365                         getOleEResourceSearchService().getNewInstance(tempDocument, tempDocument.getDocumentNumber());
366                         getDocumentService().updateDocument(tempDocument);
367                     } catch (Exception e) {
368                         throw new RiceRuntimeException(
369                                 "Exception trying to save document: " + tempDocument
370                                         .getDocumentNumber(), e);
371                     }
372                 }
373             } catch (Exception e) {
374                 throw new RiceRuntimeException(
375                         "Exception trying to save document: " + tempDocument
376                                 .getDocumentNumber(), e);
377             }
378         }
379 //        workEInstanceOlemlForm.setMessage(editorStatusMessage);
380         /*}*/
381         return workEInstanceOlemlForm;
382     }
383 
384 
385     public void processResponse(WorkEInstanceOlemlForm workEInstanceOlemlForm, String instanceId, String tokenId) {
386         OleEditorResponse oleEditorResponse = new OleEditorResponse();
387         OleBibRecord oleBibRecord = new OleBibRecord();
388         oleBibRecord.setLinkedInstanceId(instanceId);
389         oleBibRecord.setBibUUID(workEInstanceOlemlForm.getBibId());
390         oleEditorResponse.setOleBibRecord(oleBibRecord);
391         oleEditorResponse.setTokenId(tokenId);
392         oleBibRecord.seteInstance(OLEConstants.E_INSTANCE);
393         OleEditorResponseHandler oleEditorResponseHandler = new OleEditorResponseHandler();
394         String editorResponseXMLForOLE = oleEditorResponseHandler.toXML(oleEditorResponse);
395         OleExposedWebServiceImpl oleExposedWebService = (OleExposedWebServiceImpl) SpringContext.getBean("oleExposedWebService");
396         oleExposedWebService.addDoctoreResponse(editorResponseXMLForOLE);
397     }
398 
399     @Override
400     public EditorForm deleteDocument(EditorForm editorForm) {
401         return new WorkBibMarcForm();
402     }
403 
404     @Override
405     public EditorForm createNewRecord(EditorForm editorForm, Request request) {
406 
407         if (editorForm.getDocumentForm().getViewId().equalsIgnoreCase("WorkEInstanceViewPage")) {
408             editorForm.setNeedToCreateInstance(true);
409         }
410         editNewRecord(editorForm, request);
411         return editorForm.getDocumentForm();
412     }
413 
414 
415     @Override
416     public EditorForm editNewRecord(EditorForm editorForm, Request request) {
417         WorkEInstanceOlemlForm workEInstanceOlemlForm = new WorkEInstanceOlemlForm();
418         if ((editorForm.getDocumentForm() instanceof WorkInstanceOlemlForm)) {
419             workEInstanceOlemlForm = (WorkEInstanceOlemlForm) editorForm.getDocumentForm();
420             workEInstanceOlemlForm.setViewId(editorForm.getDocumentForm().getViewId());
421         }
422 
423         workEInstanceOlemlForm.setDocCategory("work");
424         workEInstanceOlemlForm.setDocType("eInstance");
425         workEInstanceOlemlForm.setDocFormat("oleml");
426 
427         /*if (request.getRequestDocuments() != null && request.getRequestDocuments().size() > 0) {
428             for (RequestDocument reqDoc : request.getRequestDocuments()) {
429                 for (RequestDocument linkedReqDoc : reqDoc.getLinkedRequestDocuments()) {
430                     if (linkedReqDoc != null && linkedReqDoc.getContent() != null
431                         && linkedReqDoc.getContent().getContent() != null) {
432                         String instContent = linkedReqDoc.getContent().getContent();
433                         org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.InstanceCollection instanceCollection = new WorkEInstanceOlemlRecordProcessor()
434                                 .fromXML(instContent);
435                         if (editorForm.getDocumentForm().getViewId().equalsIgnoreCase("WorkHoldingsViewPage")) {
436                             editorForm.setHeaderText("Import Bib Step-4 Instance Editor (Item)- OLEML Format");
437                             // validate user entered holding data before going to item tab
438                             if(!isValidHoldingsData(workEInstanceOlemlForm)){
439                                 return workInstanceOlemlForm;
440                             }
441                             instanceCollection.getInstance().get(0)
442                                               .setOleHoldings(workInstanceOlemlForm.getSelectedHolding());
443                             WorkInstanceOlemlRecordProcessor olemlReocordProcessor
444                                     = new WorkInstanceOlemlRecordProcessor();
445                             String holContent = olemlReocordProcessor.toXML(instanceCollection);
446                             linkedReqDoc.getContent().setContent(holContent);
447                             List<RequestDocument> reqDocList = new ArrayList<RequestDocument>();
448                             reqDocList.add(linkedReqDoc);
449                             reqDoc.setLinkedRequestDocuments(reqDocList);
450                             workInstanceOlemlForm.setSelectedItem(
451                                     instanceCollection.getInstance().get(0).getItems().getItem().get(0));
452                             workInstanceOlemlForm.setViewId("WorkItemViewPage");
453                             workInstanceOlemlForm.setMessage("Please enter details for new Item record.");
454                         }
455                         else if (editorForm.getDocumentForm().getViewId().equalsIgnoreCase("WorkBibEditorViewPage")) {
456                             editorForm.setHeaderText("Import Bib Step-4 Instance Editor (Holdings)- OLEML Format");
457                             workInstanceOlemlForm.setViewId("WorkHoldingsViewPage");
458                             workInstanceOlemlForm.setMessage("Please enter details for new Holdings record.");
459                             workInstanceOlemlForm
460                                     .setSelectedHolding(instanceCollection.getInstance().get(0).getOleHoldings());
461                         }
462                         else if (editorForm.getDocumentForm().getViewId().equalsIgnoreCase("WorkItemViewPage")) {
463                             if(!isValidItemData(workInstanceOlemlForm)){
464                                 return workInstanceOlemlForm;
465                             }
466                             String instanceCol = request.getRequestDocuments().get(0).getLinkedRequestDocuments().get(0)
467                                                         .getContent().getContent();
468                             WorkInstanceOlemlRecordProcessor recProcessor = new WorkInstanceOlemlRecordProcessor();
469                             InstanceCollection instCol = recProcessor.fromXML(instanceCol);
470                             Item item = workInstanceOlemlForm.getSelectedItem();
471                             try {
472                                 String holdingXmlContent = getInstanceEditorFormDataHandler()
473                                         .buildHoldingContent(workInstanceOlemlForm.getSelectedHolding());
474                                 WorkHoldingOlemlRecordProcessor holdingRecordProcessor
475                                         = new WorkHoldingOlemlRecordProcessor();
476                                 OleHoldings holding = holdingRecordProcessor.fromXML(holdingXmlContent);
477                                 instCol.getInstance().get(0).setOleHoldings(holding);
478                                 String itemXmlContent = getInstanceEditorFormDataHandler().buildItemContent(item);
479                                 WorkItemOlemlRecordProcessor itemProcessor = new WorkItemOlemlRecordProcessor();
480                                 item = itemProcessor.fromXML(itemXmlContent);
481                             }
482                             catch (Exception e) {
483                                 StringBuffer instanceEditorErrorMessage = new StringBuffer(
484                                         OLEConstants.INSTANCE_EDITOR_FAILURE).append(" :: \n" + e.getMessage());
485                                 workInstanceOlemlForm.setMessage(instanceEditorErrorMessage.toString());
486                                 LOG.error(instanceEditorErrorMessage);
487                             }
488                             List<Item> items = new ArrayList<Item>();
489                             items.add(item);
490                             instCol.getInstance().get(0).getItems().setItem(items);
491                             instanceCol = recProcessor.toXML(instCol);
492                             request.getRequestDocuments().get(0).getLinkedRequestDocuments().get(0).getContent()
493                                    .setContent(instanceCol);
494                         }
495 
496                     }
497                 }
498 
499             }
500 
501         }*/
502         editorForm.setDocumentForm(workEInstanceOlemlForm);
503         return editorForm;
504     }
505 
506     /* @Override
507     public String saveDocument(Request req, EditorForm editorForm) {
508         String responseFromDocstore = null;
509         String editorStatusMessage = "";
510         Response dsResponse = new Response();
511         WorkInstanceOlemlForm workInstanceOlemlForm = (WorkInstanceOlemlForm) editorForm.getDocumentForm();
512         if (req != null) {
513             RequestHandler reqHandler = new RequestHandler();
514             String content = reqHandler.toXML(req);
515             try {
516                 responseFromDocstore = getDocstoreHelperService()
517                         .persistToDocstoreFromUnifiedEditor(content, null, null);
518                 dsResponse = new ResponseHandler().toObject(responseFromDocstore);
519                 // Validation messages in editor screen when a record is failed to submit.
520                 if (Response.FAILURE.equalsIgnoreCase(dsResponse.getStatus())) {
521                     getInstanceEditorFormDataHandler().setLocationDetails(workInstanceOlemlForm);
522                     editorStatusMessage = "<font size='3' color='red'>" + dsResponse.getStatusMessage() + "</font>";
523                     workInstanceOlemlForm.setMessage(editorStatusMessage);
524                 }
525             }
526             catch (Exception e) {
527                 StringBuffer marcEditorErrorMessage = new StringBuffer(OLEConstants.MARC_EDITOR_FAILURE)
528                         .append("\n" + e.getMessage());
529                 //                responseFromDocstore = marcEditorErrorMessage.toString();
530                 workInstanceOlemlForm.setMessage(marcEditorErrorMessage.toString());
531 
532             }
533         }
534         editorForm.setDocumentForm(workInstanceOlemlForm);
535         return responseFromDocstore;
536     }*/
537 
538     /**
539      * This method creates a instance of InstanceEditorForm
540      *
541      * @param httpServletRequest
542      * @return InstanceEditorForm
543      */
544     protected UifFormBase createInitialForm(HttpServletRequest httpServletRequest) {
545         return new WorkInstanceOlemlForm();
546     }
547 
548     /**
549      * This method takes the initial request when click on InstanceEditor Screen.
550      *
551      * @param form
552      * @param result
553      * @param request
554      * @param response
555      * @return ModelAndView
556      */
557     public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
558                               HttpServletRequest request, HttpServletResponse response) {
559         WorkInstanceOlemlForm instanceEditorForm = (WorkInstanceOlemlForm) form;
560         //ModelAndView modelAndView = super.start(instanceEditorForm, result, request, response);
561         // return getUIFModelAndView(instanceEditorForm, "WorkHoldingsViewPage");
562         return null;
563     }
564 
565 
566     /**
567      * This method will fetch the holding/Item records based on the AdditionalAttributes.
568      *
569      * @param form
570      * @param result
571      * @param request
572      * @param response
573      * @return ModelAndView
574      */
575     public ModelAndView load(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
576                              HttpServletRequest request, HttpServletResponse response) {
577         WorkEInstanceOlemlForm workEInstanceOlemlForm = new WorkEInstanceOlemlForm();
578         workEInstanceOlemlForm.setHideFooter(false);
579         workEInstanceOlemlForm.setViewId("WorkEInstanceViewPage");
580         workEInstanceOlemlForm.setMessage("Please enter details for new Item record.");
581         //EditorForm workInstanceOlemlForm = (WorkInstanceOlemlForm) form;
582         //WorkInstanceOlemlForm workInstanceOlemlForm = new WorkInstanceOlemlForm();
583         String uuid = request.getParameter("uuid");
584         String docType = request.getParameter("docType");
585         String docFormat = request.getParameter("docFormat");
586         String docCategory = request.getParameter("docCategory");
587         if (null == uuid || StringUtils.isEmpty(uuid)) {
588             uuid = workEInstanceOlemlForm.getUuid();
589         }
590         workEInstanceOlemlForm.setExisting("true");
591         try {
592               /*if (workEInstanceOlemlForm.getInstanceId() != null && workEInstanceOlemlForm.getInstanceId().length() > 0) {
593                 String docStoreData = getDocstoreHelperService().getDocstoreData(docCategory,docType,docFormat,workEInstanceOlemlForm.getInstanceId());
594                 InstanceCollection instanceCollection = new WorkEInstanceOlemlRecordProcessor().fromXML(docStoreData);
595                 workEInstanceOlemlForm.setSelectedEInstance(instanceCollection.getEInstance().get(0));
596             }
597             String docStoreData = getDocstoreHelperService().getDocstoreData(docCategory,docType,docFormat,uuid);
598             if (docType.equalsIgnoreCase("eHoldings")) {
599                 EHoldings oleHoldings = new WorkEHoldingOlemlRecordProcessor().fromXML(docStoreData);
600                 workEInstanceOlemlForm.setSelectedEHoldings(oleHoldings);
601                 //return getUIFModelAndView(workInstanceOlemlForm,"WorkHoldingsViewPage");
602             }
603             else {
604                 InstanceCollection instanceCollection = new WorkEInstanceOlemlRecordProcessor().fromXML(docStoreData);
605                 workEInstanceOlemlForm.setSelectedEHoldings(instanceCollection.getEInstance().get(0).getEHoldings());
606                 //return getUIFModelAndView(workInstanceOlemlForm,"WorkHoldingsViewPage");
607             }*/
608         } catch (Exception e) {
609             StringBuffer instanceEditorErrorMessage = new StringBuffer(OLEConstants.INSTANCE_EDITOR_LOAD_FAILURE)
610                     .append(" :: \n" + e.getMessage());
611             //workInstanceOlemlForm.setMessage(instanceEditorErrorMessage.toString());
612             LOG.error(instanceEditorErrorMessage);
613             //return getUIFModelAndView(workInstanceOlemlForm);
614         }
615         return null;
616     }
617 
618     /**
619      * Used for Test-case
620      *
621      * @param result
622      * @param request
623      * @param response
624      * @param instanceEditorForm
625      * @return ModelAndView
626      */
627     protected ModelAndView callSuper(BindingResult result, HttpServletRequest request, HttpServletResponse response,
628                                      InstanceEditorForm instanceEditorForm) {
629         //return super.updateComponent(instanceEditorForm, result, request, response);
630         return null;
631     }
632 
633     /**
634      * Gets the docstoreHelperService attribute
635      *
636      * @return Returns DocstoreHelperService
637      */
638     private DocstoreHelperService getDocstoreHelperService() {
639         if (null == docstoreHelperService) {
640             return new DocstoreHelperService();
641         }
642         return docstoreHelperService;
643     }
644 
645     /**
646      * Sets the docstoreHelperService attribute values.
647      *
648      * @param docstoreHelperService
649      */
650     public void setDocstoreHelperService(DocstoreHelperService docstoreHelperService) {
651         this.docstoreHelperService = docstoreHelperService;
652     }
653 
654     /**
655      * Gets the InstanceEditorFormDataHandler attribute.
656      *
657      * @return Returns InstanceEditorFormDataHandler.
658      */
659     private InstanceEditorFormDataHandler getInstanceEditorFormDataHandler() {
660         if (null == instanceEditorFormDataHandler) {
661             instanceEditorFormDataHandler = new InstanceEditorFormDataHandler();
662         }
663         return instanceEditorFormDataHandler;
664     }
665 
666     private EInstanceFormDataHandler getEInstanceFormDataHandler() {
667         if (null == eInstanceFormDataHandler) {
668             eInstanceFormDataHandler = new EInstanceFormDataHandler();
669         }
670         return eInstanceFormDataHandler;
671     }
672 
673 
674 //    /**
675 //     * This method returns AdditionalAttributes instance from the list of Extension.
676 //     * @param extensionList
677 //     * @return AdditionalAttributes
678 //     */
679 //    private AdditionalAttributes getFirstAdditionalAttributes(List<Extension> extensionList) {
680 //        for (Extension ext : extensionList) {
681 //            for (Object obj : ext.getContent()) {
682 //                if (obj instanceof AdditionalAttributes) {
683 //                    return (AdditionalAttributes) obj;
684 //                }
685 //            }
686 //        }
687 //        return null;
688 //    }
689 
690     /**
691      * Sets the discoveryHelperService attribute value.
692      *
693      * @param discoveryHelperService
694      */
695     public void setDiscoveryHelperService(DiscoveryHelperService discoveryHelperService) {
696         this.discoveryHelperService = discoveryHelperService;
697     }
698 
699     /**
700      * Gets the discoveryHelperService attribute.
701      *
702      * @return Returns DiscoveryHelperService.
703      */
704     public DiscoveryHelperService getDiscoveryHelperService() {
705         if (null == discoveryHelperService) {
706             return new DiscoveryHelperService();
707         }
708         return discoveryHelperService;
709     }
710 
711     /*@RequestMapping(params = "methodToCall=addExtentOfOwnership")
712     public ModelAndView addExtentOfOwnership(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
713                                              HttpServletRequest request, HttpServletResponse response) {
714         InstanceEditorForm instanceEditorForm = (InstanceEditorForm) form;
715         int index = Integer.parseInt(form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
716         index++;
717         List<ExtentOfOwnership> extentOfOwnershipForUI = instanceEditorForm.getSelectedHolding().getExtentOfOwnership();
718         extentOfOwnershipForUI.add(index, new ExtentOfOwnership());
719         return super.updateComponent(instanceEditorForm, result, request, response);
720     }
721 
722     @RequestMapping(params = "methodToCall=removeExtentOfOwnership")
723     public ModelAndView removeExtentOfOwnership(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
724                                                 HttpServletRequest request, HttpServletResponse response) {
725         InstanceEditorForm instanceEditorForm = (InstanceEditorForm) form;
726         int index = Integer.parseInt(form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
727         List<ExtentOfOwnership> extentOfOwnershipForUI = instanceEditorForm.getSelectedHolding().getExtentOfOwnership();
728         if (extentOfOwnershipForUI.size() > 1)
729             extentOfOwnershipForUI.remove(index);
730         return super.updateComponent(instanceEditorForm, result, request, response);
731     }*/
732 
733 
734     /**
735      * Shows the linked Bibs corresponging to Holdings/Item
736      *
737      * @param editorForm
738      * @return
739      */
740     @Override
741     public EditorForm showBibs(EditorForm editorForm) {
742         WorkEInstanceOlemlForm workEInstanceOlemlForm = new WorkEInstanceOlemlForm();
743         QueryService queryService = QueryServiceImpl.getInstance();
744         List<String> bibUUIDList = new ArrayList<String>();
745         String instanceId = "";
746         String docType = editorForm.getDocType();
747         List<WorkBibDocument> workBibDocumentList = new ArrayList<WorkBibDocument>();
748         try {
749 
750             if ((docType.equalsIgnoreCase(DocType.INSTANCE.getCode()) || docType
751                     .equalsIgnoreCase(DocType.HOLDINGS.getCode())) || (docType
752                     .equalsIgnoreCase(DocType.ITEM.getCode()))) {
753                 instanceId = editorForm.getInstanceId();
754             }
755             if (StringUtils.isNotEmpty(instanceId)) {
756                 bibUUIDList = queryService.queryForBibs(instanceId);
757             }
758             DocstoreHelperService docstoreHelperService = new DocstoreHelperService();
759             if (bibUUIDList != null && bibUUIDList.size() > 0) {
760                 for (String bibUUID : bibUUIDList) {
761                     WorkBibDocument workBibDocument = new WorkBibDocument();
762                     workBibDocument.setId(bibUUID);
763                     workBibDocument = docstoreHelperService.getInfoForBibTree(workBibDocument);
764                     workBibDocumentList.add(workBibDocument);
765                 }
766             }
767 
768             editorForm.setWorkBibDocumentList(workBibDocumentList);
769         } catch (SolrServerException e) {
770             LOG.error("Exception:" + e.getMessage(), e);
771         }
772         workEInstanceOlemlForm.setViewId("ShowBibViewPage");
773         return workEInstanceOlemlForm;
774     }
775 
776     @Override
777     public EditorForm delete(EditorForm editorForm) throws Exception {
778         WorkEInstanceOlemlForm workEInstanceOlemlForm = new WorkEInstanceOlemlForm();
779         String operation = "delete";
780         BusinessObjectService boService = KRADServiceLocator.getBusinessObjectService();
781         //Map<String, String> map = new HashMap<>();
782         //Map<String, String> uuidMap = new HashMap<>();
783         //map.put("bibId", editorForm.getBibId());
784         //List<OleCopy> listOfValues = (List<OleCopy>) boService.findMatching(OleCopy.class, map);
785 
786         //if (listOfValues != null && listOfValues.size() > 0) {
787         //    GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.DELETE_PURCHASE_ORDER_FAIL_MESSAGE, "Bib");
788         //    return workEInstanceOlemlForm;
789         //}
790 
791         String responseXml = getResponseFromDocStore(editorForm, editorForm.getDocId(), operation);
792         if (responseXml.contains("Success")) {
793             GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO, "record.delete.message");
794             workEInstanceOlemlForm.setShowDeleteTree(false);
795             workEInstanceOlemlForm.setHasLink(false);
796         } else {
797             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, "record.delete.fail.message");
798         }
799         return workEInstanceOlemlForm;
800     }
801 
802     /*public boolean validateRequiredFields(WorkEInstanceOlemlForm workEInstanceOlemlForm) {
803         List<Coverage> coverages = workEInstanceOlemlForm.getSelectedEHoldings().getExtentOfOwnership().getCoverages().getCoverage();
804         if (coverages.size() > 0) {
805             for (Coverage coverage : coverages) {
806                 String date = coverage.getCoverageStartDate();
807                 if (date != null && !date.isEmpty()) {
808                     return true;
809                 }
810             }
811         } else {
812             return false;
813         }
814         return false;
815     }*/
816 
817 }