Search in sources :

Example 21 with MCRObjectID

use of org.mycore.datamodel.metadata.MCRObjectID in project mycore by MyCoRe-Org.

the class MCRDerivateURNUtils method getDFGViewerURL.

public static URL getDFGViewerURL(MCRPIRegistrationInfo urn) {
    URL url = null;
    try {
        MCRObjectID derivateId = MCRObjectID.getInstance(urn.getMycoreID());
        MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(derivateId);
        String mainDoc = Optional.ofNullable(derivate.getDerivate()).map(MCRObjectDerivate::getInternals).map(MCRMetaIFS::getMainDoc).orElseThrow(() -> new RuntimeException("Could not get main doc for " + derivateId));
        String spec = null;
        String baseURL = MCRFrontendUtil.getBaseURL();
        String id = URLEncoder.encode(derivateId.toString(), "UTF-8");
        if (mainDoc != null && mainDoc.length() > 0) {
            String mainDocEnc = URLEncoder.encode(mainDoc, "UTF-8");
            spec = MessageFormat.format(baseURL + "servlets/MCRDFGLinkServlet?deriv={0}&file={1}", id, mainDocEnc);
        } else {
            spec = baseURL + "servlets/MCRDFGLinkServlet?deriv=" + id;
        }
        LOGGER.debug("Generated URL for urn {} is {}", urn.getIdentifier(), spec);
        url = new URL(spec);
    } catch (UnsupportedEncodingException | MalformedURLException e) {
        LOGGER.error("Could not create dfg viewer url", e);
    }
    return url;
}
Also used : MalformedURLException(java.net.MalformedURLException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectDerivate(org.mycore.datamodel.metadata.MCRObjectDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) URL(java.net.URL)

Example 22 with MCRObjectID

use of org.mycore.datamodel.metadata.MCRObjectID 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 23 with MCRObjectID

use of org.mycore.datamodel.metadata.MCRObjectID in project mycore by MyCoRe-Org.

the class MCRURNGranularRESTRegistrationService method registerURN.

private MCRDNBURN registerURN(MCRDerivate deriv, String filePath) {
    MCRObjectID derivID = deriv.getId();
    Function<String, Integer> countCreatedPI = s -> MCRPersistentIdentifierManager.getInstance().getCreatedIdentifiers(derivID, getType(), getRegistrationServiceID()).size();
    int seed = Optional.of(filePath).filter(p -> !"".equals(p)).map(countCreatedPI).map(count -> count + 1).orElse(1);
    MCRDNBURN derivURN = Optional.ofNullable(deriv.getDerivate()).map(MCRObjectDerivate::getURN).flatMap(new MCRDNBURNParser()::parse).orElseGet(() -> createNewURN(deriv));
    String setID = derivID.getNumberAsString();
    GranularURNGenerator granularURNGen = new GranularURNGenerator(seed, derivURN, setID);
    Function<MCRPath, Supplier<String>> generateURN = p -> granularURNGen.getURNSupplier();
    derivateFileStream.apply(deriv).filter(notInIgnoreList().and(matchFile(filePath))).sorted().collect(Collectors.toMap(generateURN, p -> p, (m1, m2) -> m1, LinkedHashMap::new)).forEach(createFileMetadata(deriv).andThen(persistURN(deriv)));
    try {
        MCRMetadataManager.update(deriv);
    } catch (MCRPersistenceException | MCRAccessException e) {
        LOGGER.error("Error while updating derivate {}", derivID, e);
    }
    EntityTransaction transaction = MCREntityManagerProvider.getCurrentEntityManager().getTransaction();
    if (!transaction.isActive()) {
        transaction.begin();
    }
    transaction.commit();
    return derivURN;
}
Also used : MCRBase(org.mycore.datamodel.metadata.MCRBase) Arrays(java.util.Arrays) Date(java.util.Date) Function(java.util.function.Function) Supplier(java.util.function.Supplier) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) ArrayList(java.util.ArrayList) MCRDNBURN(org.mycore.pi.urn.MCRDNBURN) LinkedHashMap(java.util.LinkedHashMap) MCRPI(org.mycore.pi.backend.MCRPI) Locale(java.util.Locale) BiConsumer(java.util.function.BiConsumer) 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) MCRPersistenceException(org.mycore.common.MCRPersistenceException) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) MCREntityManagerProvider(org.mycore.backend.jpa.MCREntityManagerProvider) MCRPIRegistrationService(org.mycore.pi.MCRPIRegistrationService) List(java.util.List) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) EntityTransaction(javax.persistence.EntityTransaction) Optional(java.util.Optional) MCRPersistentIdentifierManager(org.mycore.pi.MCRPersistentIdentifierManager) Pattern(java.util.regex.Pattern) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) MCRDNBURNParser(org.mycore.pi.urn.MCRDNBURNParser) LogManager(org.apache.logging.log4j.LogManager) EntityTransaction(javax.persistence.EntityTransaction) MCRDNBURN(org.mycore.pi.urn.MCRDNBURN) MCRAccessException(org.mycore.access.MCRAccessException) MCRObjectDerivate(org.mycore.datamodel.metadata.MCRObjectDerivate) MCRDNBURNParser(org.mycore.pi.urn.MCRDNBURNParser) LinkedHashMap(java.util.LinkedHashMap) Supplier(java.util.function.Supplier) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Example 24 with MCRObjectID

use of org.mycore.datamodel.metadata.MCRObjectID in project mycore by MyCoRe-Org.

the class MCRURNGranularRESTRegistrationService method defaultDerivateFileStream.

private static Stream<MCRPath> defaultDerivateFileStream(MCRDerivate derivate) {
    MCRObjectID derivateId = derivate.getId();
    Path derivRoot = MCRPath.getPath(derivateId.toString(), "/");
    try {
        return Files.walk(derivRoot).map(MCRPath::toMCRPath).filter(p -> !Files.isDirectory(p)).filter(p -> !p.equals(derivRoot));
    } catch (IOException e) {
        LOGGER.error("I/O error while access the starting file of derivate {}!", derivateId, e);
    } catch (SecurityException s) {
        LOGGER.error("No access to starting file of derivate {}!", derivateId, s);
    }
    return Stream.empty();
}
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) Function(java.util.function.Function) Supplier(java.util.function.Supplier) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) ArrayList(java.util.ArrayList) MCRDNBURN(org.mycore.pi.urn.MCRDNBURN) LinkedHashMap(java.util.LinkedHashMap) MCRPI(org.mycore.pi.backend.MCRPI) Locale(java.util.Locale) BiConsumer(java.util.function.BiConsumer) 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) MCRPersistenceException(org.mycore.common.MCRPersistenceException) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) MCREntityManagerProvider(org.mycore.backend.jpa.MCREntityManagerProvider) MCRPIRegistrationService(org.mycore.pi.MCRPIRegistrationService) List(java.util.List) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) EntityTransaction(javax.persistence.EntityTransaction) Optional(java.util.Optional) MCRPersistentIdentifierManager(org.mycore.pi.MCRPersistentIdentifierManager) Pattern(java.util.regex.Pattern) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) MCRDNBURNParser(org.mycore.pi.urn.MCRDNBURNParser) LogManager(org.apache.logging.log4j.LogManager) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 25 with MCRObjectID

use of org.mycore.datamodel.metadata.MCRObjectID in project mycore by MyCoRe-Org.

the class MCRPersistentIdentifierManagerTest method buildMockObject.

private MCRObject buildMockObject() {
    MCRObject mcrObject = new MCRObject();
    MCRObjectID id = MCRObjectID.getNextFreeId("test", "mock");
    mcrObject.setId(id);
    mcrObject.setSchema("http://www.w3.org/2001/XMLSchema");
    return mcrObject;
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

Aggregations

MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)144 IOException (java.io.IOException)37 MCRObject (org.mycore.datamodel.metadata.MCRObject)32 MCRException (org.mycore.common.MCRException)30 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)30 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)29 MCRPath (org.mycore.datamodel.niofs.MCRPath)25 MCRAccessException (org.mycore.access.MCRAccessException)22 Document (org.jdom2.Document)20 MCRPersistenceException (org.mycore.common.MCRPersistenceException)18 MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)16 JDOMException (org.jdom2.JDOMException)15 MCRBase (org.mycore.datamodel.metadata.MCRBase)15 SAXException (org.xml.sax.SAXException)15 Date (java.util.Date)14 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)13 MCRSession (org.mycore.common.MCRSession)11 MCRContent (org.mycore.common.content.MCRContent)11 MCRPersistentIdentifierException (org.mycore.pi.exceptions.MCRPersistentIdentifierException)11 URISyntaxException (java.net.URISyntaxException)10