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