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.pi.doi;
20  
21  import java.io.IOException;
22  import java.util.Date;
23  import java.util.Locale;
24  import java.util.Map;
25  import java.util.Objects;
26  import java.util.Optional;
27  import java.util.UUID;
28  
29  import javax.naming.OperationNotSupportedException;
30  
31  import org.apache.logging.log4j.LogManager;
32  import org.apache.logging.log4j.Logger;
33  import org.jdom2.Document;
34  import org.jdom2.Element;
35  import org.jdom2.JDOMException;
36  import org.mycore.common.MCRConstants;
37  import org.mycore.common.config.MCRConfigurationException;
38  import org.mycore.common.content.MCRBaseContent;
39  import org.mycore.common.content.MCRContent;
40  import org.mycore.datamodel.metadata.MCRBase;
41  import org.mycore.datamodel.metadata.MCRMetadataManager;
42  import org.mycore.datamodel.metadata.MCRObject;
43  import org.mycore.datamodel.metadata.MCRObjectID;
44  import org.mycore.frontend.MCRFrontendUtil;
45  import org.mycore.pi.doi.client.crossref.MCRCrossrefClient;
46  import org.mycore.pi.doi.crossref.MCRCrossrefUtil;
47  import org.mycore.pi.exceptions.MCRPersistentIdentifierException;
48  import org.xml.sax.SAXException;
49  
50  public class MCRCrossrefService extends MCRDOIBaseService {
51  
52      private static final String CONFIG_TEST = "Test";
53  
54      private static final String CONFIG_REGISTRANT = "Registrant";
55  
56      private static final String CONFIG_DEPOSITOR_MAIL = "DepositorMail";
57  
58      private static final String CONFIG_DEPOSITOR = "Depositor";
59  
60      public static final String DEFAULT_SCHEMA = "http://data.crossref.org/schemas/crossref4.4.1.xsd";
61  
62      private static final String TEST_HOST = "test.crossref.org";
63  
64      private static final String PRODUCTION_HOST = "doi.crossref.org";
65  
66      private static final Logger LOGGER = LogManager.getLogger();
67  
68      private String registrant;
69  
70      private String depositor;
71  
72      private String depositorMail;
73  
74      @Override
75      protected String getDefaultSchemaPath() {
76          return DEFAULT_SCHEMA;
77      }
78  
79      @Override
80      protected void checkConfiguration() throws MCRConfigurationException {
81          super.checkConfiguration();
82          init();
83      }
84  
85      private void init() {
86          initCommonProperties();
87          registrant = requireNotEmptyProperty(CONFIG_REGISTRANT);
88          depositor = requireNotEmptyProperty(CONFIG_DEPOSITOR);
89          depositorMail = requireNotEmptyProperty(CONFIG_DEPOSITOR_MAIL);
90      }
91  
92      @Override
93      protected void registerIdentifier(MCRBase obj, String additional, MCRDigitalObjectIdentifier pi)
94          throws MCRPersistentIdentifierException {
95          final Document resultDocument = transform(obj, pi.asString());
96          validateDocument(obj.getId().toString(), resultDocument);
97      }
98  
99      @Override
100     protected Document transform(MCRBase obj, String pi)
101         throws MCRPersistentIdentifierException {
102         Document resultDocument;
103         try {
104             final MCRContent result = getTransformer().transform(new MCRBaseContent(obj));
105             resultDocument = result.asXML();
106         } catch (IOException | JDOMException | SAXException e) {
107             throw new MCRConfigurationException(
108                 String.format(Locale.ROOT, "Could not transform the object %s with the trasformer %s", obj.getId(),
109                     getTransformerID()),
110                 e);
111         }
112 
113         final Element root = resultDocument.getRootElement();
114 
115         final Element headElement = root.getChild("head", MCRConstants.CROSSREF_NAMESPACE);
116         final String batchID = UUID.randomUUID() + "_" + obj.getId();
117         final String timestampMilliseconds = String.valueOf(new Date().getTime());
118         MCRCrossrefUtil.insertBatchInformation(headElement, batchID, timestampMilliseconds, depositor, depositorMail,
119             registrant);
120 
121         MCRCrossrefUtil.replaceDOIData(root, (objectID) -> Objects.equals(obj.getId().toString(), objectID) ? pi : null,
122             MCRFrontendUtil.getBaseURL());
123 
124         return resultDocument;
125     }
126 
127     private String getHost() {
128         return Optional.ofNullable(getProperties().get(CONFIG_TEST))
129             .map(Boolean::valueOf)
130             .map(testMode -> testMode ? TEST_HOST : PRODUCTION_HOST)
131             .orElse(PRODUCTION_HOST);
132     }
133 
134     @Override
135     protected void delete(MCRDigitalObjectIdentifier identifier, MCRBase obj, String additional)
136         throws MCRPersistentIdentifierException {
137         throw new MCRPersistentIdentifierException("Delete is not Supported!",
138             new OperationNotSupportedException("Delete is not Supported!"));
139     }
140 
141     @Override
142     protected void deleteJob(Map<String, String> parameters) throws MCRPersistentIdentifierException {
143 
144     }
145 
146     @Override
147     protected void updateJob(Map<String, String> parameters) throws MCRPersistentIdentifierException {
148         String doi = parameters.get(CONTEXT_DOI);
149         String idString = parameters.get(CONTEXT_OBJ);
150 
151         if (!checkJobValid(idString, PiJobAction.UPDATE)) {
152             return;
153         }
154 
155         MCRObjectID objectID = MCRObjectID.getInstance(idString);
156         this.validateJobUserRights(objectID);
157         MCRObject object = MCRMetadataManager.retrieveMCRObject(objectID);
158 
159         Document newCrossrefBatch = transform(object, doi);
160         final MCRCrossrefClient client = new MCRCrossrefClient(getHost(), getUsername(), getPassword());
161 
162         client.doMDUpload(newCrossrefBatch);
163     }
164 
165     @Override
166     protected void registerJob(Map<String, String> parameters) throws MCRPersistentIdentifierException {
167         String doi = parameters.get(CONTEXT_DOI);
168         String idString = parameters.get(CONTEXT_OBJ);
169 
170         if (!checkJobValid(idString, PiJobAction.REGISTER)) {
171             return;
172         }
173 
174         MCRObjectID objectID = MCRObjectID.getInstance(idString);
175         this.validateJobUserRights(objectID);
176         MCRObject mcrBase = MCRMetadataManager.retrieveMCRObject(objectID);
177 
178         final Document resultDocument = transform(mcrBase, doi);
179         final MCRCrossrefClient client = new MCRCrossrefClient(getHost(), getUsername(), getPassword());
180         client.doMDUpload(resultDocument);
181     }
182 
183 }