Search in sources :

Example 1 with MCRPersistentIdentifierException

use of org.mycore.pi.exceptions.MCRPersistentIdentifierException in project mycore by MyCoRe-Org.

the class MCRURNGranularRESTRegistrationService method createNewURN.

public MCRDNBURN createNewURN(MCRDerivate deriv) {
    MCRObjectID derivID = deriv.getId();
    try {
        MCRDNBURN derivURN = getNewIdentifier(derivID, "");
        deriv.getDerivate().setURN(derivURN.asString());
        persistURNStr(deriv, new Date()).accept(derivURN::asString, "");
        if (Boolean.valueOf(getProperties().getOrDefault("supportDfgViewerURN", "false"))) {
            String suffix = "dfg";
            persistURNStr(deriv, null, getRegistrationServiceID() + "-" + suffix).accept(() -> derivURN.withNamespaceSuffix(suffix + "-").asString(), "");
        }
        return derivURN;
    } catch (MCRPersistentIdentifierException e) {
        throw new MCRPICreationException("Could not create new URN for " + derivID, e);
    }
}
Also used : MCRDNBURN(org.mycore.pi.urn.MCRDNBURN) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) Date(java.util.Date)

Example 2 with MCRPersistentIdentifierException

use of org.mycore.pi.exceptions.MCRPersistentIdentifierException in project mycore by MyCoRe-Org.

the class MCRPersistentIdentifierManagerTest method testRegistrationService.

@Test
public void testRegistrationService() throws MCRAccessException, MCRActiveLinkException, MCRPersistentIdentifierException {
    MockMetadataManager mockMetadataManager = new MockMetadataManager();
    MCRPIRegistrationService<MCRMockIdentifier> registrationService = MCRPIRegistrationServiceManager.getInstance().getRegistrationService(MOCK_SERVICE);
    MCRMockIdentifierRegistrationService casted = (MCRMockIdentifierRegistrationService) registrationService;
    Assert.assertFalse("Delete should not have been called!", casted.isDeleteCalled());
    Assert.assertFalse("Register should not have been called!", casted.isRegisterCalled());
    Assert.assertFalse("Update should not have been called!", casted.isUpdatedCalled());
    MCRObject mcrObject = buildMockObject();
    mockMetadataManager.put(mcrObject.getId(), mcrObject);
    MCRMockIdentifier identifier = registrationService.register(mcrObject, "", true);
    Assert.assertFalse("Delete should not have been called!", casted.isDeleteCalled());
    Assert.assertTrue("The identifier " + identifier.asString() + " should be registered now!", registrationService.isCreated(mcrObject.getId(), ""));
    registrationService.onUpdate(identifier, mcrObject, "");
    Assert.assertFalse("Delete should not have been called!", casted.isDeleteCalled());
    Assert.assertTrue("The identifier " + identifier.asString() + " should have been updated!", casted.isUpdatedCalled());
    registrationService.onDelete(identifier, mcrObject, "");
    Assert.assertFalse("The identifier " + identifier.asString() + " should not be registered now!", registrationService.isCreated(mcrObject.getId(), ""));
    Assert.assertTrue("There should be one resolver", MCRPersistentIdentifierManager.getInstance().getResolvers().stream().filter(r -> r.getName().equals(MCRMockResolver.NAME)).count() > 0);
}
Also used : MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRHIBConnection(org.mycore.backend.hibernate.MCRHIBConnection) Test(org.junit.Test) Field(java.lang.reflect.Field) MCRPI(org.mycore.pi.backend.MCRPI) Stream(java.util.stream.Stream) Rule(org.junit.Rule) MCRAccessBaseImpl(org.mycore.access.MCRAccessBaseImpl) MCRStoreTestCase(org.mycore.common.MCRStoreTestCase) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) Map(java.util.Map) After(org.junit.After) MCRObject(org.mycore.datamodel.metadata.MCRObject) Optional(java.util.Optional) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) Assert(org.junit.Assert) MCRAccessException(org.mycore.access.MCRAccessException) TemporaryFolder(org.junit.rules.TemporaryFolder) Before(org.junit.Before) MCRObject(org.mycore.datamodel.metadata.MCRObject) Test(org.junit.Test)

Example 3 with MCRPersistentIdentifierException

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

use of org.mycore.pi.exceptions.MCRPersistentIdentifierException 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 5 with MCRPersistentIdentifierException

use of org.mycore.pi.exceptions.MCRPersistentIdentifierException in project mycore by MyCoRe-Org.

the class MCRURNGranularOAIRegistrationService method registerURNsDerivate.

private MCRDNBURN registerURNsDerivate(MCRBase obj, String additional, MCRObjectDerivate derivate) throws MCRPersistentIdentifierException {
    LOGGER.info("Add URNs to all files of {}", obj.getId());
    Session session = MCRHIBConnection.instance().getSession();
    Path path = MCRPath.getPath(obj.getId().toString(), "/");
    MCRFileCollectingFileVisitor<Path> collectingFileVisitor = new MCRFileCollectingFileVisitor<>();
    try {
        Files.walkFileTree(path, collectingFileVisitor);
    } catch (IOException e) {
        throw new MCRPersistentIdentifierException("Could not walk derivate file tree!", e);
    }
    List<String> ignoreFileNamesList = getIgnoreFileList();
    List<Predicate<String>> predicateList = ignoreFileNamesList.stream().map(Pattern::compile).map(Pattern::asPredicate).collect(Collectors.toList());
    List<MCRPath> pathList = collectingFileVisitor.getPaths().stream().filter(file -> predicateList.stream().noneMatch(p -> p.test(file.toString().split(":")[1]))).map(p -> (MCRPath) p).sorted().collect(Collectors.toList());
    MCRDNBURN newURN = getNewIdentifier(obj.getId(), additional);
    String setID = obj.getId().getNumberAsString();
    for (int pathListIndex = 0; pathListIndex < pathList.size(); pathListIndex++) {
        MCRDNBURN subURN = newURN.toGranular(setID, pathListIndex + 1, pathList.size());
        derivate.getOrCreateFileMetadata(pathList.get(pathListIndex), subURN.asString()).setUrn(subURN.asString());
        MCRPI databaseEntry = new MCRPI(subURN.asString(), getType(), obj.getId().toString(), pathList.get(pathListIndex).getOwnerRelativePath(), this.getRegistrationServiceID(), null);
        session.save(databaseEntry);
    }
    derivate.setURN(newURN.asString());
    MCRPI databaseEntry = new MCRPI(newURN.asString(), getType(), obj.getId().toString(), "", this.getRegistrationServiceID(), new Date());
    session.save(databaseEntry);
    return newURN;
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRBase(org.mycore.datamodel.metadata.MCRBase) Arrays(java.util.Arrays) Date(java.util.Date) Session(org.hibernate.Session) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) ArrayList(java.util.ArrayList) MCRPI(org.mycore.pi.backend.MCRPI) MCRFileCollectingFileVisitor(org.mycore.pi.MCRFileCollectingFileVisitor) MCRAccessException(org.mycore.access.MCRAccessException) Path(java.nio.file.Path) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) MCRObjectDerivate(org.mycore.datamodel.metadata.MCRObjectDerivate) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRHIBConnection(org.mycore.backend.hibernate.MCRHIBConnection) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) MCRPIRegistrationService(org.mycore.pi.MCRPIRegistrationService) List(java.util.List) Logger(org.apache.logging.log4j.Logger) MCRPersistentIdentifierManager(org.mycore.pi.MCRPersistentIdentifierManager) Pattern(java.util.regex.Pattern) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) LogManager(org.apache.logging.log4j.LogManager) Pattern(java.util.regex.Pattern) MCRPI(org.mycore.pi.backend.MCRPI) IOException(java.io.IOException) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) Date(java.util.Date) Predicate(java.util.function.Predicate) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRFileCollectingFileVisitor(org.mycore.pi.MCRFileCollectingFileVisitor) Session(org.hibernate.Session)

Aggregations

MCRPersistentIdentifierException (org.mycore.pi.exceptions.MCRPersistentIdentifierException)20 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)11 MCRPI (org.mycore.pi.backend.MCRPI)8 MCRAccessException (org.mycore.access.MCRAccessException)7 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)7 MCRBase (org.mycore.datamodel.metadata.MCRBase)7 MCRException (org.mycore.common.MCRException)6 MCRObject (org.mycore.datamodel.metadata.MCRObject)6 Date (java.util.Date)5 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)5 URI (java.net.URI)4 ArrayList (java.util.ArrayList)4 LogManager (org.apache.logging.log4j.LogManager)4 Logger (org.apache.logging.log4j.Logger)4 MCRHIBConnection (org.mycore.backend.hibernate.MCRHIBConnection)4 MCRMetadataManager (org.mycore.datamodel.metadata.MCRMetadataManager)4 MCRPersistentIdentifier (org.mycore.pi.MCRPersistentIdentifier)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 List (java.util.List)3