Search in sources :

Example 11 with MCRMetaLinkID

use of org.mycore.datamodel.metadata.MCRMetaLinkID in project mycore by MyCoRe-Org.

the class MCRDefaultMetadataShareAgent method distributeMetadata.

/* (non-Javadoc)
     * @see org.mycore.datamodel.metadata.share.MCRMetadataShareAgent#inheritMetadata(org.mycore.datamodel.metadata.MCRObject)
     */
@Override
public void distributeMetadata(MCRObject parent) throws MCRPersistenceException, MCRAccessException {
    for (MCRMetaLinkID childId : parent.getStructure().getChildren()) {
        LOGGER.debug("Update metadata from Child {}", childId);
        final MCRObject child = MCRMetadataManager.retrieveMCRObject(childId.getXLinkHrefID());
        MCRMetadataManager.update(child);
    }
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID)

Example 12 with MCRMetaLinkID

use of org.mycore.datamodel.metadata.MCRMetaLinkID 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)

Example 13 with MCRMetaLinkID

use of org.mycore.datamodel.metadata.MCRMetaLinkID in project mycore by MyCoRe-Org.

the class MCRObjectCommands method replaceParent.

/**
 * Moves object to new parent.
 *
 * @param sourceId
 *            object that should be attached to new parent
 * @param newParentId
 *            the ID of the new parent
 * @throws MCRAccessException see {@link MCRMetadataManager#update(MCRObject)}
 */
@MCRCommand(syntax = "set parent of {0} to {1}", help = "replaces a parent of an object (first parameter) to the given new one (second parameter)", order = 300)
public static void replaceParent(String sourceId, String newParentId) throws MCRPersistenceException, MCRActiveLinkException, MCRAccessException {
    // child
    MCRObject sourceMCRObject = MCRMetadataManager.retrieveMCRObject(MCRObjectID.getInstance(sourceId));
    // old parent
    MCRObjectID oldParentId = sourceMCRObject.getStructure().getParentID();
    MCRObjectID newParentObjectID = MCRObjectID.getInstance(newParentId);
    if (newParentObjectID.equals(oldParentId)) {
        LOGGER.info("Object {} is already child of {}", sourceId, newParentId);
        return;
    }
    MCRObject oldParentMCRObject = null;
    if (oldParentId != null) {
        try {
            oldParentMCRObject = MCRMetadataManager.retrieveMCRObject(oldParentId);
        } catch (Exception exc) {
            LOGGER.error("Unable to get old parent object {}, its probably deleted.", oldParentId, exc);
        }
    }
    // change href to new parent
    LOGGER.info("Setting link in \"{}\" to parent \"{}\"", sourceId, newParentObjectID);
    MCRMetaLinkID parentLinkId = new MCRMetaLinkID("parent", 0);
    parentLinkId.setReference(newParentObjectID, null, null);
    sourceMCRObject.getStructure().setParent(parentLinkId);
    if (oldParentMCRObject != null) {
        // remove Child in old parent
        LOGGER.info("Remove child \"{}\" in old parent \"{}\"", sourceId, oldParentId);
        oldParentMCRObject.getStructure().removeChild(sourceMCRObject.getId());
        LOGGER.info("Update old parent \"{}\n", oldParentId);
        MCRMetadataManager.update(oldParentMCRObject);
    }
    LOGGER.info("Update \"{}\" in datastore (saving new link)", sourceId);
    MCRMetadataManager.update(sourceMCRObject);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Structure: {}", sourceMCRObject.getStructure().isValid());
        LOGGER.debug("Object: {}", sourceMCRObject.isValid());
    }
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) URISyntaxException(java.net.URISyntaxException) JDOMException(org.jdom2.JDOMException) MCRPersistenceException(org.mycore.common.MCRPersistenceException) SAXException(org.xml.sax.SAXException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) TransformerException(javax.xml.transform.TransformerException) MCRException(org.mycore.common.MCRException) MCRAccessException(org.mycore.access.MCRAccessException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 14 with MCRMetaLinkID

use of org.mycore.datamodel.metadata.MCRMetaLinkID in project mycore by MyCoRe-Org.

the class MCRUploadHandlerIFS method createDerivate.

private MCRDerivate createDerivate(MCRObjectID derivateID) throws MCRPersistenceException, IOException, MCRAccessException {
    MCRDerivate derivate = new MCRDerivate();
    derivate.setId(derivateID);
    derivate.setLabel("data object from " + documentID);
    String schema = CONFIG.getString("MCR.Metadata.Config.derivate", "datamodel-derivate.xml").replaceAll(".xml", ".xsd");
    derivate.setSchema(schema);
    MCRMetaLinkID linkId = new MCRMetaLinkID();
    linkId.setSubTag("linkmeta");
    linkId.setReference(documentID, null, null);
    derivate.getDerivate().setLinkMeta(linkId);
    MCRMetaIFS ifs = new MCRMetaIFS();
    ifs.setSubTag("internal");
    ifs.setSourcePath(null);
    derivate.getDerivate().setInternals(ifs);
    LOGGER.debug("Creating new derivate with ID {}", this.derivateID);
    MCRMetadataManager.create(derivate);
    setDefaultPermissions(derivateID);
    return derivate;
}
Also used : MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRMetaIFS(org.mycore.datamodel.metadata.MCRMetaIFS)

Example 15 with MCRMetaLinkID

use of org.mycore.datamodel.metadata.MCRMetaLinkID in project mycore by MyCoRe-Org.

the class MCRSwordUtil method createDerivate.

public static MCRDerivate createDerivate(String documentID) throws MCRPersistenceException, IOException, MCRAccessException {
    final String projectId = MCRObjectID.getInstance(documentID).getProjectId();
    MCRObjectID oid = MCRObjectID.getNextFreeId(projectId, "derivate");
    final String derivateID = oid.toString();
    MCRDerivate derivate = new MCRDerivate();
    derivate.setId(oid);
    derivate.setLabel("data object from " + documentID);
    String schema = CONFIG.getString("MCR.Metadata.Config.derivate", "datamodel-derivate.xml").replaceAll(".xml", ".xsd");
    derivate.setSchema(schema);
    MCRMetaLinkID linkId = new MCRMetaLinkID();
    linkId.setSubTag("linkmeta");
    linkId.setReference(documentID, null, null);
    derivate.getDerivate().setLinkMeta(linkId);
    MCRMetaIFS ifs = new MCRMetaIFS();
    ifs.setSubTag("internal");
    ifs.setSourcePath(null);
    derivate.getDerivate().setInternals(ifs);
    LOGGER.debug("Creating new derivate with ID {}", derivateID);
    MCRMetadataManager.create(derivate);
    if (CONFIG.getBoolean("MCR.Access.AddDerivateDefaultRule", true)) {
        MCRAccessInterface AI = MCRAccessManager.getAccessImpl();
        Collection<String> configuredPermissions = AI.getAccessPermissionsFromConfiguration();
        for (String permission : configuredPermissions) {
            MCRAccessManager.addRule(derivateID, permission, MCRAccessManager.getTrueRule(), "default derivate rule");
        }
    }
    final MCRPath rootDir = MCRPath.getPath(derivateID, "/");
    if (Files.notExists(rootDir)) {
        rootDir.getFileSystem().createRoot(derivateID);
    }
    return derivate;
}
Also used : MCRAccessInterface(org.mycore.access.MCRAccessInterface) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRMetaIFS(org.mycore.datamodel.metadata.MCRMetaIFS) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Aggregations

MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)22 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)15 MCRObject (org.mycore.datamodel.metadata.MCRObject)13 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)10 MCRAccessException (org.mycore.access.MCRAccessException)7 IOException (java.io.IOException)6 MCRPersistenceException (org.mycore.common.MCRPersistenceException)6 MCRMetaIFS (org.mycore.datamodel.metadata.MCRMetaIFS)6 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)5 Element (org.jdom2.Element)4 MCRException (org.mycore.common.MCRException)4 TransformerException (javax.xml.transform.TransformerException)3 JDOMException (org.jdom2.JDOMException)3 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)3 MCRPath (org.mycore.datamodel.niofs.MCRPath)3 MCRRestAPIException (org.mycore.restapi.v1.errors.MCRRestAPIException)3 SAXException (org.xml.sax.SAXException)3 SAXParseException (org.xml.sax.SAXParseException)3 FileNotFoundException (java.io.FileNotFoundException)2 Document (org.jdom2.Document)2