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