Search in sources :

Example 1 with MCRTreeCopier

use of org.mycore.datamodel.niofs.utils.MCRTreeCopier 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)

Example 2 with MCRTreeCopier

use of org.mycore.datamodel.niofs.utils.MCRTreeCopier 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 3 with MCRTreeCopier

use of org.mycore.datamodel.niofs.utils.MCRTreeCopier 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 4 with MCRTreeCopier

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

the class MCRObjectCommands method mergeDerivatesOfObject.

@MCRCommand(syntax = "merge derivates of object {0}", help = "Retrieves the MCRObject with the MCRObjectID {0} and if it has more then one MCRDerivate, then all" + " Files will be copied to the first Derivate and all other will be deleted.", order = 190)
public static void mergeDerivatesOfObject(String id) {
    MCRObjectID objectID = MCRObjectID.getInstance(id);
    if (!MCRMetadataManager.exists(objectID)) {
        LOGGER.error("The object with the id {} does not exist!", id);
        return;
    }
    MCRObject object = MCRMetadataManager.retrieveMCRObject(objectID);
    List<MCRMetaLinkID> derivateLinkIDs = object.getStructure().getDerivates();
    List<MCRObjectID> derivateIDs = derivateLinkIDs.stream().map(MCRMetaLinkID::getXLinkHrefID).collect(Collectors.toList());
    if (derivateIDs.size() <= 1) {
        LOGGER.error("The object with the id {} has no Derivates to merge!", id);
        return;
    }
    String mainID = derivateIDs.get(0).toString();
    MCRPath mainDerivateRootPath = MCRPath.getPath(mainID, "/");
    derivateIDs.stream().skip(1).forEach(derivateID -> {
        LOGGER.info("Merge {} into {}...", derivateID, mainID);
        MCRPath copyRootPath = MCRPath.getPath(derivateID.toString(), "/");
        try {
            MCRTreeCopier treeCopier = new MCRTreeCopier(copyRootPath, mainDerivateRootPath);
            Files.walkFileTree(copyRootPath, treeCopier);
            Files.walkFileTree(copyRootPath, MCRRecursiveDeleter.instance());
            MCRMetadataManager.deleteMCRDerivate(derivateID);
        } catch (IOException | MCRAccessException e) {
            throw new MCRException(e);
        }
    });
}
Also used : MCRTreeCopier(org.mycore.datamodel.niofs.utils.MCRTreeCopier) MCRException(org.mycore.common.MCRException) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRAccessException(org.mycore.access.MCRAccessException) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Aggregations

MCRException (org.mycore.common.MCRException)4 MCRPath (org.mycore.datamodel.niofs.MCRPath)4 MCRTreeCopier (org.mycore.datamodel.niofs.utils.MCRTreeCopier)4 IOException (java.io.IOException)3 MCRAccessException (org.mycore.access.MCRAccessException)3 File (java.io.File)2 PersistenceException (javax.persistence.PersistenceException)2 JDOMException (org.jdom2.JDOMException)2 MCRPersistenceException (org.mycore.common.MCRPersistenceException)2 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)2 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)2 SAXException (org.xml.sax.SAXException)2 FileOutputStream (java.io.FileOutputStream)1 Path (java.nio.file.Path)1 StreamResult (javax.xml.transform.stream.StreamResult)1 Document (org.jdom2.Document)1 Element (org.jdom2.Element)1 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)1 MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)1 MCRObject (org.mycore.datamodel.metadata.MCRObject)1