View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.solr.search;
20  
21  import java.io.IOException;
22  import java.text.MessageFormat;
23  import java.util.Locale;
24  
25  import org.apache.logging.log4j.LogManager;
26  import org.apache.logging.log4j.Logger;
27  import org.apache.solr.client.solrj.SolrQuery;
28  import org.apache.solr.client.solrj.SolrServerException;
29  import org.apache.solr.client.solrj.response.QueryResponse;
30  import org.mycore.frontend.servlets.MCRClassificationBrowser2.MCRQueryAdapter;
31  import org.mycore.frontend.servlets.MCRServlet;
32  import org.mycore.solr.MCRSolrClientFactory;
33  import org.mycore.solr.MCRSolrConstants;
34  
35  import jakarta.servlet.http.HttpServletRequest;
36  
37  public class MCRSolrQueryAdapter implements MCRQueryAdapter {
38      private static final Logger LOGGER = LogManager.getLogger(MCRSolrQueryAdapter.class);
39  
40      private String fieldName;
41  
42      private String restriction = "";
43  
44      private String objectType = "";
45  
46      private String category;
47  
48      private boolean filterCategory;
49  
50      private SolrQuery solrQuery;
51  
52      public void filterCategory(boolean filter) {
53          this.filterCategory = filter;
54      }
55  
56      @Override
57      public void setRestriction(String text) {
58          this.restriction = " " + text;
59      }
60  
61      @Override
62      public void setObjectType(String text) {
63          this.objectType = " +objectType:" + text;
64      }
65  
66      @Override
67      public String getObjectType() {
68          return this.objectType.isEmpty() ? null : this.objectType.split(":")[1];
69      }
70  
71      @Override
72      public void setCategory(String text) {
73          this.category = text;
74      }
75  
76      @Override
77      public long getResultCount() {
78          configureSolrQuery();
79          LOGGER.debug("query: {}", solrQuery);
80          solrQuery.set("rows", 0);
81          QueryResponse queryResponse;
82          try {
83              queryResponse = MCRSolrClientFactory.getMainSolrClient().query(solrQuery);
84          } catch (SolrServerException | IOException e) {
85              LOGGER.warn("Could not query SOLR.", e);
86              return -1;
87          }
88          return queryResponse.getResults().getNumFound();
89      }
90  
91      @Override
92      public String getQueryAsString() {
93          configureSolrQuery();
94          String queryString = solrQuery.toQueryString();
95          return queryString.isEmpty() ? "" : queryString.substring("?".length());
96      }
97  
98      public void prepareQuery() {
99          this.solrQuery = new SolrQuery();
100     }
101 
102     private void configureSolrQuery() {
103         this.solrQuery.clear();
104         String queryString = filterCategory
105             ? new MessageFormat("{0}{1}", Locale.ROOT).format(new Object[] { objectType, restriction })
106             : new MessageFormat("+{0}:\"{1}\"{2}{3}", Locale.ROOT)
107                 .format(new Object[] { fieldName, category, objectType, restriction });
108         this.solrQuery.setQuery(queryString.trim());
109         if (filterCategory) {
110             solrQuery.setFilterQueries(new MessageFormat("{0}+{1}:\"{2}\"", Locale.ROOT)
111                 .format(new Object[] { MCRSolrConstants.SOLR_JOIN_PATTERN, fieldName, category }));
112         }
113     }
114 
115     @Override
116     public void setFieldName(String fieldname) {
117         this.fieldName = fieldname;
118     }
119 
120     @Override
121     public void configure(HttpServletRequest request) {
122         String objectType = request.getParameter("objecttype");
123         if ((objectType != null) && (objectType.trim().length() > 0)) {
124             setObjectType(objectType);
125         }
126         String restriction = request.getParameter("restriction");
127         if ((restriction != null) && (restriction.trim().length() > 0)) {
128             setRestriction(restriction);
129         }
130         boolean filter = "true".equals(MCRServlet.getProperty(request, "filterCategory"));
131         filterCategory(filter);
132         prepareQuery();
133     }
134 }