View Javadoc
1   package org.kuali.ole.describe.controller;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.solr.client.solrj.SolrServerException;
5   import org.kuali.asr.service.ASRHelperServiceImpl;
6   import org.kuali.ole.DocumentUniqueIDPrefix;
7   import org.kuali.ole.OLEConstants;
8   import org.kuali.ole.deliver.bo.ASRItem;
9   import org.kuali.ole.deliver.bo.OleDeliverRequestBo;
10  import org.kuali.ole.deliver.bo.OleLoanDocument;
11  import org.kuali.ole.describe.bo.DocumentSelectionTree;
12  import org.kuali.ole.describe.bo.DocumentTreeNode;
13  import org.kuali.ole.describe.bo.InstanceEditorFormDataHandler;
14  import org.kuali.ole.describe.form.EditorForm;
15  import org.kuali.ole.describe.form.WorkBibMarcForm;
16  import org.kuali.ole.describe.form.WorkInstanceOlemlForm;
17  import org.kuali.ole.describe.keyvalue.LocationValuesBuilder;
18  import org.kuali.ole.docstore.common.client.DocstoreClient;
19  import org.kuali.ole.docstore.common.document.*;
20  import org.kuali.ole.docstore.common.document.HoldingsTree;
21  import org.kuali.ole.docstore.common.document.Item;
22  import org.kuali.ole.docstore.common.document.content.enums.DocCategory;
23  import org.kuali.ole.docstore.common.document.content.enums.DocFormat;
24  import org.kuali.ole.docstore.common.document.content.enums.DocType;
25  import org.kuali.ole.docstore.common.document.content.instance.*;
26  import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
27  import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
28  import org.kuali.ole.docstore.common.exception.DocstoreException;
29  import org.kuali.ole.docstore.engine.client.DocstoreLocalClient;
30  import org.kuali.ole.select.bo.OLESerialReceivingDocument;
31  import org.kuali.ole.select.bo.OLESerialReceivingHistory;
32  import org.kuali.ole.select.businessobject.OleCopy;
33  import org.kuali.ole.service.impl.OLESerialReceivingServiceImpl;
34  import org.kuali.ole.sys.context.SpringContext;
35  import org.kuali.rice.core.api.config.property.ConfigurationService;
36  import org.kuali.rice.core.api.util.RiceConstants;
37  import org.kuali.rice.core.api.util.tree.Node;
38  import org.kuali.rice.kim.api.permission.PermissionService;
39  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
40  import org.kuali.rice.krad.service.BusinessObjectService;
41  import org.kuali.rice.krad.service.KRADServiceLocator;
42  import org.kuali.rice.krad.uif.UifParameters;
43  import org.kuali.rice.krad.util.GlobalVariables;
44  import org.kuali.rice.krad.util.KRADConstants;
45  import org.slf4j.Logger;
46  import org.slf4j.LoggerFactory;
47  
48  import javax.servlet.http.HttpServletRequest;
49  import java.text.Format;
50  import java.text.SimpleDateFormat;
51  import java.util.*;
52  
53  /**
54   * Created with IntelliJ IDEA.
55   * User: sambasivam
56   * Date: 1/7/14
57   * Time: 6:51 PM
58   * To change this template use File | Settings | File Templates.
59   */
60  public class WorkHoldingsOlemlEditor extends AbstractEditor {
61  
62      private static final Logger LOG = LoggerFactory.getLogger(WorkHoldingsOlemlEditor.class);
63      private InstanceEditorFormDataHandler instanceEditorFormDataHandler = null;
64      private static WorkHoldingsOlemlEditor workHoldingsOlemlEditor = null;
65      private ItemOlemlRecordProcessor itemOlemlRecordProcessor ;
66      private ASRHelperServiceImpl asrHelperService ;
67      private BusinessObjectService businessObjectService ;
68      private DocstoreClient docstoreClient = getDocstoreLocalClient();
69  
70      public static WorkHoldingsOlemlEditor getInstance() {
71          if (workHoldingsOlemlEditor == null) {
72              workHoldingsOlemlEditor = new WorkHoldingsOlemlEditor();
73          }
74          return workHoldingsOlemlEditor;
75      }
76  
77      public ItemOlemlRecordProcessor getItemOlemlRecordProcessor(){
78          if(itemOlemlRecordProcessor == null){
79              itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
80          }
81          return itemOlemlRecordProcessor;
82      }
83  
84  
85      public ASRHelperServiceImpl getAsrHelperService(){
86          if(asrHelperService == null){
87              asrHelperService = new ASRHelperServiceImpl();
88          }
89          return asrHelperService;
90  
91      }
92  
93      public BusinessObjectService getBusinessObjectService(){
94          if(businessObjectService == null){
95            businessObjectService = KRADServiceLocator.getBusinessObjectService();
96          }
97          return businessObjectService;
98      }
99  
100     private WorkHoldingsOlemlEditor() {
101 
102     }
103 
104     @Override
105     public EditorForm loadDocument(EditorForm editorForm) {
106 
107         //TODO: set title info
108         WorkInstanceOlemlForm workInstanceOlemlForm = new WorkInstanceOlemlForm();
109         String directory = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(org.kuali.ole.sys.OLEConstants.EXTERNALIZABLE_HELP_URL_KEY);
110         editorForm.setExternalHelpUrl(directory+"/reference/webhelp/OLE/content/ch04s01.html#_Instance_Editor_1");
111         editorForm.setHeaderText("Holdings");
112         editorForm.setHasLink(true);
113         editorForm.setShowEditorFooter(true);
114         String parameter = getParameter(OLEConstants.APPL_ID_OLE, OLEConstants.DESC_NMSPC, OLEConstants
115                 .DESCRIBE_COMPONENT, OLEConstants.HOLDINGS_SUPRESS_SHELVINGORDER);
116         if(Boolean.valueOf(parameter)){
117             editorForm.setSupressHoldingsShelving(false);
118         }else{
119             editorForm.setSupressHoldingsShelving(true);
120         }
121         String bibId = editorForm.getBibId();
122         List<BibTree> bibTreeList = new ArrayList();
123         Date date = new Date();
124         SimpleDateFormat sdf = new SimpleDateFormat(RiceConstants.SIMPLE_DATE_FORMAT_FOR_DATE+" HH:mm:ss");
125         String dateStr = sdf.format(date);
126         BibTree bibTree = null;
127         if (!StringUtils.isNotEmpty(bibId)) {
128             workInstanceOlemlForm.setMessage("Error: Invalid bibId id:" + bibId);
129             GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO, "invalid.bib");
130             return workInstanceOlemlForm;
131         }
132 
133         Bib bib = null;
134         try {
135             bibTree = docstoreClient.retrieveBibTree(bibId);
136         }catch (DocstoreException e) {
137             LOG.error("Exception : ", e);
138             DocstoreException docstoreException = (DocstoreException) e;
139             if (org.apache.commons.lang3.StringUtils.isNotEmpty(docstoreException.getErrorCode())) {
140                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, docstoreException.getErrorCode());
141             } else {
142                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, e.getMessage());
143             }
144             return workInstanceOlemlForm;
145         } catch (Exception e) {
146             LOG.error("Exception ", e);
147             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS,"docstore.response", e.getMessage() );
148         }
149           bibTreeList.add(bibTree);
150           workInstanceOlemlForm.setBibTreeList(bibTreeList);
151           bib = bibTree.getBib();
152           String titleField = bib.getTitle() + " / " + bib.getAuthor() + " / " + DocumentUniqueIDPrefix.getDocumentId(bib.getId());
153           editorForm.setTitle(titleField);
154           editorForm.setHasLink(true);
155         if (!isValidBib(bib.getContent())) {
156             workInstanceOlemlForm.setMessage("Error: Invalid bibId id:" + bibId);
157             GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO, "invalid.bib");
158             return workInstanceOlemlForm;
159         }
160 
161             String docId = editorForm.getDocId();
162             if (StringUtils.isNotEmpty(docId)) {
163                 editorForm.setHoldingLocalIdentifier(DocumentUniqueIDPrefix.getDocumentId(editorForm.getDocId()));
164                 Holdings holdings = null;
165                 try {
166                     holdings = docstoreClient.retrieveHoldings(editorForm.getDocId());
167                 }catch (DocstoreException e) {
168                     LOG.error("Exception : ", e);
169                     DocstoreException docstoreException = (DocstoreException) e;
170                     if (org.apache.commons.lang3.StringUtils.isNotEmpty(docstoreException.getErrorCode())) {
171                         GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, docstoreException.getErrorCode());
172                     } else {
173                         GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, e.getMessage());
174                     }
175                     return workInstanceOlemlForm;
176                 } catch (Exception e) {
177                     LOG.error("Exception ", e);
178                     GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS,"docstore.response", e.getMessage() );
179                 }                String docStoreData = holdings.getContent();
180                 OleHoldings oleHoldings = null;
181                 if (StringUtils.isNotEmpty(docStoreData)) {
182                     oleHoldings = new HoldingOlemlRecordProcessor().fromXML(docStoreData);
183                     editorForm.setStaffOnlyFlagForHoldings(holdings.isStaffOnly());
184                     editorForm.setHoldingCreatedDate(holdings.getCreatedOn());
185                     editorForm.setHoldingCreatedBy(holdings.getCreatedBy());
186                     editorForm.setHoldingUpdatedDate(holdings.getUpdatedOn());
187                     editorForm.setHoldingUpdatedBy(holdings.getUpdatedBy());
188 //                  editorForm.setHoldingLocalIdentifier(holdings.getLocalId());
189 
190                     ensureMultipleValuesInOleHoldings(oleHoldings);
191 
192                     workInstanceOlemlForm.setSelectedHolding(oleHoldings);
193                     getInstanceEditorFormDataHandler().setLocationDetails(workInstanceOlemlForm);
194                 } else {
195                     GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, "ds.error.holdings.notfound");
196                     workInstanceOlemlForm.setViewId("WorkHoldingsViewPage");
197                     return workInstanceOlemlForm;
198                 }
199             //workInstanceOlemlForm.setMessage("Holdings record loaded successfully.");
200             if (editorForm.getEditable().equalsIgnoreCase("false")) {
201                 if(editorForm.getMethodToCall() != null && editorForm.getMethodToCall().equalsIgnoreCase("copy")){
202                     GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO,"record.copy.instance.message");
203                 } else {
204                     GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO,"holdings.record.load.message");
205                 }
206             } else {
207                 boolean hasPermission = canEditInstance(GlobalVariables.getUserSession().getPrincipalId());
208                 if (hasPermission) {
209                     if(editorForm.getMethodToCall() != null && editorForm.getMethodToCall().equalsIgnoreCase("copyInstance")){
210                         GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO,"record.copy.instance.message");
211                     } else {
212                         GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO, "holdings.record.load.message");
213                     }
214                 } else {
215                     editorForm.setHideFooter(false);
216                     GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_INFO, OLEConstants.ERROR_EDIT_INSTANCE);
217                 }
218             }
219         } else {
220             editorForm.setHoldingLocalIdentifier("");
221             editorForm.setHoldingUpdatedBy("");
222             editorForm.setHoldingUpdatedDate("");
223             editorForm.setStaffOnlyFlagForHoldings(false);
224             editorForm.setHoldingCreatedBy(GlobalVariables.getUserSession().getPrincipalName());
225             editorForm.setHoldingCreatedDate(dateStr);
226             boolean hasPermission = canCreateInstance(GlobalVariables.getUserSession().getPrincipalId());
227             if (hasPermission) {
228                 GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO, "holdings.record.new.load.message");
229             } else {
230                 editorForm.setHideFooter(false);
231                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_INFO, OLEConstants.ERROR_CREATE_INSTANCE);
232             }
233         }
234         if (editorForm.getDocId() != null) {
235             OLESerialReceivingServiceImpl oleSerialReceivingService = new OLESerialReceivingServiceImpl();
236             Map<String, String> map = new HashMap<>();
237             map.put("instanceId", editorForm.getDocId());
238             List<OLESerialReceivingDocument> oleSerialReceivingDocuments = (List<OLESerialReceivingDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OLESerialReceivingDocument.class, map);
239             for (OLESerialReceivingDocument oleSerialReceivingDocument : oleSerialReceivingDocuments) {
240                 if (oleSerialReceivingDocument.isActive()) {
241                     editorForm.setSerialFlag(true);
242                     oleSerialReceivingService.readReceivingRecordType(oleSerialReceivingDocument);
243                     oleSerialReceivingService.updateEnumCaptionValues(oleSerialReceivingDocument, null);
244                     if (oleSerialReceivingDocument.getOleSerialReceivingHistoryList() != null && oleSerialReceivingDocument.getOleSerialReceivingHistoryList().size() > 0) {
245                         for (int serialReceivingHistoryList = 0; serialReceivingHistoryList < oleSerialReceivingDocument.getOleSerialReceivingHistoryList().size(); serialReceivingHistoryList++) {
246                             oleSerialReceivingService.setEnumerationAndChronologyValues(oleSerialReceivingDocument.getOleSerialReceivingHistoryList().get(serialReceivingHistoryList));
247                         }
248                     }
249                     if (oleSerialReceivingDocument.getUnboundLocation() != null) {
250                         editorForm.setUnboundLocation(oleSerialReceivingDocument.getUnboundLocation());
251                     }
252                     oleSerialReceivingService.sortById(oleSerialReceivingDocument.getOleSerialReceivingHistoryList());
253                     for (OLESerialReceivingHistory oleSerialReceivingHistory : oleSerialReceivingDocument.getOleSerialReceivingHistoryList()) {
254                         if (oleSerialReceivingHistory.isPublicDisplay()) {
255                             if (oleSerialReceivingHistory.getReceivingRecordType().equalsIgnoreCase("Main")) {
256                                 editorForm.getMainSerialReceivingHistoryList().add(oleSerialReceivingHistory);
257                             }
258                             if (oleSerialReceivingHistory.getReceivingRecordType().equalsIgnoreCase("Supplementary")) {
259                                 editorForm.getSupplementSerialReceivingHistoryList().add(oleSerialReceivingHistory);
260                             }
261                             if (oleSerialReceivingHistory.getReceivingRecordType().equalsIgnoreCase("Index")) {
262                                 editorForm.getIndexSerialReceivingHistoryList().add(oleSerialReceivingHistory);
263                             }
264                         }
265                     }
266                 }
267             }
268         }
269         workInstanceOlemlForm.setViewId("WorkHoldingsViewPage");
270         return workInstanceOlemlForm;
271     }
272 
273     private void ensureMultipleValuesInOleHoldings(OleHoldings oleHoldings) {
274         List<ExtentOfOwnership> extentOfOwnerships = ensureAtleastOneExtentOfOwnership(
275                 oleHoldings.getExtentOfOwnership());
276         oleHoldings.setExtentOfOwnership(extentOfOwnerships);
277 
278         List<Note> notes = ensureAtleastOneNote(oleHoldings.getNote());
279         oleHoldings.setNote(notes);
280 
281         List<Uri> uriList = ensureAtleastOneUri(oleHoldings.getUri());
282         oleHoldings.setUri(uriList);
283     }
284 
285     @Override
286     public EditorForm saveDocument(EditorForm editorForm) {
287         WorkInstanceOlemlForm workInstanceOlemlForm = (WorkInstanceOlemlForm) editorForm.getDocumentForm();
288         String bibId = editorForm.getBibId();
289         List<BibTree> bibTreeList = null;
290         BibTree bibTree = null;
291         HoldingsTree holdingsTree = new HoldingsTree();
292         String editorMessage = "";
293         Bib bib = null;
294         Date date = new Date();
295         SimpleDateFormat sdf = new SimpleDateFormat(RiceConstants.SIMPLE_DATE_FORMAT_FOR_DATE+" HH:mm:ss");
296         String dateStr = sdf.format(date);
297         String user = GlobalVariables.getUserSession().getPrincipalName();
298         try {
299             bibTree = docstoreClient.retrieveBibTree(bibId);
300         }catch (DocstoreException e) {
301             LOG.error("Exception : ", e);
302             DocstoreException docstoreException = (DocstoreException) e;
303             if (org.apache.commons.lang3.StringUtils.isNotEmpty(docstoreException.getErrorCode())) {
304                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, docstoreException.getErrorCode());
305             } else {
306                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, e.getMessage());
307             }
308             return workInstanceOlemlForm;
309         } catch (Exception e) {
310             LOG.error("Exception ", e);
311             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS,"docstore.response", e.getMessage() );
312         }
313         bibTreeList = new ArrayList();
314         bibTreeList.add(bibTree);
315         workInstanceOlemlForm.setBibTreeList(bibTreeList);
316         bib = bibTree.getBib();
317         String titleField = bib.getTitle() + " / " + bib.getAuthor() + " / " + DocumentUniqueIDPrefix.getDocumentId(bib.getId());
318         editorForm.setTitle(titleField);
319         editorForm.setHasLink(true);
320 //        editorForm.setHeaderText("Instance Editor (Holdings) - OLEML Format");
321         editorForm.setHeaderText("Holdings");
322         editorForm.setShowEditorFooter(true);
323         String docId = editorForm.getDocId();
324         if (StringUtils.isNotEmpty(docId)) {
325             List<HoldingsTree> holdingsTreeList = new ArrayList<>();
326             for(HoldingsTree holdingsTree1 : bibTree.getHoldingsTrees()) {
327                 if(!holdingsTree1.getHoldings().getId().equals(docId)) {
328                     holdingsTreeList.add(holdingsTree1);
329                 }
330             }
331             bibTree.getHoldingsTrees().clear();
332             bibTree.getHoldingsTrees().addAll(holdingsTreeList);
333             Holdings holdings = new PHoldings();
334             OleHoldings holdingData = workInstanceOlemlForm.getSelectedHolding();
335             if (!isValidHoldingsData(workInstanceOlemlForm)) {
336                 return workInstanceOlemlForm;
337             }
338             try {
339                 String holdingXmlContent = getInstanceEditorFormDataHandler().buildHoldingContent(holdingData);
340                 holdings.setContent(holdingXmlContent);
341             } catch (Exception e) {
342                 LOG.error("Exception :", e);
343                 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS,
344                         "docstore.response", e.getMessage());
345                 getInstanceEditorFormDataHandler().setLocationDetails(workInstanceOlemlForm);
346                 return workInstanceOlemlForm;
347             }
348             holdings.setId(docId);
349             holdings.setStaffOnly(editorForm.isStaffOnlyFlagForHoldings());
350             holdings.setCreatedBy(editorForm.getCreatedBy());
351             holdings.setCreatedOn(editorForm.getCreatedDate());
352             holdings.setUpdatedBy(user);
353             holdings.setUpdatedOn(dateStr);
354             String holdingsCreatedDate = null;
355             Format formatter = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss");
356             holdingsCreatedDate = formatter.format(new Date());
357             holdings.setLastUpdated(holdingsCreatedDate);
358             holdings.setBib(bib);
359             holdings.setCategory(editorForm.getDocCategory());
360             holdings.setType(editorForm.getDocType());
361             holdings.setFormat(editorForm.getDocFormat());
362             long startTime = System.currentTimeMillis();
363             try {
364                 docstoreClient.updateHoldings(holdings);
365             } catch (Exception e) {
366                 LOG.error("Exception :", e);
367                 DocstoreException docstoreException = (DocstoreException) e;
368                 if (org.apache.commons.lang3.StringUtils.isNotEmpty(docstoreException.getErrorCode())) {
369                     GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, docstoreException.getErrorCode());
370                 } else {
371                     GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, e.getMessage());
372                 }
373                 getInstanceEditorFormDataHandler().setLocationDetails(workInstanceOlemlForm);
374                 return workInstanceOlemlForm;
375             }
376             long endTime = System.currentTimeMillis();
377             editorForm.setSolrTime(String.valueOf((endTime-startTime)/1000));
378             editorForm.setUpdatedDate(holdings.getUpdatedOn());
379             editorForm.setUpdatedBy(holdings.getUpdatedBy());
380             getInstanceEditorFormDataHandler().setLocationDetails(workInstanceOlemlForm);
381             editorMessage = "holdings.record.update.message";
382             workInstanceOlemlForm.setViewId("WorkHoldingsViewPage");
383         } else {
384 
385             if (!isValidHoldingsData(workInstanceOlemlForm)) {
386                 return workInstanceOlemlForm;
387             }
388             String content = null;
389             try {
390                 content = getInstanceEditorFormDataHandler().buildHoldingContent(workInstanceOlemlForm.getSelectedHolding());
391             } catch (Exception e) {
392                 LOG.error("Exception :", e);
393                 e.printStackTrace();
394                 return workInstanceOlemlForm;
395             }
396 
397             String staffOnlyFlagForHoldings = String.valueOf(editorForm.isStaffOnlyFlagForHoldings());
398 
399 //            String content = getInstanceEditorFormDataHandler()
400 //                    .buildInstanceRecordForDocStore(workInstanceOlemlForm, docId, staffOnlyFlagForHoldings);
401             Holdings holdings = new PHoldings();
402             holdings.setCategory(DocCategory.WORK.getCode());
403             holdings.setType(DocType.HOLDINGS.getCode());
404             holdings.setFormat(DocFormat.OLEML.getCode());
405             holdings.setCreatedOn(dateStr);
406             holdings.setCreatedBy(user);
407             holdings.setStaffOnly(editorForm.isStaffOnlyFlagForHoldings());
408             holdings.setContent(content);
409             holdings.setBib(bib);
410 
411             holdingsTree.setHoldings(holdings);
412             holdingsTree.getItems().add(getItemRecord());
413             //TODO: set additional attributes
414             long startTime = System.currentTimeMillis();
415             try {
416                 docstoreClient.createHoldingsTree(holdingsTree);
417                 editorForm.setDocId(holdingsTree.getHoldings().getId());
418                 editorForm.setHoldingCreatedBy(holdingsTree.getHoldings().getCreatedBy());
419                 editorForm.setHoldingCreatedDate(holdingsTree.getHoldings().getCreatedOn());
420                 if (workInstanceOlemlForm.getBibTreeList() == null && workInstanceOlemlForm.getBibTreeList().size() == 0) {
421                     bibTreeList = new ArrayList<>();
422                     bibTree = new BibTree();
423                     bibTree.setBib(bib);
424                     bibTreeList.add(bibTree);
425                     workInstanceOlemlForm.setBibTreeList(bibTreeList);
426                 } else {
427                     bibTreeList = workInstanceOlemlForm.getBibTreeList();
428                     bibTree = bibTreeList.get(0);
429                 }
430 
431                 List<HoldingsTree> holdingsTreeList = new ArrayList<>();
432                 holdingsTreeList.add(holdingsTree);
433 //                bibTree.getHoldingsTrees().addAll(holdingsTreeList);
434             }catch (DocstoreException e) {
435                 LOG.error("Exception : ", e);
436                 DocstoreException docstoreException = (DocstoreException) e;
437                 if (org.apache.commons.lang3.StringUtils.isNotEmpty(docstoreException.getErrorCode())) {
438                     GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, docstoreException.getErrorCode());
439                 } else {
440                     GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, e.getMessage());
441                 }
442                 getInstanceEditorFormDataHandler().setLocationDetails(workInstanceOlemlForm);
443                 return workInstanceOlemlForm;
444             } catch (Exception e) {
445                 LOG.error("Exception ", e);
446                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS,"docstore.response", e.getMessage() );
447             }
448             long endTime = System.currentTimeMillis();
449             editorForm.setSolrTime(String.valueOf((endTime-startTime)/1000));
450             getInstanceEditorFormDataHandler().setLocationDetails(workInstanceOlemlForm);
451             editorMessage =  "record.create.message";
452         }
453 
454 
455 // Separated  ASR code
456         if (processAsr(editorForm, workInstanceOlemlForm, bibTree, holdingsTree, editorMessage, bib)){
457             return workInstanceOlemlForm;
458         }
459 
460         return workInstanceOlemlForm;
461     }
462 
463     private boolean processAsr(EditorForm editorForm, WorkInstanceOlemlForm workInstanceOlemlForm, BibTree bibTree, HoldingsTree holdingsTree, String editorMessage, Bib bib) {
464         try {
465             if(holdingsTree.getHoldings() == null) {
466                 holdingsTree = docstoreClient.retrieveHoldingsTree(editorForm.getDocId());
467             }
468             String holdingLocation = null;
469             if (workInstanceOlemlForm.getSelectedHolding() != null && workInstanceOlemlForm.getSelectedHolding().getLocation() != null && workInstanceOlemlForm.getSelectedHolding().getLocation().getLocationLevel() != null) {
470                 holdingLocation = instanceEditorFormDataHandler.getLocationCode(workInstanceOlemlForm.getSelectedHolding().getLocation().getLocationLevel());
471             }
472             String callNumber;
473             String prefix;
474             if(holdingLocation != null){
475                 if(getAsrHelperService().isAnASRItem(holdingLocation)){
476                     List<Item> items = holdingsTree.getItems();
477                     List<ASRItem> asrItems = new ArrayList<ASRItem>();
478                     ASRItem asrItem = null;
479                     if(items != null && items.size()>0){
480                         for(Item item : items){
481                             org.kuali.ole.docstore.common.document.content.instance.Item itemData = getItemOlemlRecordProcessor().fromXML(item.getContent());
482                             asrItem = new ASRItem();
483                             if(itemData.getAccessInformation()!=null && itemData.getAccessInformation().getBarcode()!=null){
484                                 asrItem.setItemBarcode(itemData.getAccessInformation().getBarcode());
485                             }
486 
487                             Map<String,String> asrItemMap = new HashMap<String,String>();
488                             asrItemMap.put("itemBarcode", asrItem.getItemBarcode());
489                             List<ASRItem> existingASRItems = (List<ASRItem>)getBusinessObjectService().findMatching(ASRItem.class,asrItemMap);
490 
491                             if(existingASRItems.size()==0){
492                             if(bib.getTitle()!=null){
493                                 asrItem.setTitle((bib.getTitle().length()>37)?bib.getTitle().substring(0,36):bib.getTitle());
494                             }
495                             if(bib.getAuthor()!=null){
496                                 asrItem.setAuthor((bib.getAuthor().length()>37)?bib.getAuthor().substring(0,36):bib.getAuthor());
497                             }
498                             if (itemData.getCallNumber() != null && itemData.getCallNumber().getNumber() != null && !itemData.getCallNumber().getNumber().isEmpty()) {
499                                     callNumber=(itemData.getCallNumber().getNumber().length() > 37) ? itemData.getCallNumber().getNumber().substring(0, 36) : itemData.getCallNumber().getNumber();
500                                     prefix=itemData.getCallNumber().getPrefix()!=null&&!itemData.getCallNumber().getPrefix().isEmpty()?itemData.getCallNumber().getPrefix():"";
501                                     asrItem.setCallNumber(prefix+" "+callNumber);
502                             } else if(workInstanceOlemlForm.getSelectedHolding() !=null && workInstanceOlemlForm.getSelectedHolding().getCallNumber() !=null &&
503                                     workInstanceOlemlForm.getSelectedHolding().getCallNumber().getNumber() !=null && !workInstanceOlemlForm.getSelectedHolding().getCallNumber().getNumber().isEmpty()){
504                                 callNumber=(workInstanceOlemlForm.getSelectedHolding().getCallNumber().getNumber().length() > 37) ? workInstanceOlemlForm.getSelectedHolding().getCallNumber().getNumber().substring(0, 36) : workInstanceOlemlForm.getSelectedHolding().getCallNumber().getNumber();
505                                 prefix=workInstanceOlemlForm.getSelectedHolding().getCallNumber().getPrefix()!=null&&!workInstanceOlemlForm.getSelectedHolding().getCallNumber().getPrefix().isEmpty()?workInstanceOlemlForm.getSelectedHolding().getCallNumber().getPrefix():"";
506                                 asrItem.setCallNumber(prefix+" "+callNumber);
507                             }
508 
509                             asrItems.add(asrItem);
510                         }
511                         }
512                     }
513                     getBusinessObjectService().save(asrItems);
514                 }
515             }
516             Holdings holdings = holdingsTree.getHoldings();
517             bibTree.getHoldingsTrees().add(holdingsTree);
518             Collections.sort(bibTree.getHoldingsTrees());
519             String docStoreData  = holdings.getContent();
520             OleHoldings oleHoldings = new HoldingOlemlRecordProcessor().fromXML(docStoreData);
521             workInstanceOlemlForm.setSelectedHolding(oleHoldings);
522             getInstanceEditorFormDataHandler().setLocationDetails(workInstanceOlemlForm);
523             editorForm.setHoldingLocalIdentifier(DocumentUniqueIDPrefix.getDocumentId(holdings.getId()));
524             editorForm.setHoldingUpdatedBy(holdings.getUpdatedBy());
525             editorForm.setHoldingCreatedDate(holdings.getCreatedOn());
526             editorForm.setHoldingCreatedBy(holdings.getCreatedBy());
527             editorForm.setHoldingUpdatedDate(holdings.getUpdatedOn());
528             GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_MESSAGES, editorMessage);
529             ensureMultipleValuesInOleHoldings(oleHoldings);
530         }catch (DocstoreException e) {
531             LOG.error("Exception : ", e);
532             DocstoreException docstoreException = (DocstoreException) e;
533             if (org.apache.commons.lang3.StringUtils.isNotEmpty(docstoreException.getErrorCode())) {
534                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, docstoreException.getErrorCode());
535             } else {
536                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, e.getMessage());
537             }
538             getInstanceEditorFormDataHandler().setLocationDetails(workInstanceOlemlForm);
539             return true;
540         } catch (Exception e) {
541             LOG.error("Exception ", e);
542             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS,"docstore.response", e.getMessage() );
543         }
544         return false;
545     }
546 
547     private List<Uri> ensureAtleastOneUri(List<Uri> uris) {
548         if (uris == null) {
549             uris = new ArrayList<Uri>();
550         }
551         if (uris.size() == 0) {
552             Uri uri = new Uri();
553             uris.add(uri);
554         }
555         return uris;
556     }
557 
558     private List<Note> ensureAtleastOneNote(List<Note> notes) {
559         if (notes == null) {
560             notes = new ArrayList<Note>();
561         }
562         if (notes.size() == 0) {
563             Note note = new Note();
564             notes.add(note);
565         }
566         return notes;
567     }
568 
569 
570     private List<ExtentOfOwnership> ensureAtleastOneExtentOfOwnership(List<ExtentOfOwnership> extentOfOwnerships) {
571         if (extentOfOwnerships == null) {
572             extentOfOwnerships = new ArrayList<ExtentOfOwnership>();
573         }
574         if (extentOfOwnerships.size() == 0) {
575             ExtentOfOwnership extentOfOwnership = new ExtentOfOwnership();
576             extentOfOwnership.getNote().add(new Note());
577             extentOfOwnerships.add(0, extentOfOwnership);
578         }
579         return extentOfOwnerships;
580     }
581 
582     /**
583      * Gets the InstanceEditorFormDataHandler attribute.
584      *
585      * @return Returns InstanceEditorFormDataHandler.
586      */
587     private InstanceEditorFormDataHandler getInstanceEditorFormDataHandler() {
588         if (null == instanceEditorFormDataHandler) {
589             instanceEditorFormDataHandler = new InstanceEditorFormDataHandler();
590         }
591         return instanceEditorFormDataHandler;
592     }
593 
594     private boolean canCreateInstance(String principalId) {
595         PermissionService service = KimApiServiceLocator.getPermissionService();
596         return service.hasPermission(principalId, OLEConstants.CAT_NAMESPACE, OLEConstants.INSTANCE_EDITOR_ADD_INSTANCE);
597     }
598 
599     private boolean canEditInstance(String principalId) {
600         PermissionService service = KimApiServiceLocator.getPermissionService();
601         return service.hasPermission(principalId, OLEConstants.CAT_NAMESPACE, OLEConstants.INSTANCE_EDITOR_EDIT_INSTANCE);
602     }
603     /**
604      * Validates the Holdings data and returns true if it is valid
605      *
606      * @param workInstanceOlemlForm
607      * @return
608      */
609     private boolean isValidHoldingsData(WorkInstanceOlemlForm workInstanceOlemlForm) {
610         OleHoldings oleHoldings = workInstanceOlemlForm.getSelectedHolding();
611         String location = null;
612         if (oleHoldings != null && oleHoldings.getLocation() != null && oleHoldings.getLocation().getLocationLevel() != null) {
613             location = oleHoldings.getLocation().getLocationLevel().getName();
614         }
615         if (location != null && location.length() != 0 && !isValidLocation(location)) {
616             //workInstanceOlemlForm.setMessage("<font size='3' color='red'>Please enter valid location.</font>");
617             GlobalVariables.getMessageMap().putError("documentForm.selectedHolding.location.locationLevel.name", "error.location");
618             workInstanceOlemlForm.setValidInput(false);
619             return false;
620         }
621         return true;
622     }
623 
624     private boolean isValidLocation(String location) {
625 
626         List<String> locationList = LocationValuesBuilder.retrieveLocationDetailsForSuggest(location);
627         if (locationList != null && locationList.size() > 0) {
628             for (String locationValue : locationList) {
629                 if (locationValue.equalsIgnoreCase(location)) {
630                     return true;
631                 }
632             }
633         }
634         return false;
635     }
636 
637     @Override
638     public EditorForm addORRemoveExtentOfOwnership(EditorForm editorForm, HttpServletRequest request) {
639         String methodName = request.getParameter("methodToCall");
640         if (methodName.equalsIgnoreCase("addExtentOfOwnership")) {
641             WorkInstanceOlemlForm workInstanceOlemlForm = (WorkInstanceOlemlForm) editorForm.getDocumentForm();
642             int index = Integer.parseInt(editorForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
643             index++;
644             List<ExtentOfOwnership> extentOfOwnershipForUI = workInstanceOlemlForm.getSelectedHolding().getExtentOfOwnership();
645             ExtentOfOwnership extentOfOwnership = new ExtentOfOwnership();
646             extentOfOwnership.getNote().add(new Note());
647             extentOfOwnershipForUI.add(index, extentOfOwnership);
648             editorForm.setDocumentForm(workInstanceOlemlForm);
649         } else if (methodName.equalsIgnoreCase("removeExtentOfOwnership")) {
650             WorkInstanceOlemlForm workInstanceOlemlForm = (WorkInstanceOlemlForm) editorForm.getDocumentForm();
651             int index = Integer.parseInt(editorForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
652             List<ExtentOfOwnership> extentOfOwnershipForUI = workInstanceOlemlForm.getSelectedHolding().getExtentOfOwnership();
653             if (extentOfOwnershipForUI.size() > 1) {
654                 extentOfOwnershipForUI.remove(index);
655             } else {
656                 if (extentOfOwnershipForUI.size() == 1) {
657                     extentOfOwnershipForUI.remove(index);
658                     ExtentOfOwnership extentOfOwnership = new ExtentOfOwnership();
659                     extentOfOwnershipForUI.add(extentOfOwnership);
660                     extentOfOwnership.getNote().add(new Note());
661                 }
662             }
663             editorForm.setDocumentForm(workInstanceOlemlForm);
664         } else if (methodName.equalsIgnoreCase("addEOWHoldingNotes")) {
665             WorkInstanceOlemlForm workInstanceOlemlForm = (WorkInstanceOlemlForm) editorForm.getDocumentForm();
666             int index = Integer.parseInt(editorForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
667             String selectedPath = editorForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
668             int selectedExtentIndex = Integer.parseInt(StringUtils.substring(selectedPath,
669                     (StringUtils.indexOf(selectedPath, "[") + 1),
670                     StringUtils.lastIndexOf(selectedPath, "]")));
671             index++;
672             List<Note> holdingsNote = workInstanceOlemlForm.getSelectedHolding().getExtentOfOwnership()
673                     .get(selectedExtentIndex).getNote();
674             holdingsNote.add(index, new Note());
675             editorForm.setDocumentForm(workInstanceOlemlForm);
676         } else if (methodName.equalsIgnoreCase("removeEOWHoldingNotes")) {
677             WorkInstanceOlemlForm workInstanceOlemlForm = (WorkInstanceOlemlForm) editorForm.getDocumentForm();
678             int index = Integer.parseInt(editorForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
679             String selectedPath = editorForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
680             int selectedExtentIndex = Integer.parseInt(StringUtils.substring(selectedPath,
681                     (StringUtils.indexOf(selectedPath, "[") + 1),
682                     StringUtils.lastIndexOf(selectedPath, "]")));
683             List<Note> holdingsNote = workInstanceOlemlForm.getSelectedHolding().getExtentOfOwnership()
684                     .get(selectedExtentIndex).getNote();
685             if (holdingsNote.size() > 1) {
686                 holdingsNote.remove(index);
687             } else {
688                 if (holdingsNote.size() == 1) {
689                     holdingsNote.remove(index);
690                     Note note = new Note();
691                     holdingsNote.add(note);
692                 }
693             }
694             editorForm.setDocumentForm(workInstanceOlemlForm);
695         }
696         return editorForm;
697     }
698 
699     @Override
700     public EditorForm addORRemoveAccessInformationAndHoldingsNotes(EditorForm editorForm, HttpServletRequest request) {
701         String methodName = request.getParameter("methodToCall");
702         if (methodName.equalsIgnoreCase("addAccessInformation")) {
703             WorkInstanceOlemlForm workInstanceOlemlForm = (WorkInstanceOlemlForm) editorForm.getDocumentForm();
704             int index = Integer.parseInt(editorForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
705             index++;
706             List<Uri> accessInformation = workInstanceOlemlForm.getSelectedHolding().getUri();
707             accessInformation.add(index, new Uri());
708             editorForm.setDocumentForm(workInstanceOlemlForm);
709         } else if (methodName.equalsIgnoreCase("removeAccessInformation")) {
710             WorkInstanceOlemlForm workInstanceOlemlForm = (WorkInstanceOlemlForm) editorForm.getDocumentForm();
711             int index = Integer.parseInt(editorForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
712             List<Uri> accessInformation = workInstanceOlemlForm.getSelectedHolding().getUri();
713             if (accessInformation.size() > 1) {
714                 accessInformation.remove(index);
715             } else {
716                 if (accessInformation.size() == 1) {
717                     accessInformation.remove(index);
718                     Uri uri = new Uri();
719                     accessInformation.add(uri);
720                 }
721             }
722             editorForm.setDocumentForm(workInstanceOlemlForm);
723         } else if (methodName.equalsIgnoreCase("addHoldingNotes")) {
724             WorkInstanceOlemlForm workInstanceOlemlForm = (WorkInstanceOlemlForm) editorForm.getDocumentForm();
725             int index = Integer.parseInt(editorForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
726             index++;
727             List<Note> holdingsNote = workInstanceOlemlForm.getSelectedHolding().getNote();
728             holdingsNote.add(index, new Note());
729             editorForm.setDocumentForm(workInstanceOlemlForm);
730         } else if (methodName.equalsIgnoreCase("removeHoldingNotes")) {
731             WorkInstanceOlemlForm workInstanceOlemlForm = (WorkInstanceOlemlForm) editorForm.getDocumentForm();
732             int index = Integer.parseInt(editorForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
733             List<Note> holdingsNote = workInstanceOlemlForm.getSelectedHolding().getNote();
734             if (holdingsNote.size() > 1) {
735                 holdingsNote.remove(index);
736             } else {
737                 if (holdingsNote.size() == 1) {
738                     holdingsNote.remove(index);
739                     Note note = new Note();
740                     holdingsNote.add(note);
741                 }
742             }
743             editorForm.setDocumentForm(workInstanceOlemlForm);
744         }
745         return editorForm;
746     }
747 
748     private boolean isValidBib(String bibData) {
749         if (bibData.contains("collection") || bibData.contains("OAI-PMH")) {
750             return true;
751         }
752         return false;
753     }
754 
755     @Override
756     public EditorForm createNewRecord(EditorForm editorForm, BibTree bibTree) {
757 
758         if (editorForm.getDocumentForm().getViewId().equalsIgnoreCase("WorkHoldingsViewPage")) {
759             editorForm.setNeedToCreateInstance(true);
760         }
761         editNewRecord(editorForm, bibTree);
762         return editorForm.getDocumentForm();
763     }
764 
765 
766     @Override
767     public EditorForm editNewRecord(EditorForm editorForm, BibTree bibTree) {
768         WorkInstanceOlemlForm workInstanceOlemlForm = new WorkInstanceOlemlForm();
769         if ((editorForm.getDocumentForm() instanceof WorkInstanceOlemlForm)) {
770             workInstanceOlemlForm = (WorkInstanceOlemlForm) editorForm.getDocumentForm();
771             workInstanceOlemlForm.setViewId(editorForm.getDocumentForm().getViewId());
772         }
773 
774         workInstanceOlemlForm.setDocCategory("work");
775         workInstanceOlemlForm.setDocType("holdings");
776         workInstanceOlemlForm.setDocFormat("oleml");
777 
778         if (bibTree != null && bibTree.getHoldingsTrees() != null) {
779             HoldingsTree holdingsTree = bibTree.getHoldingsTrees().get(0);
780             HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
781             ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
782             OleHoldings oleHoldings = holdingOlemlRecordProcessor.fromXML(holdingsTree.getHoldings().getContent());
783             if (editorForm.getDocumentForm().getViewId().equalsIgnoreCase("WorkHoldingsViewPage")) {
784 //                editorForm.setHeaderText("Import Bib Step-4 Instance Editor (Item)- OLEML Format");
785                 editorForm.setHeaderText("Import Bib Step-4 Item");
786                 // validate user entered holding data before going to item tab
787                 if (!isValidHoldingsData(workInstanceOlemlForm)) {
788                     return workInstanceOlemlForm;
789                 }
790                 holdingsTree.getHoldings().setStaffOnly(editorForm.isStaffOnlyFlagForHoldings());
791                 holdingsTree.getHoldings().setCreatedBy(GlobalVariables.getUserSession().getPrincipalName());
792                 org.kuali.ole.docstore.common.document.content.instance.Item item = itemOlemlRecordProcessor.fromXML(holdingsTree.getItems().get(0).getContent());
793                 List<Note> notes = ensureAtleastOneNote(item.getNote());
794                 item.setNote(notes);
795                 workInstanceOlemlForm.setSelectedItem(item);
796                 workInstanceOlemlForm.setViewId("WorkItemViewPage");
797                 //workInstanceOlemlForm.setMessage("Please enter details for new Item record.");
798                 GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO,
799                         "item.details.new.message");
800             } else if (editorForm.getDocumentForm().getViewId().equalsIgnoreCase("WorkBibEditorViewPage")) {
801 //                editorForm.setHeaderText("Import Bib Step-4 Instance Editor (Holdings)- OLEML Format");
802                 editorForm.setHeaderText("Import Bib Step-4 Holdings");
803                 workInstanceOlemlForm.setViewId("WorkHoldingsViewPage");
804                 //workInstanceOlemlForm.setMessage("Please enter details for new Holdings record.");
805                 GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO,
806                         "holdings.record.new.load.message");
807                 workInstanceOlemlForm
808                         .setSelectedHolding(oleHoldings);
809             }
810         }
811         editorForm.setDocumentForm(workInstanceOlemlForm);
812         return editorForm;
813     }
814 
815     public EditorForm deleteVerify(EditorForm editorForm) {
816         //LOG.info("in instance editor class");
817         WorkInstanceOlemlForm workInstanceOlemlForm = new WorkInstanceOlemlForm();
818         String docId = editorForm.getDocId();
819         String operation = "deleteVerify";
820         //        String responseXml = getResponseFromDocStore(editorForm, docId, operation);
821         //        LOG.info("deleteVerify responseXml-->" + responseXml);
822         //        editorForm.setDeleteVerifyResponse(responseXml);
823         editorForm.setShowDeleteTree(true);
824         editorForm.setHasLink(true);
825         //        Node<DocumentTreeNode, String> docTree = buildDocSelectionTree(responseXml);
826         List<String> uuidList = new ArrayList<>(0);
827         uuidList.add(editorForm.getDocId());
828         DocumentSelectionTree documentSelectionTree = new DocumentSelectionTree();
829         Node<DocumentTreeNode, String> docTree = null;
830         try {
831             docTree = documentSelectionTree.add(uuidList, editorForm.getDocType());
832         } catch (SolrServerException e) {
833             LOG.error("Exception :", e);
834             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
835         }
836         StringBuffer deleteMessage = new StringBuffer();
837         deleteMessage.append("WARNING : All attached items (");
838         HoldingsTree holdingsTree = docstoreClient.retrieveHoldingsTree(docId);
839         int itemCount = holdingsTree.getItems().size();
840         deleteMessage.append(itemCount + ") on the following holdings record will be deleted.");
841         editorForm.setDeleteMessage(deleteMessage.toString());
842         editorForm.getDocTree().setRootElement(docTree);
843         editorForm.setViewId("DeleteViewPage");
844         return editorForm;
845 
846     }
847 
848     /**
849      * This method deletes the holdings record from docstore by the doc id.
850      * @param editorForm
851      * @return
852      * @throws Exception
853      */
854     public EditorForm delete(EditorForm editorForm) throws Exception {
855         return deleteFromDocStore(editorForm);
856     }
857 
858     @Override
859     public EditorForm showBibs(EditorForm editorForm) {
860         String instanceId = "";
861         String docType = editorForm.getDocType();
862         List<Bib> bibs = new ArrayList<Bib>();
863         if ((docType.equalsIgnoreCase(DocType.HOLDINGS.getCode())) || (docType
864                 .equalsIgnoreCase(DocType.ITEM.getCode()))) {
865             instanceId = editorForm.getHoldingsId();
866         }
867         if (StringUtils.isNotEmpty(instanceId)) {
868             try {
869                 Holdings holdings = docstoreClient.retrieveHoldings(instanceId);
870                 bibs = holdings.getBibs().getBibs();
871             } catch (Exception e) {
872                 LOG.error("Exception :", e);
873                 e.printStackTrace();
874             }
875         }
876         editorForm.setBibList(bibs);
877         editorForm.setViewId("ShowBibViewPage");
878         return editorForm;
879     }
880 
881 
882     public EditorForm bulkUpdate(EditorForm editorForm,List<String> holdingIds) {
883         WorkInstanceOlemlForm workInstanceOlemlForm = (WorkInstanceOlemlForm) editorForm.getDocumentForm();
884         String bibId = editorForm.getBibId();
885         List<BibTree> bibTreeList = new ArrayList<BibTree>();
886         BibTree bibTree = null;
887         String editorMessage = "";
888         Bib bib = null;
889         Date date = new Date();
890         SimpleDateFormat sdf = new SimpleDateFormat(RiceConstants.SIMPLE_DATE_FORMAT_FOR_DATE+" HH:mm:ss");
891         String dateStr = sdf.format(date);
892         String user = GlobalVariables.getUserSession().getPrincipalName();
893         workInstanceOlemlForm.setBibTreeList(bibTreeList);
894         editorForm.setHeaderText("Global Instance Editor");
895         String docId = editorForm.getDocId();
896             Holdings holdings = new PHoldings();
897             OleHoldings holdingData = workInstanceOlemlForm.getSelectedHolding();
898             if (!isValidHoldingsData(workInstanceOlemlForm)) {
899                 return workInstanceOlemlForm;
900             }
901             try {
902                 String holdingXmlContent = getInstanceEditorFormDataHandler().buildHoldingContent(holdingData);
903                 holdings.setCategory(holdingXmlContent);
904                 holdings.setContent(holdingXmlContent);
905             } catch (Exception e) {
906                 LOG.error("Exception :", e);
907                 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS,
908                         "docstore.response", e.getMessage());
909                 getInstanceEditorFormDataHandler().setLocationDetails(workInstanceOlemlForm);
910                 return workInstanceOlemlForm;
911             }
912             holdings.setId(docId);
913             String canUpdateStaffOnlyFlag = "false";
914             if (editorForm.getStaffOnlyFlagInGlobalEdit() != null && editorForm.getStaffOnlyFlagInGlobalEdit().equalsIgnoreCase("Y")) {
915                 canUpdateStaffOnlyFlag = "true";
916                 editorForm.setStaffOnlyFlagForHoldings(true);
917                 holdings.setStaffOnly(editorForm.isStaffOnlyFlagForHoldings());
918             }
919             else if (editorForm.getStaffOnlyFlagInGlobalEdit() != null && editorForm.getStaffOnlyFlagInGlobalEdit().equalsIgnoreCase("N")) {
920                 canUpdateStaffOnlyFlag = "true";
921                 editorForm.setStaffOnlyFlagForHoldings(false);
922                 holdings.setStaffOnly(editorForm.isStaffOnlyFlagForHoldings());
923             }
924             holdings.setStaffOnly(editorForm.isStaffOnlyFlagForHoldings());
925             holdings.setCreatedBy(editorForm.getCreatedBy());
926             holdings.setCreatedOn(editorForm.getCreatedDate());
927             holdings.setUpdatedBy(user);
928             holdings.setUpdatedOn(dateStr);
929             String holdingsCreatedDate = null;
930             Format formatter = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss");
931             holdingsCreatedDate = formatter.format(new Date());
932             holdings.setLastUpdated(holdingsCreatedDate);
933             //holdings.setBib(bib);
934             holdings.setCategory(editorForm.getDocCategory());
935             holdings.setType(editorForm.getDocType());
936             holdings.setFormat(editorForm.getDocFormat());
937             try {
938                 getDocstoreClientLocator().getDocstoreClient().bulkUpdateHoldings(holdings, holdingIds,canUpdateStaffOnlyFlag);
939             }
940             catch (DocstoreException e) {
941                 LOG.error("Exception :", e);
942                 DocstoreException docstoreException = (DocstoreException) e;
943                 if (org.apache.commons.lang3.StringUtils.isNotEmpty(docstoreException.getErrorCode())) {
944                     GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, docstoreException.getErrorCode());
945                 } else {
946                     GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, e.getMessage());
947                 }
948                 // getInstanceEditorFormDataHandler().setLocationDetails(workInstanceOlemlForm);
949                 return workInstanceOlemlForm;
950             }
951             catch(Exception ne){
952                 LOG.error("Exception :", ne);
953                 GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO, "record.submit.fail.message");
954                 return workInstanceOlemlForm;
955             }
956             editorForm.setUpdatedDate(holdings.getUpdatedOn());
957             editorForm.setUpdatedBy(holdings.getUpdatedBy());
958             getInstanceEditorFormDataHandler().setLocationDetails(workInstanceOlemlForm);
959             editorMessage = "holdings.record.update.message";
960             workInstanceOlemlForm.setViewId("WorkHoldingsViewPage");
961         //}
962 
963         GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO, editorMessage);
964         return workInstanceOlemlForm;
965 
966     }
967 
968     @Override
969     public EditorForm copy(EditorForm editorForm) {
970         editorForm.setDocId(null);
971         GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO, OLEConstants.MARC_EDITOR_HOLDINGS_COPY_MESSAGE);
972         return editorForm;
973     }
974 
975 }