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.frontend;
20  
21  import java.util.Locale;
22  
23  import org.apache.logging.log4j.LogManager;
24  import org.apache.logging.log4j.Logger;
25  import org.jdom2.Element;
26  import org.jdom2.JDOMException;
27  import org.jdom2.output.DOMOutputter;
28  import org.mycore.access.MCRAccessManager;
29  import org.mycore.datamodel.metadata.MCRBase;
30  import org.mycore.datamodel.metadata.MCRMetadataManager;
31  import org.mycore.datamodel.metadata.MCRObjectID;
32  import org.mycore.pi.MCRPIManager;
33  import org.mycore.pi.MCRPIService;
34  import org.mycore.pi.MCRPIServiceManager;
35  import org.mycore.pi.MCRPersistentIdentifier;
36  import org.mycore.pi.exceptions.MCRPersistentIdentifierException;
37  import org.w3c.dom.NodeList;
38  
39  public class MCRIdentifierXSLUtils {
40  
41      private static final Logger LOGGER = LogManager.getLogger();
42  
43      public static boolean hasIdentifierCreated(String service, String id, String additional) {
44          MCRPIService<MCRPersistentIdentifier> registrationService = MCRPIServiceManager
45              .getInstance().getRegistrationService(service);
46          return registrationService.isCreated(MCRObjectID.getInstance(id), additional);
47      }
48  
49      public static boolean hasIdentifierRegistrationStarted(String service, String id, String additional) {
50          MCRPIService<MCRPersistentIdentifier> registrationService = MCRPIServiceManager
51              .getInstance().getRegistrationService(service);
52          return registrationService.hasRegistrationStarted(MCRObjectID.getInstance(id), additional);
53      }
54  
55      public static boolean hasIdentifierRegistered(String service, String id, String additional) {
56          MCRPIService<MCRPersistentIdentifier> registrationService = MCRPIServiceManager
57              .getInstance().getRegistrationService(service);
58          return registrationService.isRegistered(MCRObjectID.getInstance(id), additional);
59      }
60  
61      public static boolean hasManagedPI(String objectID) {
62          return MCRPIManager.getInstance()
63              .getRegistered(MCRMetadataManager.retrieveMCRObject(MCRObjectID.getInstance(objectID))).size() > 0;
64      }
65  
66      public static boolean isManagedPI(String pi, String id) {
67          return MCRPIManager.getInstance().getInfo(pi).stream().anyMatch(info -> info.getMycoreID()
68              .equals(id));
69      }
70  
71      /**
72       * Gets all available services which are configured.
73       * e.g.
74       * <ul>
75       * <li>&lt;service id="service1" inscribed="false" permission="true" type="urn" /&gt;</li>
76       * <li>&lt;service id="service2" inscribed="true" permission="false" type="doi" /&gt;</li>
77       * </ul>
78       *
79       * @param objectID the object
80       * @return a Nodelist
81       * @throws JDOMException
82       */
83      public static NodeList getPIServiceInformation(String objectID) throws JDOMException {
84          Element e = new Element("list");
85  
86          MCRBase obj = MCRMetadataManager.retrieve(MCRObjectID.getInstance(objectID));
87          MCRPIServiceManager.getInstance().getServiceList()
88              .stream()
89              .map((rs -> {
90                  Element service = new Element("service");
91  
92                  service.setAttribute("id", rs.getServiceID());
93  
94                  // Check if the inscriber of this service can read a PI
95                  try {
96                      if (rs.getMetadataService().getIdentifier(obj, "").isPresent()) {
97                          service.setAttribute("inscribed", "true");
98                      } else {
99                          service.setAttribute("inscribed", "false");
100                     }
101                 } catch (MCRPersistentIdentifierException e1) {
102                     LOGGER.warn("Error happened while try to read PI from object: {}", objectID, e1);
103                     service.setAttribute("inscribed", "false");
104                 }
105 
106                 // rights
107                 String permission = "register-" + rs.getServiceID();
108                 Boolean canRegister = MCRAccessManager.checkPermission(objectID, "writedb") &&
109                     MCRAccessManager.checkPermission(obj.getId(), permission);
110 
111                 service.setAttribute("permission", canRegister.toString().toLowerCase(Locale.ROOT));
112 
113                 // add the type
114                 service.setAttribute("type", rs.getType());
115 
116                 return service;
117             }))
118             .forEach(e::addContent);
119         return new DOMOutputter().output(e).getElementsByTagName("service");
120     }
121 
122 }