Search in sources :

Example 96 with MCRException

use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.

the class MCRDataciteClient method changeToTestDOI.

private void changeToTestDOI(Document metadata) {
    XPathExpression<Element> compile = XPathFactory.instance().compile("//datacite:identifier[@identifierType='DOI']", Filters.element(), null, Namespace.getNamespace("datacite", "http://datacite.org/schema/kernel-3"));
    Element element = compile.evaluateFirst(metadata);
    MCRDigitalObjectIdentifier doi = new MCRDOIParser().parse(element.getText()).orElseThrow(() -> new MCRException("Datacite Document contains invalid DOI!"));
    String testDOI = doi.toTestPrefix().asString();
    element.setText(testDOI);
}
Also used : MCRException(org.mycore.common.MCRException) Element(org.jdom2.Element)

Example 97 with MCRException

use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.

the class MCRDOICommands method updateMediaListForDOI.

@MCRCommand(syntax = REPAIR_MEDIALIST_OF_0_AND_SERVICE_1, help = "Sends new media list to Datacite. {0} is the DOI. The Service ID{1} is the id from the configuration.")
public static void updateMediaListForDOI(String doiString, String serviceID) {
    MCRDOIRegistrationService registrationService = new MCRDOIRegistrationService(serviceID);
    MCRDataciteClient dataciteClient = registrationService.getDataciteClient();
    MCRDigitalObjectIdentifier doi = new MCRDOIParser().parse(doiString).orElseThrow(() -> new IllegalArgumentException("The String " + doiString + " is no valid DOI!"));
    try {
        URI uri = dataciteClient.resolveDOI(doi);
        if (uri.toString().startsWith(registrationService.getRegisterURL())) {
            String s = uri.toString();
            LOGGER.info("Checking DOI: {} / {}", doi.asString(), s);
            String idString = s.substring(s.lastIndexOf("/") + 1);
            MCRObjectID objectID = MCRObjectID.getInstance(idString);
            if (MCRMetadataManager.exists(objectID)) {
                MCRObject obj = MCRMetadataManager.retrieveMCRObject(objectID);
                List<Map.Entry<String, URI>> newMediaList = registrationService.getMediaList(obj);
                List<Map.Entry<String, URI>> oldMediaList;
                try {
                    oldMediaList = dataciteClient.getMediaList(doi);
                } catch (MCRIdentifierUnresolvableException e) {
                    LOGGER.warn("{} had no media list!", doi);
                    oldMediaList = new ArrayList<>();
                }
                HashMap<String, URI> newHashMap = new HashMap<>();
                newMediaList.forEach(e -> newHashMap.put(e.getKey(), e.getValue()));
                oldMediaList.forEach(e -> {
                    /*
                        Currently it is not possible to delete inserted values key values (mime types).
                        So we update old media mimetypes which are not present in new list to the same URL of the first
                        mimetype entry.
                        */
                    if (!newHashMap.containsKey(e.getKey())) {
                        newHashMap.put(e.getKey(), newMediaList.stream().findFirst().orElseThrow(() -> new MCRException("new media list is empty (this should not happen)")).getValue());
                    }
                });
                dataciteClient.setMediaList(doi, newHashMap.entrySet().stream().collect(Collectors.toList()));
                LOGGER.info("Updated media-list of {}", doiString);
            } else {
                LOGGER.info("Object {} does not exist in this application!", objectID);
            }
        } else {
            LOGGER.info("DOI is not from this application: ({}) {}", uri, registrationService.getRegisterURL());
        }
    } catch (MCRPersistentIdentifierException e) {
        LOGGER.error("Error occurred for DOI: {}", doi, e);
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRIdentifierUnresolvableException(org.mycore.pi.exceptions.MCRIdentifierUnresolvableException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) URI(java.net.URI) MCRDOIParser(org.mycore.pi.doi.MCRDOIParser) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRDigitalObjectIdentifier(org.mycore.pi.doi.MCRDigitalObjectIdentifier) MCRDOIRegistrationService(org.mycore.pi.doi.MCRDOIRegistrationService) MCRDataciteClient(org.mycore.pi.doi.MCRDataciteClient) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 98 with MCRException

use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.

the class MCRDOICommands method repairIncompleteRegisteredDOI.

@MCRCommand(syntax = "repair incomplete registered doi {0} with registration service {1}", help = "Use this method if a DOI is registered, but not inserted in the Database. {0} is the DOI and " + "{1} the registration service from configuration.")
public static void repairIncompleteRegisteredDOI(String doiString, String serviceID) throws MCRPersistentIdentifierException, MCRAccessException, MCRActiveLinkException {
    MCRDOIRegistrationService registrationService = new MCRDOIRegistrationService(serviceID);
    MCRDataciteClient dataciteClient = registrationService.getDataciteClient();
    MCRDigitalObjectIdentifier doi = new MCRDOIParser().parse(doiString).orElseThrow(() -> new MCRException("Invalid DOI: " + doiString));
    URI uri = dataciteClient.resolveDOI(doi);
    if (!uri.toString().startsWith(registrationService.getRegisterURL())) {
        LOGGER.info("DOI/URL is not from this application: {}/{}", doi.asString(), uri);
        return;
    }
    MCRObjectID objectID = getObjectID(uri);
    if (!MCRMetadataManager.exists(objectID)) {
        LOGGER.info("Could not find Object : {}", objectID);
        return;
    }
    MCRObject mcrObject = MCRMetadataManager.retrieveMCRObject(objectID);
    MCRPersistentIdentifierMetadataManager<MCRDigitalObjectIdentifier> synchronizer = registrationService.getMetadataManager();
    if (!registrationService.isRegistered(objectID, doiString)) {
        LOGGER.info("{} is not found in PI-Database. Insert it..", objectID);
        registrationService.insertIdentifierToDatabase(mcrObject, "", doi);
    }
    if (!synchronizer.getIdentifier(mcrObject, "").isPresent()) {
        LOGGER.info("Object doesn't have Identifier inscribed! Insert it..");
        synchronizer.insertIdentifier(doi, mcrObject, "");
        MCRMetadataManager.update(mcrObject);
    }
}
Also used : MCRDOIParser(org.mycore.pi.doi.MCRDOIParser) MCRException(org.mycore.common.MCRException) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRDigitalObjectIdentifier(org.mycore.pi.doi.MCRDigitalObjectIdentifier) MCRDOIRegistrationService(org.mycore.pi.doi.MCRDOIRegistrationService) MCRDataciteClient(org.mycore.pi.doi.MCRDataciteClient) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) URI(java.net.URI) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 99 with MCRException

use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.

the class MCRPIRegistrationService method updateFlag.

public void updateFlag(MCRObjectID id, String additional, MCRPI mcrpi) {
    MCRBase obj = MCRMetadataManager.retrieve(id);
    MCRObjectService service = obj.getService();
    ArrayList<String> flags = service.getFlags(MCRPIRegistrationService.PI_FLAG);
    Gson gson = getGson();
    String stringFlag = flags.stream().filter(_stringFlag -> {
        MCRPI flag = gson.fromJson(_stringFlag, MCRPI.class);
        return flag.getAdditional().equals(additional) && flag.getIdentifier().equals(mcrpi.getIdentifier());
    }).findAny().orElseThrow(() -> new MCRException(new MCRPersistentIdentifierException("Could find flag to update (" + id + "," + additional + "," + mcrpi.getIdentifier() + ")")));
    int flagIndex = service.getFlagIndex(stringFlag);
    service.removeFlag(flagIndex);
    addFlagToObject(obj, mcrpi);
    try {
        MCRMetadataManager.update(obj);
    } catch (Exception e) {
        throw new MCRException("Could not update flags of object " + id, e);
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRObjectService(org.mycore.datamodel.metadata.MCRObjectService) Gson(com.google.gson.Gson) MCRPI(org.mycore.pi.backend.MCRPI) MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRException(org.mycore.common.MCRException) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) MCRAccessException(org.mycore.access.MCRAccessException) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException)

Example 100 with MCRException

use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.

the class MCRPIRegistrationService method getMetadataManager.

public MCRPersistentIdentifierMetadataManager<T> getMetadataManager() {
    Map<String, String> properties = getProperties();
    MCRConfiguration configuration = MCRConfiguration.instance();
    String metadataManager;
    if (properties.containsKey(METADATA_MANAGER_PROPERTY_KEY)) {
        metadataManager = properties.get(METADATA_MANAGER_PROPERTY_KEY);
    } else if (properties.containsKey(METADATA_MANAGER_DEPRECATED_PROPERTY_KEY)) {
        LOGGER.warn(MessageFormat.format("You should use {0}{1}.{2} instead of {3}{4}.{5}", REGISTRATION_CONFIG_PREFIX, getRegistrationServiceID(), METADATA_MANAGER_PROPERTY_KEY, REGISTRATION_CONFIG_PREFIX, getRegistrationServiceID(), METADATA_MANAGER_DEPRECATED_PROPERTY_KEY));
        metadataManager = properties.get(METADATA_MANAGER_DEPRECATED_PROPERTY_KEY);
    } else {
        throw new MCRConfigurationException(getRegistrationServiceID() + " has no MetadataManager(or legacy Inscriber)!");
    }
    String className;
    String metadataManagerPropertyKey;
    metadataManagerPropertyKey = METADATA_MANAGER_CONFIG_PREFIX + metadataManager;
    className = configuration.getString(metadataManagerPropertyKey, null);
    if (className == null) {
        metadataManagerPropertyKey = METADATA_MANAGER_DEPRECATED_CONFIG_PREFIX + metadataManager;
        className = configuration.getString(metadataManagerPropertyKey, null);
        if (className == null) {
            throw new MCRConfigurationException("Missing property: " + METADATA_MANAGER_CONFIG_PREFIX + metadataManager + " or " + metadataManagerPropertyKey);
        }
        LOGGER.warn("You should use {} instead of {}", METADATA_MANAGER_CONFIG_PREFIX + metadataManager, METADATA_MANAGER_DEPRECATED_PROPERTY_KEY + metadataManager);
    }
    className = repairDeprecatedClassNames(className, metadataManagerPropertyKey);
    try {
        @SuppressWarnings("unchecked") Class<MCRPersistentIdentifierMetadataManager<T>> classObject = (Class<MCRPersistentIdentifierMetadataManager<T>>) Class.forName(className);
        Constructor<MCRPersistentIdentifierMetadataManager<T>> constructor = classObject.getConstructor(String.class);
        return constructor.newInstance(metadataManager);
    } catch (ClassNotFoundException e) {
        throw new MCRConfigurationException("Configurated class (" + metadataManagerPropertyKey + ") not found: " + className, e);
    } catch (NoSuchMethodException e) {
        throw new MCRConfigurationException("Configurated class (" + metadataManagerPropertyKey + ") needs a string constructor: " + className);
    } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
        throw new MCRException(e);
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MCRConfiguration(org.mycore.common.config.MCRConfiguration)

Aggregations

MCRException (org.mycore.common.MCRException)131 IOException (java.io.IOException)39 Element (org.jdom2.Element)26 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)19 Document (org.jdom2.Document)18 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)18 File (java.io.File)15 MCRConfigurationException (org.mycore.common.config.MCRConfigurationException)12 MCRObject (org.mycore.datamodel.metadata.MCRObject)12 ArrayList (java.util.ArrayList)11 JDOMException (org.jdom2.JDOMException)11 MCRAccessException (org.mycore.access.MCRAccessException)11 MCRPath (org.mycore.datamodel.niofs.MCRPath)10 SAXException (org.xml.sax.SAXException)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 List (java.util.List)7 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)7 SAXParseException (org.xml.sax.SAXParseException)7 URI (java.net.URI)6 Path (java.nio.file.Path)6