Search in sources :

Example 61 with MCRPath

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

the class MCRMetadataManager method update.

/**
 * Updates the derivate or creates it if it does not exist yet.
 *
 * @throws MCRPersistenceException
 *                if a persistence problem is occurred
 * @throws MCRAccessException
 *                if write permission to object or derivate is missing
 */
public static void update(final MCRDerivate mcrDerivate) throws MCRPersistenceException, MCRAccessException {
    MCRObjectID id = mcrDerivate.getId();
    // check deletion mark
    if (MCRMarkManager.instance().isMarkedForDeletion(id)) {
        return;
    }
    if (!MCRMetadataManager.exists(id)) {
        MCRMetadataManager.create(mcrDerivate);
        return;
    }
    if (!MCRAccessManager.checkPermission(id, PERMISSION_WRITE)) {
        throw MCRAccessException.missingPermission("Update derivate", id.toString(), PERMISSION_WRITE);
    }
    File fileSourceDirectory = null;
    if (mcrDerivate.getDerivate().getInternals() != null && mcrDerivate.getDerivate().getInternals().getSourcePath() != null) {
        fileSourceDirectory = new File(mcrDerivate.getDerivate().getInternals().getSourcePath());
        if (!fileSourceDirectory.exists()) {
            LOGGER.warn("{}: the directory {} was not found.", id, fileSourceDirectory);
            fileSourceDirectory = null;
        }
    }
    // get the old Item
    MCRDerivate old = MCRMetadataManager.retrieveMCRDerivate(id);
    // remove the old link to metadata
    MCRMetaLinkID oldLink = old.getDerivate().getMetaLink();
    MCRMetaLinkID newLink = mcrDerivate.getDerivate().getMetaLink();
    if (!oldLink.equals(newLink)) {
        MCRObjectID oldMetadataObjectID = oldLink.getXLinkHrefID();
        MCRObjectID newMetadataObjectID = newLink.getXLinkHrefID();
        if (!oldMetadataObjectID.equals(newLink.getXLinkHrefID())) {
            try {
                MCRMetadataManager.removeDerivateFromObject(oldMetadataObjectID, id);
            } catch (final MCRException e) {
                LOGGER.warn(e.getMessage(), e);
            }
        }
        // add the link to metadata
        final MCRMetaLinkID der = new MCRMetaLinkID("derobject", id, null, mcrDerivate.getLabel(), newLink.getXLinkRole());
        addOrUpdateDerivateToObject(newMetadataObjectID, der);
    }
    // update the derivate
    mcrDerivate.getService().setDate("createdate", old.getService().getDate("createdate"));
    if (!mcrDerivate.getService().isFlagTypeSet(MCRObjectService.FLAG_TYPE_CREATEDBY)) {
        for (String flagCreatedBy : old.getService().getFlags(MCRObjectService.FLAG_TYPE_CREATEDBY)) {
            mcrDerivate.getService().addFlag(MCRObjectService.FLAG_TYPE_CREATEDBY, flagCreatedBy);
        }
    }
    MCRMetadataManager.updateMCRDerivateXML(mcrDerivate);
    // update to IFS
    if (fileSourceDirectory != null) {
        Path sourcePath = fileSourceDirectory.toPath();
        MCRPath targetPath = MCRPath.getPath(id.toString(), "/");
        try {
            Files.walkFileTree(sourcePath, new MCRTreeCopier(sourcePath, targetPath));
        } catch (Exception exc) {
            throw new MCRPersistenceException("Unable to update IFS. Copy failed from " + sourcePath.toAbsolutePath() + " to target " + targetPath.toAbsolutePath(), exc);
        }
    }
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRTreeCopier(org.mycore.datamodel.niofs.utils.MCRTreeCopier) MCRException(org.mycore.common.MCRException) MCRPath(org.mycore.datamodel.niofs.MCRPath) File(java.io.File) MCRPersistenceException(org.mycore.common.MCRPersistenceException) MCRException(org.mycore.common.MCRException) JDOMException(org.jdom2.JDOMException) MCRAccessException(org.mycore.access.MCRAccessException) MCRPersistenceException(org.mycore.common.MCRPersistenceException) IOException(java.io.IOException) PersistenceException(javax.persistence.PersistenceException) SAXException(org.xml.sax.SAXException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException)

Example 62 with MCRPath

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

the class MCRMetadataManager method deleteDerivate.

private static void deleteDerivate(String derivateID) throws MCRPersistenceException {
    try {
        MCRPath rootPath = MCRPath.getPath(derivateID, "/");
        if (!Files.exists(rootPath)) {
            LOGGER.info("Derivate does not exist: {}", derivateID);
            return;
        }
        Files.walkFileTree(rootPath, MCRRecursiveDeleter.instance());
        rootPath.getFileSystem().removeRoot(derivateID);
    } catch (Exception exc) {
        throw new MCRPersistenceException("Unable to delete derivate " + derivateID, exc);
    }
}
Also used : MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRPersistenceException(org.mycore.common.MCRPersistenceException) MCRException(org.mycore.common.MCRException) JDOMException(org.jdom2.JDOMException) MCRAccessException(org.mycore.access.MCRAccessException) MCRPersistenceException(org.mycore.common.MCRPersistenceException) IOException(java.io.IOException) PersistenceException(javax.persistence.PersistenceException) SAXException(org.xml.sax.SAXException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException)

Example 63 with MCRPath

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

the class MCRMetadataManager method importDerivate.

private static void importDerivate(String derivateID, Path sourceDir) throws MCRPersistenceException {
    try {
        MCRPath rootPath = MCRPath.getPath(derivateID, "/");
        if (Files.exists(rootPath)) {
            LOGGER.info("Derivate does already exist: {}", derivateID);
        }
        rootPath.getFileSystem().createRoot(derivateID);
        Files.walkFileTree(sourceDir, new MCRTreeCopier(sourceDir, rootPath));
    } catch (Exception exc) {
        throw new MCRPersistenceException("Unable to import derivate " + derivateID + " from source " + sourceDir.toAbsolutePath(), exc);
    }
}
Also used : MCRTreeCopier(org.mycore.datamodel.niofs.utils.MCRTreeCopier) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRPersistenceException(org.mycore.common.MCRPersistenceException) MCRException(org.mycore.common.MCRException) JDOMException(org.jdom2.JDOMException) MCRAccessException(org.mycore.access.MCRAccessException) MCRPersistenceException(org.mycore.common.MCRPersistenceException) IOException(java.io.IOException) PersistenceException(javax.persistence.PersistenceException) SAXException(org.xml.sax.SAXException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException)

Example 64 with MCRPath

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

the class MCRFileMetaEventHandler method handleDerivateCreated.

@Override
protected void handleDerivateCreated(MCREvent evt, MCRDerivate der) {
    MCRObjectID derivateID = der.getId();
    MCRObjectDerivate objectDerivate = der.getDerivate();
    List<MCRFileMetadata> fileMetadata = objectDerivate.getFileMetadata();
    for (MCRFileMetadata metadata : fileMetadata) {
        Collection<MCRCategoryID> categories = metadata.getCategories();
        if (!categories.isEmpty()) {
            MCRPath path = MCRPath.getPath(derivateID.toString(), metadata.getName());
            MCRCategLinkReference linkReference = new MCRCategLinkReference(path);
            CATEGLINK_SERVICE.setLinks(linkReference, categories);
        }
    }
}
Also used : MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRCategLinkReference(org.mycore.datamodel.classifications2.MCRCategLinkReference)

Example 65 with MCRPath

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

the class MCRBasketPersistence method updateBasket.

/**
 * Updates the basket's data in the persistent store by saving its XML representation
 * to a file in a derivate. The ID of the derivate is given in the basket's properties.
 */
public static void updateBasket(MCRBasket basket) throws Exception {
    String derivateID = basket.getDerivateID();
    MCRObjectID derivateOID = MCRObjectID.getInstance(derivateID);
    MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(derivateOID);
    MCRPath file = getBasketFile(derivateID);
    writeBasketToFile(basket, derivate, file);
}
Also used : MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath)

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