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.common.client.DocstoreClient;
9   import org.kuali.ole.docstore.common.document.BibTree;
10  import org.kuali.ole.docstore.common.document.HoldingsTree;
11  import org.kuali.ole.docstore.common.search.*;
12  import org.kuali.ole.docstore.engine.client.DocstoreLocalClient;
13  import org.kuali.ole.docstore.model.enums.DocType;
14  import org.kuali.ole.handler.OleSRUOpacXMLResponseHandler;
15  import org.kuali.ole.service.OleCQLQueryParserService;
16  import org.kuali.ole.service.OleDiagnosticsService;
17  import org.kuali.ole.service.OleSRUDataService;
18  import org.kuali.ole.service.OleSearchRetrieveOperationService;
19  import org.kuali.rice.core.api.config.property.Config;
20  import org.kuali.rice.core.api.config.property.ConfigContext;
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Map;
26  
27  /**
28   * Created with IntelliJ IDEA.
29   * User: ?
30   * Date: 7/9/12
31   * Time: 6:50 PM
32   * To change this template use File | Settings | File Templates.
33   */
34  public class OleSearchRetrieveOperationServiceImpl implements OleSearchRetrieveOperationService {
35  
36      private Logger LOG = LoggerFactory.getLogger(this.getClass());
37      public OleSRUDataService oleSRUDataService;
38      public OleDiagnosticsService oleDiagnosticsService;
39      public OleCQLQueryParserService oleCQLQueryParserService;
40      private Config currentContextConfig;
41      private SearchParams searchParams = new SearchParams();
42      private SearchCondition searchCondition = null;
43      private SearchField searchField = null;
44      private DocstoreClient docstoreClient;
45      private SearchResultField searchResultFields = null;
46  
47      public OleSearchRetrieveOperationServiceImpl() {
48          oleSRUDataService = new OleSRUDataServiceImpl();
49          oleDiagnosticsService = new OleDiagnosticsServiceImpl();
50          oleCQLQueryParserService = new OleCQLQueryParserServiceImpl();
51      }
52  
53      /**
54       * it will return a OPAC response xml  ( overall xml )
55       *
56       * @param reqParamMap
57       * @param cqlParseBO
58       * @return OPAC xml response
59       */
60      public String getSearchRetriveResponse(Map reqParamMap, CQLResponseBO cqlParseBO) {
61  
62          SearchParams searchParams = createSearchParams(cqlParseBO, true);
63          buildSearchConditions(searchParams);
64          /*SearchCondition condition = new SearchCondition();
65          condition.setOperator("AND");
66          SearchField field = new SearchField();
67          field.setFieldName("DocFormat");
68          String format = (String)reqParamMap.get(OleSRUConstants.RECORD_SCHEMA);
69          if(format.equalsIgnoreCase("OPAC")){
70              format = "marc";
71          }
72          field.setFieldValue(format);
73          condition.setSearchField(field);
74          searchParams.getSearchConditions().add(condition);*/
75          searchParams.setStartIndex(Integer.parseInt(reqParamMap.get("startRecord").toString()));
76          searchParams.setPageSize(Integer.parseInt(reqParamMap.get("maximumRecords").toString()));
77          if (searchParams == null) {
78              OleSRUDiagnostics oleSRUDiagnostics = oleDiagnosticsService.getDiagnosticResponse(getCurrentContextConfig().getProperty(OleSRUConstants.INVALID_QUERY_DIAGNOSTIC_MSG));
79              OleSRUSearchRetrieveResponse oleSRUSearchRetrieveResponse = new OleSRUSearchRetrieveResponse();
80              oleSRUSearchRetrieveResponse.setVersion((String) reqParamMap.get(OleSRUConstants.VERSION));
81              oleSRUSearchRetrieveResponse.setOleSRUDiagnostics(oleSRUDiagnostics);
82              OleSRUOpacXMLResponseHandler oleSRUOpacXMLResponseHandler = new OleSRUOpacXMLResponseHandler();
83              return oleSRUOpacXMLResponseHandler.toXML(oleSRUSearchRetrieveResponse, (String) reqParamMap.get("recordSchema"));
84  
85          }
86          OleSRUSearchRetrieveResponse oleSRUSearchRetrieveResponse = new OleSRUSearchRetrieveResponse();
87          oleSRUSearchRetrieveResponse.setVersion((String) reqParamMap.get(OleSRUConstants.VERSION));
88          OleSRUOpacXMLResponseHandler oleSRUOpacXMLResponseHandler = new OleSRUOpacXMLResponseHandler();
89          List oleSRUBibIdList = getBibIds(reqParamMap, searchParams);
90          /*if(oleSRUBibIdList!=null && oleSRUBibIdList.size()>0 && oleSRUBibIdList.get(0) instanceof String){
91              if(oleSRUBibIdList.get(0).equals("Invalid Local Id")){
92                  OleSRUDiagnostics oleSRUDiagnostics = oleDiagnosticsService.getDiagnosticResponse("Local Id Value Should be a Number");
93                  oleSRUSearchRetrieveResponse.setOleSRUDiagnostics(oleSRUDiagnostics);
94                  return oleSRUOpacXMLResponseHandler.toXML(oleSRUSearchRetrieveResponse,(String)reqParamMap.get("recordSchema"));
95              }
96              OleSRUDiagnostics oleSRUDiagnostics = oleDiagnosticsService.getDiagnosticResponse(getCurrentContextConfig().getProperty(OleSRUConstants.INVALID_QUERY_DIAGNOSTIC_MSG));
97              oleSRUSearchRetrieveResponse.setOleSRUDiagnostics(oleSRUDiagnostics);
98              return oleSRUOpacXMLResponseHandler.toXML(oleSRUSearchRetrieveResponse,(String)reqParamMap.get("recordSchema"));
99          }*/
100         if (oleSRUBibIdList == null) {
101             OleSRUDiagnostics oleSRUDiagnostics = oleDiagnosticsService.getDiagnosticResponse(getCurrentContextConfig().getProperty(OleSRUConstants.SERVER_DIAGNOSTIC_MSG));
102             oleSRUSearchRetrieveResponse.setOleSRUDiagnostics(oleSRUDiagnostics);
103             return oleSRUOpacXMLResponseHandler.toXML(oleSRUSearchRetrieveResponse, (String) reqParamMap.get("recordSchema"));
104 
105         }
106 
107         String opacXML = oleSRUDataService.getOPACXMLSearchRetrieveResponse(oleSRUBibIdList, reqParamMap);
108         String styleSheet = (String) reqParamMap.get(OleSRUConstants.STYLE_SHEET);
109         if (styleSheet != null) {
110             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 + "\"?>");
111         }
112         LOG.info("opac xml response ---->  " + opacXML);
113         return opacXML;
114     }
115 
116 
117     private void buildSearchConditions(SearchParams searchParams) {
118 
119         List<SearchCondition> searchConditions = searchParams.getSearchConditions();
120         if (searchConditions.size() >1 && searchConditions.size() <= 2) {
121             if(searchConditions.get(0)!=null && searchConditions.get(1)!=null){
122                 searchConditions.get(0).setOperator(searchConditions.get(1).getOperator());
123             }
124         } else if (searchConditions.size() > 2) {
125             for (int i = 0; i < searchConditions.size() - 1; i++) {
126                 if(searchConditions.get(i)!=null && searchConditions.get(i)!=null){
127                     SearchCondition sc1 = searchConditions.get(i);
128                     SearchCondition sc2 = searchConditions.get(i + 1);
129                     sc1.setOperator(sc2.getOperator());
130                 }
131             }
132         }
133     }
134 
135     private Config getCurrentContextConfig() {
136         if (null == currentContextConfig) {
137             currentContextConfig = ConfigContext.getCurrentContextConfig();
138         }
139         return currentContextConfig;
140     }
141 
142     public void setCurrentContextConfig(Config currentContextConfig) {
143         this.currentContextConfig = currentContextConfig;
144     }
145 
146     /**
147      * .
148      * this will get the proper query from the oleCQLQueryParserService
149      *
150      * @param cqlResponseBO object
151      * @return query as a string
152      */
153     /*public String getSolrQueryFromCQLParseBO(CQLResponseBO cqlResponseBO) {
154         String solrQuery = null;
155         solrQuery = oleCQLQueryParserService.getSolrQueryFromCQLBO(cqlResponseBO);
156         LOG.info("solr Query ------> " + solrQuery);
157         return "(" + solrQuery + ")";
158     }*/
159     private SearchParams createSearchParams(CQLResponseBO cqlResponseBO, boolean flag) {
160         searchResultFields = new SearchResultField();
161         if (cqlResponseBO.getLeftOperand() != null) {
162             CQLResponseBO leftOperand = cqlResponseBO.getLeftOperand();
163             createSearchParams(leftOperand, false);
164             searchField = setSearchField(cqlResponseBO.getLeftOperand());
165             if (searchField != null) {
166                 searchCondition = setSearchCondition(cqlResponseBO.getLeftOperand(), searchField);
167             }
168             if (null != cqlResponseBO.getLeftOperand().getBooleanTagValue() && null != cqlResponseBO.getLeftOperand().getBooleanTagValue().getValue()) {
169                 searchCondition.setOperator(cqlResponseBO.getBooleanTagValue().getValue());
170             }
171             if (searchCondition != null && searchField != null) {
172                 searchParams.getSearchConditions().add(searchCondition);
173             }
174         }
175         if (cqlResponseBO.getRightOperand() != null) {
176             CQLResponseBO rightOperand = cqlResponseBO.getRightOperand();
177             createSearchParams(rightOperand, false);
178             searchField = setSearchField(cqlResponseBO.getRightOperand());
179             if (searchField != null) {
180                 searchCondition = setSearchCondition(cqlResponseBO.getRightOperand(), searchField);
181             }
182             if (null != cqlResponseBO.getBooleanTagValue() && null != cqlResponseBO.getBooleanTagValue().getValue()) {
183                 searchCondition.setOperator(cqlResponseBO.getBooleanTagValue().getValue());
184             }
185             if (searchCondition != null && searchField != null) {
186                 searchParams.getSearchConditions().add(searchCondition);
187             }
188         }
189         if (cqlResponseBO.getTriple() != null) {
190             CQLResponseBO triple = cqlResponseBO.getTriple();
191             if (triple != null) {
192                 createSearchParams(triple, false);
193                 searchField = setSearchField(triple);
194                 if (searchField != null)
195                     searchCondition = setSearchCondition(triple, searchField);
196             }
197             if (null != cqlResponseBO.getBooleanTagValue() && null != triple.getBooleanTagValue().getValue()) {
198                 searchCondition.setOperator(triple.getBooleanTagValue().getValue());
199             }
200             if (searchCondition != null && searchField != null) {
201                 searchParams.getSearchConditions().add(searchCondition);
202             }
203         }
204         if (cqlResponseBO.getSearchClauseTag() != null) {
205             SearchField searchField = setSearchField(cqlResponseBO);
206             SearchCondition searchCondition = setSearchCondition(cqlResponseBO, searchField);
207             SearchParams searchParams = new SearchParams();
208             searchResultFields.setDocType("bibliographic");
209             searchResultFields.setFieldName("id");
210             searchParams.getSearchResultFields().add(searchResultFields);
211             searchParams.getSearchConditions().add(searchCondition);
212             return searchParams;
213         }
214 
215         if (flag) {
216             searchResultFields.setDocType("bibliographic");
217             searchResultFields.setFieldName("id");
218             searchParams.getSearchResultFields().add(searchResultFields);
219             return searchParams;
220         }
221         return null;
222     }
223 
224     private List getBibIds(Map reqParamMap, SearchParams searchParams) {
225         SearchResponse searchResponse = getDocstoreClient().search(searchParams);
226         List bibIds = new ArrayList();
227         List<OleSRUData> oleSRUDataList = new ArrayList<>();
228         for (SearchResult searchResult : searchResponse.getSearchResults()) {
229             OleSRUData oleSRUData ;
230             for (SearchResultField searchResultField : searchResult.getSearchResultFields()) {
231                 if (searchResultField.getDocType().equalsIgnoreCase(DocType.BIB.getCode())) {
232                     if (searchResultField.getFieldName().equalsIgnoreCase("id")) {
233                         bibIds.add(searchResultField.getFieldValue());
234                         oleSRUData = new OleSRUData();
235                         oleSRUData.setBibId(searchResultField.getFieldValue());
236                         BibTree bib = getDocstoreClient().retrieveBibTree(searchResultField.getFieldValue());
237                         List<HoldingsTree> holdingsTrees = bib.getHoldingsTrees();
238                         List<String> ids = new ArrayList<>();
239                         for(HoldingsTree holdingsTree:holdingsTrees){
240                             ids.add(holdingsTree.getHoldings().getId());
241                         }
242                         /*ListIterator<HoldingsTree> holdingsTreeListIterator = holdingsTrees.listIterator();
243                         List<String> ids = new ArrayList<>();
244                         while(holdingsTreeListIterator.hasNext()){
245                             ids.add(holdingsTreeListIterator.next().getId());
246                         }*/
247                         oleSRUData.setInstanceIds(ids);
248                         oleSRUDataList.add(oleSRUData);
249                     }
250                 }
251             }
252         }
253         Integer size = bibIds.size();
254         reqParamMap.put("numberOfRecords", size.longValue());
255         return oleSRUDataList;
256     }
257 
258     private DocstoreClient getDocstoreClient() {
259 
260         if (docstoreClient == null) {
261             docstoreClient = new DocstoreLocalClient();
262         }
263         return docstoreClient;
264     }
265 
266     private SearchField setSearchField(CQLResponseBO cqlResponseBO) {
267         searchField = null;
268         if (cqlResponseBO != null) {
269             if (cqlResponseBO.getSearchClauseTag() != null) {
270                 searchField = new SearchField();
271                 if (cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase("title")) {
272                     searchField.setFieldName("Title_search");
273                 } else if (cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase("author")) {
274                     searchField.setFieldName("Author_search");
275                 } else if (cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase("publisher")) {
276                     searchField.setFieldName("Publisher_search");
277                 } else if (cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase("publicationDate")) {
278                     searchField.setFieldName("PublicationDate_search");
279                 } else if (cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase("isbn")) {
280                     searchField.setFieldName("ISBN_search");
281                 } else if (cqlResponseBO.getSearchClauseTag().getIndex().equalsIgnoreCase("cql.serverChoice")) {
282                     searchField.setFieldName("all_text");
283                 }
284             }
285             if (cqlResponseBO.getSearchClauseTag() != null && cqlResponseBO.getSearchClauseTag().getTerm() != null) {
286                 searchField.setFieldValue(cqlResponseBO.getSearchClauseTag().getTerm());
287             }
288         }
289         return searchField;
290     }
291 
292     private SearchCondition setSearchCondition(CQLResponseBO cqlResponseBO, SearchField searchField) {
293         searchCondition = new SearchCondition();
294         if (cqlResponseBO.getSearchClauseTag() != null && cqlResponseBO.getSearchClauseTag().getRelationTag() != null && cqlResponseBO.getSearchClauseTag().getRelationTag().getValue() != null && cqlResponseBO.getSearchClauseTag().getRelationTag().getValue().equalsIgnoreCase("any")) {
295             searchCondition.setSearchScope("or");
296         } else if (cqlResponseBO.getSearchClauseTag() != null && cqlResponseBO.getSearchClauseTag().getRelationTag() != null && cqlResponseBO.getSearchClauseTag().getRelationTag().getValue() != null && cqlResponseBO.getSearchClauseTag().getRelationTag().getValue().equalsIgnoreCase("all")) {
297             searchCondition.setSearchScope("and");
298         } else {
299             searchCondition.setSearchScope("phrase");
300         }
301         if (null != searchField)
302             searchCondition.setSearchField(searchField);
303         if (null != cqlResponseBO && null != cqlResponseBO.getBooleanTagValue()) {
304             searchCondition.setOperator(cqlResponseBO.getBooleanTagValue().getValue());
305         }
306         return searchCondition;
307     }
308 
309 }