Search in sources :

Example 11 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRSolrPathDocumentFactory method getDocument.

/**
 * Generates a {@link SolrInputDocument} from a {@link MCRPath} instance.
 *
 * @see MCRSolrFileIndexHandler
 * @see MCRSolrFilesIndexHandler
 * @see MCRSolrIndexHandlerFactory
 */
public SolrInputDocument getDocument(Path input, BasicFileAttributes attr) throws IOException, MCRPersistenceException {
    SolrInputDocument doc = new SolrInputDocument();
    Consumer<? super MCRSolrFileIndexAccumulator> accumulate = (accumulator) -> {
        LOGGER.debug("{} accumulates {}", accumulator, input);
        try {
            accumulator.accumulate(doc, input, attr);
        } catch (IOException e) {
            LOGGER.error("Error in Accumulator!", e);
        }
    };
    ACCUMULATOR_LIST.forEach(accumulate);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("MCRFile {} transformed to:\n{}", input, doc);
    }
    return doc;
}
Also used : MCRSolrFilesIndexHandler(org.mycore.solr.index.handlers.stream.MCRSolrFilesIndexHandler) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRPersistenceException(org.mycore.common.MCRPersistenceException) IOException(java.io.IOException) MCRConfiguration(org.mycore.common.config.MCRConfiguration) MCRSolrFileIndexHandler(org.mycore.solr.index.handlers.stream.MCRSolrFileIndexHandler) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Collectors(java.util.stream.Collectors) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) Consumer(java.util.function.Consumer) List(java.util.List) Logger(org.apache.logging.log4j.Logger) CONFIG_PREFIX(org.mycore.solr.MCRSolrConstants.CONFIG_PREFIX) MCRSolrIndexHandlerFactory(org.mycore.solr.index.handlers.MCRSolrIndexHandlerFactory) Path(java.nio.file.Path) LogManager(org.apache.logging.log4j.LogManager) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrInputDocument(org.apache.solr.common.SolrInputDocument) IOException(java.io.IOException)

Example 12 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRDerivateURNUtils method getURL.

public static URL getURL(MCRPIRegistrationInfo piInfo) {
    String derivateID = piInfo.getMycoreID();
    if (piInfo.getService().endsWith("-dfg")) {
        return getDFGViewerURL(piInfo);
    }
    try {
        // the base urn, links to frontpage (metadata + viewer)
        if (piInfo.getAdditional() == null || piInfo.getAdditional().trim().length() == 0) {
            MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derivateID));
            return new URL(MCRFrontendUtil.getBaseURL() + "receive/" + derivate.getOwnerID() + "?derivate=" + derivateID);
        } else // an urn for a certain file, links to iview2
        {
            MCRPath file = MCRPath.getPath(derivateID, piInfo.getAdditional());
            if (!Files.exists(file)) {
                LOGGER.warn("File {} in object {} could NOT be found", file.getFileName().toString(), derivateID);
                return null;
            }
            if (!isFileSupported(file)) {
                LOGGER.info("File is not displayable within iView2. Use {} as url", MCRFileNodeServlet.class.getSimpleName());
                String filePath = "/" + file.getOwner() + "/" + file.getFileName();
                return new URL(MCRFrontendUtil.getBaseURL() + "servlets/" + MCRFileNodeServlet.class.getSimpleName() + filePath);
            }
            return new URL(getViewerURL(file));
        }
    } catch (MalformedURLException e) {
        LOGGER.error("Malformed URL for URN {}", piInfo.getIdentifier(), e);
    }
    return null;
}
Also used : MalformedURLException(java.net.MalformedURLException) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRFileNodeServlet(org.mycore.datamodel.ifs.MCRFileNodeServlet) URL(java.net.URL)

Example 13 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath 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 14 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath 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 15 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRDerivateCommands method exportDerivate.

/**
 * @param dir
 * @param trans
 * @param nid
 * @throws FileNotFoundException
 * @throws TransformerException
 * @throws IOException
 */
private static void exportDerivate(File dir, Transformer trans, String nid) throws TransformerException, IOException {
    // store the XML file
    Document xml = null;
    MCRDerivate obj;
    MCRObjectID derivateID = MCRObjectID.getInstance(nid);
    try {
        obj = MCRMetadataManager.retrieveMCRDerivate(derivateID);
        String path = obj.getDerivate().getInternals().getSourcePath();
        // reset from the absolute to relative path, for later reload
        LOGGER.info("Old Internal Path ====>{}", path);
        obj.getDerivate().getInternals().setSourcePath(nid);
        LOGGER.info("New Internal Path ====>{}", nid);
        // add ACL's
        Collection<String> l = ACCESS_IMPL.getPermissionsForID(nid);
        for (String permission : l) {
            Element rule = ACCESS_IMPL.getRule(nid, permission);
            obj.getService().addRule(permission, rule);
        }
        // build JDOM
        xml = obj.createXML();
    } catch (MCRException ex) {
        LOGGER.warn("Could not read {}, continue with next ID", nid);
        return;
    }
    File xmlOutput = new File(dir, derivateID + ".xml");
    FileOutputStream out = new FileOutputStream(xmlOutput);
    dir = new File(dir, derivateID.toString());
    if (trans != null) {
        trans.setParameter("dirname", dir.getPath());
        StreamResult sr = new StreamResult(out);
        trans.transform(new org.jdom2.transform.JDOMSource(xml), sr);
    } else {
        new org.jdom2.output.XMLOutputter().output(xml, out);
        out.flush();
        out.close();
    }
    LOGGER.info("Object {} stored under {}.", nid, xmlOutput);
    // store the derivate file under dirname
    if (!dir.isDirectory()) {
        dir.mkdir();
    }
    MCRPath rootPath = MCRPath.getPath(derivateID.toString(), "/");
    Files.walkFileTree(rootPath, new MCRTreeCopier(rootPath, dir.toPath()));
    LOGGER.info("Derivate {} saved under {} and {}.", nid, dir, xmlOutput);
}
Also used : MCRTreeCopier(org.mycore.datamodel.niofs.utils.MCRTreeCopier) MCRException(org.mycore.common.MCRException) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.jdom2.Element) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) Document(org.jdom2.Document) FileOutputStream(java.io.FileOutputStream) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath) File(java.io.File)

Aggregations

MCRPath (org.mycore.datamodel.niofs.MCRPath)96 IOException (java.io.IOException)49 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)26 Path (java.nio.file.Path)25 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)22 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)22 Document (org.jdom2.Document)15 JDOMException (org.jdom2.JDOMException)15 MCRPersistenceException (org.mycore.common.MCRPersistenceException)14 MCRException (org.mycore.common.MCRException)13 MCRDirectory (org.mycore.datamodel.ifs.MCRDirectory)13 MCRAccessException (org.mycore.access.MCRAccessException)12 Files (java.nio.file.Files)11 Collectors (java.util.stream.Collectors)11 LogManager (org.apache.logging.log4j.LogManager)11 Logger (org.apache.logging.log4j.Logger)11 FileVisitResult (java.nio.file.FileVisitResult)10 NoSuchFileException (java.nio.file.NoSuchFileException)10 Date (java.util.Date)10 List (java.util.List)10