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