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  package org.mycore.oai;
19  
20  import java.time.Instant;
21  import java.util.Collection;
22  import java.util.Map;
23  import java.util.stream.Collectors;
24  
25  import org.apache.logging.log4j.LogManager;
26  import org.apache.logging.log4j.Logger;
27  import org.jdom2.Element;
28  import org.mycore.common.config.MCRConfiguration2;
29  import org.mycore.common.xml.MCRURIResolver;
30  import org.mycore.oai.pmh.DateUtils;
31  import org.mycore.oai.pmh.Description;
32  import org.mycore.oai.pmh.FriendsDescription;
33  import org.mycore.oai.pmh.Granularity;
34  import org.mycore.oai.pmh.Identify;
35  import org.mycore.oai.pmh.OAIIdentifierDescription;
36  import org.mycore.oai.pmh.SimpleIdentify;
37  
38  /**
39   * Simple MyCoRe implementation of a OAI-PMH {@link Identify} class. Uses the {@link MCRConfiguration2} to retrieve
40   * all important settings. Earliest date stamp is calculated with the 'restriction' query and sort by 'created'.
41   * Also adds custom description elements from URIs configured by MCR.OAIDataProvider.OAI.DescriptionURI
42   *
43   * @author Matthias Eichner
44   * @author Frank L\u00fctzenkirchen
45   */
46  public class MCROAIIdentify extends SimpleIdentify {
47  
48      protected static final Logger LOGGER = LogManager.getLogger(MCROAIIdentify.class);
49  
50      protected String configPrefix;
51  
52      public MCROAIIdentify(String baseURL, String configPrefix) {
53          this.configPrefix = configPrefix;
54  
55          this.setBaseURL(baseURL);
56          this.setRepositoryName(
57              MCRConfiguration2.getString(configPrefix + "RepositoryName").orElse("Undefined repository name"));
58          String deletedRecordPolicy = MCRConfiguration2.getString(configPrefix + "DeletedRecord")
59              .orElse(DeletedRecordPolicy.Transient.name());
60          this.setDeletedRecordPolicy(DeletedRecordPolicy.get(deletedRecordPolicy));
61          String granularity = MCRConfiguration2.getString(configPrefix + "Granularity")
62              .orElse(Granularity.YYYY_MM_DD.name());
63          this.setGranularity(Granularity.valueOf(granularity));
64          String adminMail = MCRConfiguration2.getString(configPrefix + "AdminEmail")
65              .orElseGet(() -> MCRConfiguration2.getStringOrThrow("MCR.Mail.Sender"));
66  
67          this.setEarliestDatestamp(calculateEarliestTimestamp());
68          this.getAdminEmailList().add(adminMail);
69          this.getDescriptionList().add(getIdentifierDescription());
70          if (!getFriendsDescription().getFriendsList().isEmpty()) {
71              this.getDescriptionList().add(getFriendsDescription());
72          }
73  
74          addCustomDescriptions();
75      }
76  
77      private void addCustomDescriptions() {
78          for (final String descriptionURI : getDescriptionURIs()) {
79              this.getDescriptionList().add(new CustomDescription(descriptionURI));
80          }
81      }
82  
83      /**
84       * Calculates the earliest date stamp.
85       *
86       * @return the create date of the oldest document within the repository
87       */
88      protected Instant calculateEarliestTimestamp() {
89          MCROAISearcher searcher = MCROAISearchManager.getSearcher(this, null, 1, null, null);
90          return searcher.getEarliestTimestamp().orElse(DateUtils
91              .parse(MCRConfiguration2.getString(this.configPrefix + "EarliestDatestamp").orElse("1970-01-01")));
92      }
93  
94      public String getConfigPrefix() {
95          return configPrefix;
96      }
97  
98      private Collection<String> getDescriptionURIs() {
99          String descriptionConfig = getConfigPrefix() + "DescriptionURI";
100         return MCRConfiguration2.getPropertiesMap()
101             .entrySet()
102             .stream()
103             .filter(p -> p.getKey().startsWith(descriptionConfig))
104             .map(Map.Entry::getValue)
105             .collect(Collectors.toSet());
106     }
107 
108     public FriendsDescription getFriendsDescription() {
109         FriendsDescription desc = new FriendsDescription();
110         MCRConfiguration2.getPropertiesMap()
111             .entrySet()
112             .stream()
113             .filter(p -> p.getKey().startsWith(this.configPrefix + "Friends."))
114             .map(Map.Entry::getValue)
115             .forEach(desc.getFriendsList()::add);
116         return desc;
117     }
118 
119     public OAIIdentifierDescription getIdentifierDescription() {
120         String reposId = MCRConfiguration2.getStringOrThrow(this.configPrefix + "RepositoryIdentifier");
121         String sampleId = MCRConfiguration2.getStringOrThrow(this.configPrefix + "RecordSampleID");
122         return new OAIIdentifierDescription(reposId, sampleId);
123     }
124 
125     class CustomDescription implements Description {
126 
127         private Element description;
128 
129         CustomDescription(String descriptionURI) {
130             description = MCRURIResolver.instance().resolve(descriptionURI);
131         }
132 
133         @Override
134         public void fromXML(Element description) {
135             this.description = description;
136         }
137 
138         @Override
139         public Element toXML() {
140             return description.getChildren().get(0).clone();
141         }
142     }
143 
144 }