001package org.kuali.ole.describe.controller;
002
003import org.apache.commons.collections.CollectionUtils;
004import org.apache.commons.lang.StringUtils;
005import org.apache.log4j.Logger;
006import org.apache.solr.client.solrj.SolrServerException;
007import org.kuali.ole.DocumentUniqueIDPrefix;
008import org.kuali.ole.OLEConstants;
009import org.kuali.ole.describe.bo.*;
010import org.kuali.ole.describe.form.*;
011import org.kuali.ole.describe.service.DiscoveryHelperService;
012import org.kuali.ole.docstore.common.client.DocstoreClient;
013import org.kuali.ole.docstore.common.document.*;
014import org.kuali.ole.docstore.common.document.HoldingsTree;
015import org.kuali.ole.docstore.common.document.content.instance.*;
016import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
017import org.kuali.ole.docstore.common.exception.DocstoreException;
018import org.kuali.ole.docstore.engine.client.DocstoreLocalClient;
019import org.kuali.ole.docstore.model.enums.DocCategory;
020import org.kuali.ole.docstore.model.enums.DocFormat;
021import org.kuali.ole.docstore.model.enums.DocType;
022import org.kuali.ole.describe.bo.InstanceEditorFormDataHandler;
023import org.kuali.ole.describe.form.InstanceEditorForm;
024import org.kuali.ole.select.bo.OLEEditorResponse;
025import org.kuali.ole.select.businessobject.*;
026import org.kuali.ole.select.document.OLEEResourceInstance;
027import org.kuali.ole.select.document.OLEEResourceRecordDocument;
028import org.kuali.ole.service.OLEEResourceSearchService;
029import org.kuali.ole.sys.context.SpringContext;
030import org.kuali.rice.core.api.config.property.ConfigurationService;
031import org.kuali.rice.core.api.exception.RiceRuntimeException;
032import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
033import org.kuali.rice.core.api.util.tree.Node;
034import org.kuali.rice.kim.api.identity.Person;
035import org.kuali.rice.kim.api.identity.PersonService;
036import org.kuali.rice.krad.service.*;
037import org.kuali.rice.krad.util.GlobalVariables;
038import org.kuali.rice.krad.util.KRADConstants;
039import org.kuali.rice.krad.web.form.UifFormBase;
040import org.springframework.validation.BindingResult;
041import org.springframework.web.bind.annotation.ModelAttribute;
042import org.springframework.web.servlet.ModelAndView;
043
044import javax.servlet.http.HttpServletRequest;
045import javax.servlet.http.HttpServletResponse;
046import java.sql.Date;
047import java.util.*;
048
049/**
050 * Created with IntelliJ IDEA.
051 * User: pp7788
052 * Date: 12/11/12
053 * Time: 2:38 PM
054 * To change this template use File | Settings | File Templates.
055 */
056
057/**
058 * InstanceEditorController is the controller class for Instance Editor
059 */
060//@Controller
061//@RequestMapping(value = "/instanceeditor")
062public class WorkEInstanceOlemlEditor
063        extends AbstractEditor
064        implements DocumentEditor {
065
066    private static final Logger LOG = Logger.getLogger(WorkEInstanceOlemlEditor.class);
067
068    //    private DocstoreHelperService docstoreHelperService;
069    private InstanceEditorFormDataHandler instanceEditorFormDataHandler;
070    private EInstanceFormDataHandler eInstanceFormDataHandler;
071    private DiscoveryHelperService discoveryHelperService;
072    private OLEEResourceSearchService oleEResourceSearchService;
073    private DocumentService documentService;
074    private HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
075    private DocstoreClient docstoreClient = getDocstoreLocalClient();
076
077    private static WorkEInstanceOlemlEditor workEInstanceOlemlEditor = new WorkEInstanceOlemlEditor();
078
079    public static WorkEInstanceOlemlEditor getInstance() {
080        return workEInstanceOlemlEditor;
081    }
082
083    public OLEEResourceSearchService getOleEResourceSearchService() {
084        if (oleEResourceSearchService == null) {
085            oleEResourceSearchService = GlobalResourceLoader.getService(OLEConstants.OLEEResourceRecord.ERESOURSE_SEARCH_SERVICE);
086        }
087        return oleEResourceSearchService;
088    }
089
090    public DocumentService getDocumentService() {
091        if (this.documentService == null) {
092            this.documentService = KRADServiceLocatorWeb.getDocumentService();
093        }
094        return this.documentService;
095    }
096
097    public void setDocumentService(DocumentService documentService) {
098        this.documentService = documentService;
099    }
100
101    private WorkEInstanceOlemlEditor() {
102    }
103
104    @Override
105    public EditorForm copy(EditorForm editorForm) {
106        loadDocument(editorForm);
107        WorkEInstanceOlemlForm workEInstanceOlemlForm = (WorkEInstanceOlemlForm) editorForm.getDocumentForm();
108        workEInstanceOlemlForm.getSelectedEHoldings().setEResourceId(workEInstanceOlemlForm.geteResourceId()!=null&&!workEInstanceOlemlForm.geteResourceId().isEmpty()?workEInstanceOlemlForm.geteResourceId():null);
109//        workEInstanceOlemlForm.getSelectedEHoldings().setEResourceTitle(null);
110        editorForm.seteResourceId(workEInstanceOlemlForm.geteResourceId()!=null&&!workEInstanceOlemlForm.geteResourceId().isEmpty()?workEInstanceOlemlForm.geteResourceId():null);
111        editorForm.seteResourceTitle(workEInstanceOlemlForm.geteResourceTitle()!=null&&!workEInstanceOlemlForm.geteResourceTitle().isEmpty()?workEInstanceOlemlForm.geteResourceTitle():null);
112        editorForm.setDocId(null);
113        editorForm.setShowEditorFooter(false);
114        editorForm.setFromSearch(false);
115
116        //editorForm.setMessage("Record copied successfully");
117        //GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO, "info.einstance.copy.success");
118        return editorForm;
119    }
120
121    @Override
122    public EditorForm loadDocument(EditorForm editorForm) {
123        LOG.info("loadDocument in E-Instance Editor");
124        WorkEInstanceOlemlForm workEInstanceOlemlForm = new WorkEInstanceOlemlForm();
125        String docId = editorForm.getDocId();
126        String bibId = editorForm.getBibId();
127        String tokenId = editorForm.getTokenId();
128        workEInstanceOlemlForm.setTokenId(tokenId);
129        workEInstanceOlemlForm.setBibId(bibId);
130        String docType = editorForm.getDocType();
131        editorForm.setHasLink(true);
132        if ((StringUtils.isBlank(editorForm.getTitle()))) {
133            getTitleInfo(editorForm);
134        }
135        if (editorForm.isFromDublin()) {
136            editorForm.setHasLink(false);
137        }
138        if (editorForm.getTitle() != null && editorForm.getTitle().contains("<")) {
139            String title = org.apache.commons.lang.StringEscapeUtils.escapeHtml((editorForm.getTitle()));
140            editorForm.setTitle(title);
141        }
142        editorForm.setShowEditorFooter(false);
143        editorForm.setFromSearch(false);
144        String eResId = null;
145        workEInstanceOlemlForm.seteResourceId(editorForm.geteResourceId());
146        Bib bib = null;
147        List<BibTree> bibTreeList = null;
148        BibTree bibTree = null;
149        if (StringUtils.isNotEmpty(bibId)) {
150            try {
151                bibTree = docstoreClient.retrieveBibTree(bibId);
152                bibTreeList = new ArrayList<>();
153                bibTreeList.add(bibTree);
154                bib = bibTree.getBib();
155                workEInstanceOlemlForm.setBibTreeList(bibTreeList);
156                editorForm.setTitle(bibTree.getBib().getTitle() + " / " + bibTree.getBib().getAuthor());
157            } catch (Exception e) {
158                LOG.error("Exception :", e);
159                e.printStackTrace();
160            }
161        }
162        try {
163            if (docId != null && docId.length() > 0) {
164
165
166                Holdings holdings = docstoreClient.retrieveHoldings(docId);
167                String docStoreData = holdings.getContent();
168                OleHoldings eHoldings = holdingOlemlRecordProcessor.fromXML(docStoreData);
169                if (eResId == null || eResId.length() == 0) {
170                    eResId = eHoldings.getEResourceId();
171                    workEInstanceOlemlForm.seteResourceId(eResId);
172                }
173                workEInstanceOlemlForm.setHoldingsId(DocumentUniqueIDPrefix.getDocumentId(holdings.getId()));
174                editorForm.setStaffOnlyFlagForHoldings(holdings.isStaffOnly());
175                getOleEResourceSearchService().getEResourcesFields(eResId, eHoldings, workEInstanceOlemlForm);
176                getOleEResourceSearchService().getEResourcesLicenseFields(eResId, workEInstanceOlemlForm);
177                if (eHoldings.getExtentOfOwnership() != null && eHoldings.getExtentOfOwnership().size() > 0
178                        && eHoldings.getExtentOfOwnership().get(0).getCoverages() != null
179                        && eHoldings.getExtentOfOwnership().get(0).getCoverages().getCoverage().size() == 0) {
180                    eHoldings.getExtentOfOwnership().get(0).getCoverages().getCoverage().add(new Coverage());
181                }
182                if (eHoldings.getExtentOfOwnership() != null && eHoldings.getExtentOfOwnership().size() > 0
183                        && eHoldings.getExtentOfOwnership().get(0).getPerpetualAccesses() != null
184                        && eHoldings.getExtentOfOwnership().get(0).getPerpetualAccesses().getPerpetualAccess().size() == 0) {
185                    eHoldings.getExtentOfOwnership().get(0).getPerpetualAccesses().getPerpetualAccess().add(new PerpetualAccess());
186                }
187                if (eHoldings.getNote() != null && eHoldings.getNote().size() == 0) {
188                    eHoldings.getNote().add(new Note());
189                }
190
191                if(eHoldings.getLink().size() == 0){
192                    List<Link> links = new ArrayList<>();
193                    Link link = new Link();
194                    link.setText("");
195                    link.setUrl("");
196                    links.add(link);
197                    eHoldings.setLink(links);
198                }
199                workEInstanceOlemlForm.setSelectedEHoldings(eHoldings);
200
201                if(workEInstanceOlemlForm.getSelectedEHoldings() != null &&  workEInstanceOlemlForm.getSelectedEHoldings().getHoldingsAccessInformation() != null
202                        && workEInstanceOlemlForm.getSelectedEHoldings().getHoldingsAccessInformation().getProxiedResource() != null
203                        && workEInstanceOlemlForm.getSelectedEHoldings().getHoldingsAccessInformation().getProxiedResource().equalsIgnoreCase(OLEConstants.OLEEResourceRecord.ON)) {
204                    workEInstanceOlemlForm.setProxiedResource(true);
205                }
206                workEInstanceOlemlForm.getSelectedEHoldings().setStatusDate(new Date(System.currentTimeMillis()).toString());
207                getEInstanceFormDataHandler().setLocationDetails(workEInstanceOlemlForm);
208                if(editorForm.getMethodToCall().equalsIgnoreCase("copy")){
209                    GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO, "record.copy.message");
210                }
211                else{
212                GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO, "record.load.message");
213                }
214                editorForm.setHoldingCreatedDate(holdings.getCreatedOn());
215                editorForm.setHoldingCreatedBy(holdings.getCreatedBy());
216                editorForm.setHoldingUpdatedDate(holdings.getUpdatedOn());
217                editorForm.setHoldingUpdatedBy(holdings.getUpdatedBy());
218                getOleEResourceSearchService().getAcquisitionInfoFromPOAndInvoice(docId, workEInstanceOlemlForm);
219            } else {
220                OleHoldings eHoldings = new OleHoldings();
221                eHoldings.setStatusDate(new Date(System.currentTimeMillis()).toString());
222                eHoldings.setEResourceId(editorForm.geteResourceId());
223                //getOleEResourceSearchService().getAccessLocationFromEInstance(eHoldings, workEInstanceOlemlForm);
224                getOleEResourceSearchService().getEResourcesFields(editorForm.geteResourceId(), eHoldings, workEInstanceOlemlForm);
225                getOleEResourceSearchService().getEResourcesLicenseFields(editorForm.geteResourceId(), workEInstanceOlemlForm);
226                if (eHoldings.getExtentOfOwnership() != null && eHoldings.getExtentOfOwnership().size() > 0
227                        && eHoldings.getExtentOfOwnership().get(0).getCoverages() != null
228                        && eHoldings.getExtentOfOwnership().get(0).getCoverages().getCoverage().size() == 0) {
229                    eHoldings.getExtentOfOwnership().get(0).getCoverages().getCoverage().add(new Coverage());
230                }
231                if (eHoldings.getExtentOfOwnership() != null && eHoldings.getExtentOfOwnership().size() > 0
232                        && eHoldings.getExtentOfOwnership().get(0).getPerpetualAccesses() != null
233                        && eHoldings.getExtentOfOwnership().get(0).getPerpetualAccesses().getPerpetualAccess().size() == 0) {
234                    eHoldings.getExtentOfOwnership().get(0).getPerpetualAccesses().getPerpetualAccess().add(new PerpetualAccess());
235                }
236                if (eHoldings.getNote() != null && eHoldings.getNote().size() == 0) {
237                    eHoldings.getNote().add(new Note());
238                }
239                List<Link> links = new ArrayList<>();
240                Link link = new Link();
241                link.setText("");
242                link.setUrl("");
243                links.add(link);
244                eHoldings.setLink(links);
245                workEInstanceOlemlForm.setSelectedEHoldings(eHoldings);
246                workEInstanceOlemlForm.setViewId("WorkEInstanceViewPage");
247                getEInstanceFormDataHandler().setLocationDetails(workEInstanceOlemlForm);
248                GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO, "eholdings.record.new.load.message");
249                editorForm.setHoldingCreatedDate("");
250                editorForm.setHoldingCreatedBy("");
251            }
252        } catch (DocstoreException e) {
253            LOG.error(e);
254            DocstoreException docstoreException = (DocstoreException) e;
255            if (org.apache.commons.lang3.StringUtils.isNotEmpty(docstoreException.getErrorCode())) {
256                GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, docstoreException.getErrorCode());
257            } else {
258                GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, e.getMessage());
259            }
260            return workEInstanceOlemlForm;
261        }
262        catch (Exception e) {
263            LOG.error("loadDocument Instance Exception:" + e);
264            GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO, "record.load.fail.message ");
265            return workEInstanceOlemlForm;
266        }
267        if(workEInstanceOlemlForm.getSelectedEHoldings()!=null && workEInstanceOlemlForm.getSelectedEHoldings().getPlatform()==null){
268            workEInstanceOlemlForm.getSelectedEHoldings().setPlatform(new Platform());
269        }
270        workEInstanceOlemlForm.setViewId("WorkEInstanceViewPage");
271        workEInstanceOlemlForm.setIssn(bib.getIssn());
272        editorForm.seteResourceTitle(workEInstanceOlemlForm.geteResourceTitle());
273        String directory = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(org.kuali.ole.sys.OLEConstants.EXTERNALIZABLE_HELP_URL_KEY);
274        editorForm.setExternalHelpUrl(directory+"/reference/webhelp/OLE/content/ch04s01.html#_EInstanceEditor");
275        editorForm.setHeaderText("E-Holdings");
276        if (workEInstanceOlemlForm.getTokenId() != null && !workEInstanceOlemlForm.getTokenId().isEmpty()) {
277            editorForm.setTokenId(workEInstanceOlemlForm.getTokenId());
278        }
279        return workEInstanceOlemlForm;
280    }
281
282    private void getTitleInfo(EditorForm editorForm) {
283//        WorkBibDocument workBibDocument = new WorkBibDocument();
284//        workBibDocument.setId(editorForm.getBibId());
285//        QueryService queryService = QueryServiceImpl.getInstance();
286//        WorkBibDocument document = null;
287//        try {
288//            document = queryService.buildBibDocumentInfo(workBibDocument);
289//        } catch (Exception e) {
290//            LOG.error("Failed to get Title / Author details : " + e.getMessage());
291//        }
292//        StringBuilder titleBuilder = new StringBuilder();
293//        if (document != null) {
294//            titleBuilder.append(document.getTitle());
295//            titleBuilder.append("/");
296//            if (document.getAuthor() != null) {
297//                titleBuilder.append(document.getAuthor());
298//            }
299//            if (workBibDocument.getDocFormat().equalsIgnoreCase("dublinunq")) {
300//                editorForm.setFromDublin(true);
301//            }
302//            editorForm.setTitle(org.apache.commons.lang.StringEscapeUtils.escapeHtml(titleBuilder.toString()));
303//        }
304    }
305
306    @Override
307    public EditorForm saveDocument(EditorForm editorForm) {
308        WorkEInstanceOlemlForm workEInstanceOlemlForm = (WorkEInstanceOlemlForm) editorForm.getDocumentForm();
309        String docId = editorForm.getDocId();
310        String docType = editorForm.getDocType();
311        String bibId = editorForm.getBibId();
312        String editorStatusMessage = "";
313        List<BibTree> bibTreeList = null;
314        BibTree bibTree = null;
315        Holdings eHoldingsDoc = null;
316        String user = GlobalVariables.getUserSession().getPrincipalName();
317        if (StringUtils.isNotEmpty(bibId)) {
318            try {
319                bibTree = docstoreClient.retrieveBibTree(bibId);
320                bibTreeList = new ArrayList<>();
321                bibTreeList.add(bibTree);
322                workEInstanceOlemlForm.setBibTreeList(bibTreeList);
323                editorForm.setTitle(bibTree.getBib().getTitle() + " / " + bibTree.getBib().getAuthor());
324            } catch (Exception e) {
325                LOG.error("Exception :", e);
326                e.printStackTrace();
327            }
328        }
329        long startTime = System.currentTimeMillis();
330        try {
331            if (docId != null && docId.length() > 0) {
332                OleHoldings eHoldings = workEInstanceOlemlForm.getSelectedEHoldings();
333                boolean dateFlag = getOleEResourceSearchService().validateDates(eHoldings);
334                if (dateFlag) {
335                    getOleEResourceSearchService().getEResourcesFields(editorForm.geteResourceId(), eHoldings, workEInstanceOlemlForm);
336                    String content = getInstanceEditorFormDataHandler().buildHoldingContent(eHoldings);
337                    getOleEResourceSearchService().getEResourcesLicenseFields(editorForm.geteResourceId(), workEInstanceOlemlForm);
338                    eHoldingsDoc = new EHoldings();
339                    eHoldingsDoc.setId(editorForm.getDocId());
340                    eHoldingsDoc.setType(editorForm.getDocType());
341                    eHoldingsDoc.setFormat(editorForm.getDocFormat());
342                    eHoldingsDoc.setCategory(editorForm.getDocCategory());
343                    eHoldingsDoc.setStaffOnly(editorForm.isStaffOnlyFlagForHoldings());
344                    eHoldingsDoc.setUpdatedBy(user);
345                    eHoldingsDoc.setBib(bibTree.getBib());
346                    eHoldings = (OleHoldings) holdingOlemlRecordProcessor.fromXML(content);
347                    eHoldings = getEHolding(editorForm, eHoldings);
348                    String newContent = holdingOlemlRecordProcessor.toXML(eHoldings);
349                    eHoldingsDoc.setContent(newContent);
350                    docstoreClient.updateHoldings(eHoldingsDoc);
351                    workEInstanceOlemlForm.getSelectedEHoldings().setHoldingsIdentifier(eHoldingsDoc.getId());
352                    // To add updated EHoldings to bib tree
353                    addEHoldingsToBibTree(workEInstanceOlemlForm.getBibTreeList(), eHoldingsDoc);
354                    editorStatusMessage = "holdings.record.update.message";
355                } else {
356                    return workEInstanceOlemlForm;
357                }
358            } else {
359                OleHoldings eHoldings = workEInstanceOlemlForm.getSelectedEHoldings();
360                boolean dateFlag = getOleEResourceSearchService().validateDates(eHoldings);
361                if (dateFlag) {
362                    getOleEResourceSearchService().getEResourcesFields(editorForm.geteResourceId(), eHoldings, workEInstanceOlemlForm);
363                    getOleEResourceSearchService().getEResourcesLicenseFields(editorForm.geteResourceId(), workEInstanceOlemlForm);
364                    String content = getInstanceEditorFormDataHandler().buildHoldingContent(eHoldings);
365                    eHoldingsDoc = new EHoldings();
366                    eHoldingsDoc.setCategory(DocCategory.WORK.getCode());
367                    eHoldingsDoc.setType(DocType.EHOLDINGS.getCode());
368                    eHoldingsDoc.setFormat(DocFormat.OLEML.getCode());
369                    eHoldingsDoc.setStaffOnly(editorForm.isStaffOnlyFlagForHoldings());
370                    eHoldingsDoc.setCreatedBy(user);
371                    eHoldingsDoc.setBib(bibTree.getBib());
372                    eHoldings = (OleHoldings) holdingOlemlRecordProcessor.fromXML(content);
373                    eHoldings = getEHolding(editorForm, eHoldings);
374                    String newContent = holdingOlemlRecordProcessor.toXML(eHoldings);
375                    eHoldingsDoc.setContent(newContent);
376                    docstoreClient.createHoldings(eHoldingsDoc);
377                    editorForm.setDocId(eHoldingsDoc.getId());
378                    workEInstanceOlemlForm.setHoldingsId(DocumentUniqueIDPrefix.getDocumentId(eHoldingsDoc.getId()));
379                    processResponse(eHoldingsDoc.getId(), eHoldingsDoc.getBib().getId(), editorForm.getTokenId(), editorForm.getLinkToOrderOption());
380                    workEInstanceOlemlForm.getSelectedEHoldings().setHoldingsIdentifier(eHoldingsDoc.getId());
381                    HoldingsTree holdingsTree = new HoldingsTree();
382                    holdingsTree.setHoldings(eHoldingsDoc);
383                    List<HoldingsTree> holdingsTreeList = new ArrayList<>();
384                    holdingsTreeList.add(holdingsTree);
385                    bibTree.getHoldingsTrees().addAll(holdingsTreeList);
386                    editorStatusMessage = "record.create.message";
387                } else {
388                    return workEInstanceOlemlForm;
389                }
390            }
391            if(editorForm.getDocId()!=null){
392            Holdings holdingsDoc = docstoreClient.retrieveHoldings(editorForm.getDocId());
393            editorForm.setHoldingUpdatedDate(holdingsDoc.getUpdatedOn());
394            editorForm.setHoldingUpdatedBy(holdingsDoc.getUpdatedBy());
395            editorForm.setHoldingCreatedDate(holdingsDoc.getCreatedOn());
396            editorForm.setHoldingCreatedBy(holdingsDoc.getCreatedBy());
397            }
398
399        }
400        catch (DocstoreException e) {
401            LOG.error(e);
402            DocstoreException docstoreException = (DocstoreException) e;
403            if (org.apache.commons.lang3.StringUtils.isNotEmpty(docstoreException.getErrorCode())) {
404                GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, docstoreException.getErrorCode());
405            } else {
406                GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, e.getMessage());
407            }
408            return workEInstanceOlemlForm;
409        }
410        catch (Exception e) {
411            LOG.error("Exception :", e);
412            GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, "docstore.response", e.getMessage());
413            return workEInstanceOlemlForm;
414
415        }
416        long endTime = System.currentTimeMillis();
417        editorForm.setSolrTime(String.valueOf((endTime-startTime)/1000));
418
419        try {
420            getEInstanceFormDataHandler().setLocationDetails(workEInstanceOlemlForm);
421            String eResId = workEInstanceOlemlForm.geteResourceId();
422            editorForm.seteResourceId(null);
423            workEInstanceOlemlForm.setViewId("WorkEInstanceViewPage");
424
425            GlobalVariables.getMessageMap().getInfoMessages().clear();
426            if (eResId != null && !eResId.isEmpty()) {
427                Map<String, String> tempId = new HashMap<String, String>();
428                tempId.put(OLEConstants.OLEEResourceRecord.ERESOURCE_IDENTIFIER, eResId);
429                OLEEResourceRecordDocument tempDocument = (OLEEResourceRecordDocument) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OLEEResourceRecordDocument.class, tempId);
430
431                Person principalPerson = SpringContext.getBean(PersonService.class).getPerson(GlobalVariables.getUserSession().getPerson().getPrincipalId());
432                tempDocument.setDocumentHeader(SpringContext.getBean(DocumentHeaderService.class).getDocumentHeaderById(tempDocument.getDocumentNumber()));
433                tempDocument.getDocumentHeader().setWorkflowDocument(KRADServiceLocatorWeb.getWorkflowDocumentService().loadWorkflowDocument(tempDocument.getDocumentNumber(), principalPerson));
434                if (tempDocument != null) {
435                    try {
436                        tempDocument.setSelectInstance(OLEConstants.OLEEResourceRecord.CREATE_NEW_INSTANCE);
437                        tempDocument.seteInstanceFlag(true);
438                        for (OLEEResourceInstance oleeResourceInstance : tempDocument.getOleERSInstances()) {
439                            if (oleeResourceInstance.getInstanceId().equals(editorForm.getDocId())) {
440                                processResponse(editorForm.getDocId(), editorForm.getBibId(), tempDocument.getDocumentNumber(), editorForm.getLinkToOrderOption());
441                            }
442                        }
443                        getOleEResourceSearchService().getNewInstance(tempDocument, tempDocument.getDocumentNumber(), eHoldingsDoc);
444                        getDocumentService().updateDocument(tempDocument);
445                    } catch (Exception e) {
446                        LOG.error("Exception :", e);
447                        throw new RiceRuntimeException(
448                                "Exception trying to save document: " + tempDocument
449                                        .getDocumentNumber(), e);
450                    }
451                }
452            }
453        }
454        catch (DocstoreException e) {
455            LOG.error(e);
456            DocstoreException docstoreException = (DocstoreException) e;
457            if (org.apache.commons.lang3.StringUtils.isNotEmpty(docstoreException.getErrorCode())) {
458                GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, docstoreException.getErrorCode());
459            } else {
460                GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, e.getMessage());
461            }
462            return workEInstanceOlemlForm;
463        }
464        catch (Exception e) {
465            LOG.error("Exception :", e);
466            GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, "docstore.response", e.getMessage());
467            return workEInstanceOlemlForm;
468        }
469        GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO, editorStatusMessage);
470
471        return workEInstanceOlemlForm;
472    }
473
474    /**
475     * This methods adds the updated EHoldings to the bib tree to build left pane tree.
476     * @param bibTreeList
477     * @param eHoldings
478     */
479    private void addEHoldingsToBibTree(List<BibTree> bibTreeList, Holdings eHoldings) {
480        if (CollectionUtils.isNotEmpty(bibTreeList)) {
481            for (BibTree bibTree : bibTreeList) {
482                if (CollectionUtils.isNotEmpty(bibTree.getHoldingsTrees())) {
483                    for (HoldingsTree holdingsTree : bibTree.getHoldingsTrees()) {
484                        if (null != holdingsTree.getHoldings() && null != eHoldings) {
485                            String holdingsId = holdingsTree.getHoldings().getId();
486                            String eHoldingsId = eHoldings.getId();
487                            if (null != holdingsId && null != eHoldingsId) {
488                                if (holdingsId.equals(eHoldingsId)) {
489                                    holdingsTree.setHoldings(eHoldings);
490                                }
491                            }
492                        }
493                    }
494                }
495            }
496        }
497    }
498
499    public void processResponse(String instanceId, String bibId, String tokenId, String linkToOrderOption) throws Exception {
500        OLEEditorResponse oleEditorResponse = new OLEEditorResponse();
501        oleEditorResponse.setTokenId(tokenId);
502        oleEditorResponse.setLinkedInstanceId(instanceId);
503        oleEditorResponse.setBib(docstoreClient.retrieveBib(bibId));
504        oleEditorResponse.setLinkToOrderOption(linkToOrderOption);
505        HashMap<String, OLEEditorResponse> oleEditorResponseMap = new HashMap<String, OLEEditorResponse>();
506        oleEditorResponseMap.put(tokenId, oleEditorResponse);
507        OleDocstoreResponse.getInstance().setEditorResponse(oleEditorResponseMap);
508    }
509    public OleHoldings getEHolding(EditorForm editorForm,OleHoldings eHoldings) {
510        WorkEInstanceOlemlForm workEInstanceOlemlForm = (WorkEInstanceOlemlForm) editorForm.getDocumentForm();
511        if (null != eHoldings.getHoldingsAccessInformation()) {
512            if (workEInstanceOlemlForm.isProxiedResource()) {
513                eHoldings.getHoldingsAccessInformation().setProxiedResource(OLEConstants.OLEEResourceRecord.ON);
514            } else {
515                eHoldings.getHoldingsAccessInformation().setProxiedResource(OLEConstants.OLEEResourceRecord.OFF);
516            }
517        }
518        if(editorForm.getAccessLocationFlag().equalsIgnoreCase(OLEConstants.FALSE) && editorForm.geteResourceTitle() != null){
519            eHoldings.getHoldingsAccessInformation().setAccessLocation(null);
520        }
521        if(editorForm.getStatisticalCodeFlag().equalsIgnoreCase(OLEConstants.FALSE) && editorForm.geteResourceTitle() != null){
522            eHoldings.getStatisticalSearchingCode().setCodeValue(null);
523        }
524        if(editorForm.getNoOfUserFlag().equalsIgnoreCase(OLEConstants.FALSE) && editorForm.geteResourceTitle() != null){
525            eHoldings.getHoldingsAccessInformation().setNumberOfSimultaneousUser(null);
526        }
527        if(editorForm.getAuthenticationTypeFlag().equalsIgnoreCase(OLEConstants.FALSE) && editorForm.geteResourceTitle() != null){
528            eHoldings.getHoldingsAccessInformation().setAuthenticationType(null);
529        }
530        if(editorForm.getPerpetualDateEndFlag().equalsIgnoreCase(OLEConstants.FALSE) && editorForm.geteResourceTitle() != null){
531            for(ExtentOfOwnership extentOfOwnershipList:eHoldings.getExtentOfOwnership()){
532                for(PerpetualAccess perpetualAccess:extentOfOwnershipList.getPerpetualAccesses().getPerpetualAccess())  {
533                    perpetualAccess.setPerpetualAccessEndDateString(StringUtils.isNotBlank(perpetualAccess.getPerpetualAccessEndDateString())?perpetualAccess.getPerpetualAccessEndDateString():"");
534                    perpetualAccess.setPerpetualAccessEndDateFormat(StringUtils.isNotBlank(perpetualAccess.getPerpetualAccessEndDateFormat())?perpetualAccess.getPerpetualAccessEndDateFormat():"");
535                    perpetualAccess.setPerpetualAccessEndIssue(StringUtils.isNotBlank(perpetualAccess.getPerpetualAccessEndIssue())?perpetualAccess.getPerpetualAccessEndIssue():"");
536                    perpetualAccess.setPerpetualAccessEndVolume(StringUtils.isNotBlank(perpetualAccess.getPerpetualAccessEndVolume())?perpetualAccess.getPerpetualAccessEndVolume():"");
537                    perpetualAccess.setPerpetualAccessEndDate(StringUtils.isNotBlank(perpetualAccess.getPerpetualAccessEndDate())?perpetualAccess.getPerpetualAccessEndDate():"");
538                }
539            }
540        }
541        if(editorForm.getPerpetualDateStartFlag().equalsIgnoreCase(OLEConstants.FALSE) && editorForm.geteResourceTitle() != null){
542            for(ExtentOfOwnership extentOfOwnershipList:eHoldings.getExtentOfOwnership()){
543                for(PerpetualAccess perpetualAccess:extentOfOwnershipList.getPerpetualAccesses().getPerpetualAccess())  {
544                    perpetualAccess.setPerpetualAccessStartDateString(StringUtils.isNotBlank(perpetualAccess.getPerpetualAccessStartDateString())?perpetualAccess.getPerpetualAccessStartDateString():"");
545                    perpetualAccess.setPerpetualAccessStartDateFormat(StringUtils.isNotBlank(perpetualAccess.getPerpetualAccessStartDateFormat())?perpetualAccess.getPerpetualAccessStartDateFormat():"");
546                    perpetualAccess.setPerpetualAccessStartIssue(StringUtils.isNotBlank(perpetualAccess.getPerpetualAccessStartIssue())?perpetualAccess.getPerpetualAccessStartIssue():"");
547                    perpetualAccess.setPerpetualAccessStartVolume(StringUtils.isNotBlank(perpetualAccess.getPerpetualAccessStartVolume())?perpetualAccess.getPerpetualAccessStartVolume():"");
548                    perpetualAccess.setPerpetualAccessStartDate(StringUtils.isNotBlank(perpetualAccess.getPerpetualAccessStartDate())?perpetualAccess.getPerpetualAccessStartDate():"");
549                }
550            }
551        }
552        if(editorForm.getCoverageDateStartFlag().equalsIgnoreCase(OLEConstants.FALSE) && editorForm.geteResourceTitle() != null){
553            for(ExtentOfOwnership extentOfOwnershipList:eHoldings.getExtentOfOwnership()){
554                for(Coverage coverage:extentOfOwnershipList.getCoverages().getCoverage())  {
555                    coverage.setCoverageStartIssue(StringUtils.isNotBlank(coverage.getCoverageStartIssue())?coverage.getCoverageStartIssue():"");
556                    coverage.setCoverageStartDateString(StringUtils.isNotBlank(coverage.getCoverageStartDateString())?coverage.getCoverageStartDateString():"");
557                    coverage.setCoverageStartDateFormat(StringUtils.isNotBlank(coverage.getCoverageStartDateFormat())?coverage.getCoverageStartDateFormat():"");
558                    coverage.setCoverageStartVolume(StringUtils.isNotBlank(coverage.getCoverageStartVolume())?coverage.getCoverageStartVolume():"");
559                    coverage.setCoverageStartDate(StringUtils.isNotBlank(coverage.getCoverageStartDate())?coverage.getCoverageStartDate():"");
560                }
561            }
562        }
563        if(editorForm.getCoverageDateEndFlag().equalsIgnoreCase(OLEConstants.FALSE) && editorForm.geteResourceTitle() != null){
564            for(ExtentOfOwnership extentOfOwnershipList:eHoldings.getExtentOfOwnership()){
565                for(Coverage coverage:extentOfOwnershipList.getCoverages().getCoverage())  {
566                    coverage.setCoverageEndIssue(StringUtils.isNotBlank(coverage.getCoverageEndIssue())?coverage.getCoverageEndIssue():"");
567                    coverage.setCoverageEndDateString(StringUtils.isNotBlank(coverage.getCoverageEndDateString())?coverage.getCoverageEndDateString():"");
568                    coverage.setCoverageEndDateFormat(StringUtils.isNotBlank(coverage.getCoverageEndDateFormat())?coverage.getCoverageEndDateFormat():"");
569                    coverage.setCoverageEndVolume(StringUtils.isNotBlank(coverage.getCoverageEndVolume())?coverage.getCoverageEndVolume():"");
570                    coverage.setCoverageEndDate(StringUtils.isNotBlank(coverage.getCoverageEndDate())?coverage.getCoverageEndDate():"");
571                }
572            }
573        }
574        return eHoldings;
575    }
576    /**
577     * This method creates a instance of InstanceEditorForm
578     *
579     * @param httpServletRequest
580     * @return InstanceEditorForm
581     */
582    protected UifFormBase createInitialForm(HttpServletRequest httpServletRequest) {
583        return new WorkInstanceOlemlForm();
584    }
585
586    /**
587     * This method takes the initial request when click on InstanceEditor Screen.
588     *
589     * @param form
590     * @param result
591     * @param request
592     * @param response
593     * @return ModelAndView
594     */
595    public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
596                              HttpServletRequest request, HttpServletResponse response) {
597        WorkInstanceOlemlForm instanceEditorForm = (WorkInstanceOlemlForm) form;
598        //ModelAndView modelAndView = super.start(instanceEditorForm, result, request, response);
599        // return getUIFModelAndView(instanceEditorForm, "WorkHoldingsViewPage");
600        return null;
601    }
602
603
604    /**
605     * This method will fetch the holding/Item records based on the AdditionalAttributes.
606     *
607     * @param form
608     * @param result
609     * @param request
610     * @param response
611     * @return ModelAndView
612     */
613    public ModelAndView load(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
614                             HttpServletRequest request, HttpServletResponse response) {
615        WorkEInstanceOlemlForm workEInstanceOlemlForm = new WorkEInstanceOlemlForm();
616        workEInstanceOlemlForm.setHideFooter(false);
617        workEInstanceOlemlForm.setViewId("WorkEInstanceViewPage");
618        workEInstanceOlemlForm.setMessage("Please enter details for new Item record.");
619        //EditorForm workInstanceOlemlForm = (WorkInstanceOlemlForm) form;
620        //WorkInstanceOlemlForm workInstanceOlemlForm = new WorkInstanceOlemlForm();
621        String uuid = request.getParameter("uuid");
622        String docType = request.getParameter("docType");
623        String docFormat = request.getParameter("docFormat");
624        String docCategory = request.getParameter("docCategory");
625        if (null == uuid || StringUtils.isEmpty(uuid)) {
626            uuid = workEInstanceOlemlForm.getUuid();
627        }
628        workEInstanceOlemlForm.setExisting("true");
629        try {
630              /*if (workEInstanceOlemlForm.getInstanceId() != null && workEInstanceOlemlForm.getInstanceId().length() > 0) {
631                String docStoreData = getDocstoreHelperService().getDocstoreData(docCategory,docType,docFormat,workEInstanceOlemlForm.getInstanceId());
632                InstanceCollection instanceCollection = new WorkEInstanceOlemlRecordProcessor().fromXML(docStoreData);
633                workEInstanceOlemlForm.setSelectedEInstance(instanceCollection.getEInstance().get(0));
634            }
635            String docStoreData = getDocstoreHelperService().getDocstoreData(docCategory,docType,docFormat,uuid);
636            if (docType.equalsIgnoreCase("eHoldings")) {
637                EHoldings oleHoldings = new WorkEHoldingOlemlRecordProcessor().fromXML(docStoreData);
638                workEInstanceOlemlForm.setSelectedEHoldings(oleHoldings);
639                //return getUIFModelAndView(workInstanceOlemlForm,"WorkHoldingsViewPage");
640            }
641            else {
642                InstanceCollection instanceCollection = new WorkEInstanceOlemlRecordProcessor().fromXML(docStoreData);
643                workEInstanceOlemlForm.setSelectedEHoldings(instanceCollection.getEInstance().get(0).getEHoldings());
644                //return getUIFModelAndView(workInstanceOlemlForm,"WorkHoldingsViewPage");
645            }*/
646        } catch (Exception e) {
647            LOG.error("Exception :", e);
648            StringBuffer instanceEditorErrorMessage = new StringBuffer(OLEConstants.INSTANCE_EDITOR_LOAD_FAILURE)
649                    .append(" :: \n" + e.getMessage());
650            //workInstanceOlemlForm.setMessage(instanceEditorErrorMessage.toString());
651            LOG.error(instanceEditorErrorMessage);
652            //return getUIFModelAndView(workInstanceOlemlForm);
653        }
654        return null;
655    }
656
657    /**
658     * Used for Test-case
659     *
660     * @param result
661     * @param request
662     * @param response
663     * @param instanceEditorForm
664     * @return ModelAndView
665     */
666    protected ModelAndView callSuper(BindingResult result, HttpServletRequest request, HttpServletResponse response,
667                                     InstanceEditorForm instanceEditorForm) {
668        //return super.updateComponent(instanceEditorForm, result, request, response);
669        return null;
670    }
671
672//    /**
673//     * Gets the docstoreHelperService attribute
674//     *
675//     * @return Returns DocstoreHelperService
676//     */
677//    private DocstoreHelperService getDocstoreHelperService() {
678//        if (null == docstoreHelperService) {
679//            return new DocstoreHelperService();
680//        }
681//        return docstoreHelperService;
682//    }
683
684//    /**
685//     * Sets the docstoreHelperService attribute values.
686//     *
687//     * @param docstoreHelperService
688//     */
689//    public void setDocstoreHelperService(DocstoreHelperService docstoreHelperService) {
690//        this.docstoreHelperService = docstoreHelperService;
691//    }
692
693    /**
694     * Gets the InstanceEditorFormDataHandler attribute.
695     *
696     * @return Returns InstanceEditorFormDataHandler.
697     */
698    private InstanceEditorFormDataHandler getInstanceEditorFormDataHandler() {
699        if (null == instanceEditorFormDataHandler) {
700            instanceEditorFormDataHandler = new InstanceEditorFormDataHandler();
701        }
702        return instanceEditorFormDataHandler;
703    }
704
705    private EInstanceFormDataHandler getEInstanceFormDataHandler() {
706        if (null == eInstanceFormDataHandler) {
707            eInstanceFormDataHandler = new EInstanceFormDataHandler();
708        }
709        return eInstanceFormDataHandler;
710    }
711
712
713//    /**
714//     * This method returns AdditionalAttributes instance from the list of Extension.
715//     * @param extensionList
716//     * @return AdditionalAttributes
717//     */
718//    private AdditionalAttributes getFirstAdditionalAttributes(List<Extension> extensionList) {
719//        for (Extension ext : extensionList) {
720//            for (Object obj : ext.getContent()) {
721//                if (obj instanceof AdditionalAttributes) {
722//                    return (AdditionalAttributes) obj;
723//                }
724//            }
725//        }
726//        return null;
727//    }
728
729    /**
730     * Sets the discoveryHelperService attribute value.
731     *
732     * @param discoveryHelperService
733     */
734    public void setDiscoveryHelperService(DiscoveryHelperService discoveryHelperService) {
735        this.discoveryHelperService = discoveryHelperService;
736    }
737
738    /**
739     * Gets the discoveryHelperService attribute.
740     *
741     * @return Returns DiscoveryHelperService.
742     */
743    public DiscoveryHelperService getDiscoveryHelperService() {
744        if (null == discoveryHelperService) {
745            return new DiscoveryHelperService();
746        }
747        return discoveryHelperService;
748    }
749
750    /*@RequestMapping(params = "methodToCall=addExtentOfOwnership")
751    public ModelAndView addExtentOfOwnership(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
752                                             HttpServletRequest request, HttpServletResponse response) {
753        InstanceEditorForm instanceEditorForm = (InstanceEditorForm) form;
754        int index = Integer.parseInt(form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
755        index++;
756        List<ExtentOfOwnership> extentOfOwnershipForUI = instanceEditorForm.getSelectedHolding().getExtentOfOwnership();
757        extentOfOwnershipForUI.add(index, new ExtentOfOwnership());
758        return super.updateComponent(instanceEditorForm, result, request, response);
759    }
760
761    @RequestMapping(params = "methodToCall=removeExtentOfOwnership")
762    public ModelAndView removeExtentOfOwnership(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
763                                                HttpServletRequest request, HttpServletResponse response) {
764        InstanceEditorForm instanceEditorForm = (InstanceEditorForm) form;
765        int index = Integer.parseInt(form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
766        List<ExtentOfOwnership> extentOfOwnershipForUI = instanceEditorForm.getSelectedHolding().getExtentOfOwnership();
767        if (extentOfOwnershipForUI.size() > 1)
768            extentOfOwnershipForUI.remove(index);
769        return super.updateComponent(instanceEditorForm, result, request, response);
770    }*/
771
772
773  /*  */
774
775    /**
776     * Shows the linked Bibs corresponging to Holdings/Item
777     *
778     * @param editorForm
779     * @return
780     *//*
781    @Override
782    public EditorForm showBibs(EditorForm editorForm) {
783        WorkEInstanceOlemlForm workEInstanceOlemlForm = new WorkEInstanceOlemlForm();
784        QueryService queryService = QueryServiceImpl.getInstance();
785        List<String> bibUUIDList = new ArrayList<String>();
786        String instanceId = "";
787        String docType = editorForm.getDocType();
788        List<WorkBibDocument> workBibDocumentList = new ArrayList<WorkBibDocument>();
789        try {
790
791            if ((docType.equalsIgnoreCase(DocType.INSTANCE.getCode()) || docType
792                    .equalsIgnoreCase(DocType.HOLDINGS.getCode())) || (docType
793                    .equalsIgnoreCase(DocType.ITEM.getCode()))) {
794                instanceId = editorForm.getInstanceId();
795            }
796            if (StringUtils.isNotEmpty(instanceId)) {
797                bibUUIDList = queryService.queryForBibs(instanceId);
798            }
799            DocstoreHelperService docstoreHelperService = new DocstoreHelperService();
800            if (bibUUIDList != null && bibUUIDList.size() > 0) {
801                for (String bibUUID : bibUUIDList) {
802                    WorkBibDocument workBibDocument = new WorkBibDocument();
803                    workBibDocument.setId(bibUUID);
804                    workBibDocument = docstoreHelperService.getInfoForBibTree(workBibDocument);
805                    workBibDocumentList.add(workBibDocument);
806                }
807            }
808
809            editorForm.setWorkBibDocumentList(workBibDocumentList);
810        } catch (SolrServerException e) {
811            LOG.error("Exception:" + e.getMessage(), e);
812        }
813        workEInstanceOlemlForm.setViewId("ShowBibViewPage");
814        return workEInstanceOlemlForm;
815    }*/
816
817    /**
818     * This method deletes the EHoldings record from docstore by the doc id.
819     * @param editorForm
820     * @return
821     * @throws Exception
822     */
823    @Override
824    public EditorForm delete(EditorForm editorForm) throws Exception {
825        return deleteFromDocStore(editorForm);
826    }
827
828    public EditorForm deleteVerify(EditorForm editorForm) {
829        //LOG.info("in instance editor class");
830        WorkInstanceOlemlForm workInstanceOlemlForm = new WorkInstanceOlemlForm();
831        String docId = editorForm.getDocId();
832        String operation = "deleteVerify";
833        //        String responseXml = getResponseFromDocStore(editorForm, docId, operation);
834        //        LOG.info("deleteVerify responseXml-->" + responseXml);
835        //        editorForm.setDeleteVerifyResponse(responseXml);
836        editorForm.setShowDeleteTree(true);
837        editorForm.setHasLink(true);
838        //        Node<DocumentTreeNode, String> docTree = buildDocSelectionTree(responseXml);
839        List<String> uuidList = new ArrayList<>(0);
840        uuidList.add(editorForm.getDocId());
841        DocumentSelectionTree documentSelectionTree = new DocumentSelectionTree();
842        Node<DocumentTreeNode, String> docTree = null;
843        try {
844            docTree = documentSelectionTree.add(uuidList, editorForm.getDocType());
845        } catch (SolrServerException e) {
846            LOG.error("Exception :", e);
847            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
848        }
849        editorForm.getDocTree().setRootElement(docTree);
850        editorForm.setViewId("DeleteViewPage");
851        return editorForm;
852    }
853
854    /*@Override
855    public EditorForm delete(EditorForm editorForm) throws Exception {
856        WorkEInstanceOlemlForm workEInstanceOlemlForm = new WorkEInstanceOlemlForm();
857        String operation = OLEConstants.DELETE;
858        BusinessObjectService boService = KRADServiceLocator.getBusinessObjectService();
859        //Map<String, String> map = new HashMap<>();
860        //Map<String, String> uuidMap = new HashMap<>();
861        //map.put("bibId", editorForm.getBibId());
862        //List<OleCopy> listOfValues = (List<OleCopy>) boService.findMatching(OleCopy.class, map);
863
864        //if (listOfValues != null && listOfValues.size() > 0) {
865        //    GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.DELETE_PURCHASE_ORDER_FAIL_MESSAGE, "Bib");
866        //    return workEInstanceOlemlForm;
867        //}
868
869        getResponseFromDocStore(editorForm, editorForm.getDocId(), operation);
870        GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO, OLEConstants.RECORD_DELETE_MESSAGE);
871
872       *//* if (responseXml.contains("Success")) {
873            GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO, "record.delete.message");
874            workEInstanceOlemlForm.setShowDeleteTree(false);
875            workEInstanceOlemlForm.setHasLink(false);
876        } else {
877            GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, "record.delete.fail.message");
878        }*//*
879        removeDocumentFromTree(editorForm);
880        editorForm.setShowDeleteTree(false);
881        editorForm.setHasLink(false);
882        workEInstanceOlemlForm.setViewId(OLEConstants.DELETE_VIEW_PAGE);
883        return workEInstanceOlemlForm;
884    }*/
885
886    /*public boolean validateRequiredFields(WorkEInstanceOlemlForm workEInstanceOlemlForm) {
887        List<Coverage> coverages = workEInstanceOlemlForm.getSelectedEHoldings().getExtentOfOwnership().getCoverages().getCoverage();
888        if (coverages.size() > 0) {
889            for (Coverage coverage : coverages) {
890                String date = coverage.getCoverageStartDate();
891                if (date != null && !date.isEmpty()) {
892                    return true;
893                }
894            }
895        } else {
896            return false;
897        }
898        return false;
899    }*/
900
901    @Override
902    public EditorForm bulkUpdate(EditorForm editorForm, List<String> ids) {
903        WorkEInstanceOlemlForm workEInstanceOlemlForm = (WorkEInstanceOlemlForm) editorForm.getDocumentForm();
904        String docId = editorForm.getDocId();
905        String docType = editorForm.getDocType();
906        String bibId = editorForm.getBibId();
907        String editorStatusMessage = "";
908        //List<BibTree> bibTreeList = null;
909        //BibTree bibTree = null;
910        try {
911            OleHoldings eHoldings = workEInstanceOlemlForm.getSelectedEHoldings();
912            getOleEResourceSearchService().getEResourcesFields(editorForm.geteResourceId(), eHoldings, workEInstanceOlemlForm);
913            getOleEResourceSearchService().getEResourcesLicenseFields(editorForm.geteResourceId(), workEInstanceOlemlForm);
914            String content = getInstanceEditorFormDataHandler().buildHoldingContent(eHoldings);
915            Holdings eHoldingsDoc = new EHoldings();
916            eHoldingsDoc.setCategory(DocCategory.WORK.getCode());
917            eHoldingsDoc.setType(DocType.EHOLDINGS.getCode());
918            eHoldingsDoc.setFormat(DocFormat.OLEML.getCode());
919            String canUpdateStaffOnlyFlag = "false";
920            if (editorForm.getStaffOnlyFlagInGlobalEdit() != null && editorForm.getStaffOnlyFlagInGlobalEdit().equalsIgnoreCase("Y")) {
921                canUpdateStaffOnlyFlag = "true";
922                editorForm.setStaffOnlyFlagForHoldings(true);
923                eHoldingsDoc.setStaffOnly(editorForm.isStaffOnlyFlagForHoldings());
924            }
925            else if (editorForm.getStaffOnlyFlagInGlobalEdit() != null && editorForm.getStaffOnlyFlagInGlobalEdit().equalsIgnoreCase("N")) {
926                canUpdateStaffOnlyFlag = "true";
927                editorForm.setStaffOnlyFlagForHoldings(false);
928                eHoldingsDoc.setStaffOnly(editorForm.isStaffOnlyFlagForHoldings());
929            }
930            eHoldingsDoc.setStaffOnly(editorForm.isStaffOnlyFlagForHoldings());
931            Bib bib = new BibMarc();
932            bib.setId(workEInstanceOlemlForm.getBibId());
933            eHoldingsDoc.setBib(bib);
934            eHoldingsDoc.setContent(content);
935            getDocstoreClientLocator().getDocstoreClient().bulkUpdateHoldings(eHoldingsDoc,ids,canUpdateStaffOnlyFlag);
936            editorForm.setDocId(eHoldingsDoc.getId());
937           // processResponse(eHoldingsDoc.getId(), eHoldingsDoc.getBib().getId(), editorForm.getTokenId());
938            workEInstanceOlemlForm.getSelectedEHoldings().setHoldingsIdentifier(eHoldingsDoc.getId());
939            HoldingsTree holdingsTree = new HoldingsTree();
940            holdingsTree.setHoldings(eHoldingsDoc);
941            List<HoldingsTree> holdingsTreeList = new ArrayList<>();
942            holdingsTreeList.add(holdingsTree);
943            //bibTree.getHoldingsTrees().addAll(holdingsTreeList);
944            editorStatusMessage = "record.create.message";
945        }
946        catch (DocstoreException e) {
947            LOG.error(e);
948            DocstoreException docstoreException = (DocstoreException) e;
949            if (org.apache.commons.lang3.StringUtils.isNotEmpty(docstoreException.getErrorCode())) {
950                GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, docstoreException.getErrorCode());
951            } else {
952                GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, e.getMessage());
953            }
954            return workEInstanceOlemlForm;
955        }
956        catch (Exception e) {
957            LOG.error("Exception :", e);
958            GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, "docstore.response", e.getMessage());
959            return workEInstanceOlemlForm;
960
961        }
962
963
964        try {
965            getEInstanceFormDataHandler().setLocationDetails(workEInstanceOlemlForm);
966            String eResId = workEInstanceOlemlForm.geteResourceId();
967            editorForm.seteResourceId(null);
968            workEInstanceOlemlForm.setViewId("WorkEInstanceViewPage");
969
970            GlobalVariables.getMessageMap().getInfoMessages().clear();
971        }
972        catch (DocstoreException e) {
973            LOG.error(e);
974            DocstoreException docstoreException = (DocstoreException) e;
975            if (org.apache.commons.lang3.StringUtils.isNotEmpty(docstoreException.getErrorCode())) {
976                GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, docstoreException.getErrorCode());
977            } else {
978                GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, e.getMessage());
979            }
980            return workEInstanceOlemlForm;
981        }
982        catch (Exception e) {
983            LOG.error("Exception :", e);
984            GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, "docstore.response", e.getMessage());
985            return workEInstanceOlemlForm;
986        }
987        GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO, editorStatusMessage);
988
989        return workEInstanceOlemlForm;
990    }
991
992}