use of org.mycore.pi.exceptions.MCRPersistentIdentifierException in project mycore by MyCoRe-Org.
the class MCRURNObjectXPathMetadataManager method getIdentifier.
@Override
public Optional<MCRPersistentIdentifier> getIdentifier(MCRBase obj, String additional) throws MCRPersistentIdentifierException {
String xpath = getProperties().get("Xpath");
Document xml = obj.createXML();
XPathFactory xpfac = XPathFactory.instance();
XPathExpression<Text> xp = xpfac.compile(xpath, Filters.text());
List<Text> evaluate = xp.evaluate(xml);
if (evaluate.size() > 1) {
throw new MCRPersistentIdentifierException("Got " + evaluate.size() + " matches for " + obj.getId() + " with xpath " + xpath + "");
}
if (evaluate.size() == 0) {
return Optional.empty();
}
Text identifierText = evaluate.listIterator().next();
String identifierString = identifierText.getTextNormalize();
Optional<MCRDNBURN> parsedIdentifierOptional = PARSER.parse(identifierString);
return parsedIdentifierOptional.map(MCRPersistentIdentifier.class::cast);
}
use of org.mycore.pi.exceptions.MCRPersistentIdentifierException in project mycore by MyCoRe-Org.
the class MCRIDPURLGenerator method generate.
@Override
public MCRPersistentUniformResourceLocator generate(MCRObjectID mcrID, String additional) throws MCRPersistentIdentifierException {
String baseUrlTemplate = getProperties().getOrDefault("BaseURLTemplate", "");
String replace = baseUrlTemplate;
String before;
do {
before = replace;
replace = baseUrlTemplate.replace("$ID", mcrID.toString());
} while (!replace.equals(before));
try {
URL url = new URL(replace);
return new MCRPersistentUniformResourceLocator(url);
} catch (MalformedURLException e) {
throw new MCRPersistentIdentifierException("Error while creating URL object from string: " + replace, e);
}
}
use of org.mycore.pi.exceptions.MCRPersistentIdentifierException in project mycore by MyCoRe-Org.
the class MCRPURLJobRegistrationService method updateJob.
@Override
public void updateJob(Map<String, String> parameters) throws MCRPersistentIdentifierException {
String purlString = parameters.get(CONTEXT_PURL);
String objId = parameters.get(CONTEXT_OBJECT);
validateJobUserRights(MCRObjectID.getInstance(objId));
MCRPersistentUniformResourceLocator purl;
try {
purl = new MCRPersistentUniformResourceLocator(new URL(purlString));
} catch (MalformedURLException e) {
throw new MCRPersistentIdentifierException("Could not parse purl: " + purlString, e);
}
doWithPURLManager((purlManager) -> {
if (!purlManager.isPURLTargetURLUnchanged(purl.getUrl().toString(), buildTargetURL(objId))) {
purlManager.updateExistingPURL(purl.getUrl().getPath(), buildTargetURL(objId), "302", getProperties().getOrDefault(PURL_MAINTAINER_CONFIG, "test"));
}
});
}
use of org.mycore.pi.exceptions.MCRPersistentIdentifierException in project mycore by MyCoRe-Org.
the class MCRURNGranularOAIRegistrationService method register.
@Override
public MCRDNBURN register(MCRBase obj, String additional, boolean updateObject) throws MCRAccessException, MCRActiveLinkException, MCRPersistentIdentifierException {
this.validateRegistration(obj, additional);
MCRObjectDerivate derivate = ((MCRDerivate) obj).getDerivate();
MCRDNBURN newURN;
if (additional.equals("")) {
/* Multiple URN for entire Derivate... */
newURN = registerURNsDerivate(obj, additional, derivate);
} else {
/* Single URN to one File... */
newURN = registerSingleURN(obj, additional, derivate);
}
try {
MCRMetadataManager.update(obj);
} catch (Exception e) {
throw new MCRPersistentIdentifierException("Error while updating derivate " + obj.getId(), e);
}
return newURN;
}
use of org.mycore.pi.exceptions.MCRPersistentIdentifierException in project mycore by MyCoRe-Org.
the class MCRURNGranularOAIRegistrationService method registerSingleURN.
private MCRDNBURN registerSingleURN(MCRBase obj, String additional, MCRObjectDerivate derivate) throws MCRPersistentIdentifierException {
MCRDNBURN newURN;
LOGGER.info("Add single urn to {} / {}", obj.getId(), additional);
Session session = MCRHIBConnection.instance().getSession();
MCRPath filePath;
if (!Files.exists(filePath = MCRPath.getPath(obj.getId().toString(), additional))) {
throw new MCRPersistentIdentifierException("Invalid path : " + additional);
}
int count = Math.toIntExact(derivate.getFileMetadata().stream().filter(file -> file.getUrn() != null).count());
MCRDNBURN dnbURN = newURN = (MCRDNBURN) MCRPersistentIdentifierManager.getInstance().get(derivate.getURN()).findFirst().get();
String setID = obj.getId().getNumberAsString();
MCRDNBURN urntoAssign = dnbURN.toGranular(setID, count + 1, count + 1);
derivate.getOrCreateFileMetadata(filePath, urntoAssign.asString()).setUrn(urntoAssign.asString());
MCRPI databaseEntry = new MCRPI(urntoAssign.asString(), getType(), obj.getId().toString(), additional, this.getRegistrationServiceID(), new Date());
session.save(databaseEntry);
return newURN;
}
Aggregations