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;
}
Aggregations