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);
}
}
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);
}
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);
}
}
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();
}
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;
}
Aggregations