Search in sources :

Example 1 with MCRFileCollectingFileVisitor

use of org.mycore.pi.MCRFileCollectingFileVisitor 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)

Aggregations

IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Date (java.util.Date)1 List (java.util.List)1 Predicate (java.util.function.Predicate)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 Session (org.hibernate.Session)1 MCRAccessException (org.mycore.access.MCRAccessException)1 MCRHIBConnection (org.mycore.backend.hibernate.MCRHIBConnection)1 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)1 MCRBase (org.mycore.datamodel.metadata.MCRBase)1 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)1 MCRMetadataManager (org.mycore.datamodel.metadata.MCRMetadataManager)1 MCRObjectDerivate (org.mycore.datamodel.metadata.MCRObjectDerivate)1