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.oai.set;
20  
21  import org.apache.logging.log4j.LogManager;
22  import org.apache.solr.client.solrj.SolrQuery;
23  import org.apache.solr.common.SolrDocument;
24  import org.mycore.common.config.MCRConfiguration2;
25  import org.mycore.common.config.MCRConfigurationException;
26  
27  /**
28   * Default implementation for a set configuration. Loads the information from the
29   * {@link MCRConfiguration2} (MCR.OAIDataProvider.myprovider.Sets.<b>SET_ID</b>).
30   * 
31   * @author Matthias Eichner
32   */
33  public class MCROAISolrSetConfiguration implements MCROAISetConfiguration<SolrQuery, SolrDocument, String> {
34  
35      private static final String SETS_PREFIX = "Sets.";
36  
37      private String id;
38  
39      private String uri;
40  
41      private MCROAISetHandler<SolrQuery, SolrDocument, String> handler;
42  
43      public MCROAISolrSetConfiguration(String configPrefix, String setId) {
44          String setConfigPrefix = configPrefix + SETS_PREFIX + setId;
45          String defaultname = getFallbackHandler(configPrefix, setId);
46          MCROAISetHandler<SolrQuery, SolrDocument, String> handler = defaultname == null
47              ? MCRConfiguration2.getOrThrow(setConfigPrefix + ".Handler", MCRConfiguration2::instantiateClass)
48              : MCRConfiguration2.<MCROAISetHandler<SolrQuery, SolrDocument, String>>getInstanceOf(
49                  setConfigPrefix + ".Handler")
50                  .orElseGet(() -> MCRConfiguration2.instantiateClass(defaultname));
51          handler.init(configPrefix, setId);
52          this.id = setId;
53          this.uri = getURI(setConfigPrefix);
54          this.handler = handler;
55      }
56  
57      private String getURI(String setConfigPrefix) {
58          String uriProperty = setConfigPrefix + ".URI";
59          try {
60              return MCRConfiguration2.getStringOrThrow(uriProperty);
61          } catch (MCRConfigurationException e) {
62              String legacy = MCRConfiguration2.getString(setConfigPrefix).orElseThrow(() -> e);
63              LogManager.getLogger().warn("Please rename deprecated property '{}' to '{}'.", setConfigPrefix,
64                  uriProperty);
65              return legacy;
66          }
67      }
68  
69      private String getFallbackHandler(String configPrefix, String setId) {
70          String queryProperty = configPrefix + SETS_PREFIX + setId + ".Query";
71          if (MCRConfiguration2.getString(queryProperty)
72              .orElse(MCRConfiguration2.getString(configPrefix + "MapSetToQuery." + setId).orElse(null)) != null) {
73              return MCROAIQueryToSetHandler.class.getName();
74          }
75          String classProperty = configPrefix + SETS_PREFIX + setId + ".Classification";
76          if (MCRConfiguration2.getString(classProperty)
77              .orElse(MCRConfiguration2.getString(configPrefix + "MapSetToClassification." + setId)
78                  .orElse(null))
79              != null) {
80              return MCROAIClassificationToSetHandler.class.getName();
81          }
82          if (MCRConfiguration2.getString(configPrefix + SETS_PREFIX + setId + ".Handler").orElse(null) == null) {
83              LogManager.getLogger().error(
84                  "Neither '{}' nor '{}' is defined. Please map set '{}' to classification or query.", classProperty,
85                  queryProperty, setId);
86              return MCROAIClassificationToSetHandler.class.getName();
87          }
88          return null;
89      }
90  
91      @Override
92      public String getId() {
93          return this.id;
94      }
95  
96      @Override
97      public String getURI() {
98          return this.uri;
99      }
100 
101     @Override
102     public MCROAISetHandler<SolrQuery, SolrDocument, String> getHandler() {
103         return this.handler;
104     }
105 
106 }