Search in sources :

Example 16 with MCRPersistentIdentifierException

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);
}
Also used : XPathFactory(org.jdom2.xpath.XPathFactory) Text(org.jdom2.Text) Document(org.jdom2.Document) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRPersistentIdentifier(org.mycore.pi.MCRPersistentIdentifier)

Example 17 with MCRPersistentIdentifierException

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);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) URL(java.net.URL)

Example 18 with MCRPersistentIdentifierException

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"));
        }
    });
}
Also used : MalformedURLException(java.net.MalformedURLException) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) URL(java.net.URL)

Example 19 with MCRPersistentIdentifierException

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;
}
Also used : MCRObjectDerivate(org.mycore.datamodel.metadata.MCRObjectDerivate) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRAccessException(org.mycore.access.MCRAccessException) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) IOException(java.io.IOException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException)

Example 20 with MCRPersistentIdentifierException

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;
}
Also used : MCRPI(org.mycore.pi.backend.MCRPI) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) Date(java.util.Date) 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