View Javadoc
1   package org.kuali.ole.docstore.discovery.service;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.solr.client.solrj.SolrQuery;
5   import org.apache.solr.client.solrj.SolrServer;
6   import org.apache.solr.client.solrj.SolrServerException;
7   import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
8   import org.apache.solr.client.solrj.response.QueryResponse;
9   import org.apache.solr.common.SolrDocument;
10  import org.apache.solr.common.SolrDocumentList;
11  import org.kuali.ole.docstore.discovery.model.CallNumberBrowseParams;
12  import org.kuali.ole.docstore.discovery.model.SearchCondition;
13  import org.kuali.ole.docstore.discovery.model.SearchParams;
14  import org.kuali.ole.docstore.discovery.solr.work.bib.WorkBibCommonFields;
15  import org.kuali.ole.docstore.model.bo.*;
16  import org.kuali.ole.docstore.model.enums.DocType;
17  import org.kuali.ole.pojo.OLESerialReceivingRecord;
18  import org.kuali.ole.utility.callnumber.CallNumber;
19  import org.kuali.ole.utility.callnumber.CallNumberFactory;
20  import org.kuali.rice.core.api.config.property.ConfigContext;
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  
24  import java.io.IOException;
25  import java.text.CharacterIterator;
26  import java.text.Format;
27  import java.text.SimpleDateFormat;
28  import java.text.StringCharacterIterator;
29  import java.util.ArrayList;
30  import java.util.Collection;
31  import java.util.Date;
32  import java.util.HashMap;
33  import java.util.Iterator;
34  import java.util.LinkedHashMap;
35  import java.util.List;
36  import java.util.Map;
37  import java.util.Set;
38  
39  /**
40   * User: tirumalesh.b
41   * Date: 16/1/12 Time: 12:49 PM
42   */
43  public class QueryServiceImpl
44          implements QueryService {
45      private static final Logger LOG = LoggerFactory.getLogger(QueryServiceImpl.class);
46      private static QueryService queryService = null;
47      private static String docSearchUrl = null;
48      private final String INSTANCE_IDENTIFIER = "instanceIdentifier";
49      private final String BIBLIOGRAPHIC = "bibliographic";
50      private final String DOC_TYPE = "DocType";
51      private final String DOC_FORMAT = "DocFormat";
52      private final String HOLDINGS_IDENTIFIER = "holdingsIdentifier";
53      private final String ITEM_IDENTIFIER = "itemIdentifier";
54      private final String INSTANCE = "instance";
55      private final String OLEML = "oleml";
56      private final String DELETE_WITH_LINKED_DOCS = "deleteWithLinkedDocs";
57      private final String DELETE = "delete";
58  
59      private QueryServiceImpl() {
60          init();
61      }
62  
63      public static QueryService getInstance() {
64          if (null == queryService) {
65              queryService = new QueryServiceImpl();
66          }
67          return queryService;
68      }
69  
70      protected void init() {
71          LOG.debug("QueryServiceImpl init ");
72          if(ConfigContext.getCurrentContextConfig()!=null){
73              docSearchUrl = ConfigContext.getCurrentContextConfig().getProperty("docSearchURL");
74              if ((null != docSearchUrl) && !docSearchUrl.endsWith("/")) {
75                  docSearchUrl = docSearchUrl + "/";
76              }
77          }
78  
79      }
80  
81      @Override
82      public List<OleDocument> queryForDocs(OleDocument queryDoc) throws Exception {
83          List<OleDocument> oleDocuments;
84          List<Map<String, Object>> solrHits;
85          String queryString = buildQueryForDoc(queryDoc);
86          solrHits = getSolrHitsForQuery(queryString);
87          oleDocuments = buildOleDocuments(solrHits);
88          return oleDocuments;
89      }
90  
91      private List<Map<String, Object>> getSolrHits(SearchParams searchParams) throws Exception {
92          List<Map<String, Object>> solrHits;
93          String queryString = buildQuery(searchParams);
94          solrHits = getSolrHitsForQuery(queryString);
95          return solrHits;
96      }
97  
98      /**
99       * To get the records from solr
100      * @param searchParams
101      * @return solrHits
102      * @throws Exception
103      */
104     private List<Map<String, Object>> getSolrHitsWithParams(SearchParams searchParams) throws Exception {
105         int totalRecords = 0;
106         List<Map<String, Object>> solrHits;
107         StringBuffer initialQuery = new StringBuffer();
108         buildInitialQuery(initialQuery, searchParams);
109         if(StringUtils.isNotEmpty(searchParams.getSortField()) && StringUtils.isNotEmpty(searchParams.getSortOrder())) {
110             buildSortQuery(initialQuery , searchParams);
111         }
112         totalRecords = getSolrHitsForQueryParams(initialQuery.toString());
113         LOG.info("Total Records count:" + totalRecords);
114         searchParams.setTotalRecCount(totalRecords);
115         searchParams.setSearchQuery(initialQuery.toString());
116         solrHits = getSolrHitsForQueryParams(searchParams);
117         return solrHits;
118     }
119 
120     /**
121      * prepare the sort solr query based on sortFiled and sortOrder type in search params
122      * @param solrQuery , searchParams
123      * @return
124      */
125     private void buildSortQuery(StringBuffer solrQuery , SearchParams searchParams) {
126         String sortField = getSortField(searchParams.getSortField());
127         String sortOrder = searchParams.getSortOrder();
128         if(StringUtils.isNotEmpty(sortField))  {
129             solrQuery.append("&sort="+sortField+" "+sortOrder);
130         }
131     }
132 
133     /**
134      * return the sort filed according to docstore
135      * @param sortField
136      * @return
137      */
138     private String getSortField(String sortField) {
139 
140         if("Title".equals(sortField)) {
141             return "Title_sort";
142         }else  if("Author".equals(sortField)) {
143             return "Author_sort";
144         }else  if("Publication Date".equals(sortField)) {
145             return "PublicationDate_sort";
146         } else  if("Location".equals(sortField)) {
147             return "Location_sort";
148         }else  if("Call Number".equals(sortField)) {
149             return "CallNumber_sort";
150         }
151         return null;
152 
153     }
154 
155     @Override
156     public List<WorkBibDocument> getBibDocuments(SearchParams searchParams) throws Exception {
157         List<WorkBibDocument> workBibDocuments;
158         List<Map<String, Object>> solrHits = getSolrHitsWithParams(searchParams);
159         workBibDocuments = buildWorkBibDocuments(solrHits);
160         return workBibDocuments;
161     }
162 
163     @Override
164     public List<WorkHoldingsDocument> getHoldingDocuments(SearchParams searchParams) throws Exception {
165         List<WorkHoldingsDocument> workHoldingsDocuments;
166         List<Map<String, Object>> solrHits = getSolrHitsWithParams(searchParams);
167         workHoldingsDocuments = buildWorkHoldingDocuments(solrHits);
168         // Set bibTitle and showBibMessage for HoldingsDocument
169         for (WorkHoldingsDocument workHoldingsDocument : workHoldingsDocuments) {
170             if(workHoldingsDocument.getBibIdentifier() != null){
171                 WorkBibDocument workBibDocument = new WorkBibDocument();
172                 workBibDocument.setId(workHoldingsDocument.getBibIdentifier());
173                 workBibDocument = buildBibDocumentInfo(workBibDocument);
174                 workHoldingsDocument.setBibUUIDList(queryService.queryForBibs(workHoldingsDocument.getInstanceIdentifier()));
175                 String showBibMessage = getLinkedBibCount(workHoldingsDocument.getBibUUIDList());
176                 workHoldingsDocument.setBibTitle(workBibDocument.getTitle());
177                 workHoldingsDocument.setLinkedBibCount(showBibMessage);
178             }
179 
180         }
181         return workHoldingsDocuments;
182     }
183 
184     @Override
185     public List<OLESerialReceivingRecord> getOleSerialReceivingRecords(SearchParams searchParams) throws Exception {
186         List<OLESerialReceivingRecord> oleSerialReceivingRecordList;
187         List<Map<String, Object>> solrHits = getSolrHits(searchParams);
188         oleSerialReceivingRecordList = buildOleSerialReceivingRecords(solrHits);
189         return oleSerialReceivingRecordList;
190     }
191 
192     @Override
193     public List<WorkItemDocument> getItemDocuments(SearchParams searchParams) throws Exception {
194         List<WorkItemDocument> workItemDocuments;
195         List<Map<String, Object>> solrHits = getSolrHitsWithParams(searchParams);
196         workItemDocuments = buildWorkItemDocuments(solrHits);
197         // Set bibTitle and showBibMessage for ItemDocument
198         for (WorkItemDocument workItemDocument : workItemDocuments) {
199             if(workItemDocument.getBibIdentifier() != null) {
200                 WorkBibDocument workBibDocument = new WorkBibDocument();
201                 workBibDocument.setId(workItemDocument.getBibIdentifier());
202                 workBibDocument = buildBibDocumentInfo(workBibDocument);
203                 workItemDocument.setBibUUIDList(queryService.queryForBibs(workItemDocument.getInstanceIdentifier()));
204                 String linkedBibCount = getLinkedBibCount(workItemDocument.getBibUUIDList());
205                 workItemDocument.setBibTitle(workBibDocument.getTitle());
206                 workItemDocument.setLinkedBibCount(linkedBibCount);
207             }
208         }
209         return workItemDocuments;
210     }
211 
212 
213     /**
214      *  Get List of records from solr after click next or previous
215      * @param searchParams
216      * @return List Of Documents
217      * @throws Exception
218      * */
219     @Override
220     public List getDocuments(SearchParams searchParams) {
221         List<WorkItemDocument> workItemDocumentList = null;
222         List<WorkHoldingsDocument> workHoldingsDocumentList = null;
223         List<WorkBibDocument> workBibDocumentList = null;
224         List<WorkEHoldingsDocument> workEHoldingsDocumentList = null;
225 
226         try {
227             if (searchParams.getDocType().equalsIgnoreCase("item")) {
228                 workItemDocumentList = getItemDocuments(searchParams);
229                 return workItemDocumentList;
230             } else if (searchParams.getDocType().equalsIgnoreCase("holdings")) {
231                 workHoldingsDocumentList = getHoldingDocuments(searchParams);
232                 return workHoldingsDocumentList;
233             } else if (searchParams.getDocType().equalsIgnoreCase("bibliographic")) {
234                 workBibDocumentList = getBibDocuments(searchParams);
235                 return workBibDocumentList;
236             } else if (searchParams.getDocType().equalsIgnoreCase("eholdings")) {
237                 workEHoldingsDocumentList = getEHoldingsDocuments(searchParams);
238                 return workEHoldingsDocumentList;
239             }
240         } catch (Exception e) {
241             LOG.info("Exception in DocumentSearch " + e);
242         }
243         return null;
244 
245     }
246 
247 
248     @Override
249     public String queryField(SearchParams searchParams, String fieldName) throws Exception {
250         SolrQuery solrQuery = new SolrQuery();
251         SolrServer server = SolrServerManager.getInstance().getSolrServer();
252         String query = ServiceLocator.getDiscoveryService().buildQuery(searchParams);
253         int i = query.indexOf("=");
254         String query1 = query.substring(i + 1);
255         solrQuery.setQuery(query1);
256         solrQuery.setStart(1);
257         solrQuery.setRows(100);
258         QueryResponse response = server.query(solrQuery);
259         return getFieldValue(response, fieldName);
260     }
261 
262     public String getFieldValue(QueryResponse response, String fieldName) {
263         String result = null;
264         SolrDocumentList docs = response.getResults();
265         if (docs != null) {
266             SolrDocument doc = docs.get(0);
267             result = (String) doc.getFieldValue(fieldName);
268         }
269         return result;
270     }
271 
272     /**
273      * @param hitsOnPage
274      * @return List of OleDocuments
275      */
276     public List<OLESerialReceivingRecord> buildOleSerialReceivingRecords(List<Map<String, Object>> hitsOnPage) throws SolrServerException {
277         List<OLESerialReceivingRecord> oleSerialReceivingRecordList = new ArrayList<OLESerialReceivingRecord>();
278         for (Map<String, Object> hitsOnPageItr : hitsOnPage) {
279             OLESerialReceivingRecord oleSerialReceivingRecord = new OLESerialReceivingRecord();
280             Map map = hitsOnPageItr;
281             Set keys = map.keySet();
282             for (Object key : keys) {
283                 if (key.toString().equalsIgnoreCase("Title_display")) {
284                     Object value = map.get(key);
285                     if (value instanceof String) {
286                         oleSerialReceivingRecord.setTitle((String) value);
287                     }
288                 }
289                 if (key.toString().equalsIgnoreCase("CallNumber_display")) {
290                     Object value = map.get(key);
291                     if (value instanceof String) {
292                         oleSerialReceivingRecord.setCallNumber((String) value);
293                     }
294                 }
295                 if (key.toString().equalsIgnoreCase("ISSN_display")) {
296                     Object value = map.get(key);
297                     if (value instanceof String) {
298                         oleSerialReceivingRecord.setIssn((String) value);
299                     }
300                 }
301 
302                 if (key.toString().equalsIgnoreCase("id")) {
303                     Object value = map.get(key);
304                     if (value instanceof String) {
305                         oleSerialReceivingRecord.setBibId((String) value);
306                     }
307                 }
308 
309                 if (key.toString().equalsIgnoreCase("instanceIdentifier")) {
310                     Object value = map.get(key);
311                     if (value instanceof String) {
312                         WorkInstanceDocument workInstanceDocument = new WorkInstanceDocument();
313                         workInstanceDocument.setId(value.toString());
314                         workInstanceDocument =  queryForInstanceTree(workInstanceDocument);
315                         String callNumber = workInstanceDocument!=null && workInstanceDocument.getHoldingsDocument()!=null ?
316                                 workInstanceDocument.getHoldingsDocument().getCallNumber():null;
317                         String location = workInstanceDocument!=null && workInstanceDocument.getHoldingsDocument()!=null ?
318                                 workInstanceDocument.getHoldingsDocument().getLocationName():null;
319                         oleSerialReceivingRecord.setInstanceId((String) value);
320                         oleSerialReceivingRecord.setCallNumber(callNumber);
321                         oleSerialReceivingRecord.setBoundLocation(location);
322                         oleSerialReceivingRecordList.add(oleSerialReceivingRecord);
323                     } else if (value instanceof List) {
324                         ArrayList<String> list = (ArrayList<String>) value;
325                         for (String str : list) {
326                             WorkInstanceDocument workInstanceDocument = new WorkInstanceDocument();
327                             workInstanceDocument.setId(str);
328                             workInstanceDocument =  queryForInstanceTree(workInstanceDocument);
329                             String callNumber = workInstanceDocument!=null && workInstanceDocument.getHoldingsDocument()!=null ?
330                                     workInstanceDocument.getHoldingsDocument().getCallNumber():null;
331                             String location = workInstanceDocument!=null && workInstanceDocument.getHoldingsDocument()!=null ?
332                                     workInstanceDocument.getHoldingsDocument().getLocationName():null;
333                             OLESerialReceivingRecord oleSerialReceiving_Record = new OLESerialReceivingRecord();
334                             oleSerialReceiving_Record.setBibId(oleSerialReceivingRecord.getBibId());
335                             oleSerialReceiving_Record.setTitle(oleSerialReceivingRecord.getTitle());
336                             oleSerialReceiving_Record.setCallNumber(callNumber);
337                             oleSerialReceiving_Record.setIssn(oleSerialReceivingRecord.getIssn());
338                             oleSerialReceiving_Record.setInstanceId(str);
339                             oleSerialReceiving_Record.setBoundLocation(location);
340                             oleSerialReceivingRecordList.add(oleSerialReceiving_Record);
341                         }
342 
343                     }
344                 }
345 
346 
347             }
348 
349         }
350         return oleSerialReceivingRecordList;
351     }
352 
353     /**
354      * @param hitsOnPage
355      * @return List of OleDocuments
356      */
357     public List<OleDocument> buildOleDocuments(List<Map<String, Object>> hitsOnPage) {
358         List<OleDocument> oleDocuments = new ArrayList<OleDocument>();
359         for (Map<String, Object> hitsOnPageItr : hitsOnPage) {
360             WorkBibDocument workBibDocument = new WorkBibDocument();
361             Map map = hitsOnPageItr;
362             Set keys = map.keySet();
363             for (Object key : keys) {
364                 if (key.toString().equalsIgnoreCase("Title_display")) {
365                     Object value = map.get(key);
366                     if (value instanceof String) {
367                         workBibDocument.setTitle((String) value);
368                     } else if (value instanceof List) {
369                         workBibDocument.setTitles((List<String>) value);
370                     }
371                 }
372 
373                 if (key.toString().equalsIgnoreCase("Author_display")) {
374                     Object value = map.get(key);
375                     if (value instanceof String) {
376                         workBibDocument.setAuthor((String) value);
377                     } else if (value instanceof List) {
378                         workBibDocument.setAuthors((List<String>) value);
379                     }
380                 }
381 
382                 if (key.toString().equalsIgnoreCase("PublicationDate_display")) {
383                     Object value = map.get(key);
384                     if (value instanceof String) {
385                         workBibDocument.setPublicationDate((String) value);
386                     } else if (value instanceof List) {
387                         workBibDocument.setPublicationDates((List<String>) value);
388                     }
389                 }
390 
391                 if (key.toString().equalsIgnoreCase("ISBN_display")) {
392                     Object value = map.get(key);
393                     if (value instanceof String) {
394                         workBibDocument.setIsbn((String) value);
395                     } else if (value instanceof List) {
396                         workBibDocument.setIsbns((List<String>) value);
397                     }
398                 }
399 
400                 if (key.toString().equalsIgnoreCase("Publisher_display")) {
401                     Object value = map.get(key);
402                     if (value instanceof String) {
403                         workBibDocument.setPublisher((String) value);
404                     } else if (value instanceof List) {
405                         workBibDocument.setPublishers((List<String>) value);
406                     }
407                 }
408                 if (key.toString().equalsIgnoreCase("id")) {
409                     workBibDocument.setId((String) map.get(key));
410                 }
411                 if (key.toString().equalsIgnoreCase("bibIdentifier")) {
412                     workBibDocument.setId((String) map.get(key));
413                 }
414 
415             }
416             oleDocuments.add(workBibDocument);
417         }
418         return oleDocuments;
419     }
420 
421     /**
422      * @param hitsOnPage
423      * @return List of workBibDocuments
424      */
425     public List<WorkBibDocument> buildWorkBibDocuments(List<Map<String, Object>> hitsOnPage) {
426         List<WorkBibDocument> workBibDocuments = new ArrayList<WorkBibDocument>();
427         for (Map<String, Object> hitsOnPageItr : hitsOnPage) {
428             WorkBibDocument workBibDocument = new WorkBibDocument();
429             Map map = hitsOnPageItr;
430             Set keys = map.keySet();
431             for (Object key : keys) {
432 
433                 if (key.toString().equalsIgnoreCase("LocalId_display")) {
434                     Object value = map.get(key);
435                     if (value instanceof String) {
436                         workBibDocument.setLocalIds((String) value);
437                     } else if (value instanceof List) {
438                         workBibDocument.setLocalId((List<String>) value);
439                     }
440                 }
441                 if (key.toString().equalsIgnoreCase("Title_display")) {
442                     Object value = map.get(key);
443                     if (value instanceof String) {
444                         workBibDocument.setTitle((String) value);
445                     } else if (value instanceof List) {
446                         workBibDocument.setTitles((List<String>) value);
447                     }
448                 }
449 
450                 if (key.toString().equalsIgnoreCase("Author_display")) {
451                     Object value = map.get(key);
452                     if (value instanceof String) {
453                         workBibDocument.setAuthor((String) value);
454                     } else if (value instanceof List) {
455                         workBibDocument.setAuthors((List<String>) value);
456                     }
457                 }
458 
459                 if (key.toString().equalsIgnoreCase("PublicationDate_display")) {
460                     Object value = map.get(key);
461                     if (value instanceof String) {
462                         workBibDocument.setPublicationDate((String) value);
463                     } else if (value instanceof List) {
464                         workBibDocument.setPublicationDates((List<String>) value);
465                     }
466                 }
467 
468                 if (key.toString().equalsIgnoreCase("ISBN_display")) {
469                     Object value = map.get(key);
470                     if (value instanceof String) {
471                         workBibDocument.setIsbn((String) value);
472                     } else if (value instanceof List) {
473                         workBibDocument.setIsbns((List<String>) value);
474                     }
475                 }
476 
477                 if (key.toString().equalsIgnoreCase("Publisher_display")) {
478                     Object value = map.get(key);
479                     if (value instanceof String) {
480                         workBibDocument.setPublisher((String) value);
481                     } else if (value instanceof List) {
482                         workBibDocument.setPublishers((List<String>) value);
483                     }
484                 }
485 
486                 if (key.toString().equalsIgnoreCase("instanceIdentifier")) {
487                     Object value = map.get(key);
488                     if (value instanceof String) {
489                         workBibDocument.setInstanceIdentifier((String) value);
490                     } else if (value instanceof List) {
491                         ArrayList<String> list = (ArrayList<String>) value;
492                         workBibDocument.setInstanceIdentifier(list.get(0));
493                     }
494                 }
495 
496                 if (key.toString().equalsIgnoreCase("DocFormat")) {
497                     Object value = map.get(key);
498                     if (value instanceof String) {
499                         workBibDocument.setDocFormat((String) value);
500                     } else if (value instanceof List) {
501                         ArrayList<String> list = (ArrayList<String>) value;
502                         workBibDocument.setDocFormat(list.get(0));
503                     }
504                 }
505 
506                 if (key.toString().equalsIgnoreCase("staffOnlyFlag")) {
507                     Object value = map.get(key);
508                     if (value instanceof String) {
509                         workBibDocument.setStaffOnlyFlag((String) value);
510                     } else if (value instanceof List) {
511                         ArrayList<String> list = (ArrayList<String>) value;
512                         workBibDocument.setStaffOnlyFlag(list.get(0));
513                     }
514                 }
515 
516                 if (key.toString().equalsIgnoreCase("id")) {
517                     workBibDocument.setId((String) map.get(key));
518                 }
519 
520             }
521             workBibDocuments.add(workBibDocument);
522         }
523         return workBibDocuments;
524     }
525 
526     /**
527      * @param hitsOnPage
528      * @return List of workHoldingsDocuments
529      */
530     public List<WorkHoldingsDocument> buildWorkHoldingDocuments(List<Map<String, Object>> hitsOnPage) {
531         List<WorkHoldingsDocument> workHoldingsDocuments = new ArrayList<WorkHoldingsDocument>();
532         for (Map<String, Object> hitsOnPageItr : hitsOnPage) {
533             WorkHoldingsDocument workHoldingsDocument = new WorkHoldingsDocument();
534             Map map = hitsOnPageItr;
535             if (map.get("DocType").equals("holdings")) {
536                 Set keys = map.keySet();
537                 for (Object key : keys) {
538                     if (key.toString().equalsIgnoreCase("LocalId_display")) {
539                         Object value = map.get(key);
540                         if (value instanceof String) {
541                             workHoldingsDocument.setLocalId((String) value);
542                         }
543                     }
544                     if (key.toString().equalsIgnoreCase("Location_display")) {
545                         Object value = map.get(key);
546                         if (value instanceof String) {
547                             workHoldingsDocument.setLocationName((String) value);
548                         }
549                     }
550                     if (key.toString().equalsIgnoreCase("CallNumber_display")) {
551                         Object value = map.get(key);
552                         if (value instanceof String) {
553                             workHoldingsDocument.setCallNumber((String) value);
554                         }
555                     }
556                     if (key.toString().equalsIgnoreCase("id")) {
557                         workHoldingsDocument.setHoldingsIdentifier((String) map.get(key));
558                     }
559                     if (key.toString().equalsIgnoreCase("bibIdentifier")) {
560                         Object value = map.get(key);
561                         if (value instanceof String) {
562                             workHoldingsDocument.setBibIdentifier((String) value);
563                         } else if (value instanceof List) {
564                             ArrayList<String> list = (ArrayList<String>) value;
565                             workHoldingsDocument.setBibIdentifier(list.get(0));
566                         }
567                     }
568                     if (key.toString().equalsIgnoreCase("instanceIdentifier")) {
569                         Object value = map.get(key);
570                         if (value instanceof String) {
571                             workHoldingsDocument.setInstanceIdentifier((String) value);
572                         } else if (value instanceof List) {
573                             ArrayList<String> list = (ArrayList<String>) value;
574                             workHoldingsDocument.setInstanceIdentifier(list.get(0));
575                         }
576                     }
577                     if (key.toString().equalsIgnoreCase("staffOnlyFlag")) {
578                         Object value = map.get(key);
579                         if (value instanceof String) {
580                             workHoldingsDocument.setStaffOnlyFlag((String) value);
581                         } else if (value instanceof List) {
582                             ArrayList<String> list = (ArrayList<String>) value;
583                             workHoldingsDocument.setStaffOnlyFlag(list.get(0));
584 
585                         }
586                     }
587                 }
588                 workHoldingsDocuments.add(workHoldingsDocument);
589             }
590         }
591         return workHoldingsDocuments;
592     }
593 
594     /**
595      * @param hitsOnPage
596      * @return List of workItemDocuments
597      */
598     public List<WorkItemDocument> buildWorkItemDocuments(List<Map<String, Object>> hitsOnPage) {
599         List<WorkItemDocument> workItemDocuments = new ArrayList<WorkItemDocument>();
600         for (Map<String, Object> hitsOnPageItr : hitsOnPage) {
601             WorkItemDocument workItemDocument = new WorkItemDocument();
602             Map map = hitsOnPageItr;
603             Set keys = map.keySet();
604             for (Object key : keys) {
605                 if (key.toString().equalsIgnoreCase("LocalId_display")) {
606                     Object value = map.get(key);
607                     if (value instanceof String) {
608                         workItemDocument.setLocalId((String) value);
609                     }
610                 }
611                 if (key.toString().equalsIgnoreCase("Location_display")) {
612                     Object value = map.get(key);
613                     if (value instanceof String) {
614                         workItemDocument.setLocation((String) value);
615                     } else if (value instanceof List) {
616                         ArrayList<String> list = (ArrayList<String>) value;
617                         workItemDocument.setLocation(list.get(0));
618                     }
619                 }
620                 if (key.toString().equalsIgnoreCase("CallNumber_display")) {
621                     Object value = map.get(key);
622                     if (value instanceof String) {
623                         workItemDocument.setCallNumber((String) value);
624                     } else if (value instanceof List) {
625                         ArrayList<String> list = (ArrayList<String>) value;
626                         workItemDocument.setCallNumber(list.get(0));
627                     }
628                 }
629                 if (key.toString().equalsIgnoreCase("ItemBarcode_display")) {
630                     Object value = map.get(key);
631                     if (value instanceof String) {
632                         workItemDocument.setBarcode((String) value);
633                     } else if (value instanceof List) {
634                         ArrayList<String> list = (ArrayList<String>) value;
635                         workItemDocument.setBarcode(list.get(0));
636                     }
637                 }
638                 if (key.toString().equalsIgnoreCase("ShelvingOrder_display")) {
639                     Object value = map.get(key);
640                     if (value instanceof String) {
641                         workItemDocument.setShelvingOrder((String) value);
642                     } else if (value instanceof List) {
643                         ArrayList<String> list = (ArrayList<String>) value;
644                         workItemDocument.setShelvingOrder(list.get(0));
645                     }
646                 }
647                 if (key.toString().equalsIgnoreCase("Enumeration_display")) {
648                     Object value = map.get(key);
649                     if (value instanceof String) {
650                         workItemDocument.setEnumeration((String) value);
651                     } else if (value instanceof List) {
652                         ArrayList<String> list = (ArrayList<String>) value;
653                         workItemDocument.setEnumeration(list.get(0));
654                     }
655                 }
656                 if (key.toString().equalsIgnoreCase("Chronology_display")) {
657                     Object value = map.get(key);
658                     if (value instanceof String) {
659                         workItemDocument.setChronology((String) value);
660                     } else if (value instanceof List) {
661                         ArrayList<String> list = (ArrayList<String>) value;
662                         workItemDocument.setChronology(list.get(0));
663                     }
664                 }
665                 if (key.toString().equalsIgnoreCase("CopyNumber_display")) {
666                     Object value = map.get(key);
667                     if (value instanceof String) {
668                         workItemDocument.setCopyNumber((String) value);
669                     } else if (value instanceof List) {
670                         ArrayList<String> list = (ArrayList<String>) value;
671                         workItemDocument.setCopyNumber(list.get(0));
672                     }
673                 }
674 
675                 if (key.toString().equalsIgnoreCase("bibIdentifier")) {
676                     Object value = map.get(key);
677                     if (value instanceof String) {
678                         workItemDocument.setBibIdentifier((String) value);
679                     } else if (value instanceof List) {
680                         ArrayList<String> list = (ArrayList<String>) value;
681                         workItemDocument.setBibIdentifier(list.get(0));
682                     }
683                 }
684 
685                 if (key.toString().equalsIgnoreCase("id")) {
686                     workItemDocument.setId((String) map.get(key));
687                 }
688 
689                 if (key.toString().equalsIgnoreCase("id")) {
690                     workItemDocument.setItemIdentifier((String) map.get(key));
691                 }
692 
693                 if (key.toString().equalsIgnoreCase("instanceIdentifier")) {
694                     Object value = map.get(key);
695                     if (value instanceof String) {
696                         workItemDocument.setInstanceIdentifier((String) value);
697                     } else if (value instanceof List) {
698                         ArrayList<String> list = (ArrayList<String>) value;
699                         workItemDocument.setInstanceIdentifier(list.get(0));
700                     }
701                 }
702 
703                 if (key.toString().equalsIgnoreCase("staffOnlyFlag")) {
704                     Object value = map.get(key);
705                     if (value instanceof String) {
706                         workItemDocument.setStaffOnlyFlag((String) value);
707                     } else if (value instanceof List) {
708                         ArrayList<String> list = (ArrayList<String>) value;
709                         workItemDocument.setStaffOnlyFlag(list.get(0));
710                     }
711                 }
712 
713             }
714             workItemDocuments.add(workItemDocument);
715         }
716         return workItemDocuments;
717     }
718 
719 
720     /**
721      * @param queryDoc
722      * @return
723      * @throws Exception
724      */
725     @Override
726     public WorkBibDocument queryForBibTree(WorkBibDocument queryDoc) throws Exception {
727         String id = queryDoc.getId();
728         LOG.info("id-->" + id);
729         List<WorkInstanceDocument> workInstanceDocumentList = new ArrayList<WorkInstanceDocument>();
730         List<WorkItemDocument> workItemDocumentList = new ArrayList<WorkItemDocument>();
731         WorkInstanceDocument workInstanceDocument = null;
732         WorkHoldingsDocument workHoldingsDocument;
733         WorkItemDocument workItemDocument;
734         List<WorkEInstanceDocument> workEInstanceDocumentList = new ArrayList<>();
735         queryDoc = buildBibDocumentInfo(queryDoc);
736         String queryString = buildQueryForBibTree(queryDoc);
737         SolrQuery solrQuery = new SolrQuery();
738         SolrServer server = SolrServerManager.getInstance().getSolrServer();
739         solrQuery.setQuery(queryString);
740         solrQuery.addSortField("Location_sort", SolrQuery.ORDER.asc);
741         solrQuery.addSortField("ShelvingOrder_sort", SolrQuery.ORDER.asc);
742         solrQuery.setRows(50000); // There can bib can have more items.
743         LOG.info("solr query-->" + solrQuery);
744         QueryResponse response = server.query(solrQuery);
745         List<SolrDocument> solrDocumentList = response.getResults();
746         for (SolrDocument solrDocument : solrDocumentList) {
747 
748             //build instance document
749             String docType = (String) solrDocument.getFieldValue("DocType");
750             if (docType.equalsIgnoreCase(DocType.HOLDINGS.getCode())) {
751                 workInstanceDocument = new WorkInstanceDocument();
752                 String instanceIdentifier = (String) solrDocument.getFieldValue("instanceIdentifier");
753                 String holdingsIdentifier = (String) solrDocument.getFieldValue("id");
754                 workItemDocumentList = new ArrayList<WorkItemDocument>();
755                 workHoldingsDocument = new WorkHoldingsDocument();
756                 workHoldingsDocument.setHoldingsIdentifier(holdingsIdentifier);
757                 workHoldingsDocument.setInstanceIdentifier(instanceIdentifier);
758                 workHoldingsDocument.setBibUUIDList(queryService.queryForBibs(workHoldingsDocument.getInstanceIdentifier()));
759                 String locationName = (String) solrDocument.getFieldValue("Location_display");
760                 workHoldingsDocument.setLocationName(locationName);
761                 workHoldingsDocument.setCallNumberType(getFieldVal(solrDocument, "CallNumberType_display"));
762                 workHoldingsDocument.setCallNumberPrefix(getFieldVal(solrDocument, "CallNumberPrefix_display"));
763                 workHoldingsDocument.setCallNumber(getFieldVal(solrDocument, "CallNumber_display"));
764                 workHoldingsDocument.setCopyNumber(getFieldVal(solrDocument, "CopyNumber_display"));
765                 workHoldingsDocument.setDateEntered(getFieldDateVal(solrDocument, "dateEntered"));
766                 workHoldingsDocument.setDateUpdated(getFieldDateVal(solrDocument, "dateUpdated"));
767                 workHoldingsDocument.setCreatedBy(getFieldVal(solrDocument, "createdBy"));
768                 workHoldingsDocument.setUpdatedBy(getFieldVal(solrDocument, "updatedBy"));
769                 workHoldingsDocument.setLocalId(getFieldVal(solrDocument, "LocalId_display"));
770                 workHoldingsDocument.setBibIdentifier(getFieldVal(solrDocument, "bibIdentifier"));
771                 workInstanceDocument.setHoldingsDocument(workHoldingsDocument);
772 
773                 for (SolrDocument itemSolrDocument : solrDocumentList) {
774                     String itemDocType = (String) itemSolrDocument.getFieldValue("DocType");
775                     if ((itemDocType.equalsIgnoreCase(DocType.ITEM.getCode())) && (itemSolrDocument.getFieldValue("holdingsIdentifier").equals(holdingsIdentifier))) {
776 
777                         workItemDocument = new WorkItemDocument();
778                         String itemIdentifier = (String) itemSolrDocument.getFieldValue("id");
779                         workItemDocument.setItemIdentifier(itemIdentifier);
780                         String callNumberType = (String) itemSolrDocument.getFieldValue("CallNumberType_display");
781                         String callNumberPrefix = (String) itemSolrDocument.getFieldValue("CallNumberPrefix_display");
782                         String callNumber = (String) itemSolrDocument.getFieldValue("CallNumber_display");
783                         workItemDocument.setLocation(getFieldVal(itemSolrDocument, "Location_display"));
784                         workItemDocument.setCopyNumber(getFieldVal(itemSolrDocument, "CopyNumber_display"));
785                         workItemDocument.setEnumeration(getFieldVal(itemSolrDocument, "Enumeration_display"));
786                         workItemDocument.setChronology(getFieldVal(itemSolrDocument, "Chronology_display"));
787                         workItemDocument.setItemStatus(getFieldVal(itemSolrDocument, "ItemStatus_display"));
788                         workItemDocument.setBarcode(getFieldVal(itemSolrDocument, "ItemBarcode_display"));
789                         workItemDocument.setVolumeNumber(getFieldVal(itemSolrDocument, "VolumeNumber_display"));
790                         workItemDocument.setLocalId(getFieldVal(itemSolrDocument, "LocalId_display"));
791                         workItemDocument.setCallNumberType(callNumberType);
792                         workItemDocument.setCallNumberPrefix(callNumberPrefix);
793                         workItemDocument.setItemType(getFieldVal(itemSolrDocument, "ItemTypeCodeValue_display"));
794                         workItemDocument.setCallNumber(callNumber);
795                         String staffOnlyFlag = (String) itemSolrDocument.getFieldValue("staffOnlyFlag");
796                         workItemDocument.setStaffOnlyFlag(staffOnlyFlag);
797                         workItemDocument.setDateEntered(getFieldDateVal(itemSolrDocument, "dateEntered"));
798                         workItemDocument.setDateUpdated(getFieldDateVal(itemSolrDocument, "dateUpdated"));
799                         workItemDocument.setCreatedBy(getFieldVal(itemSolrDocument, "createdBy"));
800                         workItemDocument.setUpdatedBy(getFieldVal(itemSolrDocument, "updatedBy"));
801                         workItemDocumentList.add(workItemDocument);
802                         workInstanceDocument.setItemDocumentList(workItemDocumentList);
803 
804                         LOG.debug("workItemDocumentList size-->" + workItemDocumentList.size());
805                     }
806                 }
807                 workInstanceDocument.setInstanceIdentifier(instanceIdentifier);
808                 workInstanceDocumentList.add(workInstanceDocument);
809             }
810 
811             if (docType.equalsIgnoreCase(DocType.EHOLDINGS.getCode())) {
812                 WorkEInstanceDocument workEInstanceDocument = new WorkEInstanceDocument();
813                 WorkEHoldingsDocument workEHoldingsDocument = new WorkEHoldingsDocument();
814                 workEHoldingsDocument.setHoldingsIdentifier((String) solrDocument.getFieldValue("id"));
815                 workEHoldingsDocument.setLocalId((String) solrDocument.getFieldValue("LocalId_display"));
816                 workEHoldingsDocument.setImprint((String) solrDocument.getFieldValue("Imprint_display"));
817                 workEHoldingsDocument.setInstanceIdentifier((String) solrDocument.getFieldValue("instanceIdentifier"));
818                 workEHoldingsDocument.setAccessStatus(getFieldVal(solrDocument, "AccessStatus_display"));
819                 workEHoldingsDocument.setSubscriptionStatus(getFieldVal(solrDocument, "SubscriptionStatus_display"));
820                 workEHoldingsDocument.setPlatForm(getFieldVal(solrDocument, "Platform_display"));
821                 workEHoldingsDocument.setLocation(getFieldVal(solrDocument, "Location_display"));
822                 workEHoldingsDocument.setUrl(getFieldVal(solrDocument, "Url_display"));
823                 workEHoldingsDocument.setStaffOnly(getFieldVal(solrDocument, "staffOnlyFlag"));
824                 workEHoldingsDocument.seteResourceName(getFieldVal(solrDocument, "EResource_name_display"));
825                 workEInstanceDocument.setInstanceIdentifier((String) solrDocument.getFieldValue("instanceIdentifier"));
826                 workEHoldingsDocument.setCoverageDates(getFieldValues(solrDocument,"CoverageDate_display" ));
827                 workEInstanceDocument.setWorkEHoldingsDocument(workEHoldingsDocument);
828                 workEInstanceDocument.setPublisher(getFieldVal(solrDocument, "E_Publisher_display"));
829                 workEInstanceDocument.setPublicDisplayNote(getFieldVal(solrDocument, "Public_Note_display"));
830                 workEInstanceDocumentList.add(workEInstanceDocument);
831             }
832 
833             queryDoc.setInstanceDocument(workInstanceDocument);
834             queryDoc.setWorkInstanceDocumentList(workInstanceDocumentList);
835             queryDoc.setWorkEInstanceDocumentList(workEInstanceDocumentList);
836         }
837 
838         return queryDoc;
839     }
840 
841     private String getFieldVal(SolrDocument solrDocument, String field) {
842         String fieldValue = null;
843         if (solrDocument.getFieldValue(field) instanceof String) {
844             fieldValue = (String) solrDocument.getFieldValue(field);
845         } else if (solrDocument.getFieldValue(field) instanceof List) {
846             List<String> locList = (List<String>) solrDocument.getFieldValue(field);
847             fieldValue = locList.get(0);
848         }
849         return fieldValue;
850     }
851 
852     private List<String> getFieldValues(SolrDocument solrDocument, String field) {
853         List<String> list = new ArrayList<>();
854         if (solrDocument.getFieldValue(field) instanceof String) {
855             list.add((String) solrDocument.getFieldValue(field));
856         } else if (solrDocument.getFieldValue(field) instanceof List) {
857             list.addAll((List<String>) solrDocument.getFieldValue(field));
858         }
859         return list;
860     }
861 
862     private String getFieldDateVal(SolrDocument solrDocument, String field) {
863         String fieldValue = null;
864 
865         if (solrDocument.getFieldValue(field) instanceof Date) {
866             Format formatter = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss");
867             fieldValue = formatter.format(solrDocument.getFieldValue(field));
868         }
869         return fieldValue;
870     }
871     public WorkBibDocument buildBibDocumentInfo(WorkBibDocument bibDocument) throws SolrServerException, IOException {
872         String queryString = buildQueryForBibInfo(bibDocument);
873         SolrQuery solrQuery = new SolrQuery();
874         SolrServer server = SolrServerManager.getInstance().getSolrServer();
875         solrQuery.setQuery(queryString);
876         QueryResponse response = server.query(solrQuery);
877         List<SolrDocument> solrDocumentList = response.getResults();
878 
879         for (SolrDocument solrDocument : solrDocumentList) {
880             StringBuffer titleBuffer = new StringBuffer();
881             String title = (String) solrDocument.getFieldValue("Title_display");
882             String format = (String) solrDocument.getFieldValue("DocFormat");
883             String staffOnlyFlag = (String) solrDocument.getFieldValue("staffOnlyFlag");
884             /* if("true".equals(staffOnlyFlag))  {
885                 titleBuffer.append("<font color='red'>");
886                 titleBuffer.append(title);
887                 titleBuffer.append("</font>");
888             bibDocument.setTitle(titleBuffer.toString());
889             }else{
890                 bibDocument.setTitle(title);
891             }*/
892             bibDocument.setTitle(title);
893             bibDocument.setAuthor(getFieldVal(solrDocument, "Author_display"));
894             bibDocument.setPublicationDate(getFieldVal(solrDocument, "PublicationDate_display"));
895             bibDocument.setPublisher(getFieldVal(solrDocument, "Publisher_display"));
896             bibDocument.setIssn(getFieldVal(solrDocument, "ISSN_display"));
897             bibDocument.setIsbn(getFieldVal(solrDocument, "ISBN_display"));
898             bibDocument.setEdition(getFieldVal(solrDocument, "Edition_display"));
899             bibDocument.setStaffOnlyFlag(staffOnlyFlag);
900             bibDocument.setDocFormat(format);
901         }
902 
903         return bibDocument;
904     }
905 
906 
907     /**
908      * @param workBibDocumentList
909      * @return
910      * @throws Exception
911      */
912     @Override
913     public List<WorkBibDocument> queryForBibTree(List<WorkBibDocument> workBibDocumentList) throws Exception {
914         for (WorkBibDocument bibDocument : workBibDocumentList) {
915             bibDocument = queryForBibTree(bibDocument);
916             workBibDocumentList.add(bibDocument);
917         }
918         return workBibDocumentList;
919     }
920 
921 
922     /**
923      * This method returns the Bib ids for the selected bound-with instance.
924      *
925      * @param instancedId
926      * @return
927      * @throws SolrServerException
928      */
929     @Override
930     public List<String> queryForBibs(String instancedId) throws SolrServerException {
931         List<String> bibUuidList = new ArrayList<String>();
932         SolrQuery solrQuery = new SolrQuery();
933         SolrServer server = SolrServerManager.getInstance().getSolrServer();
934         solrQuery.setQuery("instanceIdentifier:" + instancedId);
935         solrQuery.setRows(500);
936         LOG.info("solr query-->" + solrQuery);
937         QueryResponse response = server.query(solrQuery);
938         List<SolrDocument> solrDocumentList = response.getResults();
939         for (SolrDocument solrDocument : solrDocumentList) {
940             String docType = (String) solrDocument.getFieldValue("DocType");
941             if (docType.equalsIgnoreCase(DocType.BIB.getDescription())) {
942                 bibUuidList.add((String) solrDocument.getFieldValue("id"));
943             }
944 
945         }
946         return bibUuidList;
947     }
948 
949     public List<String> queryForItems(String bibId) throws SolrServerException {
950         List<String> itemIdList = new ArrayList<String>();
951         SolrQuery solrQuery = new SolrQuery();
952         SolrServer server = SolrServerManager.getInstance().getSolrServer();
953         solrQuery.setQuery("(DocType:item)AND(bibIdentifier:" + bibId + ")");
954         solrQuery.setRows(500);
955         LOG.info("solr query-->" + solrQuery);
956         QueryResponse response = server.query(solrQuery);
957         List<SolrDocument> solrDocumentList = response.getResults();
958         for (SolrDocument solrDocument : solrDocumentList) {
959             Object itemId = solrDocument.getFieldValue("id");
960             if (itemId instanceof String) {
961                 itemIdList.add((String) itemId);
962             }
963             if (itemId instanceof List) {
964                 itemIdList.addAll((ArrayList) itemId);
965             }
966         }
967 
968 
969         return itemIdList;
970     }
971 
972     @Override
973     public String queryForBib(String itemId) throws SolrServerException {
974         List<String> bibUuidList = new ArrayList<String>();
975         SolrQuery solrQuery = new SolrQuery();
976         SolrServer server = SolrServerManager.getInstance().getSolrServer();
977         solrQuery.setQuery("itemIdentifier:" + itemId);
978         solrQuery.setRows(500);
979         LOG.info("solr query-->" + solrQuery);
980         QueryResponse response = server.query(solrQuery);
981         List<SolrDocument> solrDocumentList = response.getResults();
982         for (SolrDocument solrDocument : solrDocumentList) {
983             String docType = (String) solrDocument.getFieldValue("DocType");
984             if (docType.equalsIgnoreCase(DocType.INSTANCE.getDescription())) {
985                 return getFieldVal(solrDocument, "bibIdentifier");
986             }
987         }
988         return "";
989     }
990 
991     /**
992      * This method used to get the instances for the selected bibs.
993      *
994      * @param uuid
995      * @return
996      * @throws SolrServerException
997      */
998     @Override
999     public List<String> queryForInstances(String uuid) throws SolrServerException {
1000         List<String> instanceIdList = new ArrayList<String>();
1001         SolrQuery solrQuery = new SolrQuery();
1002         SolrServer server = SolrServerManager.getInstance().getSolrServer();
1003         solrQuery.setQuery("id:" + uuid);
1004         LOG.info("solr query-->" + solrQuery);
1005         solrQuery.setRows(500);
1006         QueryResponse response = server.query(solrQuery);
1007         List<SolrDocument> solrDocumentList = response.getResults();
1008         for (SolrDocument solrDocument : solrDocumentList) {
1009             String docType = (String) solrDocument.getFieldValue("DocType");
1010             if (docType.equalsIgnoreCase(DocType.BIB.getDescription())) {
1011                 Object instanceId = solrDocument.getFieldValue("instanceIdentifier");
1012                 if (instanceId instanceof String) {
1013                     instanceIdList.add((String) instanceId);
1014                 }
1015                 if (instanceId instanceof List) {
1016                     instanceIdList = (List<String>) instanceId;
1017                 }
1018             }
1019 
1020         }
1021         return instanceIdList;
1022     }
1023 
1024     @Override
1025     public WorkInstanceDocument queryForInstanceTree(WorkInstanceDocument instanceDocument) throws SolrServerException {
1026 
1027         String id = instanceDocument.getId();
1028         LOG.info("id-->" + id);
1029         List<WorkInstanceDocument> workInstanceDocumentList = new ArrayList<WorkInstanceDocument>();
1030         List<WorkHoldingsDocument> workHoldingsDocumentList = new ArrayList<WorkHoldingsDocument>();
1031         List<WorkItemDocument> workItemDocumentList = new ArrayList<WorkItemDocument>();
1032         WorkInstanceDocument workInstanceDocument = new WorkInstanceDocument();
1033         WorkHoldingsDocument workHoldingsDocument;
1034         WorkItemDocument workItemDocument;
1035         //String queryString = buildQueryForInstanceTree(instanceDocument);
1036         SolrQuery solrQuery = new SolrQuery();
1037         SolrServer server = SolrServerManager.getInstance().getSolrServer();
1038         solrQuery.setQuery("DocType:holdings AND instanceIdentifier:" + id);
1039         LOG.info("solr query-->" + solrQuery);
1040         solrQuery.addSortField("Location_sort", SolrQuery.ORDER.asc);
1041         solrQuery.addSortField("ShelvingOrder_sort", SolrQuery.ORDER.asc);
1042         solrQuery.setRows(500);
1043         QueryResponse response = server.query(solrQuery);
1044         List<SolrDocument> holdingsSolrDocumentList = response.getResults();
1045         String docType;
1046         workItemDocumentList = new ArrayList<WorkItemDocument>();
1047         workHoldingsDocumentList = new ArrayList<WorkHoldingsDocument>();//
1048         for (SolrDocument holdingSolrDocument : holdingsSolrDocumentList) {
1049             docType = (String) holdingSolrDocument.getFieldValue("DocType");
1050             if ((docType.equalsIgnoreCase(DocType.HOLDINGS.getCode())) && (holdingSolrDocument.getFieldValue("instanceIdentifier").equals(id))) {
1051                 workHoldingsDocument = new WorkHoldingsDocument();
1052                 String holdingsIdentifier = (String) holdingSolrDocument.getFieldValue("id");
1053                 String bibIdentifier = getFieldVal(holdingSolrDocument, "bibIdentifier");
1054                 workHoldingsDocument.setHoldingsIdentifier(holdingsIdentifier);
1055                 String locationName = getFieldVal(holdingSolrDocument, "Location_display");
1056                 String instanceIdentifier = getFieldVal(holdingSolrDocument, "instanceIdentifier");
1057                 workHoldingsDocument.setInstanceIdentifier(instanceIdentifier);
1058                 workHoldingsDocument.setBibUUIDList(queryService.queryForBibs(instanceIdentifier));
1059                 workHoldingsDocument.setLocationName(locationName);
1060                 workHoldingsDocument.setCallNumberType(getFieldVal(holdingSolrDocument, "CallNumberType_display"));
1061                 workHoldingsDocument.setCallNumberPrefix(getFieldVal(holdingSolrDocument, "CallNumberPrefix_display"));
1062                 workHoldingsDocument.setCallNumber(getFieldVal(holdingSolrDocument, "CallNumber_display"));
1063                 workHoldingsDocument.setCopyNumber(getFieldVal(holdingSolrDocument, "CopyNumber_display"));
1064                 String staffOnlyFlag = (String) holdingSolrDocument.getFieldValue("staffOnlyFlag");
1065                 workHoldingsDocument.setStaffOnlyFlag(staffOnlyFlag);
1066                 workHoldingsDocumentList.add(workHoldingsDocument);
1067                 workInstanceDocument.setBibIdentifier(bibIdentifier);
1068                 workInstanceDocument.setHoldingsDocument(workHoldingsDocument);
1069             }
1070         }
1071         SolrQuery solrQueryForItem = new SolrQuery();
1072         solrQueryForItem.setQuery("DocType:item AND instanceIdentifier:" + id);
1073         LOG.info("solr query-->" + solrQueryForItem);
1074         solrQueryForItem.addSortField("Location_sort", SolrQuery.ORDER.asc);
1075         solrQueryForItem.addSortField("CallNumberPrefix_sort", SolrQuery.ORDER.asc);
1076         solrQueryForItem.addSortField("CallNumber_sort", SolrQuery.ORDER.asc);
1077         solrQueryForItem.addSortField("Enumeration_sort", SolrQuery.ORDER.asc);
1078         solrQueryForItem.addSortField("Chronology_sort", SolrQuery.ORDER.asc);
1079         solrQueryForItem.addSortField("CopyNumber_sort", SolrQuery.ORDER.asc);
1080         solrQueryForItem.addSortField("ItemBarcode_sort", SolrQuery.ORDER.asc);
1081         solrQueryForItem.setRows(5000);
1082         QueryResponse responseItem = server.query(solrQueryForItem);
1083         List<SolrDocument> itemSolrDocumentList = responseItem.getResults();
1084         for (SolrDocument itemSolrDocument : itemSolrDocumentList) {
1085             docType = (String) itemSolrDocument.getFieldValue("DocType");
1086             if ((docType.equalsIgnoreCase(DocType.ITEM.getCode()))) {
1087                 workItemDocument = new WorkItemDocument();
1088                 String itemIdentifier = getFieldVal(itemSolrDocument, "id");
1089                 String callNumberType = getFieldVal(itemSolrDocument, "CallNumberType_display");
1090                 String callNumberPrefix = getFieldVal(itemSolrDocument, "CallNumberPrefix_display");
1091                 String bibIdentifier = getFieldVal(itemSolrDocument, "bibIdentifier");
1092                 String callNumber = getFieldVal(itemSolrDocument, "CallNumber_display");
1093                 workItemDocument.setLocation(getFieldVal(itemSolrDocument, "Location_display"));
1094                 workItemDocument.setBarcode(getFieldVal(itemSolrDocument, "ItemBarcode_display"));
1095                 workItemDocument.setItemType(getFieldVal(itemSolrDocument, "ItemTypeCodeValue_display"));
1096                 workItemDocument.setLocationName(getFieldVal(itemSolrDocument, "LocationLevelName_display"));
1097                 workItemDocument.setItemStatus(getFieldVal(itemSolrDocument, "ItemStatus_display"));
1098                 workItemDocument.setCopyNumber(getFieldVal(itemSolrDocument, "CopyNumber_display"));
1099                 workItemDocument.setEnumeration(getFieldVal(itemSolrDocument, "Enumeration_display"));
1100                 workItemDocument.setChronology(getFieldVal(itemSolrDocument, "Chronology_display"));
1101                 workItemDocument.setCallNumberType(callNumberType);
1102                 String staffOnlyFlag = (String) itemSolrDocument.getFieldValue("staffOnlyFlag");
1103                 workItemDocument.setStaffOnlyFlag(staffOnlyFlag);
1104                 workItemDocument.setCallNumberPrefix(callNumberPrefix);
1105                 workItemDocument.setCallNumber(callNumber);
1106                 workItemDocument.setItemIdentifier(itemIdentifier);
1107                 workItemDocumentList.add(workItemDocument);
1108                 workInstanceDocument.setBibIdentifier(bibIdentifier);
1109             }
1110         }
1111         workInstanceDocument.setItemDocumentList(workItemDocumentList);
1112         workInstanceDocument.setInstanceIdentifier(id);
1113         workInstanceDocumentList.add(workInstanceDocument);
1114         return workInstanceDocument;
1115     }
1116 
1117     @Override
1118     public WorkItemDocument queryForItemTree(WorkItemDocument itemDocument) throws SolrServerException {
1119 
1120         String id = itemDocument.getId();
1121         LOG.info("id-->" + id);
1122         List<WorkHoldingsDocument> workHoldingsDocumentList = new ArrayList<WorkHoldingsDocument>();
1123         List<WorkItemDocument> workItemDocumentList = new ArrayList<WorkItemDocument>();
1124         WorkItemDocument workItemDocument;
1125         //String queryString = buildQueryForItemTree(itemDocument);
1126         SolrQuery solrQuery = new SolrQuery();
1127         SolrServer server = SolrServerManager.getInstance().getSolrServer();
1128         solrQuery.setQuery("id:" + id);
1129         solrQuery.setRows(500);
1130         LOG.info("solr query-->" + solrQuery);
1131         QueryResponse response = server.query(solrQuery);
1132         List<SolrDocument> solrDocumentList = response.getResults();
1133         String docType;
1134         workItemDocument = new WorkItemDocument();
1135         workItemDocumentList = new ArrayList<WorkItemDocument>();
1136 
1137         for (SolrDocument itemSolrDocument : solrDocumentList) {
1138             docType = (String) itemSolrDocument.getFieldValue("DocType");
1139             if ((docType.equalsIgnoreCase(DocType.ITEM.getCode())) && (itemSolrDocument.getFieldValue("id").equals(id))) {
1140 
1141 
1142                 String itemIdentifier = (String) itemSolrDocument.getFieldValue("id");
1143 //                String instanceIdentifier = (String) itemSolrDocument.getFieldValue("instanceIdentifier");
1144 //                String bibIdentifier = (String) itemSolrDocument.getFieldValue("bibIdentifier");
1145 //                String callNumberType = (String) itemSolrDocument.getFieldValue("CallNumberType_display");
1146 //                String callNumberPrefix = (String) itemSolrDocument.getFieldValue("CallNumberPrefix_display");
1147 //                String callNumber = (String) itemSolrDocument.getFieldValue("CallNumber_display");
1148 
1149                 String instanceIdentifier = getFieldVal(itemSolrDocument, "instanceIdentifier");
1150                 String bibIdentifier = getFieldVal(itemSolrDocument, "bibIdentifier");
1151                 String callNumberType = getFieldVal(itemSolrDocument, "CallNumberType_display");
1152                 String callNumberPrefix = getFieldVal(itemSolrDocument, "CallNumberPrefix_display");
1153                 String callNumber = getFieldVal(itemSolrDocument, "CallNumber_display");
1154 
1155                 workItemDocument.setLocation(getFieldVal(itemSolrDocument, "Location_display"));
1156                 workItemDocument.setBarcode(getFieldVal(itemSolrDocument, "ItemBarcode_display"));
1157                 workItemDocument.setItemType(getFieldVal(itemSolrDocument, "ItemTypeCodeValue_display"));
1158                 workItemDocument.setLocationName(getFieldVal(itemSolrDocument, "LocationLevelName_display"));
1159                 workItemDocument.setCopyNumber(getFieldVal(itemSolrDocument, "CopyNumber_display"));
1160                 workItemDocument.setVolumeNumber(getFieldVal(itemSolrDocument, "VolumeNumber_display"));
1161                 workItemDocument.setItemStatus(getFieldVal(itemSolrDocument, "ItemStatus_display"));
1162                 workItemDocument.setCallNumber(callNumber);
1163                 workItemDocument.setCallNumberType(callNumberType);
1164                 workItemDocument.setCallNumberPrefix(callNumberPrefix);
1165                 workItemDocument.setItemIdentifier(itemIdentifier);
1166                 workItemDocument.setInstanceIdentifier(instanceIdentifier);
1167                 workItemDocument.setBibIdentifier(bibIdentifier);
1168                 workItemDocumentList.add(workItemDocument);
1169                 LOG.debug("workItemDocumentList size-->" + workItemDocumentList.size());
1170             }
1171         }
1172 
1173 
1174         return workItemDocument;  //To change body of implemented methods use File | Settings | File Templates.
1175     }
1176 
1177     @Override
1178     public WorkEInstanceDocument queryForEInstanceTree(WorkEInstanceDocument eInstanceDocument) throws SolrServerException {
1179 
1180         String id = eInstanceDocument.getId();
1181         LOG.info("id-->" + id);
1182         WorkEInstanceDocument workEInstanceDocument = new WorkEInstanceDocument();
1183         WorkEHoldingsDocument workEHoldingsDocument;
1184         SolrQuery solrQuery = new SolrQuery();
1185         SolrServer server = SolrServerManager.getInstance().getSolrServer();
1186         solrQuery.setQuery("instanceIdentifier:" + id);
1187         LOG.info("solr query-->" + solrQuery);
1188         QueryResponse response = server.query(solrQuery);
1189         List<SolrDocument> solrDocumentList = response.getResults();
1190         String docType;
1191 
1192         for (SolrDocument holdingSolrDocument : solrDocumentList) {
1193             docType = (String) holdingSolrDocument.getFieldValue("DocType");
1194 
1195             if ((docType.equalsIgnoreCase(DocType.EHOLDINGS.getCode())) && (holdingSolrDocument.getFieldValue("instanceIdentifier").equals(id))) {
1196                 workEHoldingsDocument = new WorkEHoldingsDocument();
1197                 String holdingsIdentifier = (String) holdingSolrDocument.getFieldValue("id");
1198                 String bibIdentifier = getFieldVal(holdingSolrDocument, "bibIdentifier");
1199                 workEHoldingsDocument.setHoldingsIdentifier(holdingsIdentifier);
1200 //                String locationName = (String) holdingSolrDocument.getFieldValue("LocationLevel_display");
1201                 String locationName = getFieldVal(holdingSolrDocument, "Location_display");
1202                 String instanceIdentifier = getFieldVal(holdingSolrDocument, "instanceIdentifier");
1203                 workEHoldingsDocument.setInstanceIdentifier(instanceIdentifier);
1204                 workEHoldingsDocument.setAccessStatus(getFieldVal(holdingSolrDocument, "AccessStatus_display"));
1205                 workEHoldingsDocument.setSubscriptionStatus(getFieldVal(holdingSolrDocument, "SubscriptionStatus_display"));
1206                 workEHoldingsDocument.setImprint(getFieldVal(holdingSolrDocument, "Imprint_display"));
1207                 workEHoldingsDocument.setPlatForm(getFieldVal(holdingSolrDocument, "Platform_display"));
1208                 workEHoldingsDocument.setLocation(getFieldVal(holdingSolrDocument, "Location_display"));
1209                 workEHoldingsDocument.seteResourceName(getFieldVal(holdingSolrDocument, "EResource_name_display"));
1210                 workEHoldingsDocument.setCoverageDates(getFieldValues(holdingSolrDocument,"CoverageDate_display" ));
1211                 workEHoldingsDocument.setStaffOnly(getFieldVal(holdingSolrDocument, "staffOnlyFlag"));
1212                 workEHoldingsDocument.setBibIdentifier(bibIdentifier);
1213                 workEInstanceDocument.setBibIdentifier(bibIdentifier);
1214                 workEInstanceDocument.setInstanceIdentifier(instanceIdentifier);
1215                 workEInstanceDocument.setWorkEHoldingsDocument(workEHoldingsDocument);
1216             }
1217         }
1218 
1219         workEInstanceDocument.setInstanceIdentifier(id);
1220 
1221         return workEInstanceDocument;
1222     }
1223 
1224 
1225     private String buildQueryForBibTree(OleDocument queryDoc) {
1226         StringBuilder query = new StringBuilder();
1227 
1228         if (((WorkBibDocument) queryDoc).getId() != null && query.length() > 0) {
1229             query.append("(bibIdentifier :").append((((WorkBibDocument) queryDoc).getId())).append(")");
1230         } else if (((WorkBibDocument) queryDoc).getId() != null) {
1231             query.append("(bibIdentifier :").append((((WorkBibDocument) queryDoc).getId())).append(")");
1232 
1233 
1234         }
1235         return query.toString();
1236     }
1237 
1238     private String buildQueryForInstanceTree(OleDocument queryDoc) {
1239         StringBuilder query = new StringBuilder();
1240 
1241         if (((WorkInstanceDocument) queryDoc).getId() != null && query.length() > 0) {
1242             query.append("(instanceIdentifier :").append((((WorkInstanceDocument) queryDoc).getId())).append(")");
1243         } else if (((WorkInstanceDocument) queryDoc).getId() != null) {
1244             query.append("(instanceIdentifier :").append((((WorkInstanceDocument) queryDoc).getId())).append(")");
1245 
1246 
1247         }
1248         return query.toString();
1249     }
1250 
1251     private String buildQueryForItemTree(OleDocument queryDoc) {
1252         StringBuilder query = new StringBuilder();
1253 
1254         if (((WorkItemDocument) queryDoc).getId() != null && query.length() > 0) {
1255             query.append("(id :").append((((WorkItemDocument) queryDoc).getId())).append(")");
1256         } else if (((WorkItemDocument) queryDoc).getId() != null) {
1257             query.append("(id :").append((((WorkItemDocument) queryDoc).getId())).append(")");
1258 
1259 
1260         }
1261         return query.toString();
1262     }
1263 
1264     private String buildQueryForBibInfo(OleDocument queryDoc) {
1265         StringBuilder query = new StringBuilder();
1266 
1267         if (((WorkBibDocument) queryDoc).getId() != null && query.length() > 0) {
1268             query.append("(id :").append((((WorkBibDocument) queryDoc).getId())).append(" AND DocType:bibliographic").append(")");
1269         } else if (((WorkBibDocument) queryDoc).getId() != null) {
1270             query.append("(id :").append((((WorkBibDocument) queryDoc).getId())).append(" AND DocType:bibliographic").append(")");
1271 
1272 
1273         }
1274         return query.toString();
1275     }
1276 
1277     /**
1278      * @param queryDoc
1279      * @return query
1280      *         Usage: Builds query string using OleDocument Data
1281      */
1282     public String buildQueryForDoc(OleDocument queryDoc) {
1283         StringBuilder query = new StringBuilder();
1284         if ((((WorkBibDocument) queryDoc).getInstanceDocument() == null)
1285                 || ((WorkBibDocument) queryDoc).getInstanceDocument().getInstanceIdentifier() == null) {
1286             if (((WorkBibDocument) queryDoc).getTitle() != null) {
1287                 query.append("(Title_display:").append(((WorkBibDocument) queryDoc).getTitle()).append(")");
1288             }
1289             if (((WorkBibDocument) queryDoc).getAuthor() != null && query.length() > 0) {
1290                 query.append("AND (Author_display:").append(((WorkBibDocument) queryDoc).getAuthor()).append(")");
1291             } else if (((WorkBibDocument) queryDoc).getAuthor() != null) {
1292                 query.append("(Author_display:").append(((WorkBibDocument) queryDoc).getAuthor()).append(")");
1293             }
1294             if (((WorkBibDocument) queryDoc).getPublicationDate() != null && query.length() > 0) {
1295                 query.append("AND (PublicationDate_display:").append(((WorkBibDocument) queryDoc).getPublicationDate()).append(")");
1296             } else if (((WorkBibDocument) queryDoc).getPublicationDate() != null) {
1297                 query.append("(PublicationDate_display:").append(((WorkBibDocument) queryDoc).getPublicationDate()).append(")");
1298             }
1299             if (((WorkBibDocument) queryDoc).getIsbn() != null && query.length() > 0) {
1300                 query.append("AND (ISBN_display:").append(((WorkBibDocument) queryDoc).getIsbn()).append(")");
1301             } else if (((WorkBibDocument) queryDoc).getIsbn() != null) {
1302                 query.append("(ISBN_display:").append(((WorkBibDocument) queryDoc).getIsbn()).append(")");
1303             }
1304             if (((WorkBibDocument) queryDoc).getPublisher() != null && query.length() > 0) {
1305                 query.append("AND (Publisher_display:").append(((WorkBibDocument) queryDoc).getPublisher()).append(")");
1306             } else if (((WorkBibDocument) queryDoc).getPublisher() != null) {
1307                 query.append("(Publisher_display:").append(((WorkBibDocument) queryDoc).getPublisher()).append(")");
1308             }
1309             if (((WorkBibDocument) queryDoc).getId() != null && query.length() > 0) {
1310                 query.append("(id:").append((((WorkBibDocument) queryDoc).getId())).append(")");
1311             } else if (((WorkBibDocument) queryDoc).getId() != null) {
1312                 query.append("(id:").append((((WorkBibDocument) queryDoc).getId())).append(")");
1313 
1314             }
1315         } else {
1316             query.append("(" + DOC_TYPE + ":" + queryDoc.getDocType().getDescription() + ")");
1317             if (((WorkBibDocument) queryDoc).getInstanceDocument() != null) {
1318                 query.append(" AND ");
1319                 String instanseIdentifier = ((WorkBibDocument) queryDoc).getInstanceDocument().getInstanceIdentifier();
1320                 query.append("(" + INSTANCE_IDENTIFIER + ":" + instanseIdentifier + ")");
1321             }
1322         }
1323         query.append(
1324                 "&fl=id,instanceIdentifier,Title_display,Author_display,PublicationDate_display,ISBN_display,Publisher_display");
1325         return query.toString();
1326     }
1327 
1328 
1329     /**
1330      * @param inputQuery
1331      * @return hitsOnPage
1332      * @throws Exception Usage: Gets Solr response for input query and builds List of Maps holding Solr Doc Data
1333      */
1334     public List<Map<String, Object>> getSolrHitsForQuery(String inputQuery) throws Exception {
1335         SolrServer server;
1336         List<Map<String, Object>> hitsOnPage = new ArrayList<Map<String, Object>>();
1337         server = SolrServerManager.getInstance().getSolrServer();
1338         SolrQuery solrQuery = new SolrQuery();
1339         solrQuery.setQuery(inputQuery);
1340         solrQuery.setSortField("Title_sort", SolrQuery.ORDER.asc);
1341         solrQuery.setIncludeScore(true);
1342         solrQuery.setRows(20000);
1343         LOG.info("solr Query -->" + solrQuery.getQuery());
1344         QueryResponse queryResponse = server.query(solrQuery);
1345         SolrDocumentList solrDocumentList = queryResponse.getResults();
1346 
1347         for (SolrDocument solrDocument : solrDocumentList) {
1348             hitsOnPage.add(solrDocument);
1349         }
1350         return hitsOnPage;
1351     }
1352 
1353 
1354     /**
1355      * Gets Solr response for search Params
1356      * @param searchParams
1357      * @return hitsOnPage
1358      * @throws Exception
1359      */
1360     public List<Map<String, Object>> getSolrHitsForQueryParams(SearchParams searchParams) throws Exception {
1361         SolrServer server;
1362         List<Map<String, Object>> hitsOnPage = new ArrayList<Map<String, Object>>();
1363         server = SolrServerManager.getInstance().getSolrServer();
1364         SolrQuery solrQuery = new SolrQuery();
1365         solrQuery.setQuery(searchParams.getSearchQuery());
1366         if(StringUtils.isNotEmpty(searchParams.getSortField()) && StringUtils.isNotEmpty(searchParams.getSortOrder())) {
1367             String solrField = getSortField(searchParams.getSortField()).toString();
1368             if("asc".equals(searchParams.getSortOrder())) {
1369                 solrQuery.setSortField(solrField ,SolrQuery.ORDER.asc);
1370             }
1371             else if("desc".equals(searchParams.getSortOrder())) {
1372                 solrQuery.setSortField(solrField ,SolrQuery.ORDER.desc);
1373             }
1374         }
1375         else {
1376             solrQuery.setSortField("Title_sort", SolrQuery.ORDER.asc);
1377         }
1378         solrQuery.setIncludeScore(true);
1379         solrQuery.setRows(searchParams.getRows());
1380         solrQuery.setStart(searchParams.getStart());
1381         LOG.info("solr Query -->" + solrQuery.getQuery());
1382         QueryResponse queryResponse = server.query(solrQuery);
1383         SolrDocumentList solrDocumentList = queryResponse.getResults();
1384 
1385         for (SolrDocument solrDocument : solrDocumentList) {
1386             hitsOnPage.add(solrDocument);
1387         }
1388         return hitsOnPage;
1389     }
1390 
1391 
1392     /**
1393      * Gets total number of records from solr
1394      * @param inputQuery
1395      * @return numFound
1396      * @throws Exception
1397      */
1398     public int getSolrHitsForQueryParams(String inputQuery) throws Exception {
1399         SolrServer server;
1400         List<Map<String, Object>> hitsOnPage = new ArrayList<Map<String, Object>>();
1401         server = SolrServerManager.getInstance().getSolrServer();
1402         SolrQuery solrQuery = new SolrQuery();
1403         solrQuery.setQuery(inputQuery);
1404         solrQuery.setIncludeScore(true);
1405         solrQuery.setRows(0);
1406 
1407         LOG.info("solr Query -->" + solrQuery.getQuery());
1408         QueryResponse queryResponse = server.query(solrQuery);
1409         int numFound = (int) queryResponse.getResults().getNumFound();
1410         return numFound;
1411     }
1412 
1413 
1414     /**
1415      * @param idList
1416      * @param identifierType
1417      * @return
1418      */
1419     @Override
1420     public List<String> getUUIDList(List<String> idList, String identifierType) {
1421         SolrServer solrServer;
1422         SolrQuery solrQuery = new SolrQuery();
1423         SolrDocumentList solrDocumentList;
1424         String id;
1425         StringBuilder builder = new StringBuilder();
1426         List<String> uuidList = new ArrayList<String>();
1427         String searchField = null;
1428         if (identifierType.equalsIgnoreCase("SCN")) {
1429             searchField = WorkBibCommonFields.SYSTEM_CONTROL_NUMBER + ":";
1430         } else if (identifierType.equalsIgnoreCase("ISBN")) {
1431             searchField = WorkBibCommonFields.ISBN_SEARCH + ":";
1432         }
1433         try {
1434             solrServer = SolrServerManager.getInstance().getSolrServer();
1435             for (String ssn : idList) {
1436                 if (builder.length() > 0) {
1437                     builder.append(" OR ");
1438                 }
1439                 builder.append("(");
1440                 builder.append(searchField);
1441                 builder.append(ssn);
1442                 builder.append(")");
1443             }
1444 
1445             LOG.debug("query-->" + builder.toString());
1446             solrQuery.setQuery(builder.toString());
1447             LOG.debug("solr query-->" + solrQuery);
1448             QueryResponse response = solrServer.query(solrQuery);
1449             solrDocumentList = response.getResults();
1450             for (SolrDocument solrDocument : solrDocumentList) {
1451                 id = (String) solrDocument.getFieldValue("id");
1452                 LOG.debug("id-->" + id);
1453                 uuidList.add(id);
1454             }
1455 
1456         } catch (SolrServerException e) {
1457 
1458         }
1459         return uuidList;
1460     }
1461 
1462     /**
1463      * This method returns the list of titles based on the given search value.
1464      *
1465      * @param fieldValue
1466      * @return
1467      */
1468     @Override
1469     public List<String> getTitleValues(String fieldValue) {
1470         SolrServer solrServer;
1471         SolrQuery solrQuery = new SolrQuery();
1472         SolrDocumentList solrDocumentList;
1473         Object title;
1474         StringBuilder builder = new StringBuilder();
1475         List<String> titleList = new ArrayList<String>();
1476         String searchField = null;
1477         searchField = WorkBibCommonFields.TITLE_SEARCH + ":";
1478         try {
1479             solrServer = SolrServerManager.getInstance().getSolrServer();
1480             builder.append("(");
1481             builder.append("DocType");
1482             builder.append(":");
1483             builder.append("bibliographic");
1484             builder.append(")");
1485             builder.append("AND");
1486             builder.append("(");
1487             builder.append("(");
1488             builder.append(searchField);
1489             builder.append("(");
1490             builder.append(fieldValue);
1491             builder.append(")");
1492             builder.append(")");
1493             builder.append(")");
1494             LOG.debug("query-->" + builder.toString());
1495             solrQuery.setQuery(builder.toString());
1496             LOG.info("solr query-->" + solrQuery);
1497             QueryResponse response = solrServer.query(solrQuery);
1498             solrDocumentList = response.getResults();
1499             for (SolrDocument solrDocument : solrDocumentList) {
1500                 title = solrDocument.getFieldValue("Title_search");
1501                 LOG.debug("title-->" + title);
1502                 if (title instanceof List) {
1503                     titleList.addAll((Collection<? extends String>) title);
1504                 } else {
1505                     titleList.add(title.toString());
1506                 }
1507             }
1508 
1509         } catch (SolrServerException e) {
1510 
1511         }
1512         return titleList;
1513 
1514     }
1515 
1516     public String buildQuery(SearchParams searchParams) {
1517         StringBuffer query = new StringBuffer();
1518         buildInitialQuery(query, searchParams);
1519         if (searchParams.getResultPageSize() != null) {
1520             query.append("&rows=" + searchParams.getResultPageSize());
1521         }
1522         if (searchParams.getResultFromIndex() != null) {
1523             query.append("&start=" + searchParams.getResultFromIndex());
1524         }
1525         query.append(buildQueryWithSortFields(searchParams.getSortField(), searchParams.getSortOrder()));
1526         query.append(buildQueryWithFieldListParameters(searchParams.getFieldList()));
1527         return query.toString();
1528     }
1529 
1530     /**
1531      * @param query
1532      * @param searchParams Usage: This method builds initial SOLR query with DocType and DocFormat as SolrParams
1533      */
1534     private void buildInitialQuery(StringBuffer query, SearchParams searchParams) {
1535 
1536         if (searchParams.getDocType() == null && searchParams.getSearchFieldsList().isEmpty())
1537             return;
1538         if (searchParams.getDocType() != null && searchParams.getSearchFieldsList().isEmpty()) {
1539             query.append("(DocType:" + searchParams.getDocType() + ")");
1540         } else if (searchParams.getDocType() != null && !searchParams.getSearchFieldsList().isEmpty()) {
1541             query.append("(DocType:" + searchParams.getDocType() + ")");
1542             query.append("AND");
1543         }
1544 
1545         /*  if (searchParams.getDocFormat().equalsIgnoreCase("marc")) {
1546             query.append("((DocType:" + searchParams.getDocType() + ")" + "OR(DocType:item))");
1547         }
1548         else {
1549             query.append("(DocType:" + searchParams.getDocType() + ")");
1550         }*/
1551         if (searchParams.getSearchFieldsList().isEmpty() && searchParams.getDocFormat() != null && !searchParams.getDocFormat().equalsIgnoreCase("all")) {
1552             query.append("AND");
1553             query.append("(DocFormat:" + searchParams.getDocFormat() + ")");
1554         } else if (searchParams.getDocFormat() != null && !searchParams.getDocFormat().equalsIgnoreCase("all")) {
1555             query.append("(DocFormat:" + searchParams.getDocFormat() + ")");
1556             query.append("AND");
1557         }
1558 
1559         if (searchParams.getSearchFieldsList().size() > 0) {
1560             query.append(buildQueryWithSearchParameters(searchParams.getSearchFieldsList()));
1561         }
1562     }
1563 
1564     public String buildQueryWithSearchParameters(List<SearchCondition> searchFieldsList) {
1565         SearchCondition docSearchFieldsDTO = null;
1566         StringBuffer queryStringbuffer = new StringBuffer();
1567         StringBuffer highlightBuffer = new StringBuffer("&hl.fl=");
1568         if (searchFieldsList != null && searchFieldsList.size() > 0) {
1569             queryStringbuffer.append("(");
1570             for (int i = 0; i < searchFieldsList.size(); i++) {
1571                 int searchScopeAddLimit = i;
1572                 docSearchFieldsDTO = searchFieldsList.get(i);
1573                 if (docSearchFieldsDTO.getOperator() != null) {
1574                     // queryStringbuffer.append(docSearchFieldsDTO.getOperator());
1575                 }
1576                 queryStringbuffer.append("(");
1577                 if (docSearchFieldsDTO.getDocField().equalsIgnoreCase("all") || (
1578                         docSearchFieldsDTO.getDocField() == null || docSearchFieldsDTO.getDocField().length() == 0)) {
1579                     queryStringbuffer.append("all_text");
1580                     highlightBuffer.append("*");
1581 
1582                 } else {
1583                     queryStringbuffer.append(docSearchFieldsDTO.getDocField());
1584                     highlightBuffer.append(docSearchFieldsDTO.getDocField());
1585 
1586                     if (i != searchFieldsList.size() - 1) {
1587                         highlightBuffer.append(",");
1588                     }
1589                 }
1590                 queryStringbuffer.append(":");
1591                 String searchScope = docSearchFieldsDTO.getSearchScope();
1592                 String searchText = docSearchFieldsDTO.getSearchText();
1593                 String operator = docSearchFieldsDTO.getOperator();
1594                 if (docSearchFieldsDTO.getFieldType() != null && docSearchFieldsDTO.getFieldType().equals("range")) {
1595                     //do nothing to searchText
1596                 } else {
1597                     if (searchText.equalsIgnoreCase("")) {
1598                         searchText = "(*:*)";
1599                     } else {
1600                         //commented line for jira-4802
1601                         //searchText = searchText.toLowerCase();
1602                         //searchText = searchText.replaceAll("[~!(){}\\[\\]':-]+", " ");
1603                         searchText=getModifiedText(searchText);
1604 
1605                         searchText.replaceAll(" ", "+");
1606                     }
1607                 }
1608                 LOG.debug("searchText-->" + searchText);
1609                 String searchTextVal = null;
1610                 if (searchText.length() > 0) {
1611                     queryStringbuffer.append("(");
1612                     if (searchScope.equalsIgnoreCase("AND")) {
1613                         searchText = searchText.replaceAll("\\s+", " ");
1614                         searchTextVal = searchText.trim().replace(" ", " AND ");
1615                     } else if (searchScope.equalsIgnoreCase("OR")) {
1616                         searchText = searchText.replaceAll("\\s+", " ");
1617                         searchTextVal = searchText.trim().replace(" ", " OR ");
1618                     } else if (searchScope.equalsIgnoreCase("phrase")) {
1619                         searchTextVal = "\"" + searchText + "\"";
1620                     } else if (searchScope.equalsIgnoreCase("none")) {
1621                         //do nothing to the search text
1622                         searchTextVal = searchText;
1623                     }
1624                     /* try {
1625                         searchTextVal = URLEncoder.encode(searchTextVal, "UTF-8");
1626                     }
1627                     catch (UnsupportedEncodingException e) {
1628                         e.printStackTrace();
1629                     }*/
1630                     queryStringbuffer.append(searchTextVal);
1631                     LOG.debug("searchTextVal............" + searchTextVal + "........" + queryStringbuffer.toString());
1632                     queryStringbuffer.append(")");
1633                 }
1634                 queryStringbuffer.append(")");
1635                 ++searchScopeAddLimit;
1636                 if (operator == null) {
1637                     break;
1638                 }
1639                 if (searchScopeAddLimit != searchFieldsList.size()) {
1640                     queryStringbuffer.append(operator);
1641                 }
1642             }
1643             queryStringbuffer.append(")");
1644             queryStringbuffer.append(highlightBuffer.toString());
1645 
1646             queryStringbuffer.append("&hl=true");
1647         }
1648         return queryStringbuffer.toString();
1649     }
1650 
1651     public String buildQueryWithSortFields(String sortField, String sortOrder) {
1652         StringBuffer sortFieldsQuery = new StringBuffer();
1653         if (null != sortField) {
1654             sortFieldsQuery.append("&");
1655             sortFieldsQuery.append("sort=");
1656             sortFieldsQuery.append(sortField);
1657             if (null != sortOrder) {
1658                 sortFieldsQuery.append(" ");
1659                 sortFieldsQuery.append(sortOrder);
1660             }
1661         }
1662         return sortFieldsQuery.toString();
1663     }
1664 
1665     public String buildQueryWithFieldListParameters(List<String> fieldsList) {
1666         String queryWithFieldListParameters = "";
1667         if (fieldsList != null) {
1668             StringBuffer fieldsListQueryStringbuffer = new StringBuffer();
1669             fieldsListQueryStringbuffer.append("&");
1670             fieldsListQueryStringbuffer.append("fl=");
1671             for (int i = 0; i < fieldsList.size(); i++) {
1672                 fieldsListQueryStringbuffer.append(fieldsList.get(i));
1673                 fieldsListQueryStringbuffer.append(",");
1674             }
1675             queryWithFieldListParameters = fieldsListQueryStringbuffer
1676                     .substring(0, fieldsListQueryStringbuffer.length() - 1);
1677         }
1678         return queryWithFieldListParameters;
1679     }
1680 
1681     /**
1682      * Prepares the solr query based on user input values location, classification and call number browse text
1683      * Calculates totalCallNumberCount and totalForwardCallNumberCount based on solr query results
1684      * Evaluates matchIndex based on totalCallNumberCount and totalForwardCallNumberCount
1685      *
1686      * @param callNumberBrowseParams contains required parameters for call number browse functionality
1687      * @throws Exception
1688      */
1689     public void initCallNumberBrowse(CallNumberBrowseParams callNumberBrowseParams) throws Exception {
1690         int matchIndex = 0;
1691         int totalCallNumberCount = 0;
1692         int totalForwardCallNumberCount = 0;
1693         String location = callNumberBrowseParams.getLocation();
1694         String classificationScheme = callNumberBrowseParams.getClassificationScheme();
1695         String callNumberBrowseText = callNumberBrowseParams.getCallNumberBrowseText();
1696         String docType = callNumberBrowseParams.getDocTye();
1697         if (StringUtils.isNotEmpty(classificationScheme)) {
1698             String queryString = buildQueryForTotalCallNumberCount(location, classificationScheme, docType);
1699             List<Map<String, Object>> solrHits;
1700             totalCallNumberCount = getSolrHitsForCallNumberBrowse(queryString);
1701             LOG.info("Total Call Number count:" + totalCallNumberCount);
1702 
1703             if (StringUtils.isNotEmpty(callNumberBrowseText)) {
1704                 CallNumber callNumber = CallNumberFactory.getInstance().getCallNumber(classificationScheme);
1705                 String normalizedCallNumberBrowseText = callNumber.getSortableKey(callNumberBrowseText);
1706                 normalizedCallNumberBrowseText = normalizedCallNumberBrowseText.replaceAll(" ", "-");
1707 
1708                 queryString = buildQueryForTotalForwardCallNumberCount(location, classificationScheme, normalizedCallNumberBrowseText, docType);
1709                 totalForwardCallNumberCount = getSolrHitsForCallNumberBrowse(queryString);
1710             } else
1711                 totalForwardCallNumberCount = totalCallNumberCount;
1712         }
1713 
1714         LOG.info("Total Forward Call Number Count:" + totalForwardCallNumberCount);
1715         matchIndex = (totalCallNumberCount - totalForwardCallNumberCount) + 1;
1716         callNumberBrowseParams.setMatchIndex(matchIndex);
1717         callNumberBrowseParams.setTotalCallNumberCount(totalCallNumberCount);
1718         callNumberBrowseParams.setTotalForwardCallNumberCount(totalForwardCallNumberCount);
1719     }
1720 
1721     /**
1722      * Used to display closest record based on input fields classification, rows and start index
1723      *
1724      * @param callNumberBrowseParams contains required parameters for call number browse functionality
1725      * @return
1726      * @throws Exception
1727      */
1728     public List<WorkBibDocument> browseCallNumbers(CallNumberBrowseParams callNumberBrowseParams) throws Exception {
1729         List<WorkBibDocument> workBibDocuments = new ArrayList<WorkBibDocument>();
1730         if (StringUtils.isNotEmpty(callNumberBrowseParams.getClassificationScheme())) {
1731             int startIndex = callNumberBrowseParams.getStartIndex();
1732             if (startIndex > 0) {
1733                 startIndex = startIndex - 1;
1734             }
1735             String queryString = buildQueryForBrowseCallNumbers(callNumberBrowseParams.getLocation(), callNumberBrowseParams.getClassificationScheme(), callNumberBrowseParams.getNumRows(), startIndex, callNumberBrowseParams.getDocTye());
1736             List<Map<String, Object>> solrHits;
1737             solrHits = getSolrHitsForCallNumberBrowse(queryString, startIndex, callNumberBrowseParams.getNumRows());
1738             List<WorkItemDocument> workItemDocuments = buildWorkItemDocuments(solrHits);
1739             buildItemCallNumber(workItemDocuments);
1740             workBibDocuments = getWorkBibDocuments(workItemDocuments);
1741             LOG.info("Before Merging");
1742             LOG.info("Item list size:" + workItemDocuments.size());
1743             LOG.info("Bib list size:" + workBibDocuments.size());
1744             workBibDocuments = mergeBibAndItemDocuments(workItemDocuments, workBibDocuments);
1745             LOG.info("After Merging");
1746             LOG.info("Item list size:" + workItemDocuments.size());
1747             LOG.info("Bib list size:" + workBibDocuments.size());
1748         }
1749         return workBibDocuments;
1750     }
1751 
1752     /**
1753      * Merges the item documents with corresponding Bib document.
1754      * The main intention is to make equal count for both Item and Bib documents.
1755      *
1756      * @param workItemDocumentList contains list of Item records
1757      * @param workBibDocumentList  contains list of Bib records.
1758      * @return
1759      */
1760     public List<WorkBibDocument> mergeBibAndItemDocuments(List<WorkItemDocument> workItemDocumentList, List<WorkBibDocument> workBibDocumentList) {
1761 
1762         List<WorkBibDocument> workBibDocuments = new ArrayList<WorkBibDocument>();
1763         for (WorkItemDocument workItemDocument : workItemDocumentList) {
1764 //            WorkBibDocument workBibDocument=new WorkBibDocument();
1765             for (WorkBibDocument workBibDoc : workBibDocumentList) {
1766                 WorkBibDocument workBibDocument = new WorkBibDocument();
1767                 if (workBibDoc.getId().equals(workItemDocument.getBibIdentifier())) {
1768                     //workBibDocument = workBibDoc;
1769                     workBibDocument.setAuthor(workBibDoc.getAuthor());
1770                     workBibDocument.setTitle(workBibDoc.getTitle());
1771                     workBibDocument.setId(workBibDoc.getId());
1772                     workBibDocument.setDocFormat(workBibDoc.getDocFormat());
1773                     workBibDocument.setInstanceIdentifier(workBibDoc.getInstanceIdentifier());
1774                     List<WorkInstanceDocument> workInstanceDocuments = new ArrayList<WorkInstanceDocument>();
1775                     WorkInstanceDocument workInstanceDocument = new WorkInstanceDocument();
1776                     List<WorkItemDocument> workitemDocuments = new ArrayList<WorkItemDocument>();
1777                     workitemDocuments.add(workItemDocument);
1778                     workInstanceDocument.setItemDocumentList(workitemDocuments);
1779                     workInstanceDocuments.add(workInstanceDocument);
1780                     workBibDocument.setWorkInstanceDocumentList(workInstanceDocuments);
1781                     workBibDocuments.add(workBibDocument);
1782                     break;
1783                 }
1784             }
1785 
1786         }
1787         return workBibDocuments;
1788     }
1789 
1790     /**
1791      * Prepares a list of Bib records based on available Item records.
1792      *
1793      * @param workItemDocuments contains list of Item documents
1794      * @return
1795      * @throws Exception
1796      */
1797     public List<WorkBibDocument> getWorkBibDocuments(List<WorkItemDocument> workItemDocuments) throws Exception {
1798         List<WorkBibDocument> workBibDocumentList = new ArrayList<WorkBibDocument>();
1799         if (workItemDocuments.size() <= 0)
1800             return workBibDocumentList;
1801         String queryString = buildQueryForBib(workItemDocuments);
1802         List<Map<String, Object>> solrHits = getSolrHitsForQuery(queryString);
1803         workBibDocumentList = buildWorkBibDocuments(solrHits);
1804         return workBibDocumentList;
1805     }
1806 
1807     /**
1808      * Building solr query with list of Item records
1809      * The solr query is used to get Bib records.
1810      *
1811      * @param workItemDocumentList contains list of Item records
1812      * @return the solr query for Bib records
1813      */
1814     public String buildQueryForBib(List<WorkItemDocument> workItemDocumentList) {
1815         if (workItemDocumentList != null && workItemDocumentList.size() > 0) {
1816             StringBuilder query = new StringBuilder();
1817             StringBuilder idQuery = new StringBuilder();
1818             query.append("(DocType:bibliographic) AND");
1819             int i = 0;
1820             for (; i < workItemDocumentList.size() - 1; i++) {
1821                 idQuery.append(("(id:" + workItemDocumentList.get(i).getBibIdentifier()) + ") OR ");
1822             }
1823             idQuery.append("(id:" + workItemDocumentList.get(i).getBibIdentifier() + ")");
1824             if (idQuery.length() > 0)
1825                 query.append("(" + idQuery + ")");
1826             return query.toString();
1827         }
1828         return null;
1829     }
1830 
1831 
1832     /**
1833      * Preparing solr query based on user input fields location and classification scheme.
1834      *
1835      * @param location             contains user input location
1836      * @param classificationScheme user input classification
1837      * @return solr query for total call number count
1838      */
1839     public String buildQueryForTotalCallNumberCount(String location, String classificationScheme, String docType) {
1840         StringBuffer query = new StringBuffer();
1841         query.append("(DocCategory:work)AND(DocType:" + docType + ")AND(DocFormat:oleml)");
1842         if (StringUtils.isNotEmpty(location))
1843             query.append("AND (Location_search:" + location + ")");
1844         if (StringUtils.isNotEmpty(classificationScheme))
1845             query.append("AND(ShelvingSchemeCode_search:" + classificationScheme + ")");
1846         query.append("AND (ShelvingOrder_sort:{* TO *})");
1847         query.append("&sort=ShelvingOrder_sort asc");
1848         return query.toString();
1849     }
1850 
1851     /**
1852      * Building solr query for getting Item/Holdings records with call number
1853      *
1854      * @param location             the Item/Holdings location
1855      * @param classificationScheme the Item/Holdings classification
1856      * @param numRows              the number of rows for displaying results
1857      * @param startIndex           index used to specify the
1858      * @return the solr query for browse call numbers.
1859      */
1860     public String buildQueryForBrowseCallNumbers(String location, String classificationScheme, int numRows, int startIndex, String docType) {
1861         StringBuffer query = new StringBuffer();
1862         String queryString = buildCommonQueryForForwardCallNumber(location, classificationScheme, "", docType);
1863         query.append(queryString);
1864         query.append("&fl=");
1865         // query.append("CallNumber_display,ShelvingSchemeCode_display,bibIdentifier,id");
1866         query.append("CallNumber_display,bibIdentifier,id");
1867         query.append("&rows=" + numRows + "&start=" + startIndex);
1868         query.append("&sort=ShelvingOrder_sort asc");
1869         return query.toString();
1870     }
1871 
1872     /**
1873      * @param location             the Item/Holdings location.
1874      * @param classificationScheme the Item/Holdings classification.
1875      * @param callNumberBrowseText the user input field callNumberBrowseText.
1876      * @return the solr query for getting forward call number count.
1877      */
1878     public String buildQueryForTotalForwardCallNumberCount(String location, String classificationScheme, String callNumberBrowseText, String docType) {
1879         StringBuffer query = new StringBuffer();
1880         String queryString = buildCommonQueryForForwardCallNumber(location, classificationScheme, callNumberBrowseText, docType);
1881         query.append(queryString);
1882         return query.toString();
1883     }
1884 
1885     /**
1886      * @param location             the Item/Holdings location
1887      * @param classificationScheme the Item/Holdings classification
1888      * @param callNumberBrowseText the user input field callNumberBrowseText.
1889      * @return solr query
1890      */
1891     public String buildCommonQueryForForwardCallNumber(String location, String classificationScheme, String callNumberBrowseText, String docType) {
1892         StringBuffer query = new StringBuffer();
1893         //TODO: Supporting other DocType
1894         query.append("(DocCategory:work)AND(DocType:" + docType + ")AND(DocFormat:oleml)");
1895         StringBuffer locations = new StringBuffer();
1896         if (StringUtils.isNotEmpty(location)) {
1897             query.append("AND((Location_display:" + location + "*)");
1898             if (location.contains("-")) {
1899                 locations.append(location.split("-")[0]);
1900                 locations.append("*");
1901                 locations.append(location.split("-")[1]);
1902                 location = locations.substring(0,locations.length());
1903             }
1904             query.append("OR(Location_search:" + "" + location.toLowerCase() + "*" + "))");
1905         }
1906         if (StringUtils.isNotEmpty(classificationScheme))
1907             query.append("AND(ShelvingSchemeCode_search:" + classificationScheme + ")");
1908         if (StringUtils.isNotEmpty(callNumberBrowseText))
1909             query.append("AND (ShelvingOrder_sort:{\"" + callNumberBrowseText + "*\" TO *})");
1910         else
1911             query.append("AND (ShelvingOrder_sort:{* TO *})");
1912         return query.toString();
1913     }
1914 
1915     public List<WorkBibDocument> getWorkBibRecords(List<LinkedHashMap<String, String>> uuidsMapList) throws Exception {
1916         List<WorkBibDocument> workBibDocuments = new ArrayList<WorkBibDocument>();
1917         for (LinkedHashMap<String, String> uuidsMap : uuidsMapList) {
1918             WorkBibDocument workBibDocument = new WorkBibDocument();
1919             if (uuidsMap.containsKey(DocType.BIB.getDescription())) {
1920                 String bibId = uuidsMap.get(DocType.BIB.getDescription());
1921                 if (LOG.isInfoEnabled()) {
1922                     LOG.info(" bibId ---------------> " + bibId);
1923                 }
1924                 workBibDocument.setId(bibId.toString());
1925                 workBibDocument = queryForBibTree(workBibDocument);
1926             } else if (uuidsMap.containsKey(DocType.INSTANCE.getDescription())) {
1927                 WorkInstanceDocument workInstanceDocument = new WorkInstanceDocument();
1928                 List<WorkInstanceDocument> workInstanceDocuments = new ArrayList<WorkInstanceDocument>();
1929                 String instanceId = uuidsMap.get(DocType.INSTANCE.getDescription());
1930                 if (LOG.isInfoEnabled()) {
1931                     LOG.info(" instanceId ---------------> " + instanceId);
1932                 }
1933                 workInstanceDocument.setId(instanceId.toString());
1934                 workInstanceDocument.setInstanceIdentifier(instanceId.toString());
1935                 workInstanceDocument = queryForInstanceTree(workInstanceDocument);
1936                 workInstanceDocuments.add(workInstanceDocument);
1937                 workBibDocument.setId(workInstanceDocument.getBibIdentifier());
1938                 workBibDocument.setWorkInstanceDocumentList(workInstanceDocuments);
1939                 workBibDocument = queryForBibTree(workBibDocument);
1940             } else if (uuidsMap.containsKey(DocType.ITEM.getDescription())) {
1941                 WorkItemDocument workItemDocument = new WorkItemDocument();
1942                 List<WorkInstanceDocument> workInstanceDocuments = new ArrayList<WorkInstanceDocument>();
1943                 List<WorkItemDocument> workItemDocumentList = new ArrayList<WorkItemDocument>();
1944                 WorkInstanceDocument workInstanceDocument = new WorkInstanceDocument();
1945                 String itemId = uuidsMap.get(DocType.ITEM.getDescription());
1946                 if (LOG.isInfoEnabled()) {
1947                     LOG.info(" itemId ---------------> " + itemId);
1948                 }
1949                 workItemDocument.setId(itemId.toString());
1950                 workItemDocument = queryForItemTree(workItemDocument);
1951                 workItemDocumentList.add(workItemDocument);
1952                 workInstanceDocument.setItemDocumentList(workItemDocumentList);
1953                 workInstanceDocument.setId(workItemDocument.getInstanceIdentifier());
1954                 //workInstanceDocument = queryForInstanceTree(workInstanceDocument);
1955                 workInstanceDocuments.add(workInstanceDocument);
1956                 workBibDocument.setId(workItemDocument.getBibIdentifier());
1957                 workBibDocument.setWorkInstanceDocumentList(workInstanceDocuments);
1958                 workBibDocument = queryForBibTree(workBibDocument);
1959             } else if (uuidsMap.containsKey(DocType.HOLDINGS.getDescription())) {
1960                 WorkHoldingsDocument workHoldingsDocument = new WorkHoldingsDocument();
1961                 List<WorkInstanceDocument> workInstanceDocuments = new ArrayList<WorkInstanceDocument>();
1962                 WorkInstanceDocument workInstanceDocument = new WorkInstanceDocument();
1963                 String holdingsId = uuidsMap.get(DocType.HOLDINGS.getDescription());
1964                 if (LOG.isInfoEnabled()) {
1965                     LOG.info(" holdingsId ---------------> " + holdingsId);
1966                 }
1967                 workHoldingsDocument.setHoldingsIdentifier(holdingsId);
1968                 workInstanceDocument.setHoldingsDocument(workHoldingsDocument);
1969                 workInstanceDocument = queryForInstanceTree(workInstanceDocument);
1970                 workInstanceDocuments.add(workInstanceDocument);
1971                 workBibDocument.setId(workInstanceDocument.getBibIdentifier());
1972                 workBibDocument.setWorkInstanceDocumentList(workInstanceDocuments);
1973                 workBibDocument = queryForBibTree(workBibDocument);
1974             } else if (uuidsMap.containsKey(DocType.EINSTANCE.getDescription())) {
1975                 WorkEInstanceDocument workEInstanceDocument = new WorkEInstanceDocument();
1976                 List<WorkEInstanceDocument> workEInstanceDocuments = new ArrayList<WorkEInstanceDocument>();
1977                 String instanceId = uuidsMap.get(DocType.EINSTANCE.getDescription());
1978                 if (LOG.isInfoEnabled()) {
1979                     LOG.info(" eInstanceId ---------------> " + instanceId);
1980                 }
1981                 workEInstanceDocument.setId(instanceId.toString());
1982                 workEInstanceDocument.setInstanceIdentifier(instanceId.toString());
1983                 workEInstanceDocument = queryForEInstanceTree(workEInstanceDocument);
1984                 workEInstanceDocuments.add(workEInstanceDocument);
1985                 workBibDocument.setId(workEInstanceDocument.getBibIdentifier());
1986                 workBibDocument.setWorkEInstanceDocumentList(workEInstanceDocuments);
1987                 workBibDocument = queryForBibTree(workBibDocument);
1988             }
1989             workBibDocuments.add(workBibDocument);
1990         }
1991         return workBibDocuments;
1992     }
1993 
1994     /**
1995      * @param inputQuery input query
1996      * @param start      start index
1997      * @param numRows    the number of rows for displaying results
1998      * @return the list of solr hits for call number browse
1999      * @throws Exception
2000      */
2001     public List<Map<String, Object>> getSolrHitsForCallNumberBrowse(String inputQuery, int start, int numRows) throws Exception {
2002         SolrServer server;
2003         List<Map<String, Object>> hitsOnPage = new ArrayList<Map<String, Object>>();
2004         server = SolrServerManager.getInstance().getSolrServer();
2005         SolrQuery solrQuery = new SolrQuery();
2006         solrQuery.setQuery(inputQuery);
2007         solrQuery.setIncludeScore(true);
2008         solrQuery.setRows(numRows);
2009         solrQuery.setStart(start);
2010         solrQuery.setSortField("ShelvingOrder_sort", SolrQuery.ORDER.asc);
2011         LOG.info("solr Query -->" + solrQuery.getQuery());
2012         QueryResponse queryResponse = server.query(solrQuery);
2013         SolrDocumentList solrDocumentList = queryResponse.getResults();
2014 
2015         for (SolrDocument solrDocument : solrDocumentList) {
2016             hitsOnPage.add(solrDocument);
2017         }
2018         return hitsOnPage;
2019     }
2020 
2021     public int getSolrHitsForCallNumberBrowse(String inputQuery) throws Exception {
2022         SolrServer server;
2023         List<Map<String, Object>> hitsOnPage = new ArrayList<Map<String, Object>>();
2024         server = SolrServerManager.getInstance().getSolrServer();
2025         SolrQuery solrQuery = new SolrQuery();
2026         solrQuery.setQuery(inputQuery);
2027         solrQuery.setIncludeScore(true);
2028         solrQuery.setRows(0);
2029         solrQuery.setSortField("ShelvingOrder_sort", SolrQuery.ORDER.asc);
2030         LOG.info("solr Query -->" + solrQuery.getQuery());
2031         QueryResponse queryResponse = server.query(solrQuery);
2032         int numFound = (int) queryResponse.getResults().getNumFound();
2033         return numFound;
2034     }
2035 
2036     /**
2037      * Verify solr for the field and return true if exists (Ingest)
2038      * Verify solr for the field other updating record (Update)
2039      *
2040      * @param docType
2041      * @param field
2042      * @param value
2043      * @param id
2044      * @return
2045      * @throws Exception
2046      */
2047     public boolean isFieldValueExists(String docType, String field, String value, String id) throws Exception {
2048         String queryString = "(DocType:" + docType + ")AND(" + field + ":" + value + ")";
2049         QueryResponse queryResponse = getSolrResponse(queryString, field);
2050         long i = queryResponse.getResults().getNumFound();
2051         // verify barcode while updating an item
2052         if (id != null) {
2053             if (i == 1) {      // verify the result record id with given id.
2054                 SolrDocumentList solrDocumentList = queryResponse.getResults();
2055                 for (SolrDocument solrDocument : solrDocumentList) {
2056                     if (!id.equalsIgnoreCase(solrDocument.getFieldValue("id").toString())) {
2057                         return true;   // other record is having barcode value
2058                     }
2059                 }
2060             } else if (i > 1) {
2061                 return true;   // other records having same barcode value.
2062             }
2063         } else if (i > 0) {    // verify barcode while ingesting an item.
2064             return true;
2065         }
2066         return false;
2067     }
2068 
2069     private QueryResponse getSolrResponse(String queryString, String field) throws SolrServerException {
2070         SolrQuery solrQuery = new SolrQuery();
2071         SolrServer server = SolrServerManager.getInstance().getSolrServer();
2072         solrQuery.setQuery(queryString);
2073         solrQuery.setRows(500);
2074         solrQuery.setFields("id");
2075         return server.query(solrQuery);
2076     }
2077 
2078 
2079     /**
2080      * Append 'Enumeration', 'Chronology' and 'Copy Number' to item call number based on their availability.
2081      *
2082      * @param itemDocuments
2083      */
2084     public void buildItemCallNumber(List<WorkItemDocument> itemDocuments) {
2085         String itemCallNumber = "";
2086         for (WorkItemDocument workItemDocument : itemDocuments) {
2087             itemCallNumber = workItemDocument.getCallNumber();
2088             if (StringUtils.isNotEmpty(itemCallNumber)) {
2089                 itemCallNumber = itemCallNumber + " " + StringUtils.trimToEmpty(workItemDocument.getEnumeration()) + " "
2090                         + StringUtils.trimToEmpty(workItemDocument.getChronology()) + " " + StringUtils
2091                         .trimToEmpty(workItemDocument.getCopyNumber());
2092 
2093             }
2094             workItemDocument.setCallNumber(itemCallNumber);
2095         }
2096     }
2097 
2098 
2099     public List retriveResults(String queryString) {
2100         CommonsHttpSolrServer server = null;
2101         ArrayList<HashMap<String, Object>> hitsOnPage = new ArrayList<HashMap<String, Object>>();
2102         try {
2103             String serverUrl = ConfigContext.getCurrentContextConfig().getProperty("ole.docstore.url.base") + "/bib";
2104             server = new CommonsHttpSolrServer(serverUrl);
2105         } catch (Exception e) {
2106             LOG.error(e.getMessage(), e);
2107         }
2108         SolrQuery query = new SolrQuery();
2109         query.setQuery(queryString);
2110         query.setIncludeScore(true);
2111         try {
2112             QueryResponse qr = server.query(query);
2113 
2114             SolrDocumentList sdl = qr.getResults();
2115 
2116 
2117             for (SolrDocument d : sdl) {
2118                 HashMap<String, Object> values = new HashMap<String, Object>();
2119 
2120                 for (Iterator<Map.Entry<String, Object>> i = d.iterator(); i.hasNext(); ) {
2121                     Map.Entry<String, Object> e2 = i.next();
2122 
2123                     values.put(e2.getKey(), e2.getValue());
2124                 }
2125 
2126                 hitsOnPage.add(values);
2127             }
2128         } catch (SolrServerException e) {
2129             e.printStackTrace();
2130         }
2131         return hitsOnPage;
2132     }
2133 
2134 
2135     /**
2136      * Retrieve instance,item and bib uuid using item barcode or item uuid.
2137      *
2138      * @param itemBarcode
2139      * @return Map
2140      */
2141     public Map getItemDetails(String itemBarcode, String itemUUID) throws Exception {
2142         LOG.debug("Inside the getItemDetails method");
2143 
2144         HashMap itemAndTitleDetails = new HashMap();
2145         HashMap<String, Object> itemvalues = new HashMap<String, Object>();
2146         try {
2147             if (itemBarcode != null && !"".equals(itemBarcode)) {
2148                 List<HashMap<String, Object>> documentList = retriveResults("ItemBarcode_display:" + itemBarcode);
2149                 if(documentList!=null && documentList.size()>0){
2150                     itemvalues = documentList.get(0);
2151                     if (itemvalues.get("instanceIdentifier") != null) {
2152                         Object object = itemvalues.get("instanceIdentifier");
2153                         if(object instanceof List) {
2154                             List<String> list = (List<String>) itemvalues.get("instanceIdentifier");
2155                             if(list.size() > 0)
2156                                 itemAndTitleDetails.put("instanceUuid", list.get(0));
2157                         }
2158                         else
2159                             itemAndTitleDetails.put("instanceUuid", (String) itemvalues.get("instanceIdentifier"));
2160                     }
2161 
2162                     if (itemvalues.get("ItemIdentifier_display") != null) {
2163                         Object object = itemvalues.get("ItemIdentifier_display");
2164                         if(object instanceof List) {
2165                             List<String> list = (List<String>) itemvalues.get("ItemIdentifier_display");
2166                             if(list.size() > 0)
2167                                 itemAndTitleDetails.put("itemUuid", list.get(0));
2168                         }
2169                         else
2170                             itemAndTitleDetails.put("itemUuid", (String) itemvalues.get("ItemIdentifier_display"));
2171                     }
2172 
2173                     if (itemvalues.get("bibIdentifier") != null) {
2174                         Object object = itemvalues.get("bibIdentifier");
2175                         if(object instanceof List) {
2176                             List<String> list = (List<String>) itemvalues.get("bibIdentifier");
2177                             if(list.size() > 0)
2178                                 itemAndTitleDetails.put("bibUuid", list.get(0));
2179                         }
2180                         else
2181                             itemAndTitleDetails.put("bibUuid", (String) itemvalues.get("bibIdentifier"));
2182                     }
2183                 }
2184             } else {
2185                 List<HashMap<String, Object>> documentList = retriveResults("id:" + itemUUID);
2186                 if(documentList!=null && documentList.size()>0){
2187                     itemvalues = documentList.get(0);
2188                     if (itemvalues.get("instanceIdentifier") != null) {
2189                         Object object = itemvalues.get("instanceIdentifier");
2190                         if(object instanceof List) {
2191                             List<String> list = (List<String>) itemvalues.get("instanceIdentifier");
2192                             if(list.size() > 0)
2193                                 itemAndTitleDetails.put("instanceUuid", list.get(0));
2194                         }
2195                         else
2196                             itemAndTitleDetails.put("instanceUuid", (String) itemvalues.get("instanceIdentifier"));
2197                     }
2198 
2199                     if (itemvalues.get("ItemIdentifier_display") != null) {
2200                         Object object = itemvalues.get("ItemIdentifier_display");
2201                         if(object instanceof List) {
2202                             List<String> list = (List<String>) itemvalues.get("ItemIdentifier_display");
2203                             if(list.size() > 0)
2204                                 itemAndTitleDetails.put("itemUuid", list.get(0));
2205                         }
2206                         else
2207                             itemAndTitleDetails.put("itemUuid", (String) itemvalues.get("ItemIdentifier_display"));
2208                     }
2209 
2210                     if (itemvalues.get("bibIdentifier") != null) {
2211                         Object object = itemvalues.get("bibIdentifier");
2212                         if(object instanceof List) {
2213                             List<String> list = (List<String>) itemvalues.get("bibIdentifier");
2214                             if(list.size() > 0)
2215                                 itemAndTitleDetails.put("bibUuid", list.get(0));
2216                         }
2217                         else
2218                             itemAndTitleDetails.put("bibUuid", (String) itemvalues.get("bibIdentifier"));
2219                     }
2220                 }
2221             }
2222             return itemAndTitleDetails;
2223         } catch (Exception e) {
2224             LOG.error("Item barcode does not exist.", e);
2225             throw new Exception("Item barcode does not exist.");
2226         }
2227     }
2228 
2229     /**
2230      * Retrieve title and author from docstore using solr query.
2231      *
2232      * @param bibUuid
2233      * @return
2234      * @throws Exception
2235      */
2236     public Map getTitleAndAuthorfromBib(String bibUuid) throws Exception {
2237         LOG.debug("Inside the getTitleAndAuthorfromBib method");
2238         Map<String, String> bibInformationMap = new HashMap<String, String>();
2239         try {
2240             List<HashMap<String, Object>> bibDocumentList = QueryServiceImpl.getInstance().retriveResults("id:" + bibUuid);
2241             HashMap<String, Object> bibvalues = bibDocumentList.get(0);
2242             bibInformationMap.put("title", (String) ((ArrayList) bibvalues.get("245a")).get(0));
2243             if (bibvalues.get("100a") != null)
2244                 bibInformationMap.put("author", (String) ((ArrayList) bibvalues.get("100a")).get(0));
2245             else
2246                 bibInformationMap.put("author", "No Author");
2247             return bibInformationMap;
2248         } catch (Exception e) {
2249             LOG.error("Title does not exist.", e);
2250             throw new Exception("Title does not exist.");
2251         }
2252     }
2253 
2254 
2255     public Map<String, String> getBibInformation(String bibIdentifier, Map<String, String> searchCriteria) {
2256 
2257         HashMap bibDetails = new HashMap();
2258         String title = (String) searchCriteria.get("title");
2259         String author = (String) searchCriteria.get("author");
2260         String publisher = (String) searchCriteria.get("publisher");
2261         String isbn = (String) searchCriteria.get("ISBN");
2262 
2263         StringBuffer solrQuery = new StringBuffer();
2264         solrQuery.append("(id:" + bibIdentifier + ") AND ");
2265 
2266         if (title != null && !title.equals(""))
2267             solrQuery.append("(Title_search:" + title.toLowerCase() + "*) AND ");
2268         if (author != null && !author.equals(""))
2269             solrQuery.append("(Author_search:" + author.toLowerCase() + "*) AND ");
2270         if (publisher != null && !publisher.equals(""))
2271             solrQuery.append("(Publisher_search:" + publisher.toLowerCase() + "*) AND");
2272         if (isbn != null && !isbn.equals(""))
2273             solrQuery.append("(ISBN_display:" + isbn.toLowerCase() + ") AND");
2274 
2275         String query = solrQuery.substring(0, solrQuery.lastIndexOf("AND"));
2276 
2277         List<HashMap<String, Object>> documentList = retriveResults("(DocType:bibliographic) AND (" + query + ")");
2278         if (documentList.size() > 0) {
2279             HashMap<String, Object> itemvalues = documentList.get(0);
2280             if (itemvalues.get("Title_display") != null)
2281                 bibDetails.put("Title", (String) ((ArrayList) itemvalues.get("Title_display")).get(0));
2282             else if (itemvalues.get("Title_search") != null)
2283                 bibDetails.put("Title", (String) ((ArrayList) itemvalues.get("Title_search")).get(0));
2284             if (itemvalues.get("Author_display") != null)
2285                 bibDetails.put("Author", (String) ((ArrayList) itemvalues.get("Author_display")).get(0));
2286             else if (itemvalues.get("Author_search") != null)
2287                 bibDetails.put("Author", (String) ((ArrayList) itemvalues.get("Author_search")).get(0));
2288             if (itemvalues.get("Publisher_display") != null)
2289                 bibDetails.put("Publisher", (String) ((ArrayList) itemvalues.get("Publisher_display")).get(0));
2290             else if (itemvalues.get("Publisher_search") != null)
2291                 bibDetails.put("Publisher", (String) ((ArrayList) itemvalues.get("Publisher_search")).get(0));
2292             if (itemvalues.get("ISBN_display") != null)
2293                 bibDetails.put("ISBN", (String) ((ArrayList) itemvalues.get("ISBN_display")).get(0));
2294         }
2295 
2296         if ((title != null && !title.equals("")) || (author != null && !author.equals("")) || (publisher != null && !publisher.equals("")))
2297             if (bibDetails != null && bibDetails.isEmpty())
2298                 return null;
2299 
2300         return bibDetails;
2301 
2302     }
2303 
2304     /**
2305      * This method is for item search in deliver
2306      *
2307      * @param queryString
2308      * @param rowSize
2309      * @return
2310      */
2311     public List retriveResults(String queryString, int rowSize) {
2312         CommonsHttpSolrServer server = null;
2313         ArrayList<HashMap<String, Object>> hitsOnPage = new ArrayList<HashMap<String, Object>>();
2314         try {
2315             String serverUrl = ConfigContext.getCurrentContextConfig().getProperty("ole.docstore.url.base") + "/bib";
2316             server = new CommonsHttpSolrServer(serverUrl);
2317         } catch (Exception e) {
2318             LOG.error(e.getMessage(), e);
2319         }
2320         SolrQuery query = new SolrQuery();
2321         query.setQuery(queryString);
2322         query.setIncludeScore(true);
2323         query.setRows(rowSize);
2324         try {
2325             QueryResponse qr = server.query(query);
2326 
2327             SolrDocumentList sdl = qr.getResults();
2328 
2329 
2330             for (SolrDocument d : sdl) {
2331                 HashMap<String, Object> values = new HashMap<String, Object>();
2332 
2333                 for (Iterator<Map.Entry<String, Object>> i = d.iterator(); i.hasNext(); ) {
2334                     Map.Entry<String, Object> e2 = i.next();
2335 
2336                     values.put(e2.getKey(), e2.getValue());
2337                 }
2338 
2339                 hitsOnPage.add(values);
2340             }
2341         } catch (SolrServerException e) {
2342             e.printStackTrace();
2343         }
2344         return hitsOnPage;
2345     }
2346 
2347 
2348     /**
2349      * Verifies whether the given field for the given document id has a value from the given value list.
2350      * Returns true if the field has a value.
2351      *
2352      * @param uuid
2353      * @param fieldName
2354      * @param fieldValueList - "," separated values
2355      * @return
2356      * @throws SolrServerException
2357      */
2358     @Override
2359     public boolean verifyFieldValue(String uuid, String fieldName, List<String> fieldValueList) throws SolrServerException {
2360         SolrQuery solrQuery = new SolrQuery();
2361         SolrServer server = SolrServerManager.getInstance().getSolrServer();
2362         solrQuery.setQuery("id:" + uuid);
2363         solrQuery.setFields(fieldName);
2364         QueryResponse queryResponse = server.query(solrQuery);
2365         SolrDocumentList solrDocuments = queryResponse.getResults();
2366 
2367         for (String fieldValue : fieldValueList) {
2368             for (SolrDocument solrDocument : solrDocuments) {
2369                 if (solrDocument.getFieldValue(fieldName) instanceof String) {
2370                     String fieldVal = "";
2371                     fieldVal = (String) solrDocument.getFieldValue(fieldName);
2372                     if ((fieldVal != null) && (fieldVal.equalsIgnoreCase(fieldValue))) {
2373                         return true;
2374                     }
2375                 } else if (solrDocument.getFieldValue(fieldName) instanceof List) {
2376                     List<String> fieldValuesFromSolr = (List<String>) solrDocument.getFieldValue(fieldName);
2377                     for (String fieldVal : fieldValuesFromSolr) {
2378                         if (fieldVal.equalsIgnoreCase(fieldValue)) {
2379                             return true;
2380                         }
2381                     }
2382                 }
2383             }
2384         }
2385         return false;
2386     }
2387 
2388     /**
2389      * @param instanceIds
2390      * @return
2391      * @throws SolrServerException
2392      */
2393     @Override
2394     public List<String> getItemIdsForInstanceIds(List<String> instanceIds) throws SolrServerException {
2395 
2396         List<String> itemIds = new ArrayList<String>();
2397 
2398         for (String uuid : instanceIds) {
2399             SolrQuery solrQuery = new SolrQuery();
2400             SolrServer server = SolrServerManager.getInstance().getSolrServer();
2401             solrQuery.setQuery("id:" + uuid);
2402             solrQuery.setFields("itemIdentifier");
2403             QueryResponse queryResponse = server.query(solrQuery);
2404             SolrDocumentList solrDocuments = queryResponse.getResults();
2405             for (SolrDocument solrDocument : solrDocuments) {
2406                 if (solrDocument.getFieldValue("itemIdentifier") instanceof String) {
2407                     String fieldVal = (String) solrDocument.getFieldValue("itemIdentifier");
2408                     itemIds.add(fieldVal);
2409                 } else if (solrDocument.getFieldValue("itemIdentifier") instanceof List) {
2410                     List<String> fieldValuesFromSolr = (List<String>) solrDocument.getFieldValue("itemIdentifier");
2411                     itemIds.addAll(fieldValuesFromSolr);
2412                 }
2413             }
2414         }
2415         return itemIds;
2416     }
2417 
2418     @Override
2419     public List<WorkEHoldingsDocument> getEHoldingsDocuments(SearchParams searchParams) throws Exception {
2420         List<WorkEHoldingsDocument> workEHoldingsDocuments;
2421         List<Map<String, Object>> solrHits = getSolrHitsWithParams(searchParams);
2422         workEHoldingsDocuments = buildWorkEHoldingDocuments(solrHits);
2423         return workEHoldingsDocuments;
2424     }
2425 
2426     private List<WorkEHoldingsDocument> buildWorkEHoldingDocuments(List<Map<String, Object>> solrHits) {
2427         List<WorkEHoldingsDocument> workEHoldingsDocuments = new ArrayList<>();
2428         for (Map<String, Object> hitsOnPageItr : solrHits) {
2429             WorkEHoldingsDocument workEHoldingsDocument = new WorkEHoldingsDocument();
2430             Map map = hitsOnPageItr;
2431             Set keys = map.keySet();
2432             for (Object key : keys) {
2433                 if (key.toString().equalsIgnoreCase("id")) {
2434                     workEHoldingsDocument.setHoldingsIdentifier((String) map.get(key));
2435                 }
2436                 if (key.toString().equalsIgnoreCase("LocalId_display")) {
2437                     Object value = map.get(key);
2438                     if (value instanceof String) {
2439                         workEHoldingsDocument.setLocalId((String) value);
2440                     }
2441                 }
2442                 if (key.toString().equalsIgnoreCase("bibIdentifier")) {
2443                     Object value = map.get(key);
2444                     if (value instanceof String) {
2445                         workEHoldingsDocument.setBibIdentifier((String) value);
2446                     } else if (value instanceof List) {
2447                         ArrayList<String> list = (ArrayList<String>) value;
2448                         workEHoldingsDocument.setBibIdentifier(list.get(0));
2449                     }
2450                 }
2451                 if (key.toString().equalsIgnoreCase("instanceIdentifier")) {
2452                     Object value = map.get(key);
2453                     if (value instanceof String) {
2454                         workEHoldingsDocument.setInstanceIdentifier((String) value);
2455                     } else if (value instanceof List) {
2456                         ArrayList<String> list = (ArrayList<String>) value;
2457                         workEHoldingsDocument.setInstanceIdentifier(list.get(0));
2458                     }
2459                 }
2460                 if (key.toString().equalsIgnoreCase("Imprint_display")) {
2461                     Object value = map.get(key);
2462                     if (value instanceof String) {
2463                         workEHoldingsDocument.setImprint((String) value);
2464                     } else if (value instanceof List) {
2465                         ArrayList<String> list = (ArrayList<String>) value;
2466                         workEHoldingsDocument.setImprint(list.get(0));
2467                     }
2468                 }
2469                 if (key.toString().equalsIgnoreCase("AccessStatus_display")) {
2470                     Object value = map.get(key);
2471                     if (value instanceof String) {
2472                         workEHoldingsDocument.setAccessStatus((String) value);
2473                     } else if (value instanceof List) {
2474                         ArrayList<String> list = (ArrayList<String>) value;
2475                         workEHoldingsDocument.setAccessStatus(list.get(0));
2476                     }
2477                 }
2478                 if (key.toString().equalsIgnoreCase("Platform_display")) {
2479                     Object value = map.get(key);
2480                     if (value instanceof String) {
2481                         workEHoldingsDocument.setPlatForm((String) value);
2482                     } else if (value instanceof List) {
2483                         ArrayList<String> list = (ArrayList<String>) value;
2484                         workEHoldingsDocument.setPlatForm(list.get(0));
2485                     }
2486                 }
2487                 if (key.toString().equalsIgnoreCase("StatisticalSearchingFullValue_display")) {
2488                     Object value = map.get(key);
2489                     if (value instanceof String) {
2490                         workEHoldingsDocument.setStatisticalCode((String) value);
2491                     } else if (value instanceof List) {
2492                         ArrayList<String> list = (ArrayList<String>) value;
2493                         workEHoldingsDocument.setStatisticalCode(list.get(0));
2494                     }
2495                 }
2496                 if (key.toString().equalsIgnoreCase("SubscriptionStatus_display")) {
2497                     Object value = map.get(key);
2498                     if (value instanceof String) {
2499                         workEHoldingsDocument.setSubscriptionStatus((String) value);
2500                     } else if (value instanceof List) {
2501                         ArrayList<String> list = (ArrayList<String>) value;
2502                         workEHoldingsDocument.setSubscriptionStatus(list.get(0));
2503                     }
2504                 }
2505                 if (key.toString().equalsIgnoreCase("bibIdentifier")) {
2506                     Object value = map.get(key);
2507                     if (value instanceof String) {
2508                         workEHoldingsDocument.setBibIdentifier((String) value);
2509                     } else if (value instanceof List) {
2510                         ArrayList<String> list = (ArrayList<String>) value;
2511                         workEHoldingsDocument.setBibIdentifier(list.get(0));
2512                     }
2513                 }
2514                 if (key.toString().equalsIgnoreCase("staffOnlyFlag")) {
2515                     Object value = map.get(key);
2516                     if (value instanceof String) {
2517                         workEHoldingsDocument.setStaffOnly((String) value);
2518                     } else if (value instanceof List) {
2519                         ArrayList<String> list = (ArrayList<String>) value;
2520                         workEHoldingsDocument.setStaffOnly(list.get(0));
2521                     }
2522                 }
2523 
2524 
2525             }
2526             workEHoldingsDocuments.add(workEHoldingsDocument);
2527         }
2528         return workEHoldingsDocuments;
2529     }
2530 
2531     /**
2532      * Show additional linked Bib count based on input Bib UUID list
2533      *
2534      * @param bibUUIDList
2535      * @return
2536      */
2537     private String getLinkedBibCount(List<String> bibUUIDList) {
2538         String showBibMessage = "";
2539         if (bibUUIDList != null) {
2540             if ((bibUUIDList.size() > 1)) {
2541                 // showBibMessage = (bibUUIDList.size() - 1) + " more Bib(s)";
2542                 showBibMessage = "Bound with Bibs(" + (bibUUIDList.size()) + ")";
2543             } else if ((bibUUIDList.size() == 1)) {
2544                 //  showBibMessage = "No more Bibs";
2545             }
2546         }
2547         return showBibMessage;
2548     }
2549 
2550     public List<String> getBibUuidsForBibMatchPoints(String code, String value) throws SolrServerException {
2551         List<String> uuids = new ArrayList<>();
2552         SolrQuery solrQuery = new SolrQuery();
2553         SolrServer server = SolrServerManager.getInstance().getSolrServer();
2554         String query = null;
2555         if ("001".equals(code)) {
2556             query = "id" + ":\"wbm-" + value + "\"";
2557         } else {
2558             query = code + ":\"" + value + "\"";
2559         }
2560         solrQuery.setQuery(query);
2561         solrQuery.setFields("id");
2562         QueryResponse queryResponse = server.query(solrQuery);
2563         SolrDocumentList solrDocuments = queryResponse.getResults();
2564         if (solrDocuments.size() > 0) {
2565             for (SolrDocument solrDoc : solrDocuments) {
2566                 uuids.add((String) solrDoc.getFieldValue("id"));
2567             }
2568         }
2569         return uuids;
2570     }
2571 
2572     private String getModifiedText(String searchText) {
2573         StringBuffer modifiedText = new StringBuffer();
2574         StringCharacterIterator stringCharacterIterator = new StringCharacterIterator(searchText);
2575         char character = stringCharacterIterator.current();
2576         while (character != CharacterIterator.DONE) {
2577 
2578             if (character == '\\') {
2579                 modifiedText.append("\\\\");
2580             } else if (character == '?') {
2581                 modifiedText.append("\\?");
2582             } else if (character == '*' && StringUtils.isEmpty(modifiedText.toString())) {
2583                 modifiedText.append("\\*");
2584             } else if (character == '+') {
2585                 modifiedText.append("\\+");
2586             } else if (character == ':') {
2587                 modifiedText.append("\\:");
2588             } else if (character == '{') {
2589                 modifiedText.append("\\{");
2590             } else if (character == '}') {
2591                 modifiedText.append("\\}");
2592             } else if (character == '[') {
2593                 modifiedText.append("\\[");
2594             } else if (character == ']') {
2595                 modifiedText.append("\\]");
2596             } else if (character == '(') {
2597                 modifiedText.append("\\(");
2598             } else if (character == ')') {
2599                 modifiedText.append("\\)");
2600             } else if (character == '^') {
2601                 modifiedText.append("\\^");
2602             } else if (character == '~') {
2603                 modifiedText.append("\\~");
2604             } else if (character == '-') {
2605                 modifiedText.append("\\-");
2606             } else if (character == '!') {
2607                 modifiedText.append("\\!");
2608             } else if (character == '\'') {
2609                 modifiedText.append("\\'");
2610             } else if (character == '@') {
2611                 modifiedText.append("\\@");
2612             } else if (character == '#') {
2613                 modifiedText.append("\\#");
2614             } else if (character == '$') {
2615                 modifiedText.append("\\$");
2616             } else if (character == '%') {
2617                 modifiedText.append("\\%");
2618             } else {
2619                 //the char is not a special one
2620                 //add it to the result as is
2621                 modifiedText.append(character);
2622             }
2623             character = stringCharacterIterator.next();
2624         }
2625 
2626         return modifiedText.toString().toLowerCase();
2627     }
2628     public List<String>  getBibDetailsForPurchaseOrderSearch(Map<String, String> searchCriteria) {
2629 
2630         List<String> finallist = new ArrayList<>();
2631         String solrQuery;
2632 
2633         StringBuffer query = new StringBuffer("");
2634         String title = ((String) searchCriteria.get("title"))!=null?searchCriteria.get("title") :"";
2635         String author = ((String) searchCriteria.get("author"))!=null?searchCriteria.get("author") :"";
2636         String issn = ((String) searchCriteria.get("issn"))!=null?searchCriteria.get("issn") :"";
2637         if (title.equalsIgnoreCase("")) {
2638             String searchText;
2639 
2640             searchText = "(*:*)";
2641             query.append("(Title_search:"+searchText+")AND");
2642         } else {
2643             String searchText;
2644             searchText=getModifiedText(title);
2645             searchText.replaceAll(" ", "+");
2646             String searchTextVal=null;
2647             if (searchText.length() > 0) {
2648                 searchTextVal = searchText.trim().replace(" ", " AND ");
2649             }
2650 
2651             query.append("(Title_search:("+searchTextVal+"))AND");
2652         }
2653         if (author.equalsIgnoreCase("")) {
2654             String searchText;
2655 
2656             searchText = "(*:*)";
2657             query.append("(Author_search:"+searchText+")AND");
2658         } else {
2659             String searchText;
2660             searchText=getModifiedText(author);
2661             searchText.replaceAll(" ", "+");
2662             String searchTextVal=null;
2663             if (searchText.length() > 0) {
2664                 searchTextVal = searchText.trim().replace(" ", " AND ");
2665             }
2666             query.append("(Author_search:("+searchTextVal+"))AND");
2667         }
2668         if (issn.equalsIgnoreCase("")) {
2669             String searchText;
2670             searchText = "(*:*)";
2671             query.append("(ISSN_search:"+searchText+")AND");
2672         } else {
2673             String searchText;
2674             searchText=getModifiedText(issn);
2675             String searchTextVal=null;
2676             searchText.replaceAll(" ", "+");
2677             if (searchText.length() > 0) {
2678                 searchTextVal = searchText.trim().replace(" ", " AND ");
2679             }
2680             query.append("(ISSN_search:("+searchTextVal+"))AND");
2681         }
2682 
2683 
2684         if (!query.toString().equals("")) {
2685             query = new StringBuffer(query.substring(0, query.lastIndexOf("AND")));
2686             solrQuery=query.toString();
2687             int rowSize=10;
2688             if (searchCriteria.get("rowSize") != null)
2689                 rowSize = Integer.parseInt(searchCriteria.get("rowSize"));
2690             try {
2691                 List<HashMap<String, Object>> documentList = QueryServiceImpl.getInstance().retriveResults("(DocType:bibliographic) AND (" + solrQuery + ")", rowSize);
2692 
2693                 for (int i = 0; i < documentList.size(); i++) {
2694                     HashMap<String, Object> itemvalues = documentList.get(i);
2695                     if (itemvalues.get("id") != null) {
2696                         finallist.add((String) itemvalues.get("id"));
2697                     }
2698                 }
2699 
2700             } catch (Exception e) {
2701 
2702                 LOG.info("Exception ------> " + e);
2703 
2704             }
2705 
2706         }
2707         return finallist;
2708 
2709     }
2710 
2711 
2712 }