View Javadoc
1   package org.kuali.ole.describe.controller;
2   
3   import org.apache.commons.collections.CollectionUtils;
4   import org.apache.commons.lang.StringUtils;
5   import org.apache.log4j.Logger;
6   import org.kuali.ole.DocumentUniqueIDPrefix;
7   import org.kuali.ole.OLEConstants;
8   import org.kuali.ole.describe.bo.SearchResultDisplayFields;
9   import org.kuali.ole.describe.bo.SearchResultDisplayRow;
10  import org.kuali.ole.describe.bo.marc.structuralfields.ControlFields;
11  import org.kuali.ole.describe.bo.marc.structuralfields.controlfield006.ControlField006Text;
12  import org.kuali.ole.describe.bo.marc.structuralfields.controlfield007.ControlField007Text;
13  import org.kuali.ole.describe.bo.marc.structuralfields.controlfield008.ControlField008;
14  import org.kuali.ole.describe.form.GlobalEditForm;
15  import org.kuali.ole.describe.form.OLESearchForm;
16  import org.kuali.ole.describe.service.BrowseService;
17  import org.kuali.ole.describe.service.impl.BrowseServiceImpl;
18  import org.kuali.ole.docstore.OleException;
19  import org.kuali.ole.docstore.common.client.DocstoreClient;
20  import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
21  import org.kuali.ole.docstore.common.document.*;
22  import org.kuali.ole.docstore.common.document.config.*;
23  import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecord;
24  import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecords;
25  import org.kuali.ole.docstore.common.document.content.bib.marc.ControlField;
26  import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
27  import org.kuali.ole.docstore.common.document.content.enums.DocFormat;
28  import org.kuali.ole.docstore.common.document.content.enums.DocType;
29  import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
30  import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
31  import org.kuali.ole.docstore.common.search.*;
32  import org.kuali.ole.docstore.common.search.SearchResult;
33  import org.kuali.ole.docstore.engine.client.DocstoreLocalClient;
34  import org.kuali.ole.docstore.engine.service.index.solr.BibConstants;
35  import org.kuali.ole.docstore.engine.service.index.solr.ItemConstants;
36  import org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.BibRecord;
37  import org.kuali.ole.docstore.utility.ISBNUtil;
38  import org.kuali.ole.select.bo.OLEEditorResponse;
39  import org.kuali.ole.select.businessobject.OleCopy;
40  import org.kuali.ole.select.businessobject.OleDocstoreResponse;
41  import org.kuali.ole.select.document.OLEEResourceRecordDocument;
42  import org.kuali.ole.service.OLEEResourceSearchService;
43  import org.kuali.ole.sys.context.SpringContext;
44  import org.kuali.rice.core.api.exception.RiceRuntimeException;
45  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
46  import org.kuali.rice.kim.api.identity.Person;
47  import org.kuali.rice.kim.api.identity.PersonService;
48  import org.kuali.rice.kim.api.permission.PermissionService;
49  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
50  import org.kuali.rice.krad.service.BusinessObjectService;
51  import org.kuali.rice.krad.service.DocumentService;
52  import org.kuali.rice.krad.service.KRADServiceLocator;
53  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
54  import org.kuali.rice.krad.uif.UifParameters;
55  import org.kuali.rice.krad.uif.view.View;
56  import org.kuali.rice.krad.util.GlobalVariables;
57  import org.kuali.rice.krad.util.KRADConstants;
58  import org.kuali.rice.krad.web.controller.UifControllerBase;
59  import org.kuali.rice.krad.web.form.UifFormBase;
60  import org.springframework.stereotype.Controller;
61  import org.springframework.validation.BindingResult;
62  import org.springframework.web.bind.annotation.ModelAttribute;
63  import org.springframework.web.bind.annotation.RequestMapping;
64  import org.springframework.web.bind.annotation.RequestMethod;
65  import org.springframework.web.servlet.ModelAndView;
66  
67  import javax.servlet.http.HttpServletRequest;
68  import javax.servlet.http.HttpServletResponse;
69  import java.util.*;
70  
71  /**
72   * Created with IntelliJ IDEA.
73   * User: chandrasekharag
74   * Date: 26/2/14
75   * Time: 7:25 PM
76   * To change this template use File | Settings | File Templates.
77   */
78  @Controller
79  @RequestMapping(value = "/olesearchcontroller")
80  public class OLESearchController extends UifControllerBase {
81  
82      private static final Logger LOG = Logger.getLogger(OLESearchController.class);
83      private String eResourceId;
84      private String tokenId;
85      private int totalRecCount;
86      private int start;
87      private int pageSize;
88      private DocstoreClient docstoreClient = getDocstoreLocalClient();
89      private OLEEResourceSearchService oleEResourceSearchService;
90      private BrowseService browseService;
91      private DocumentService documentService;
92  
93      public DocstoreClient getDocstoreLocalClient() {
94          if (null == docstoreClient) {
95              return new DocstoreLocalClient();
96          }
97          return docstoreClient;
98      }
99  
100     public BrowseService getBrowseService() {
101         if(browseService == null) {
102             browseService = new BrowseServiceImpl();
103         }
104         return browseService;
105     }
106     public OLEEResourceSearchService getOleEResourceSearchService() {
107         if (oleEResourceSearchService == null) {
108             oleEResourceSearchService = GlobalResourceLoader.getService(OLEConstants.OLEEResourceRecord.ERESOURSE_SEARCH_SERVICE);
109         }
110         return oleEResourceSearchService;
111     }
112     public DocumentService getDocumentService() {
113         if (this.documentService == null) {
114             this.documentService = KRADServiceLocatorWeb.getDocumentService();
115         }
116         return this.documentService;
117     }
118 
119     public void setDocumentService(DocumentService documentService) {
120         this.documentService = documentService;
121     }
122     DocumentSearchConfig documentSearchConfig = DocumentSearchConfig.getDocumentSearchConfig();
123 
124     public int getTotalRecCount() {
125         return totalRecCount;
126     }
127 
128     public void setTotalRecCount(int totalRecCount) {
129         this.totalRecCount = totalRecCount;
130     }
131 
132     public int getStart() {
133         return start;
134     }
135 
136     public void setStart(int start) {
137         this.start = start;
138     }
139 
140     public int getPageSize() {
141         return pageSize;
142     }
143 
144     public void setPageSize(int pageSize) {
145         this.pageSize = pageSize;
146     }
147 
148     public boolean getPreviousFlag() {
149         if (this.start == 0)
150             return false;
151         return true;
152     }
153 
154     public boolean getNextFlag() {
155         if (this.start + this.pageSize < this.totalRecCount)
156             return true;
157         return false;
158     }
159 
160     public String getPageShowEntries() {
161         return "Showing " + ((this.start == 0) ? 1 : this.start + 1) + " to "
162                 + (((this.start + this.pageSize) > this.totalRecCount) ? this.totalRecCount : (this.start + this.pageSize))
163                 + " of " + this.totalRecCount + " entries";
164     }
165 
166     public String getFacetShowEntries(SearchParams searchParams, int totalRecordCount) {
167         return "Showing " + ((searchParams.getFacetOffset() == 0) ? 1 : searchParams.getFacetOffset() + 1) + " to "
168                 + (((searchParams.getFacetOffset()  + searchParams.getFacetLimit()) > totalRecordCount) ? totalRecordCount : (searchParams.getFacetOffset() + searchParams.getFacetLimit()))
169                 + " of " + totalRecordCount + " entries";
170     }
171 
172     @Override
173     protected UifFormBase createInitialForm(HttpServletRequest httpServletRequest) {
174         return new OLESearchForm();
175 
176     }
177 
178     @Override
179     @RequestMapping(params = "methodToCall=start")
180     public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
181                               HttpServletRequest request, HttpServletResponse response) {
182         this.start = 0;
183         LOG.debug("Inside the olesearchform start method");
184         OLESearchForm oleSearchForm = (OLESearchForm) form;
185 
186         request.getSession().setAttribute("selectedFacetResults", null);
187         if (oleSearchForm.getDocType() == null) {
188             oleSearchForm.setDocType(DocType.BIB.getCode());
189         }
190         if (StringUtils.isEmpty(oleSearchForm.getSearchType())) {
191             oleSearchForm.setSearchType("search");
192         }
193         if (StringUtils.isEmpty(oleSearchForm.getBrowseField())) {
194             oleSearchForm.setBrowseField("title");
195         }
196 
197         oleSearchForm.getSearchConditions().clear();
198         SearchCondition searchCondition = new SearchCondition();
199         searchCondition.setOperator("AND");
200         SearchField searchField = new SearchField();
201         searchField.setFieldName("any");
202         searchField.setDocType(oleSearchForm.getDocType());
203         searchCondition.setSearchField(searchField);
204         oleSearchForm.getSearchConditions().add(searchCondition);
205         String eInstance = request.getParameter(OLEConstants.E_INSTANCE);
206         if (eInstance != null && eInstance.equalsIgnoreCase(OLEConstants.LINK_EXISTING_INSTANCE)) {
207             oleSearchForm.setLinkExistingInstance(eInstance);
208         }
209         if (request.getParameter(OLEConstants.E_RESOURCE_ID) != null) {
210             eResourceId = request.getParameter(OLEConstants.E_RESOURCE_ID);
211         }
212         if (request.getParameter(OLEConstants.TOKEN_ID) != null) {
213             tokenId = request.getParameter(OLEConstants.TOKEN_ID);
214         }
215         oleSearchForm.setStart(0);
216         if(oleSearchForm.getSearchParams() != null) {
217             oleSearchForm.getSearchParams().setStartIndex(0);
218         }
219         clearForm(oleSearchForm);
220         GlobalVariables.getMessageMap().clearErrorMessages();
221         boolean hasSearchPermission = canSearch(GlobalVariables.getUserSession().getPrincipalId());
222         if (!hasSearchPermission && oleSearchForm.getDocType().equalsIgnoreCase(OLEConstants.BIB_DOC_TYPE)) {
223             boolean hasLinkPermission = canLinkBibForRequisition(GlobalVariables.getUserSession().getPrincipalId());
224             if (!hasLinkPermission) {
225                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.ERROR_AUTHORIZATION);
226                 return super.navigate(oleSearchForm, result, request, response);
227             }
228         } else if (!hasSearchPermission) {
229             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.ERROR_AUTHORIZATION);
230             return super.navigate(oleSearchForm, result, request, response);
231         }
232         oleSearchForm.setMessage(null);
233         oleSearchForm.setSearchResultDisplayRowList(null);
234         oleSearchForm.setBibSearchResultDisplayRowList(null);
235         oleSearchForm.setHoldingSearchResultDisplayRowList(null);
236         oleSearchForm.setShowTime(true);
237         return super.navigate(oleSearchForm, result, request, response);
238     }
239 
240     private boolean canSearch(String principalId) {
241         PermissionService service = KimApiServiceLocator.getPermissionService();
242         return service.hasPermission(principalId, OLEConstants.CAT_NAMESPACE, OLEConstants.DESC_WORKBENCH_SEARCH);
243     }
244 
245     private boolean canLinkBibForRequisition(String principalId) {
246         PermissionService service = KimApiServiceLocator.getPermissionService();
247         return service.hasPermission(principalId, OLEConstants.SELECT_NMSPC, OLEConstants.LINK_EXISTING_BIB);
248     }
249 
250     @RequestMapping(params = "methodToCall=submit")
251     public ModelAndView submit(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
252                                HttpServletRequest request, HttpServletResponse response) throws Exception {
253         OLESearchForm oleSearchForm = (OLESearchForm) form;
254         boolean isValid = false;
255         BusinessObjectService boService = KRADServiceLocator.getBusinessObjectService();
256         Map<String, String> map = new HashMap<>();
257         List<Integer> resultList = new ArrayList<>();
258         for (SearchResultDisplayRow searchResultDisplayRow : oleSearchForm.getSearchResultDisplayRowList()) {
259             if (searchResultDisplayRow.isSelect()) {
260                 map.put(OLEConstants.BIB_ID, DocumentUniqueIDPrefix.getPrefixedId(DocumentUniqueIDPrefix.PREFIX_WORK_BIB_MARC, searchResultDisplayRow.getLocalId()));
261                 List<OleCopy> listOfValues = (List<OleCopy>) boService.findMatching(OleCopy.class, map);
262                 if (listOfValues.size() > 0 && (oleSearchForm.getMessage() == null || oleSearchForm.getMessage().equals(""))) {
263                     for (OleCopy oleCopy : listOfValues) {
264                         if (oleCopy.getReqDocNum() != null) {
265                             resultList.add(oleCopy.getReqDocNum());
266                         }
267                     }
268                     if (resultList.size() > 0) {
269                         Set<Integer> resultSet = new HashSet<>(resultList);
270                         resultList = new ArrayList<>(resultSet);
271                         StringBuffer reqIds = new StringBuffer();
272                         if (resultList.size() > 0) {
273                             int count = 0;
274                             for (; count < resultList.size() - 1; count++) {
275                                 reqIds.append(resultList.get(count) + OLEConstants.COMMA);
276                             }
277                             reqIds.append(resultList.get(count));
278                         }
279                         oleSearchForm.setMessage(OLEConstants.POPUP_MESSAGE + reqIds.toString() + OLEConstants.PROCEED_MESSAGE);
280                         return getUIFModelAndView(oleSearchForm);
281                     }
282                 }
283                 oleSearchForm.setMessage(OLEConstants.OLEEResourceRecord.SPACE);
284                 processNewRecordResponseForOLE(searchResultDisplayRow.getLocalId(), oleSearchForm.getTokenId(), oleSearchForm.getLinkToOrderOption());
285                 oleSearchForm.setSuccessMessage(OLEConstants.LINK_SUCCESS_MESSAGE);
286                 isValid = true;
287                 break;
288             }
289         }
290         if (isValid == false) {
291             GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.BIB_SELECT);
292             return getUIFModelAndView(oleSearchForm);
293         }
294         return getUIFModelAndView(oleSearchForm);
295     }
296 
297     @RequestMapping(params = "methodToCall=search")
298     public ModelAndView search(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
299                                HttpServletRequest request, HttpServletResponse response) {
300         float start = System.currentTimeMillis()/1000;
301         OLESearchForm oleSearchForm = (OLESearchForm) form;
302         boolean hasSearchPermission = canSearch(GlobalVariables.getUserSession().getPrincipalId());
303         if (!hasSearchPermission && oleSearchForm.getDocType().equalsIgnoreCase(OLEConstants.BIB_DOC_TYPE)) {
304             boolean hasLinkPermission = canLinkBibForRequisition(GlobalVariables.getUserSession().getPrincipalId());
305             if (!hasLinkPermission) {
306                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.ERROR_AUTHORIZATION);
307                 return super.navigate(oleSearchForm, result, request, response);
308             }
309         } else if (!hasSearchPermission) {
310             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.ERROR_AUTHORIZATION);
311             return super.navigate(oleSearchForm, result, request, response);
312         }
313         oleSearchForm.getSearchParams().setFacetOffset(0);
314         searchDocstoreData(oleSearchForm, request);
315         float end = System.currentTimeMillis()/1000;
316         oleSearchForm.setServerTime(String.valueOf(end-start));
317         return super.navigate(oleSearchForm, result, request, response);
318     }
319     private void processNewRecordResponseForOLE(String bibId, String tokenId, String linkToOrderOption) throws Exception {
320         String instanceUUID = null;
321         BibTree bibTree = getDocstoreLocalClient().retrieveBibTree(bibId);
322         OLEEditorResponse oleEditorResponse = new OLEEditorResponse();
323         if (bibTree.getHoldingsTrees() != null && bibTree.getHoldingsTrees().size() > 0) {
324             instanceUUID = bibTree.getHoldingsTrees().get(0).getHoldings().getId();
325         }
326         oleEditorResponse.setLinkedInstanceId(instanceUUID);
327         oleEditorResponse.setBib(bibTree.getBib());
328         oleEditorResponse.setTokenId(tokenId);
329         oleEditorResponse.setLinkToOrderOption(linkToOrderOption);
330         HashMap<String, OLEEditorResponse> oleEditorResponseMap = new HashMap<String, OLEEditorResponse>();
331         oleEditorResponseMap.put(tokenId, oleEditorResponse);
332         OleDocstoreResponse.getInstance().setEditorResponse(oleEditorResponseMap);
333     }
334 
335     protected void setShowPageSizeEntries(OLESearchForm oleSearchForm) {
336         List<Integer> pageSizes = documentSearchConfig.getPageSizes();
337         if (CollectionUtils.isEmpty(pageSizes)) {
338             pageSizes.add(10);
339             pageSizes.add(25);
340             pageSizes.add(50);
341             pageSizes.add(100);
342         }
343         oleSearchForm.setShowPageSize(pageSizes.toString());
344     }
345 
346     @RequestMapping(params = "methodToCall=pageNumberSearch")
347     public ModelAndView pageNumberSearch(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
348                                          HttpServletRequest request, HttpServletResponse response) throws Exception {
349 
350         OLESearchForm oleSearchForm = (OLESearchForm) form;
351         SearchParams searchParams = oleSearchForm.getSearchParams();
352         try {
353             int start = Math.max(0,
354                     (Integer.parseInt(oleSearchForm.getPageNumber()) - 1)
355                             * searchParams.getPageSize());
356             searchParams.setStartIndex(start);
357         } catch (NumberFormatException e) {
358             LOG.warn("Invalid page number " + oleSearchForm.getPageNumber(), e);
359         }
360         return search(oleSearchForm, result, request, response);
361     }
362 
363     @RequestMapping(params = "methodToCall=lastPageSearch")
364     public ModelAndView lastPageSearch(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
365                                        HttpServletRequest request, HttpServletResponse response) throws Exception {
366 
367         OLESearchForm oleSearchForm = (OLESearchForm) form;
368         SearchParams searchParams = oleSearchForm.getSearchParams();
369         try {
370             int totalcount = oleSearchForm.getTotalRecordCount();
371             int pageSize = searchParams.getPageSize();
372             int totlaPages = totalcount/pageSize;
373             int lastNumber= pageSize*totlaPages;
374             int lastPage = totalcount - pageSize;
375             oleSearchForm.setPageNumber(Integer.toString(totlaPages));
376             if(lastNumber < totalcount){
377                 lastPage = lastNumber;
378                 oleSearchForm.setPageNumber(Integer.toString(totlaPages+1));
379             }
380             int start = Math.max(0, lastPage);
381             searchParams.setStartIndex(start);
382         } catch (NumberFormatException e) {
383             LOG.warn("Invalid page number " + oleSearchForm.getPageNumber(), e);
384         }
385         return search(oleSearchForm, result, request, response);
386     }
387 
388     @RequestMapping(params = "methodToCall=nextSearch")
389     public ModelAndView nextSearch(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
390                                    HttpServletRequest request, HttpServletResponse response) throws Exception {
391 
392         OLESearchForm oleSearchForm = (OLESearchForm) form;
393         SearchParams searchParams = oleSearchForm.getSearchParams();
394         int start = Math.max(0, searchParams.getStartIndex() + searchParams.getPageSize());
395         searchParams.setStartIndex(start);
396         return search(oleSearchForm, result, request, response);
397     }
398 
399     @RequestMapping(params = "methodToCall=previousSearch")
400     public ModelAndView previousSearch(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
401                                        HttpServletRequest request, HttpServletResponse response) throws Exception {
402 
403         OLESearchForm oleSearchForm = (OLESearchForm) form;
404         SearchParams searchParams = oleSearchForm.getSearchParams();
405         int start = Math.max(0, searchParams.getStartIndex() - oleSearchForm.getPageSize());
406         searchParams.setStartIndex(start);
407         return search(oleSearchForm, result, request, response);
408     }
409 
410     @RequestMapping(params = "methodToCall=facetSearch")
411     public ModelAndView facetSearch(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
412                                     HttpServletRequest request, HttpServletResponse response) {
413         OLESearchForm oleSearchForm = (OLESearchForm) form;
414         String docType = request.getParameter("docType");
415         String selectedFacet = request.getParameter("selectedFacet");
416         String selectedFacetName = request.getParameter("selectedFacetName");
417         oleSearchForm.setDocType(docType);
418         if (oleSearchForm.getSearchParams() == null) {
419             SearchParams searchParams = (SearchParams) request.getSession().getAttribute("searchParams");
420             oleSearchForm.setSearchParams(searchParams);
421         }
422         oleSearchForm.getSearchConditions().clear();
423         SearchCondition searchCondition = new SearchCondition();
424         List<Integer> pageSizes = documentSearchConfig.getPageSizes();
425         if(!pageSizes.isEmpty() || pageSizes.size() > 0) {
426             oleSearchForm.setPageSize(pageSizes.get(0));
427         }
428         searchCondition.setOperator("AND");
429         oleSearchForm.getSearchConditions().addAll(oleSearchForm.getSearchParams().getSearchConditions());
430         String eInstance = request.getParameter(OLEConstants.E_INSTANCE);
431         if (eInstance != null && eInstance.equalsIgnoreCase(OLEConstants.LINK_EXISTING_INSTANCE)) {
432             oleSearchForm.setLinkExistingInstance(eInstance);
433         }
434         if (request.getParameter(OLEConstants.TOKEN_ID) != null) {
435             tokenId = request.getParameter(OLEConstants.TOKEN_ID);
436         }
437         oleSearchForm.getSearchParams().getFacetFields().addAll(getFacetFields(oleSearchForm.getDocType()));
438         oleSearchForm.getSearchParams().setFacetPrefix("");
439         oleSearchForm.getSearchParams().setFacetLimit(documentSearchConfig.getFacetPageSizeShort());
440         FacetCondition facetCondition = new FacetCondition();
441         facetCondition.setFieldName(selectedFacetName);
442         facetCondition.setFieldValue(selectedFacet);
443         oleSearchForm.getSearchParams().getFacetConditions().add(facetCondition);
444         oleSearchForm.setSearchType("search");
445         GlobalVariables.getMessageMap().clearErrorMessages();
446         return search(oleSearchForm, result, request, response);
447     }
448     @RequestMapping(params = "methodToCall=removeFacet")
449     public ModelAndView removeFacet(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
450                                     HttpServletRequest request, HttpServletResponse response) {
451         OLESearchForm oleSearchForm = (OLESearchForm) form;
452         int index = Integer.parseInt(oleSearchForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
453         if(!CollectionUtils.isEmpty(oleSearchForm.getSearchParams().getFacetConditions()) && oleSearchForm.getSearchParams().getFacetConditions().size() > index) {
454             oleSearchForm.getSearchParams().getFacetConditions().remove(index);
455         }
456         return search(oleSearchForm, result, request, response);
457     }
458 
459     @RequestMapping(params = "methodToCall=moreFacets")
460     public ModelAndView moreFacets(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
461                                    HttpServletRequest request, HttpServletResponse response) {
462         OLESearchForm oleSearchForm = (OLESearchForm) form;
463         if(StringUtils.isEmpty(oleSearchForm.getDocType())) {
464             oleSearchForm.setDocType(request.getParameter("docType"));
465         }
466         SearchParams searchParams = oleSearchForm.getSearchParams();
467         if(searchParams == null) {
468             searchParams = (SearchParams) request.getSession().getAttribute("searchParams");
469         }
470         if(StringUtils.isNotEmpty(request.getParameter("facetField"))) {
471             if(!CollectionUtils.isEmpty(searchParams.getFacetFields())) {
472                 searchParams.getFacetFields().clear();
473             }
474             searchParams.getFacetFields().add(request.getParameter("facetField"));
475         }
476         if(StringUtils.isNotEmpty(request.getParameter("facetPrefix"))) {
477             searchParams.setFacetPrefix(request.getParameter("facetPrefix"));
478         }
479         oleSearchForm.setSearchParams(searchParams);
480         oleSearchForm.getSearchConditions().addAll(searchParams.getSearchConditions());
481         searchParams.setFacetOffset(0);
482         searchParams.setFacetLimit(documentSearchConfig.getFacetPageSizeLong());
483         oleSearchForm.setMoreFacets(true);
484         searchDocstoreData(oleSearchForm, request);
485         return super.navigate(oleSearchForm, result, request, response);
486     }
487 
488     @RequestMapping(params = "methodToCall=nextFacet")
489     public ModelAndView nextFacet(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
490                                   HttpServletRequest request, HttpServletResponse response) {
491         OLESearchForm oleSearchForm = (OLESearchForm) form;
492         if(StringUtils.isEmpty(oleSearchForm.getDocType())) {
493             oleSearchForm.setDocType(request.getParameter("docType"));
494         }
495         SearchParams searchParams = oleSearchForm.getSearchParams();
496         if(searchParams == null) {
497             searchParams = (SearchParams) request.getSession().getAttribute("searchParams");
498         }
499         if(StringUtils.isNotEmpty(request.getParameter("facetField"))) {
500             if(!CollectionUtils.isEmpty(searchParams.getFacetFields())) {
501                 searchParams.getFacetFields().clear();
502             }
503             searchParams.getFacetFields().add(request.getParameter("facetField"));
504         }
505         int facetCount = oleSearchForm.getSearchResponse().getFacetResult().getFacetResultFields().get(0).getTotalCount();
506         int facetOffset = searchParams.getFacetOffset() + documentSearchConfig.getFacetPageSizeLong();
507         searchParams.setFacetOffset(facetOffset);
508         oleSearchForm.setSearchParams(searchParams);
509         oleSearchForm.getSearchConditions().addAll(searchParams.getSearchConditions());
510         oleSearchForm.setMoreFacets(true);
511         oleSearchForm.setFacetPageEntries(getFacetShowEntries(searchParams, facetCount));
512         searchDocstoreData(oleSearchForm, request);
513         return super.navigate(oleSearchForm, result, request, response);
514     }
515 
516     @RequestMapping(params = "methodToCall=previousFacet")
517     public ModelAndView previousFacet(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
518                                       HttpServletRequest request, HttpServletResponse response) {
519         OLESearchForm oleSearchForm = (OLESearchForm) form;
520         if(StringUtils.isEmpty(oleSearchForm.getDocType())) {
521             oleSearchForm.setDocType(request.getParameter("docType"));
522         }
523         SearchParams searchParams = oleSearchForm.getSearchParams();
524         if(searchParams == null) {
525             searchParams = (SearchParams) request.getSession().getAttribute("searchParams");
526         }
527         if(StringUtils.isNotEmpty(request.getParameter("facetField"))) {
528             if(!CollectionUtils.isEmpty(searchParams.getFacetFields())) {
529                 searchParams.getFacetFields().clear();
530             }
531             searchParams.getFacetFields().add(request.getParameter("facetField"));
532         }
533         int facetCount = oleSearchForm.getSearchResponse().getFacetResult().getFacetResultFields().get(0).getTotalCount();
534         int facetLimit = searchParams.getFacetOffset() - documentSearchConfig.getFacetPageSizeLong();
535         searchParams.setFacetOffset(facetLimit > 0 ? facetLimit : 0);
536         oleSearchForm.setSearchParams(searchParams);
537         oleSearchForm.getSearchConditions().addAll(searchParams.getSearchConditions());
538         oleSearchForm.setMoreFacets(true);
539         oleSearchForm.setFacetPageEntries(getFacetShowEntries(searchParams, facetCount));
540         searchDocstoreData(oleSearchForm, request);
541         return super.navigate(oleSearchForm, result, request, response);
542     }
543 
544     @RequestMapping(params = "methodToCall=browse")
545     public ModelAndView browse(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
546                                HttpServletRequest request, HttpServletResponse response) {
547         OLESearchForm oleSearchForm = (OLESearchForm) form;
548         boolean hasPermission = performBrowse(GlobalVariables.getUserSession().getPrincipalId());
549         if (!hasPermission) {
550             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_INFO, OLEConstants.ERROR_AUTHORIZATION);
551             return getUIFModelAndView(oleSearchForm);
552         }
553 //        oleSearchForm.setPageSize(10);
554         if ("title".equals(oleSearchForm.getBrowseField())) {
555             oleSearchForm.setDocType(DocType.BIB.getCode());
556             List<SearchResultDisplayRow> searchResultDisplayRowList = getBrowseService().browse(oleSearchForm);
557             oleSearchForm.setSearchResultDisplayRowList(searchResultDisplayRowList);
558 
559         } else {
560             String location = getBrowseService().validateLocation(oleSearchForm.getLocation());
561             oleSearchForm.setLocation(location);
562             if (oleSearchForm.getDocType().equalsIgnoreCase(DocType.ITEM.getCode())) {
563                 List<Item> itemList = getBrowseService().browse(oleSearchForm);
564                 oleSearchForm.setItemList(itemList);
565             } else {
566                 List<Holdings> holdingsList = getBrowseService().browse(oleSearchForm);
567                 oleSearchForm.setHoldingsList(holdingsList);
568             }
569         }
570         setBrowsePageNextPrevoiusAndEntriesInfo(oleSearchForm);
571         return getUIFModelAndView(form);
572     }
573 
574     @RequestMapping(params = "methodToCall=workBenchBrowseClear")
575     public ModelAndView workBenchClear(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
576                                        HttpServletRequest request, HttpServletResponse response) throws Exception {
577         OLESearchForm oleSearchForm = (OLESearchForm) form;
578         List<Integer> pageSizes = documentSearchConfig.getPageSizes();
579         if(!pageSizes.isEmpty() || pageSizes.size() > 0) {
580             oleSearchForm.setPageSize(pageSizes.get(0));
581         }
582         oleSearchForm.setBrowseText("");
583         if(CollectionUtils.isNotEmpty(oleSearchForm.getSearchResultDisplayRowList())) {
584             oleSearchForm.setSearchResultDisplayRowList(null);
585         }
586         return navigate(oleSearchForm, result, request, response);
587     }
588 
589     @RequestMapping(params = "methodToCall=rowsBrowse")
590     public ModelAndView rowsBrowse(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
591                                    HttpServletRequest request, HttpServletResponse response) throws Exception {
592         LOG.debug("Inside the browse method");
593         OLESearchForm oleSearchForm = (OLESearchForm) form;
594         if ("title".equals(oleSearchForm.getBrowseField())) {
595             oleSearchForm.setDocType(DocType.BIB.getCode());
596             List<SearchResultDisplayRow> searchResultDisplayRowList = getBrowseService().browseOnChange(oleSearchForm);
597             oleSearchForm.setSearchResultDisplayRowList(searchResultDisplayRowList);
598 
599         } else {
600 
601             if (oleSearchForm.getDocType().equalsIgnoreCase(DocType.ITEM.getCode())) {
602                 List<Item> itemList = getBrowseService().browseOnChange(oleSearchForm);
603                 oleSearchForm.setItemList(itemList);
604             } else {
605                 List<Holdings> holdingsList = getBrowseService().browseOnChange(oleSearchForm);
606                 oleSearchForm.setHoldingsList(holdingsList);
607             }
608         }
609         setBrowsePageNextPrevoiusAndEntriesInfo(oleSearchForm);
610         return getUIFModelAndView(form);
611     }
612 
613     @RequestMapping(params = "methodToCall=previous")
614     public ModelAndView previous(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
615                                  HttpServletRequest request, HttpServletResponse response) throws Exception {
616         LOG.debug("Inside the browse method");
617         OLESearchForm oleSearchForm = (OLESearchForm) form;
618         if ("title".equals(oleSearchForm.getBrowseField())) {
619             oleSearchForm.setDocType(DocType.BIB.getCode());
620             List<SearchResultDisplayRow> searchResultDisplayRowList = getBrowseService().browsePrev(oleSearchForm);
621             oleSearchForm.setSearchResultDisplayRowList(searchResultDisplayRowList);
622 
623         } else {
624 
625             if (oleSearchForm.getDocType().equalsIgnoreCase(DocType.ITEM.getCode())) {
626                 List<Item> itemList = getBrowseService().browsePrev(oleSearchForm);
627                 oleSearchForm.setItemList(itemList);
628             } else {
629                 List<Holdings> holdingsList = getBrowseService().browsePrev(oleSearchForm);
630                 oleSearchForm.setHoldingsList(holdingsList);
631             }
632         }
633         setBrowsePageNextPrevoiusAndEntriesInfo(oleSearchForm);
634         return getUIFModelAndView(form);
635     }
636 
637     @RequestMapping(params = "methodToCall=next")
638     public ModelAndView next(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
639                              HttpServletRequest request, HttpServletResponse response) throws Exception {
640         LOG.debug("Inside the browse method");
641         OLESearchForm oleSearchForm = (OLESearchForm) form;
642         if ("title".equals(oleSearchForm.getBrowseField())) {
643             oleSearchForm.setDocType(DocType.BIB.getCode());
644             List<SearchResultDisplayRow> searchResultDisplayRowList = getBrowseService().browseNext(oleSearchForm);
645             oleSearchForm.setSearchResultDisplayRowList(searchResultDisplayRowList);
646 
647         } else {
648 
649             if (oleSearchForm.getDocType().equalsIgnoreCase(DocType.ITEM.getCode())) {
650                 List<Item> itemList = getBrowseService().browseNext(oleSearchForm);
651                 oleSearchForm.setItemList(itemList);
652             } else {
653                 List<Holdings> holdingsList = getBrowseService().browseNext(oleSearchForm);
654                 oleSearchForm.setHoldingsList(holdingsList);
655             }
656         }
657         setBrowsePageNextPrevoiusAndEntriesInfo(oleSearchForm);
658         return getUIFModelAndView(form);
659     }
660 
661 
662     @RequestMapping(params = "methodToCall=clear")
663     public ModelAndView clear(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
664                               HttpServletRequest request, HttpServletResponse response) {
665         LOG.debug("Inside the clear method");
666         OLESearchForm oleSearchForm = (OLESearchForm) form;
667         clearForm(oleSearchForm);
668         return super.start(oleSearchForm, result, request, response);
669     }
670 
671     @RequestMapping(params = "methodToCall=back")
672     public ModelAndView back(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
673                              HttpServletRequest request, HttpServletResponse response) {
674         LOG.debug("Inside the clear method");
675         return super.back(form, result, request, response);
676     }
677 
678     @RequestMapping(params = "methodToCall=exportToXml")
679     public ModelAndView exportToXml(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
680                                     HttpServletRequest request, HttpServletResponse response) {
681         OLESearchForm oleSearchForm = (OLESearchForm) form;
682         boolean hasPermission = canExportToRequestXml(GlobalVariables.getUserSession().getPrincipalId());
683         if (!hasPermission) {
684             oleSearchForm.setJumpToId("breadcrumb_label");
685             oleSearchForm.setFocusId("breadcrumb_label");
686             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.ERROR_AUTHORIZATION);
687             return navigate(oleSearchForm, result, request, response);
688         }
689         List<String> idsToExport = new ArrayList<>();
690         if(oleSearchForm.isSelectAllRecords()){
691             if(StringUtils.isEmpty(oleSearchForm.getIdsToBeOpened())){
692                 if("browse".equalsIgnoreCase(oleSearchForm.getSearchType())){
693                     browseDocstoreForLocalIds(oleSearchForm, request);
694                 }else{
695                     searchDocstoreForLocalIds(oleSearchForm, request);
696                 }
697             }
698             String[]  localIds=oleSearchForm.getIdsToBeOpened().split(",");
699             for(String localId:localIds){
700                 String[] ids= localId.split("#");
701                 idsToExport.add(ids[0]);
702             }
703 
704         }else{
705             for (SearchResultDisplayRow searchResultDisplayRow : oleSearchForm.getSearchResultDisplayRowList()) {
706                 if(searchResultDisplayRow.isSelect()) {
707                     idsToExport.add(searchResultDisplayRow.getLocalId());
708                 }
709             }
710         }
711 
712 
713         String requestXml = "";
714         if(oleSearchForm.getDocType().equalsIgnoreCase(DocType.BIB.getCode())) {
715 
716             try {
717                 BibTrees bibTrees = new BibTrees();
718                 for(String id : idsToExport) {
719                     BibTree bibTree =getDocstoreLocalClient().retrieveBibTree(id);
720                     setControlFields(bibTree);
721                     bibTrees.getBibTrees().add(bibTree);
722                 }
723                 requestXml = BibTrees.serialize(bibTrees);
724             } catch (Exception e) {
725                 e.printStackTrace();
726             }
727         }
728 
729         if(oleSearchForm.getDocType().equalsIgnoreCase(DocType.HOLDINGS.getCode())) {
730             try {
731                 HoldingsTrees holdingsTrees = new HoldingsTrees();
732                 for (String id : idsToExport) {
733                     HoldingsTree holdingsTree = getDocstoreLocalClient().retrieveHoldingsTree(id);
734                     holdingsTrees.getHoldingsTrees().add(holdingsTree);
735                 }
736                 requestXml = HoldingsTrees.serialize(holdingsTrees);
737             } catch (Exception e) {
738                 e.printStackTrace();
739             }
740 
741         }
742 
743         if(oleSearchForm.getDocType().equalsIgnoreCase(DocType.ITEM.getCode())) {
744             try {
745                 List<Item> itemList = getDocstoreLocalClient().retrieveItems(idsToExport);
746                 Items items = new Items();
747                 items.getItems().addAll(itemList);
748                 requestXml = Items.serialize(items);
749 
750             } catch (Exception e) {
751                 e.printStackTrace();
752             }
753         }
754         oleSearchForm.setShowRequestXml(true);
755         oleSearchForm.setRequestXMLTextArea(requestXml);
756         return super.navigate(oleSearchForm, result, request, response);
757     }
758 
759     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addLine")
760     public ModelAndView addLine(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
761                                 HttpServletRequest request, HttpServletResponse response) {
762         OLESearchForm oleSearchForm = (OLESearchForm) uifForm;
763         String selectedCollectionPath = oleSearchForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
764         if (StringUtils.isBlank(selectedCollectionPath)) {
765             throw new RuntimeException("Selected collection was not set for add line action, cannot add new line");
766         }
767 
768         View view = oleSearchForm.getPostedView();
769         view.getViewHelperService().processCollectionAddLine(view, oleSearchForm, selectedCollectionPath);
770         SearchCondition searchCondition = oleSearchForm.getSearchParams().getSearchConditions().get(oleSearchForm.getSearchParams().getSearchConditions().size() -1);
771         if(StringUtils.isEmpty(searchCondition.getSearchField().getFieldValue())) {
772             oleSearchForm.getSearchParams().getSearchConditions().remove(oleSearchForm.getSearchParams().getSearchConditions().size() -1);
773             GlobalVariables.getMessageMap().putErrorForSectionId("SearchConditionsSection", OLEConstants.DESCRIBE_ENTER_SEARCH_TEXT);
774             return getUIFModelAndView(oleSearchForm);
775         }
776         oleSearchForm.getSearchParams().setStartIndex(0);
777         return getUIFModelAndView(oleSearchForm);
778     }
779 
780     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=deleteLine")
781     public ModelAndView deleteLine(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
782                                    HttpServletRequest request, HttpServletResponse response) {
783         OLESearchForm oleSearchForm = (OLESearchForm) uifForm;
784         String selectedCollectionPath = oleSearchForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
785         if (StringUtils.isBlank(selectedCollectionPath)) {
786             throw new RuntimeException("Selected collection was not set for delete line action, cannot delete line");
787         }
788 
789         int selectedLineIndex = -1;
790         String selectedLine = oleSearchForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
791         if (StringUtils.isNotBlank(selectedLine)) {
792             selectedLineIndex = Integer.parseInt(selectedLine);
793         }
794 
795         if (selectedLineIndex == -1) {
796             throw new RuntimeException("Selected line index was not set for delete line action, cannot delete line");
797         }
798 
799         View view = oleSearchForm.getPostedView();
800         view.getViewHelperService().processCollectionDeleteLine(view, oleSearchForm, selectedCollectionPath,
801                 selectedLineIndex);
802         oleSearchForm.getSearchParams().setStartIndex(0);
803         return getUIFModelAndView(oleSearchForm);
804     }
805 
806 
807 
808     private boolean performBrowse(String principalId) {
809         PermissionService service = KimApiServiceLocator.getPermissionService();
810         return service.hasPermission(principalId, OLEConstants.CAT_NAMESPACE, OLEConstants.CALL_NUMBER_BROWSE);
811     }
812 
813     public Set<String> getFacetFields(String docType) {
814         Set<String> facetFields = new TreeSet<String>();
815         for(DocTypeConfig docTypeConfig : documentSearchConfig.getDocTypeConfigs()) {
816             if(docTypeConfig.getName().equalsIgnoreCase(docType)) {
817                 for( DocFormatConfig docFormatConfig : docTypeConfig.getDocFormatConfigList()) {
818                     if(docFormatConfig.getName().equalsIgnoreCase(DocFormat.MARC.getCode())) {
819                         for(DocFieldConfig docFieldConfig : docFormatConfig.getDocFieldConfigList()) {
820                             if (docFieldConfig.isFacet() && docFieldConfig.getDocType().getName().equalsIgnoreCase(docType)) {
821                                 facetFields.add(docFieldConfig.getName());
822                             }
823                         }
824                     }
825                 }
826             }
827         }
828         return facetFields;
829     }
830 
831     public SearchResultDisplayFields getDisplayFields(OLESearchForm oleSearchForm) {
832         SearchResultDisplayFields searchResultDisplayFields = new SearchResultDisplayFields();
833         searchResultDisplayFields.buildSearchResultDisplayFields(documentSearchConfig.getDocTypeConfigs(),oleSearchForm.getDocType());
834         return searchResultDisplayFields;
835     }
836 
837     public void setPageNextPreviousAndEntriesInfo(OLESearchForm oleSearchForm) {
838         this.totalRecCount = oleSearchForm.getSearchResponse().getTotalRecordCount();
839         this.start = oleSearchForm.getSearchResponse().getStartIndex();
840         this.pageSize = oleSearchForm.getPageSize();
841         oleSearchForm.setPreviousFlag(getPreviousFlag());
842         oleSearchForm.setNextFlag(getNextFlag());
843         oleSearchForm.setPageShowEntries(getPageShowEntries());
844         oleSearchForm.setTotalRecordCount(this.totalRecCount);
845     }
846 
847 
848     public void setBrowsePageNextPrevoiusAndEntriesInfo(OLESearchForm oleSearchForm) {
849         oleSearchForm.setPreviousFlag(getBrowseService().getPreviosFlag());
850         oleSearchForm.setNextFlag(getBrowseService().getNextFlag());
851         oleSearchForm.setPageShowEntries(getBrowseService().getPageShowEntries());
852     }
853 
854     private boolean canExportToRequestXml(String principalId) {
855         PermissionService service = KimApiServiceLocator.getPermissionService();
856         return service.hasPermission(principalId, OLEConstants.CAT_NAMESPACE, OLEConstants.DESC_WORKBENCH_EXPORT_XML);
857     }
858 
859 
860     @RequestMapping(params = "methodToCall=addLineField")
861     public ModelAndView addLineField(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
862                                      HttpServletRequest request, HttpServletResponse response) {
863 
864         String selectedCollectionPath = uifForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
865         if (StringUtils.isBlank(selectedCollectionPath)) {
866             throw new RuntimeException("Selected collection was not set for add line action, cannot add new line");
867         }
868         OLESearchForm oleSearchForm = (OLESearchForm) uifForm;
869         int index = Integer.parseInt(oleSearchForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
870         if(oleSearchForm.getSearchConditions().get(index).getSearchField().getFieldName().isEmpty()&&
871                 oleSearchForm.getSearchConditions().get(index).getSearchField().getFieldValue().isEmpty()){
872             return getUIFModelAndView(uifForm);
873         }
874         List<SearchCondition> searchConditions = oleSearchForm.getSearchConditions();
875         String previousDocType =oleSearchForm.getSearchConditions().get(index).getSearchField().getDocType();
876         index++;
877         SearchCondition searchCondition=new SearchCondition();
878         searchCondition.setOperator("AND");
879         SearchField searchField = new SearchField();
880         searchField.setDocType(previousDocType);
881         searchField.setFieldName("any");
882         searchCondition.setSearchField(searchField);
883         searchConditions.add(index,searchCondition);
884         return getUIFModelAndView(uifForm);
885     }
886 
887     public void searchDocstoreData(OLESearchForm oleSearchForm,HttpServletRequest request) {
888 
889         oleSearchForm.getSearchParams().setDocType(oleSearchForm.getDocType());
890 
891         boolean isRemoveSearchCondition = false;
892         setShowPageSizeEntries(oleSearchForm);
893         SearchParams searchParams = oleSearchForm.getSearchParams();
894         oleSearchForm.getSearchParams().getSearchConditions().clear();
895         searchParams.getSearchConditions().addAll(oleSearchForm.getSearchConditions());
896         searchParams.getSearchResultFields().clear();
897         searchParams.setPageSize(oleSearchForm.getPageSize());
898         int startIndex = searchParams.getStartIndex();
899         if(oleSearchForm.getPageNumber().equalsIgnoreCase("1")){
900             searchParams.setStartIndex(0);
901         }else{
902             searchParams.setStartIndex(startIndex - startIndex % searchParams.getPageSize());
903         }
904         for (SearchCondition searchCondition : oleSearchForm.getSearchConditions()) {
905             if(searchCondition.getSearchField().getFieldName().equalsIgnoreCase(BibConstants.ISBN_SEARCH)){
906                String fieldValue= searchCondition.getSearchField().getFieldValue().replaceAll("-","");
907                     ISBNUtil isbnUtil = new ISBNUtil();
908                     try {
909                         fieldValue = isbnUtil.normalizeISBN(fieldValue);
910                     } catch (OleException e) {
911                         LOG.error("Exception "+e);
912                     }
913                searchCondition.getSearchField().setFieldValue(fieldValue);
914                searchCondition.setSearchScope("phrase");
915             }
916             if(DocType.ITEM.getCode().equals(oleSearchForm.getDocType()) && searchCondition.getSearchField().getFieldName().equalsIgnoreCase(ItemConstants.BIB_IDENTIFIER)){
917                 if(!DocumentUniqueIDPrefix.hasPrefix(searchCondition.getSearchField().getFieldValue())){
918                     searchCondition.getSearchField().setFieldValue(DocumentUniqueIDPrefix.PREFIX_WORK_BIB_MARC+"-"+searchCondition.getSearchField().getFieldValue());
919                 }
920 
921             }
922         }
923 
924 
925         if(CollectionUtils.isEmpty(searchParams.getSearchConditions())) {
926             isRemoveSearchCondition = true;
927             searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField(oleSearchForm.getDocType(), "", ""), ""));
928         }
929         request.getSession().setAttribute("searchParams", searchParams);
930         if(!oleSearchForm.isMoreFacets()) {
931             searchParams.getFacetFields().clear();
932             Set<String> facetFields = getFacetFields(oleSearchForm.getDocType());
933             searchParams.getFacetFields().addAll(facetFields);
934             searchParams.setFacetLimit(documentSearchConfig.getFacetPageSizeShort());
935         }
936         oleSearchForm.setFacetLimit(documentSearchConfig.getFacetPageSizeShort()-1);
937         SearchResponse searchResponse = null;
938         oleSearchForm.setSearchResultDisplayFields(getDisplayFields(oleSearchForm));
939         searchParams.buildSearchParams(searchParams,oleSearchForm.getDocType());
940         if (oleSearchForm instanceof GlobalEditForm) {
941             if (((GlobalEditForm) oleSearchForm).isSelectAll()) {
942                 searchParams.setStartIndex(0);
943             }
944         }
945         float start = System.currentTimeMillis() / 1000;
946         try {
947             if (searchParams.getSortConditions() != null && searchParams.getSortConditions().size() > 0) {
948                 searchParams.getSortConditions().clear();
949             }
950             SortCondition sortCondition = new SortCondition();
951             sortCondition.setSortField(oleSearchForm.getSortField());
952             sortCondition.setSortOrder(oleSearchForm.getSortOrder());
953             searchParams.getSortConditions().add(sortCondition);
954             searchResponse = getDocstoreLocalClient().search(searchParams);
955             oleSearchForm.setSearchResponse(searchResponse);
956         } catch (Exception e) {
957             LOG.error("Exception : ", e);
958         }
959 
960 
961 
962         float end = System.currentTimeMillis()/1000;
963         oleSearchForm.setSolrTime(String.valueOf(end-start));
964         List<SearchResultDisplayRow> searchResultDisplayRows = new ArrayList<>();
965         List<SearchResultDisplayRow> bibSearchResultDisplayRows=new ArrayList<>();
966         List<SearchResultDisplayRow> holdingsSearchResultDisplayRows=new ArrayList<>();
967         if(searchResponse!=null){
968             for (SearchResult searchResult : searchResponse.getSearchResults()) {
969                 SearchResultDisplayRow searchResultDisplayRow = new SearchResultDisplayRow();
970                 if (DocType.BIB.getCode().equalsIgnoreCase(oleSearchForm.getDocType())) {
971                     searchResultDisplayRow.buildBibSearchResultField(searchResult.getSearchResultFields(), eResourceId);
972                     bibSearchResultDisplayRows.add(searchResultDisplayRow);
973                 } else if (DocType.HOLDINGS.getCode().equals(oleSearchForm.getDocType())) {
974                     searchResultDisplayRow.buildHoldingSearchResultField(searchResult.getSearchResultFields());
975                     holdingsSearchResultDisplayRows.add(searchResultDisplayRow);
976                 } else if (DocType.EHOLDINGS.getCode().equals(oleSearchForm.getDocType())) {
977                     searchResultDisplayRow.buildEHoldingSearchResultField(searchResult.getSearchResultFields());
978                     holdingsSearchResultDisplayRows.add(searchResultDisplayRow);
979                 } else if (DocType.ITEM.getCode().equals(oleSearchForm.getDocType())) {
980                     searchResultDisplayRow.buildItemSearchResultField(searchResult.getSearchResultFields());
981                 }
982                 searchResultDisplayRows.add(searchResultDisplayRow);
983             }
984         } else {
985             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.ERROR_INFIELD);
986             return;
987         }
988         oleSearchForm.setSearchResultDisplayRowList(searchResultDisplayRows);
989         oleSearchForm.setBibSearchResultDisplayRowList(bibSearchResultDisplayRows);
990         oleSearchForm.setHoldingSearchResultDisplayRowList(holdingsSearchResultDisplayRows);
991         if (searchResponse != null && searchResponse.getFacetResult() != null) {
992             oleSearchForm.setFacetResultFields(searchResponse.getFacetResult().getFacetResultFields());
993         }
994         if (searchResultDisplayRows.size() == 0) {
995             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.DESCRIBE_SEARCH_MESSAGE);
996         }
997         request.getSession().setAttribute("searchResultDisplayRowList", searchResultDisplayRows);
998         setPageNextPreviousAndEntriesInfo(oleSearchForm);
999         if(isRemoveSearchCondition) {
1000             oleSearchForm.getSearchParams().getSearchConditions().clear();
1001         }
1002 //        oleSearchForm.getSearchParams().setFacetPrefix(null);
1003         if(oleSearchForm.isMoreFacets())  {
1004             showFacetPreviousNext(oleSearchForm);
1005         }
1006         if (oleSearchForm instanceof GlobalEditForm) {
1007             ((GlobalEditForm) oleSearchForm).setTotalRecords(totalRecCount);
1008         }
1009 
1010         for (SearchCondition searchCondition : oleSearchForm.getSearchConditions()) {
1011               if(DocType.ITEM.getCode().equals(oleSearchForm.getDocType()) && searchCondition.getSearchField().getFieldName().equalsIgnoreCase(ItemConstants.BIB_IDENTIFIER)){
1012                 if(DocumentUniqueIDPrefix.hasPrefix(searchCondition.getSearchField().getFieldValue())){
1013                     searchCondition.getSearchField().setFieldValue(DocumentUniqueIDPrefix.getDocumentId(searchCondition.getSearchField().getFieldValue()));
1014                 }
1015             }
1016         }
1017     }
1018 
1019     private void showFacetPreviousNext(OLESearchForm oleSearchForm) {
1020         int offset = oleSearchForm.getSearchParams().getFacetOffset();
1021         int size = oleSearchForm.getSearchParams().getFacetLimit();
1022         int totalRecordCount = 0;
1023         if(oleSearchForm.getSearchResponse() != null && oleSearchForm.getSearchResponse().getFacetResult() != null && oleSearchForm.getSearchResponse().getFacetResult().getFacetResultFields() != null && oleSearchForm.getSearchResponse().getFacetResult().getFacetResultFields().get(0) != null) {
1024             totalRecordCount = oleSearchForm.getSearchResponse().getFacetResult().getFacetResultFields().get(0).getValueCounts().size();
1025         }
1026         if(offset - size >= 0) {
1027             oleSearchForm.setShowMoreFacetPrevious(true);
1028         }
1029         else {
1030             oleSearchForm.setShowMoreFacetPrevious(false);
1031         }
1032 
1033         if(totalRecordCount >= size) {
1034             oleSearchForm.setShowMoreFacetNext(true);
1035         }
1036         else {
1037             oleSearchForm.setShowMoreFacetNext(false);
1038         }
1039     }
1040 
1041     @RequestMapping(params = "methodToCall=deleteLineField")
1042     public ModelAndView deleteLineField(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
1043                                         HttpServletRequest request, HttpServletResponse response) {
1044 
1045         String selectedCollectionPath = uifForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
1046         if (StringUtils.isBlank(selectedCollectionPath)) {
1047             throw new RuntimeException("Selected collection was not set for add line action, cannot add new line");
1048         }
1049         OLESearchForm oleSearchForm = (OLESearchForm) uifForm;
1050         int index = Integer.parseInt(oleSearchForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
1051         List<SearchCondition> searchConditions = oleSearchForm.getSearchConditions();
1052         if (searchConditions.size() > 1) {
1053             searchConditions.remove(index);
1054         }
1055         return getUIFModelAndView(uifForm);
1056     }
1057 
1058     @RequestMapping(params = "methodToCall=showBibList")
1059     public ModelAndView showBibList(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
1060                                     HttpServletRequest request, HttpServletResponse response) {
1061         this.start = 0;
1062         LOG.debug("Inside the showBibList method");
1063         OLESearchForm oleSearchForm = (OLESearchForm) uifForm;
1064         oleSearchForm.setSearchTypeField("OLESearch");
1065         String listOfBib = request.getParameter("listOfBib");
1066         String bibList[] = listOfBib.split(",");
1067         SearchParams searchParams = new SearchParams();
1068         for(int bibCount = 0;bibCount<bibList.length;bibCount++){
1069             String uuid = listOfBib.split(",")[bibCount].replaceAll("\\D+","");
1070             SearchCondition searchCondition = searchParams.buildSearchCondition("",searchParams.buildSearchField("bibliographic","LocalId_search",uuid),"OR");
1071             searchParams.getSearchConditions().add(searchCondition);
1072             oleSearchForm.getSearchConditions().add(searchCondition);
1073         }
1074         oleSearchForm.setSearchParams(searchParams);
1075         request.getSession().setAttribute("selectedFacetResults", null);
1076         if (oleSearchForm.getDocType() == null) {
1077             oleSearchForm.setDocType(DocType.BIB.getCode());
1078         }
1079         if(StringUtils.isEmpty(oleSearchForm.getSearchType())) {
1080             oleSearchForm.setSearchType("search");
1081         }
1082         if(StringUtils.isEmpty(oleSearchForm.getBrowseField())) {
1083             oleSearchForm.setBrowseField("title");
1084         }
1085         oleSearchForm.setBrowseText(null);
1086         oleSearchForm.setShowRequestXml(false);
1087         oleSearchForm.setHoldingsList(null);
1088         oleSearchForm.setItemList(null);
1089         oleSearchForm.setSearchResultDisplayRowList(null);
1090         oleSearchForm.setCallNumberBrowseText(null);
1091         oleSearchForm.setLocation(null);
1092         GlobalVariables.getMessageMap().clearErrorMessages();
1093         boolean hasSearchPermission = canSearch(GlobalVariables.getUserSession().getPrincipalId());
1094         if (!hasSearchPermission && oleSearchForm.getDocType().equalsIgnoreCase(OLEConstants.BIB_DOC_TYPE)) {
1095             boolean hasLinkPermission = canLinkBibForRequisition(GlobalVariables.getUserSession().getPrincipalId());
1096             if (!hasLinkPermission) {
1097                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.ERROR_AUTHORIZATION);
1098                 return search(oleSearchForm, result, request, response);
1099             }
1100         } else if (!hasSearchPermission) {
1101             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.ERROR_AUTHORIZATION);
1102             return search(oleSearchForm, result, request, response);
1103         }
1104         return search(oleSearchForm,result,request,response);
1105     }
1106 
1107     private void clearForm(OLESearchForm oleSearchForm) {
1108 
1109         List<Integer> pageSizes = documentSearchConfig.getPageSizes();
1110         if(!pageSizes.isEmpty() || pageSizes.size() > 0) {
1111             oleSearchForm.setPageSize(pageSizes.get(0));
1112         }
1113         oleSearchForm.setPreviousFlag(false);
1114         oleSearchForm.setNextFlag(false);
1115         oleSearchForm.setBrowseText(null);
1116         oleSearchForm.setShowRequestXml(false);
1117         oleSearchForm.setHoldingsList(null);
1118         oleSearchForm.setItemList(null);
1119         oleSearchForm.setSearchResultDisplayRowList(null);
1120         oleSearchForm.setCallNumberBrowseText(null);
1121         oleSearchForm.setLocation(null);
1122         oleSearchForm.setClassificationScheme("LCC");
1123         oleSearchForm.setFacetLimit(0);
1124         oleSearchForm.setTotalRecordCount(0);
1125         oleSearchForm.setSearchParams(new SearchParams());
1126         oleSearchForm.setPageShowEntries(null);
1127         oleSearchForm.setShowPageSize(null);
1128         oleSearchForm.setShowFieldSort(null);
1129         oleSearchForm.setBibSearchResultDisplayRowList(null);
1130         oleSearchForm.setHoldingSearchResultDisplayRowList(null);
1131         oleSearchForm.setSearchResponse(null);
1132         oleSearchForm.setFacetResultFields(null);
1133 
1134         oleSearchForm.setWorkBibDocumentList(null);
1135         oleSearchForm.setWorkHoldingsDocumentList(null);
1136         oleSearchForm.setWorkItemDocumentList(null);
1137         oleSearchForm.setWorkEHoldingsDocumentList(null);
1138         oleSearchForm.setSearchTypeField("OLESearch");
1139         oleSearchForm.setSelectAllRecords(false);
1140 
1141         if (oleSearchForm.getSearchParams() != null) {
1142             for (SearchCondition searchCondition : oleSearchForm.getSearchConditions()) {
1143                 if (searchCondition.getSearchField() != null) {
1144                     searchCondition.getSearchField().setFieldName("");
1145                     searchCondition.getSearchField().setFieldValue("");
1146                 }
1147             }
1148 
1149             if (oleSearchForm.getSearchParams().getFacetFields() != null) {
1150                 oleSearchForm.getSearchParams().getFacetFields().clear();
1151             }
1152             if (oleSearchForm.getSearchParams().getFacetConditions() != null) {
1153                 oleSearchForm.getSearchParams().getFacetConditions().clear();
1154             }
1155             oleSearchForm.getSearchParams().getSearchResultFields().clear();
1156 
1157         }
1158         if (oleSearchForm.getSearchResultDisplayRowList() != null && oleSearchForm.getSearchResultDisplayRowList().size() > 0) {
1159             oleSearchForm.getSearchResultDisplayRowList().clear();
1160         }
1161 
1162         if (oleSearchForm.getFacetResultFields() != null) {
1163             oleSearchForm.getFacetResultFields().clear();
1164         }
1165 
1166     }
1167 
1168     @RequestMapping(params = "methodToCall=getHoldingsList")
1169     public ModelAndView getHoldingsList(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1170                                         HttpServletRequest request, HttpServletResponse response) throws Exception {
1171         OLESearchForm oleSearchForm = (OLESearchForm) form;
1172         oleSearchForm.setErrorMessage(null);
1173         oleSearchForm.setHoldingSearchResultDisplayRowList(null);
1174         oleSearchForm.seteHoldingsFlag("false");
1175         List<SearchResultDisplayRow> searchResultDisplayRowList = new ArrayList<>();
1176 
1177         if (oleSearchForm.getSearchResultDisplayRowList() != null && oleSearchForm.getSearchResultDisplayRowList().size() > 0) {
1178             for (SearchResultDisplayRow searchResultDisplay : oleSearchForm.getSearchResultDisplayRowList()) {
1179                 if (searchResultDisplay.isSelect()) {
1180                     BibTree bibTree = getDocstoreLocalClient().retrieveBibTree(searchResultDisplay.getLocalId());
1181                     List<HoldingsTree> holdingsTreeList = bibTree.getHoldingsTrees();
1182                     if (holdingsTreeList.size() > 0) {
1183                         oleSearchForm.setHoldingsFlag("true");
1184                         for (HoldingsTree holdingsTree : holdingsTreeList) {
1185                             if (holdingsTree.getHoldings().getHoldingsType().equalsIgnoreCase("print")) {
1186                                 SearchResultDisplayRow searchResultDisplayRow = new SearchResultDisplayRow();
1187                                 searchResultDisplayRow.setHoldingsIdentifier(holdingsTree.getHoldings().getId());
1188                                 searchResultDisplayRow.setTitle(holdingsTree.getHoldings().getBib().getTitle());
1189                                 searchResultDisplayRow.setBibIdentifier(holdingsTree.getHoldings().getBib().getId());
1190                                 searchResultDisplayRow.setCallNumber(holdingsTree.getHoldings().getCallNumber());
1191                                 searchResultDisplayRow.setLocalId(DocumentUniqueIDPrefix.getDocumentId(holdingsTree.getHoldings().getId()));
1192                                 searchResultDisplayRow.setLocationName(holdingsTree.getHoldings().getLocationName());
1193                                 searchResultDisplayRow.setInstanceIdentifier(holdingsTree.getHoldings().getId());
1194                                 searchResultDisplayRowList.add(searchResultDisplayRow);
1195                             }
1196                         }
1197                     }
1198                 }
1199             }
1200         }
1201         if (searchResultDisplayRowList.size() == 0) {
1202             oleSearchForm.setErrorMessage("selected bib doesnt have Holdings");
1203         }
1204         oleSearchForm.setHoldingSearchResultDisplayRowList(searchResultDisplayRowList);
1205         oleSearchForm.setWorkEHoldingsDocumentList(null);
1206         return super.navigate(oleSearchForm, result, request, response);
1207     }
1208 
1209     @RequestMapping(params = "methodToCall=getEHoldingsList")
1210     public ModelAndView getEHoldingsList(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1211                                          HttpServletRequest request, HttpServletResponse response) throws Exception {
1212         OLESearchForm oleSearchForm = (OLESearchForm) form;
1213         oleSearchForm.setErrorMessage(null);
1214         oleSearchForm.setHoldingsFlag("false");
1215         oleSearchForm.setHoldingSearchResultDisplayRowList(null);
1216         List<SearchResultDisplayRow> searchResultDisplayRowArrayList = new ArrayList<>();
1217         if (oleSearchForm.getSearchResultDisplayRowList() != null && oleSearchForm.getSearchResultDisplayRowList().size() > 0) {
1218             for (SearchResultDisplayRow searchResultDisplay : oleSearchForm.getSearchResultDisplayRowList()) {
1219                 if (searchResultDisplay.isSelect()) {
1220                     BibTree bibTree = getDocstoreLocalClient().retrieveBibTree(searchResultDisplay.getLocalId());
1221                     if (bibTree.getHoldingsTrees().size() > 0) {
1222                         oleSearchForm.seteHoldingsFlag("true");
1223                         for (HoldingsTree holdingsTree : bibTree.getHoldingsTrees()) {
1224                             if (holdingsTree.getHoldings().getHoldingsType().equalsIgnoreCase("electronic")) {
1225                                 OleHoldings oleHoldings = new OleHoldings();
1226                                 SearchResultDisplayRow searchResultDisplayRow = new SearchResultDisplayRow();
1227                                 HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
1228                                 oleHoldings = (OleHoldings) holdingOlemlRecordProcessor.fromXML(holdingsTree.getHoldings().getContent());
1229                                 searchResultDisplayRow.setAccessStatus(oleHoldings.getAccessStatus());
1230                                 searchResultDisplayRow.setPlatForm(oleHoldings.getPlatform() != null ? oleHoldings.getPlatform().getPlatformName() : null);
1231                                 searchResultDisplayRow.setImprint(oleHoldings.getImprint());
1232                                 searchResultDisplayRow.setTitle(holdingsTree.getHoldings().getBib().getTitle());
1233                                 searchResultDisplayRow.setStatisticalCode(oleHoldings.getStatisticalSearchingCode() != null ? oleHoldings.getStatisticalSearchingCode().getCodeValue() : null);
1234                                 searchResultDisplayRow.setLocationName(holdingsTree.getHoldings().getLocationName());
1235                                 searchResultDisplayRow.setBibIdentifier(holdingsTree.getHoldings().getBib().getId());
1236                                 searchResultDisplayRow.setInstanceIdentifier(holdingsTree.getHoldings().getId());
1237                                 searchResultDisplayRow.setHoldingsIdentifier(holdingsTree.getHoldings().getId());
1238                                 searchResultDisplayRow.setLocalId(DocumentUniqueIDPrefix.getDocumentId(holdingsTree.getHoldings().getId()));
1239                                 searchResultDisplayRowArrayList.add(searchResultDisplayRow);
1240                             }
1241                         }
1242                     }
1243                 }
1244             }
1245         }
1246         if (searchResultDisplayRowArrayList.size() == 0) {
1247             oleSearchForm.setErrorMessage("selected bib doesnt have EHoldings");
1248         }
1249         oleSearchForm.setHoldingSearchResultDisplayRowList(searchResultDisplayRowArrayList);
1250         oleSearchForm.setWorkHoldingsDocumentList(null);
1251         return navigate(oleSearchForm, result, request, response);
1252     }
1253 
1254     @RequestMapping(params = "methodToCall=linkToBib")
1255     public ModelAndView linkToBib(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1256                                   HttpServletRequest request, HttpServletResponse response) throws Exception {
1257         OLESearchForm oleSearchForm = (OLESearchForm) form;
1258         List<SearchResultDisplayRow> searchResultDisplayRowList = oleSearchForm.getHoldingSearchResultDisplayRowList();
1259         oleSearchForm.setSuccessMessage(null);
1260         if (searchResultDisplayRowList != null && searchResultDisplayRowList.size() > 0) {
1261             for (SearchResultDisplayRow searchResultDisplayRow : searchResultDisplayRowList) {
1262                 if (searchResultDisplayRow.isSelect()) {
1263                     processNewHoldingsResponse(searchResultDisplayRow, oleSearchForm.getTokenId());
1264                     Holdings holdings =getDocstoreLocalClient().retrieveHoldings(searchResultDisplayRow.getHoldingsIdentifier());
1265                     if (holdings.getHoldingsType().equalsIgnoreCase("electronic")) {
1266                         saveRecordToDocstore(searchResultDisplayRow, eResourceId);
1267                     }
1268                     oleSearchForm.setSuccessMessage("");
1269                     break;
1270                 } else {
1271                     oleSearchForm.setSuccessMessage(OLEConstants.HOLDINGS_ERROR_MESSAGE);
1272                 }
1273             }
1274         }
1275         if (eResourceId != null && !eResourceId.isEmpty()) {
1276             Map<String, String> tempId = new HashMap<String, String>();
1277             tempId.put(OLEConstants.OLEEResourceRecord.ERESOURCE_IDENTIFIER, eResourceId);
1278             OLEEResourceRecordDocument tempDocument = (OLEEResourceRecordDocument) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OLEEResourceRecordDocument.class, tempId);
1279             try {
1280                 Person principalPerson = SpringContext.getBean(PersonService.class).getPerson(GlobalVariables.getUserSession().getPerson().getPrincipalId());
1281                 tempDocument.getDocumentHeader().setWorkflowDocument(KRADServiceLocatorWeb.getWorkflowDocumentService().loadWorkflowDocument(tempDocument.getDocumentNumber(), principalPerson));
1282                 if (tempDocument != null) {
1283                     try {
1284                         tempDocument.setSelectInstance(OLEConstants.OLEEResourceRecord.LINK_EXIST_INSTANCE);
1285                         tempDocument.seteInstanceFlag(true);
1286                         getOleEResourceSearchService().getNewInstance(tempDocument, tempDocument.getDocumentNumber());
1287                         getDocumentService().updateDocument(tempDocument);
1288                     } catch (Exception e) {
1289                         throw new RiceRuntimeException(
1290                                 "Exception trying to save document: " + tempDocument
1291                                         .getDocumentNumber(), e);
1292                     }
1293                 }
1294             } catch (Exception e) {
1295                 throw new RiceRuntimeException(
1296                         "Exception trying to save document: " + tempDocument
1297                                 .getDocumentNumber(), e);
1298             }
1299         }
1300         return getUIFModelAndView(oleSearchForm);
1301     }
1302 
1303     private void processNewHoldingsResponse(SearchResultDisplayRow searchResultDisplayRow, String tokenId) throws Exception {
1304         OLEEditorResponse oleEditorResponse = new OLEEditorResponse();
1305         oleEditorResponse.setLinkedInstanceId(searchResultDisplayRow.getHoldingsIdentifier());
1306         oleEditorResponse.setTokenId(tokenId);
1307         HashMap<String, OLEEditorResponse> oleEditorResponseMap = new HashMap<String, OLEEditorResponse>();
1308         oleEditorResponseMap.put(tokenId, oleEditorResponse);
1309         OleDocstoreResponse.getInstance().setEditorResponse(oleEditorResponseMap);
1310     }
1311 
1312     private void saveRecordToDocstore(SearchResultDisplayRow searchResultDisplayRow, String eResourceId) throws Exception {
1313         Holdings eHoldings = new org.kuali.ole.docstore.common.document.EHoldings();
1314         OleHoldings oleHoldings = new OleHoldings();
1315         eHoldings = getDocstoreLocalClient().retrieveHoldings(searchResultDisplayRow.getHoldingsIdentifier());
1316         oleHoldings = new HoldingOlemlRecordProcessor().fromXML(eHoldings.getContent());
1317         oleHoldings.setEResourceId(eResourceId);
1318         eHoldings.setContent(new HoldingOlemlRecordProcessor().toXML(oleHoldings));
1319         getDocstoreLocalClient().updateHoldings(eHoldings);
1320     }
1321 
1322     @RequestMapping(params = "methodToCall=filterBibs")
1323     public ModelAndView filterBibs(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
1324                                    HttpServletRequest request, HttpServletResponse response) {
1325         OLESearchForm oleSearchForm = (OLESearchForm) uifForm;
1326         oleSearchForm.setDocType(DocType.BIB.getCode());
1327         oleSearchForm.setShowTime(true);
1328         oleSearchForm.setSearchTypeField("OLESearch");
1329         String author = request.getParameter("author");
1330         String isbn = request.getParameter("isbn");
1331         String oclcNumber = request.getParameter("oclcNumber");
1332         SearchParams searchParams = new SearchParams();
1333         SearchCondition searchCondition = new SearchCondition();
1334         SearchField searchField = new SearchField();
1335         if (StringUtils.isNotBlank(author)) {
1336             searchField.setFieldName("Author_search");
1337             searchField.setFieldValue(author);
1338             request.getSession().setAttribute("author", null);
1339         } else if (StringUtils.isNotBlank(isbn)) {
1340             searchField.setFieldName("ISBN_search");
1341             searchField.setFieldValue(isbn);
1342             request.getSession().setAttribute("isbn", null);
1343         } else if (StringUtils.isNotBlank(oclcNumber)) {
1344             searchField.setFieldName("mdf_035a");
1345             searchField.setFieldValue(oclcNumber);
1346             request.getSession().setAttribute("oclcNumber", null);
1347         }
1348         searchCondition.setOperator("AND");
1349         searchCondition.setSearchScope("AND");
1350         searchCondition.setSearchField(searchField);
1351         searchParams.getSearchConditions().add(searchCondition);
1352         oleSearchForm.getSearchConditions().add(searchCondition);
1353         oleSearchForm.setSearchParams(searchParams);
1354         if (StringUtils.isEmpty(oleSearchForm.getSearchType())) {
1355             oleSearchForm.setSearchType("search");
1356         }
1357         if (StringUtils.isEmpty(oleSearchForm.getBrowseField())) {
1358             oleSearchForm.setBrowseField("title");
1359         }
1360         oleSearchForm.setBrowseText(null);
1361         oleSearchForm.setShowRequestXml(false);
1362         oleSearchForm.setHoldingsList(null);
1363         oleSearchForm.setItemList(null);
1364         oleSearchForm.setSearchResultDisplayRowList(null);
1365         oleSearchForm.setCallNumberBrowseText(null);
1366         oleSearchForm.setLocation(null);
1367         GlobalVariables.getMessageMap().clearErrorMessages();
1368         oleSearchForm.setSearchResultDisplayFields(new SearchResultDisplayFields());
1369         return search(oleSearchForm, result, request, response);
1370     }
1371 
1372     @RequestMapping(params = "methodToCall=openAll")
1373     public ModelAndView openAll(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1374                                HttpServletRequest request, HttpServletResponse response) {
1375         OLESearchForm oleSearchForm = (OLESearchForm) form;
1376         oleSearchForm.getSearchParams().setFacetOffset(0);
1377         if("browse".equalsIgnoreCase(oleSearchForm.getSearchType())){
1378             browseDocstoreForLocalIds(oleSearchForm, request);
1379         }else{
1380             searchDocstoreForLocalIds(oleSearchForm, request);
1381         }
1382         return super.navigate(oleSearchForm, result, request, response);
1383     }
1384 
1385     private void browseDocstoreForLocalIds(OLESearchForm oleSearchForm, HttpServletRequest request) {
1386         int pageSize = oleSearchForm.getPageSize();
1387         oleSearchForm.setPageSize(40);
1388         StringBuilder ids = new StringBuilder();
1389         if ("title".equals(oleSearchForm.getBrowseField())) {
1390             oleSearchForm.setDocType(DocType.BIB.getCode());
1391             List<SearchResultDisplayRow> searchResultDisplayRowList = getBrowseService().browse(oleSearchForm);
1392             for( SearchResultDisplayRow searchResultDisplayRow:searchResultDisplayRowList){
1393                 ids.append("," + searchResultDisplayRow.getLocalId() +"#"+ searchResultDisplayRow.getBibIdentifier());
1394             }
1395         } else {
1396             String location = getBrowseService().validateLocation(oleSearchForm.getLocation());
1397             oleSearchForm.setLocation(location);
1398             if (oleSearchForm.getDocType().equalsIgnoreCase(DocType.ITEM.getCode())) {
1399                 List<Item> itemList = getBrowseService().browse(oleSearchForm);
1400                 for( Item item:itemList){
1401                     ids.append("," + item.getLocalId() + "#" + item.getHolding().getBib().getId());
1402                 }
1403             } else {
1404                 List<Holdings> holdingsList = getBrowseService().browse(oleSearchForm);
1405                 for( Holdings holdings:holdingsList){
1406                     ids.append("," + holdings.getLocalId() + "#" + holdings.getBib().getId());
1407                 }
1408             }
1409         }
1410         oleSearchForm.setPageSize(pageSize);
1411         oleSearchForm.setIdsToBeOpened(ids.toString().replaceFirst(",", ""));
1412     }
1413 
1414     public void searchDocstoreForLocalIds(OLESearchForm oleSearchForm,HttpServletRequest request) {
1415         setShowPageSizeEntries(oleSearchForm);
1416         SearchParams searchParams = oleSearchForm.getSearchParams();
1417         oleSearchForm.getSearchConditions().clear();
1418         oleSearchForm.getSearchConditions().addAll(searchParams.getSearchConditions());
1419         searchParams.getSearchResultFields().clear();
1420         searchParams.setPageSize(totalRecCount);
1421         searchParams.setStartIndex(0);
1422         for (SearchCondition searchCondition : oleSearchForm.getSearchConditions()) {
1423             if(searchCondition.getSearchField().getFieldName().equalsIgnoreCase(BibConstants.ISBN_SEARCH)){
1424                 String fieldValue= searchCondition.getSearchField().getFieldValue().replaceAll("-","");
1425                 ISBNUtil isbnUtil = new ISBNUtil();
1426                 try {
1427                     fieldValue = isbnUtil.normalizeISBN(fieldValue);
1428                 } catch (OleException e) {
1429                     LOG.error("Exception "+e);
1430                 }
1431                 searchCondition.getSearchField().setFieldValue(fieldValue);
1432                 searchCondition.setSearchScope("phrase");
1433             }
1434             if(DocType.ITEM.getCode().equals(oleSearchForm.getDocType()) && searchCondition.getSearchField().getFieldName().equalsIgnoreCase(ItemConstants.BIB_IDENTIFIER)){
1435                 if(!DocumentUniqueIDPrefix.hasPrefix(searchCondition.getSearchField().getFieldValue())){
1436                     searchCondition.getSearchField().setFieldValue(DocumentUniqueIDPrefix.PREFIX_WORK_BIB_MARC+"-"+searchCondition.getSearchField().getFieldValue());
1437                 }
1438             }
1439         }
1440 
1441         SearchResponse searchResponse = null;
1442         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(oleSearchForm.getDocType(), "LocalId_display"));
1443         if(oleSearchForm.getDocType().equalsIgnoreCase("bibliographic")){
1444             searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(oleSearchForm.getDocType(), Bib.ID));
1445         }else{
1446             searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(oleSearchForm.getDocType(), Bib.BIBIDENTIFIER));
1447             searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(oleSearchForm.getDocType(), Holdings.HOLDINGSIDENTIFIER));
1448         }
1449 
1450         if (oleSearchForm instanceof GlobalEditForm) {
1451             if (((GlobalEditForm) oleSearchForm).isSelectAll()) {
1452                 searchParams.setStartIndex(0);
1453             }
1454         }
1455         try {
1456             searchResponse = getDocstoreLocalClient().search(searchParams);
1457             StringBuilder ids = new StringBuilder();
1458             for( SearchResult searchResult:searchResponse.getSearchResults()){
1459                 if(oleSearchForm.getDocType().equalsIgnoreCase("bibliographic")){
1460                     ids.append("," + searchResult.getSearchResultFields().get(0).getFieldValue() +"#"+ searchResult.getSearchResultFields().get(1).getFieldValue());
1461                 }else{
1462                     ids.append("," + searchResult.getSearchResultFields().get(0).getFieldValue() +"#"+ searchResult.getSearchResultFields().get(1).getFieldValue()+"#"+ searchResult.getSearchResultFields().get(2).getFieldValue());
1463                 }
1464 
1465             }
1466             oleSearchForm.setIdsToBeOpened(ids.toString().replaceFirst(",",""));
1467 
1468         } catch (Exception e) {
1469             LOG.error("Exception : ", e);
1470         }
1471 
1472     }
1473 
1474 
1475     @RequestMapping(params = "methodToCall=changeDocType")
1476     public ModelAndView changeDocType(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1477                                       HttpServletRequest request, HttpServletResponse response) {
1478         OLESearchForm oleSearchForm = (OLESearchForm) form;
1479         Set<String> docTypes = new HashSet<>();
1480         docTypes.add(oleSearchForm.getDocType());
1481         for (SearchCondition searchCondition : oleSearchForm.getSearchConditions()) {
1482             if (searchCondition.getSearchField() != null) {
1483                 if (searchCondition.getSearchField().getDocType() != null && !searchCondition.getSearchField().getDocType().isEmpty()) {
1484                     docTypes.add(searchCondition.getSearchField().getDocType());
1485                 }
1486             }
1487         }
1488         if (docTypes != null && docTypes.size() > 2) {
1489             String errorMessage = null;
1490             if (oleSearchForm.getDocType().equalsIgnoreCase(OLEConstants.HOLDING_DOC_TYPE)) {
1491                 errorMessage = OLEConstants.DOCTYPE_HOLDING_COMBINATION;
1492             } else if (oleSearchForm.getDocType().equalsIgnoreCase(OLEConstants.ITEM_DOC_TYPE)) {
1493                 errorMessage = OLEConstants.DOCTYPE_ITEM_COMBINATION;
1494             } else if (oleSearchForm.getDocType().equalsIgnoreCase(OLEConstants.BIB_DOC_TYPE)) {
1495                 errorMessage = OLEConstants.DOCTYPE_BIB_COMBINATION;
1496             }
1497             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.DOCTYPE_COMBINATION_ERROR, errorMessage);
1498         }
1499         return getUIFModelAndView(oleSearchForm);
1500     }
1501 
1502     public void setControlFields(BibTree bibTree) {
1503         BibMarcRecordProcessor bibMarcRecordProcessor = new BibMarcRecordProcessor();
1504         BibMarcRecords bibRecords = bibMarcRecordProcessor.fromXML(bibTree.getBib().getContent());
1505         for (BibMarcRecord bibMarcRecord : bibRecords.getRecords()) {
1506             for (ControlField controlField : bibMarcRecord.getControlFields()) {
1507                 controlField.setValue(controlField.getValue().replaceAll(" ", "#"));
1508             }
1509         }
1510         bibTree.getBib().setContent(bibMarcRecordProcessor.toXml(bibRecords));
1511     }
1512 }