Search in sources :

Example 1 with MCRObjectDerivate

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

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

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

the class MCRUploadHandlerIFS method updateMainFile.

private void updateMainFile() throws IOException {
    String mainFile = derivate.getDerivate().getInternals().getMainDoc();
    MCRObjectDerivate der = MCRMetadataManager.retrieveMCRDerivate(getOrCreateDerivateID()).getDerivate();
    boolean hasNoMainFile = ((der.getInternals().getMainDoc() == null) || (der.getInternals().getMainDoc().trim().isEmpty()));
    if ((mainFile == null) || mainFile.trim().isEmpty() && hasNoMainFile) {
        mainFile = getPathOfMainFile();
        LOGGER.debug("Setting main file to {}", mainFile);
        derivate.getDerivate().getInternals().setMainDoc(mainFile);
        MCRMetadataManager.updateMCRDerivateXML(derivate);
    }
}
Also used : MCRObjectDerivate(org.mycore.datamodel.metadata.MCRObjectDerivate)

Example 4 with MCRObjectDerivate

use of org.mycore.datamodel.metadata.MCRObjectDerivate 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)

Example 5 with MCRObjectDerivate

use of org.mycore.datamodel.metadata.MCRObjectDerivate 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)

Aggregations

MCRObjectDerivate (org.mycore.datamodel.metadata.MCRObjectDerivate)5 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)4 IOException (java.io.IOException)3 MCRAccessException (org.mycore.access.MCRAccessException)3 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)3 MCRPersistentIdentifierException (org.mycore.pi.exceptions.MCRPersistentIdentifierException)3 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Date (java.util.Date)2 List (java.util.List)2 Predicate (java.util.function.Predicate)2 Pattern (java.util.regex.Pattern)2 Collectors (java.util.stream.Collectors)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 MCRBase (org.mycore.datamodel.metadata.MCRBase)2 MCRMetadataManager (org.mycore.datamodel.metadata.MCRMetadataManager)2 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)2