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