View Javadoc
1   package org.kuali.ole.serviceimpl;
2   
3   import org.kuali.ole.OleSRUConstants;
4   import org.kuali.ole.bo.cql.CQLResponseBO;
5   import org.kuali.ole.bo.diagnostics.OleSRUDiagnostics;
6   import org.kuali.ole.bo.serachRetrieve.OleSRUData;
7   import org.kuali.ole.bo.serachRetrieve.OleSRUSearchRetrieveResponse;
8   import org.kuali.ole.docstore.OleException;
9   import org.kuali.ole.docstore.common.client.DocstoreClient;
10  import org.kuali.ole.docstore.common.document.BibTree;
11  import org.kuali.ole.docstore.common.document.HoldingsTree;
12  import org.kuali.ole.docstore.common.search.*;
13  import org.kuali.ole.docstore.engine.client.DocstoreLocalClient;
14  import org.kuali.ole.docstore.engine.service.index.solr.BibConstants;
15  import org.kuali.ole.docstore.model.enums.DocType;
16  import org.kuali.ole.docstore.utility.ISBNUtil;
17  import org.kuali.ole.handler.OleSRUOpacXMLResponseHandler;
18  import org.kuali.ole.service.OleCQLQueryParserService;
19  import org.kuali.ole.service.OleDiagnosticsService;
20  import org.kuali.ole.service.OleSRUDataService;
21  import org.kuali.ole.service.OleSearchRetrieveOperationService;
22  import org.kuali.rice.core.api.config.property.Config;
23  import org.kuali.rice.core.api.config.property.ConfigContext;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  import java.util.ArrayList;
27  import java.util.List;
28  import java.util.Map;
29  
30  /**
31   * Created with IntelliJ IDEA.
32   * User: ?
33   * Date: 7/9/12
34   * Time: 6:50 PM
35   * To change this template use File | Settings | File Templates.
36   */
37  public class OleSearchRetrieveOperationServiceImpl implements OleSearchRetrieveOperationService {
38  
39      private Logger LOG = LoggerFactory.getLogger(this.getClass());
40      public OleSRUDataService oleSRUDataService;
41      public OleDiagnosticsService oleDiagnosticsService;
42      public OleCQLQueryParserService oleCQLQueryParserService;
43      private Config currentContextConfig;
44      private SearchParams searchParams = new SearchParams();
45      private SearchCondition searchCondition = null;
46      private SearchField searchField = null;
47      private DocstoreClient docstoreClient;
48      private SearchResultField searchResultFields = null;
49  
50      public OleSearchRetrieveOperationServiceImpl() {
51          oleSRUDataService = new OleSRUDataServiceImpl();
52          oleDiagnosticsService = new OleDiagnosticsServiceImpl();
53          oleCQLQueryParserService = new OleCQLQueryParserServiceImpl();
54      }
55  
56      /**
57       * it will return a OPAC response xml  ( overall xml )
58       *
59       * @param reqParamMap
60       * @param cqlParseBO
61       * @return OPAC xml response
62       */
63      public String getSearchRetriveResponse(Map reqParamMap, CQLResponseBO cqlParseBO) {
64  
65          SearchParams searchParams = createSearchParams(cqlParseBO, true);
66          buildSearchConditions(searchParams);
67          /*SearchCondition condition = new SearchCondition();
68          condition.setOperator("AND");
69          SearchField field = new SearchField();
70          field.setFieldName("DocFormat");
71          String format = (String)reqParamMap.get(OleSRUConstants.RECORD_SCHEMA);
72          if(format.equalsIgnoreCase("OPAC")){
73              format = "marc";
74          }
75          field.setFieldValue(format);
76          condition.setSearchField(field);
77          searchParams.getSearchConditions().add(condition);*/
78          searchParams.setStartIndex(Integer.parseInt(reqParamMap.get("startRecord").toString()));
79          searchParams.setPageSize(Integer.parseInt(reqParamMap.get("maximumRecords").toString()));
80          if (searchParams == null) {
81              OleSRUDiagnostics oleSRUDiagnostics = oleDiagnosticsService.getDiagnosticResponse(getCurrentContextConfig().getProperty(OleSRUConstants.INVALID_QUERY_DIAGNOSTIC_MSG));
82              OleSRUSearchRetrieveResponse oleSRUSearchRetrieveResponse = new OleSRUSearchRetrieveResponse();
83              oleSRUSearchRetrieveResponse.setVersion((String) reqParamMap.get(OleSRUConstants.VERSION));
84              oleSRUSearchRetrieveResponse.setOleSRUDiagnostics(oleSRUDiagnostics);
85              OleSRUOpacXMLResponseHandler oleSRUOpacXMLResponseHandler = new OleSRUOpacXMLResponseHandler();
86              return oleSRUOpacXMLResponseHandler.toXML(oleSRUSearchRetrieveResponse, (String) reqParamMap.get("recordSchema"));
87  
88          }
89          // add q=staffOnlyFlag:false to search
90          FacetCondition staffOnlyFacet = new FacetCondition();
91          staffOnlyFacet.setFieldName(BibConstants.STAFF_ONLY_FLAG);
92          staffOnlyFacet.setFieldValue(OleSRUConstants.SRU_STAFF_ONLY_FLAG);
93          searchParams.getFacetConditions().add(staffOnlyFacet);
94  
95          // add fq=DocType:bibliographic to search
96          FacetCondition docTypeFacet = new FacetCondition();
97          docTypeFacet.setFieldName(BibConstants.DOC_TYPE);
98          docTypeFacet.setFieldValue(OleSRUConstants.BIBLIOGRAPHIC);
99          searchParams.getFacetConditions().add(docTypeFacet);
100 
101         OleSRUSearchRetrieveResponse oleSRUSearchRetrieveResponse = new OleSRUSearchRetrieveResponse();
102         oleSRUSearchRetrieveResponse.setVersion((String) reqParamMap.get(OleSRUConstants.VERSION));
103         OleSRUOpacXMLResponseHandler oleSRUOpacXMLResponseHandler = new OleSRUOpacXMLResponseHandler();
104         List oleSRUBibIdList = getBibIds(reqParamMap, searchParams);
105         /*if(oleSRUBibIdList!=null && oleSRUBibIdList.size()>0 && oleSRUBibIdList.get(0) instanceof String){
106             if(oleSRUBibIdList.get(0).equals("Invalid Local Id")){
107                 OleSRUDiagnostics oleSRUDiagnostics = oleDiagnosticsService.getDiagnosticResponse("Local Id Value Should be a Number");
108                 oleSRUSearchRetrieveResponse.setOleSRUDiagnostics(oleSRUDiagnostics);
109                 return oleSRUOpacXMLResponseHandler.toXML(oleSRUSearchRetrieveResponse,(String)reqParamMap.get("recordSchema"));
110             }
111             OleSRUDiagnostics oleSRUDiagnostics = oleDiagnosticsService.getDiagnosticResponse(getCurrentContextConfig().getProperty(OleSRUConstants.INVALID_QUERY_DIAGNOSTIC_MSG));
112             oleSRUSearchRetrieveResponse.setOleSRUDiagnostics(oleSRUDiagnostics);
113             return oleSRUOpacXMLResponseHandler.toXML(oleSRUSearchRetrieveResponse,(String)reqParamMap.get("recordSchema"));
114         }*/
115         if (oleSRUBibIdList == null) {
116             OleSRUDiagnostics oleSRUDiagnostics = oleDiagnosticsService.getDiagnosticResponse(getCurrentContextConfig().getProperty(OleSRUConstants.SERVER_DIAGNOSTIC_MSG));
117             oleSRUSearchRetrieveResponse.setOleSRUDiagnostics(oleSRUDiagnostics);
118             return oleSRUOpacXMLResponseHandler.toXML(oleSRUSearchRetrieveResponse, (String) reqParamMap.get("recordSchema"));
119 
120         }
121 
122         String opacXML = oleSRUDataService.getOPACXMLSearchRetrieveResponse(oleSRUBibIdList, reqParamMap);
123         String styleSheet = (String) reqParamMap.get(OleSRUConstants.STYLE_SHEET);
124         if (styleSheet != null) {
125             opacXML = opacXML.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n <?xml-stylesheet type=\"text/xsl\" href=\"" + styleSheet + "\"?>");
126         }
127         opacXML = opacXML.replace("<collection xmlns=\"http://www.loc.gov/MARC21/slim\">", "");
128         opacXML = opacXML.replace("<collection>","");
129         opacXML = opacXML.replace("</collection>", "");
130         LOG.info("opac xml response ---->  " + opacXML);
131         return opacXML;
132     }
133 
134 
135     private void buildSearchConditions(SearchParams searchParams) {
136 
137         List<SearchCondition> searchConditions = searchParams.getSearchConditions();
138         if (searchConditions.size() >1 && searchConditions.size() <= 2) {
139             if(searchConditions.get(0)!=null && searchConditions.get(1)!=null){
140                 searchConditions.get(0).setOperator(searchConditions.get(1).getOperator());
141             }
142         } else if (searchConditions.size() > 2) {
143             for (int i = 0; i < searchConditions.size() - 1; i++) {
144                 if(searchConditions.get(i)!=null && searchConditions.get(i)!=null){
145                     SearchCondition sc1 = searchConditions.get(i);
146                     SearchCondition sc2 = searchConditions.get(i + 1);
147                     sc1.setOperator(sc2.getOperator());
148                 }
149             }
150         }
151     }
152 
153     private Config getCurrentContextConfig() {
154         if (null == currentContextConfig) {
155             currentContextConfig = ConfigContext.getCurrentContextConfig();
156         }
157         return currentContextConfig;
158     }
159 
160     public void setCurrentContextConfig(Config currentContextConfig) {
161         this.currentContextConfig = currentContextConfig;
162     }
163 
164     /**
165      * .
166      * this will get the proper query from the oleCQLQueryParserService
167      *
168      * @param cqlResponseBO object
169      * @return query as a string
170      */
171     /*public String getSolrQueryFromCQLParseBO(CQLResponseBO cqlResponseBO) {
172         String solrQuery = null;
173         solrQuery = oleCQLQueryParserService.getSolrQueryFromCQLBO(cqlResponseBO);
174         LOG.info("solr Query ------> " + solrQuery);
175         return "(" + solrQuery + ")";
176     }*/
177     private SearchParams createSearchParams(CQLResponseBO cqlResponseBO, boolean flag) {
178         searchResultFields = new SearchResultField();
179         if (cqlResponseBO.getLeftOperand() != null) {
180             CQLResponseBO leftOperand = cqlResponseBO.getLeftOperand();
181             createSearchParams(leftOperand, false);
182             searchField = setSearchField(cqlResponseBO.getLeftOperand());
183             if (searchField != null) {
184                 searchCondition = setSearchCondition(cqlResponseBO.getLeftOperand(), searchField);
185             }
186             if (null != cqlResponseBO.getLeftOperand().getBooleanTagValue() && null != cqlResponseBO.getLeftOperand().getBooleanTagValue().getValue()) {
187                 searchCondition.setOperator(cqlResponseBO.getBooleanTagValue().getValue());
188             }
189             if (searchCondition != null && searchField != null) {
190                 searchParams.getSearchConditions().add(searchCondition);
191             }
192         }
193         if (cqlResponseBO.getRightOperand() != null) {
194             CQLResponseBO rightOperand = cqlResponseBO.getRightOperand();
195             createSearchParams(rightOperand, false);
196             searchField = setSearchField(cqlResponseBO.getRightOperand());
197             if (searchField != null) {
198                 searchCondition = setSearchCondition(cqlResponseBO.getRightOperand(), searchField);
199             }
200             if (null != cqlResponseBO.getBooleanTagValue() && null != cqlResponseBO.getBooleanTagValue().getValue()) {
201                 searchCondition.setOperator(cqlResponseBO.getBooleanTagValue().getValue());
202             }
203             if (searchCondition != null && searchField != null) {
204                 searchParams.getSearchConditions().add(searchCondition);
205             }
206         }
207         if (cqlResponseBO.getTriple() != null) {
208             CQLResponseBO triple = cqlResponseBO.getTriple();
209             if (triple != null) {
210                 createSearchParams(triple, false);
211                 searchField = setSearchField(triple);
212                 if (searchField != null)
213                     searchCondition = setSearchCondition(triple, searchField);
214             }
215             if (null != cqlResponseBO.getBooleanTagValue() && null != triple.getBooleanTagValue().getValue()) {
216                 searchCondition.setOperator(triple.getBooleanTagValue().getValue());
217             }
218             if (searchCondition != null && searchField != null) {
219                 searchParams.getSearchConditions().add(searchCondition);
220             }
221         }
222         if (cqlResponseBO.getSearchClauseTag() != null) {
223             SearchField searchField = setSearchField(cqlResponseBO);
224             SearchCondition searchCondition = setSearchCondition(cqlResponseBO, searchField);
225             SearchParams searchParams = new SearchParams();
226             searchResultFields.setDocType(OleSRUConstants.BIBLIOGRAPHIC);
227             searchResultFields.setFieldName("id");
228             searchParams.getSearchResultFields().add(searchResultFields);
229             searchParams.getSearchConditions().add(searchCondition);
230             return searchParams;
231         }
232 
233         if (flag) {
234             searchResultFields.setDocType(OleSRUConstants.BIBLIOGRAPHIC);
235             searchResultFields.setFieldName("id");
236             searchParams.getSearchResultFields().add(searchResultFields);
237             return searchParams;
238         }
239         return null;
240     }
241 
242     private List getBibIds(Map reqParamMap, SearchParams searchParams) {
243         SearchResponse searchResponse = getDocstoreClient().search(searchParams);
244         List bibIds = new ArrayList();
245         List<OleSRUData> oleSRUDataList = new ArrayList<>();
246         for (SearchResult searchResult : searchResponse.getSearchResults()) {
247             OleSRUData oleSRUData ;
248             for (SearchResultField searchResultField : searchResult.getSearchResultFields()) {
249                 if (searchResultField.getDocType().equalsIgnoreCase(DocType.BIB.getCode())) {
250                     if (searchResultField.getFieldName().equalsIgnoreCase("id")) {
251                         bibIds.add(searchResultField.getFieldValue());
252                         oleSRUData = new OleSRUData();
253                         oleSRUData.setBibId(searchResultField.getFieldValue());
254                         BibTree bib = getDocstoreClient().retrieveBibTree(searchResultField.getFieldValue());
255                         List<HoldingsTree> holdingsTrees = bib.getHoldingsTrees();
256                         List<String> ids = new ArrayList<>();
257                         for(HoldingsTree holdingsTree:holdingsTrees){
258                             ids.add(holdingsTree.getHoldings().getId());
259                         }
260                         /*ListIterator<HoldingsTree> holdingsTreeListIterator = holdingsTrees.listIterator();
261                         List<String> ids = new ArrayList<>();
262                         while(holdingsTreeListIterator.hasNext()){
263                             ids.add(holdingsTreeListIterator.next().getId());
264                         }*/
265                         oleSRUData.setInstanceIds(ids);
266                         oleSRUDataList.add(oleSRUData);
267                     }
268                 }
269             }
270         }
271         Integer size = searchResponse.getTotalRecordCount();
272         reqParamMap.put("numberOfRecords", size.longValue());
273         return oleSRUDataList;
274     }
275 
276     private DocstoreClient getDocstoreClient() {
277 
278         if (docstoreClient == null) {
279             docstoreClient = new DocstoreLocalClient();
280         }
281         return docstoreClient;
282     }
283 
284     private SearchField setSearchField(CQLResponseBO cqlResponseBO) {
285         searchField = null;
286         if (cqlResponseBO != null) {
287             if (cqlResponseBO.getSearchClauseTag() != null) {
288                 searchField = new SearchField();
289                 if (cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase(OleSRUConstants.TITLE) ||
290                 		cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase(OleSRUConstants.CS_TITLE)) {
291                     searchField.setFieldName(OleSRUConstants.TITLE_SEARCH);
292                 } else if (cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase(OleSRUConstants.AUTHOR) ||
293                 		cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase(OleSRUConstants.CS_AUTHOR)) {
294                     searchField.setFieldName(OleSRUConstants.AUTHOR_SEARCH);
295                 } else if (cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase(OleSRUConstants.PUBLISHER) ||
296                 		cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase(OleSRUConstants.CS_PUBLISHER)) {
297                     searchField.setFieldName(OleSRUConstants.PUBLISHER_SEARCH);
298                 } else if (cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase(OleSRUConstants.PUBLICATION_DATE) ||
299                 		cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase(OleSRUConstants.PUB_DATE) ||
300                 		cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase(OleSRUConstants.CS_PUBLICATION_DATE) ||
301                 		cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase(OleSRUConstants.CS_PUB_DATE)) {
302                     searchField.setFieldName(OleSRUConstants.PUBLICATION_DATE_SEARCH);
303                 } else if (cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase(OleSRUConstants.ISBN) ||
304                 		cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase(OleSRUConstants.CS_ISBN)) {
305                     searchField.setFieldName(OleSRUConstants.ISBN_SEARCH);
306                 } else if (cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase(OleSRUConstants.ISSN) ||
307                 		cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase(OleSRUConstants.CS_ISSN)) {
308                     searchField.setFieldName(OleSRUConstants.ISSN_SEARCH);
309                 } else if (cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase(OleSRUConstants.LOCALID) ||
310                 		cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase(OleSRUConstants.CS_LOCALID)) {
311                     searchField.setFieldName(OleSRUConstants.LOCALID_SEARCH);
312                 } else if (cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase(OleSRUConstants.SUBJECT) ||
313                 		cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase(OleSRUConstants.CS_SUBJECT)) {
314                     searchField.setFieldName(OleSRUConstants.SUBJECT_SEARCH);
315                 } else if (cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase(OleSRUConstants.CHOICE) ||
316                 		cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase(OleSRUConstants.CS_CHOICE)) {
317                     searchField.setFieldName(OleSRUConstants.ALL_TEXT);
318                 }
319             }
320             if (cqlResponseBO.getSearchClauseTag() != null && cqlResponseBO.getSearchClauseTag().getTerm() != null) {
321                 String fieldValue=cqlResponseBO.getSearchClauseTag().getTerm().replaceAll("-","");
322                 if (cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase(OleSRUConstants.ISBN)){
323                     ISBNUtil isbnUtil = new ISBNUtil();
324                     try {
325                         fieldValue = isbnUtil.normalizeISBN(fieldValue);
326                     } catch (OleException e) {
327                         e.printStackTrace();
328                     }
329                     searchField.setFieldValue(fieldValue);
330                 }else{
331                     searchField.setFieldValue(cqlResponseBO.getSearchClauseTag().getTerm());
332                 }
333             }
334         }
335         return searchField;
336     }
337 
338     private SearchCondition setSearchCondition(CQLResponseBO cqlResponseBO, SearchField searchField) {
339         searchCondition = new SearchCondition();
340         if (cqlResponseBO.getSearchClauseTag() != null && cqlResponseBO.getSearchClauseTag().getRelationTag() != null && cqlResponseBO.getSearchClauseTag().getRelationTag().getValue() != null && cqlResponseBO.getSearchClauseTag().getRelationTag().getValue().equalsIgnoreCase("any")) {
341             searchCondition.setSearchScope("or");
342         } else if (cqlResponseBO.getSearchClauseTag() != null && cqlResponseBO.getSearchClauseTag().getRelationTag() != null && cqlResponseBO.getSearchClauseTag().getRelationTag().getValue() != null && cqlResponseBO.getSearchClauseTag().getRelationTag().getValue().equalsIgnoreCase("all")) {
343             searchCondition.setSearchScope("and");
344         }else if (cqlResponseBO.getSearchClauseTag() != null && cqlResponseBO.getSearchClauseTag().getRelationTag() != null && cqlResponseBO.getSearchClauseTag().getRelationTag().getValue() != null && cqlResponseBO.getSearchClauseTag().getRelationTag().getValue().equalsIgnoreCase("adj")) {
345             searchCondition.setSearchScope("phrase");
346         } else {
347             searchCondition.setSearchScope("phrase");
348         }
349         if (null != searchField)
350             searchCondition.setSearchField(searchField);
351         if (null != cqlResponseBO && null != cqlResponseBO.getBooleanTagValue()) {
352             searchCondition.setOperator(cqlResponseBO.getBooleanTagValue().getValue());
353         }
354         return searchCondition;
355     }
356 }