Search in sources :

Example 1 with MCRDOIRegistrationService

use of org.mycore.pi.doi.MCRDOIRegistrationService in project mycore by MyCoRe-Org.

the class MCRDOICommands method synchronizeDatabase.

@MCRCommand(syntax = "repair registered dois {0}", help = "Contacts the Registration Service and inserts all registered DOIs to the Database. It also updates all media files. The Service ID{0} is the id from the configuration.", order = 10)
public static void synchronizeDatabase(String serviceID) {
    MCRDOIRegistrationService registrationService = new MCRDOIRegistrationService(serviceID);
    try {
        MCRDataciteClient dataciteClient = registrationService.getDataciteClient();
        List<MCRDigitalObjectIdentifier> doiList = dataciteClient.getDOIList();
        doiList.stream().filter(doi -> {
            boolean isTestDOI = doi.getPrefix().equals(MCRDigitalObjectIdentifier.TEST_DOI_PREFIX);
            return !isTestDOI || registrationService.usesTestPrefix();
        }).forEach(doi -> {
            try {
                URI uri = dataciteClient.resolveDOI(doi);
                if (uri.toString().startsWith(registrationService.getRegisterURL())) {
                    LOGGER.info("Checking DOI: {}", doi.asString());
                    MCRObjectID objectID = getObjectID(uri);
                    if (MCRMetadataManager.exists(objectID)) {
                        if (!registrationService.isRegistered(objectID, "")) {
                            LOGGER.info("DOI is not registered in MyCoRe. Add to Database: {}", doi.asString());
                            MCRPI databaseEntry = new MCRPI(doi.asString(), registrationService.getType(), objectID.toString(), "", serviceID, new Date());
                            MCRHIBConnection.instance().getSession().save(databaseEntry);
                        }
                        // Update main files
                        MCRObject obj = MCRMetadataManager.retrieveMCRObject(objectID);
                        List<Map.Entry<String, URI>> entryList = registrationService.getMediaList(obj);
                        dataciteClient.setMediaList(doi, entryList);
                    } else {
                        LOGGER.info("Could not find Object : {}", objectID);
                    }
                } else {
                    LOGGER.info("DOI/URL is not from this application: {}/{}", doi.asString(), uri);
                }
            } catch (MCRPersistentIdentifierException e) {
                LOGGER.error("Error occurred for DOI: {}", doi, e);
            }
        });
    } catch (MCRPersistentIdentifierException e) {
        LOGGER.error("Error while receiving DOI list from Registration-Service!", e);
    }
}
Also used : MCRDOIParser(org.mycore.pi.doi.MCRDOIParser) Date(java.util.Date) HashMap(java.util.HashMap) MCRException(org.mycore.common.MCRException) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) MCRPI(org.mycore.pi.backend.MCRPI) MCRIdentifierUnresolvableException(org.mycore.pi.exceptions.MCRIdentifierUnresolvableException) Map(java.util.Map) URI(java.net.URI) MCRCommandGroup(org.mycore.frontend.cli.annotation.MCRCommandGroup) MCRAccessException(org.mycore.access.MCRAccessException) MCRDataciteClient(org.mycore.pi.doi.MCRDataciteClient) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRHIBConnection(org.mycore.backend.hibernate.MCRHIBConnection) Collectors(java.util.stream.Collectors) MCRDOIRegistrationService(org.mycore.pi.doi.MCRDOIRegistrationService) List(java.util.List) Logger(org.apache.logging.log4j.Logger) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRDigitalObjectIdentifier(org.mycore.pi.doi.MCRDigitalObjectIdentifier) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) MCRPersistentIdentifierMetadataManager(org.mycore.pi.MCRPersistentIdentifierMetadataManager) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) MCRPI(org.mycore.pi.backend.MCRPI) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) URI(java.net.URI) Date(java.util.Date) 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 2 with MCRDOIRegistrationService

use of org.mycore.pi.doi.MCRDOIRegistrationService in project mycore by MyCoRe-Org.

the class MCRDOICommands method updateMediaListOfAllDOI.

@MCRCommand(syntax = "repair media list of {0}", help = "Sends new media lists to Datacite. The Service ID{0} is the id from the configuration.")
public static List<String> updateMediaListOfAllDOI(String serviceID) {
    MCRDOIRegistrationService registrationService = new MCRDOIRegistrationService(serviceID);
    try {
        MCRDataciteClient dataciteClient = registrationService.getDataciteClient();
        List<MCRDigitalObjectIdentifier> doiList = dataciteClient.getDOIList();
        return doiList.stream().filter(doi -> {
            boolean isTestDOI = doi.getPrefix().equals(MCRDigitalObjectIdentifier.TEST_DOI_PREFIX);
            return !isTestDOI || registrationService.usesTestPrefix();
        }).map(MCRDigitalObjectIdentifier::asString).map(doiStr -> MessageFormat.format(REPAIR_MEDIALIST_OF_0_AND_SERVICE_1, doiStr, serviceID)).collect(Collectors.toList());
    } catch (MCRPersistentIdentifierException e) {
        LOGGER.error("Error while receiving DOI list from Registration-Service!", e);
    }
    return Collections.emptyList();
}
Also used : MCRDOIParser(org.mycore.pi.doi.MCRDOIParser) Date(java.util.Date) HashMap(java.util.HashMap) MCRException(org.mycore.common.MCRException) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) MCRPI(org.mycore.pi.backend.MCRPI) MCRIdentifierUnresolvableException(org.mycore.pi.exceptions.MCRIdentifierUnresolvableException) Map(java.util.Map) URI(java.net.URI) MCRCommandGroup(org.mycore.frontend.cli.annotation.MCRCommandGroup) MCRAccessException(org.mycore.access.MCRAccessException) MCRDataciteClient(org.mycore.pi.doi.MCRDataciteClient) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRHIBConnection(org.mycore.backend.hibernate.MCRHIBConnection) Collectors(java.util.stream.Collectors) MCRDOIRegistrationService(org.mycore.pi.doi.MCRDOIRegistrationService) List(java.util.List) Logger(org.apache.logging.log4j.Logger) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRDigitalObjectIdentifier(org.mycore.pi.doi.MCRDigitalObjectIdentifier) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) MCRPersistentIdentifierMetadataManager(org.mycore.pi.MCRPersistentIdentifierMetadataManager) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) MCRDigitalObjectIdentifier(org.mycore.pi.doi.MCRDigitalObjectIdentifier) MCRDOIRegistrationService(org.mycore.pi.doi.MCRDOIRegistrationService) MCRDataciteClient(org.mycore.pi.doi.MCRDataciteClient) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 3 with MCRDOIRegistrationService

use of org.mycore.pi.doi.MCRDOIRegistrationService 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 4 with MCRDOIRegistrationService

use of org.mycore.pi.doi.MCRDOIRegistrationService 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)

Aggregations

URI (java.net.URI)4 MCRException (org.mycore.common.MCRException)4 MCRObject (org.mycore.datamodel.metadata.MCRObject)4 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)4 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)4 MCRDOIParser (org.mycore.pi.doi.MCRDOIParser)4 MCRDOIRegistrationService (org.mycore.pi.doi.MCRDOIRegistrationService)4 MCRDataciteClient (org.mycore.pi.doi.MCRDataciteClient)4 MCRDigitalObjectIdentifier (org.mycore.pi.doi.MCRDigitalObjectIdentifier)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 MCRIdentifierUnresolvableException (org.mycore.pi.exceptions.MCRIdentifierUnresolvableException)3 MCRPersistentIdentifierException (org.mycore.pi.exceptions.MCRPersistentIdentifierException)3 MessageFormat (java.text.MessageFormat)2 Collections (java.util.Collections)2 Date (java.util.Date)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 LogManager (org.apache.logging.log4j.LogManager)2