use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRXMLMetadataManager method update.
/**
* Updates metadata of existing MCRObject in the persistent store.
*
* @param mcrid the MCRObjectID
* @param xml the xml metadata of the MCRObject
* @param lastModified the date of last modification to set
* @return the stored metadata as IFS2 object
*/
public MCRStoredMetadata update(MCRObjectID mcrid, MCRContent xml, Date lastModified) throws IOException {
if (!exists(mcrid)) {
String msg = "Object to update does not exist: " + mcrid;
throw new MCRPersistenceException(msg);
}
MCRStoredMetadata sm = getStore(mcrid).retrieve(mcrid.getNumberAsInteger());
try {
sm.update(xml);
} catch (JDOMException e) {
throw new MCRPersistenceException("Error while updating object: " + mcrid, e);
}
sm.setLastModified(lastModified);
MCRConfiguration.instance().systemModified();
return sm;
}
use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRXMLMetadataManager method getStore.
/**
* Returns IFS2 MCRMetadataStore for the given project and object type
*
* @param project the project, e.g. DocPortal
* @param type the object type, e.g. document
*/
public MCRMetadataStore getStore(String project, String type) {
String projectType = getStoryKey(project, type);
String prefix = "MCR.IFS2.Store." + projectType + ".";
String forceXML = MCRConfiguration.instance().getString(prefix + "ForceXML", null);
if (forceXML == null) {
synchronized (this) {
forceXML = MCRConfiguration.instance().getString(prefix + "ForceXML", null);
if (forceXML == null) {
try {
setupStore(project, type, prefix);
} catch (ReflectiveOperationException e) {
throw new MCRPersistenceException(MessageFormat.format("Could not instantiate store for project {0} and object type {1}.", project, type), e);
}
}
}
}
MCRMetadataStore store = MCRStoreManager.getStore(projectType, MCRMetadataStore.class);
if (store == null) {
throw new MCRPersistenceException(MessageFormat.format("Metadata store for project {0} and object type {1} is unconfigured.", project, type));
}
return store;
}
use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRMetadataManager method delete.
/**
* Deletes the <code>mcrObject</code>.
*
* @param mcrObject
* the object to be deleted
* @param parentOperation
* function to handle the parent of the object @see {@link #removeChildObject(MCRObject, MCRObjectID)}
* @throws MCRPersistenceException
* if persistence problem occurs
* @throws MCRActiveLinkException
* object couldn't be deleted because its linked somewhere
* @throws MCRAccessException
* if delete permission is missing
*/
private static void delete(final MCRObject mcrObject, BiConsumer<MCRObject, MCRObjectID> parentOperation) throws MCRPersistenceException, MCRActiveLinkException, MCRAccessException {
MCRObjectID id = mcrObject.getId();
if (id == null) {
throw new MCRPersistenceException("The MCRObjectID is null.");
}
if (!MCRAccessManager.checkPermission(id, PERMISSION_DELETE)) {
throw MCRAccessException.missingPermission("Delete object", mcrObject.getId().toString(), PERMISSION_DELETE);
}
// check for active links
final Collection<String> sources = MCRLinkTableManager.instance().getSourceOf(mcrObject.mcr_id, MCRLinkTableManager.ENTRY_TYPE_REFERENCE);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Sources size:{}", sources.size());
}
if (sources.size() > 0) {
final MCRActiveLinkException activeLinks = new MCRActiveLinkException("Error while deleting object " + id + ". This object is still referenced by other objects and can not be removed until all links are released.");
for (final String curSource : sources) {
activeLinks.addLink(curSource, id.toString());
}
throw activeLinks;
}
// mark for deletion
MCRMarkManager.instance().mark(id, Operation.DELETE);
// remove child from parent
final MCRObjectID parentId = mcrObject.getStructure().getParentID();
if (parentId != null) {
parentOperation.accept(mcrObject, parentId);
}
// remove all children
for (MCRMetaLinkID child : mcrObject.getStructure().getChildren()) {
MCRObjectID childId = child.getXLinkHrefID();
if (!MCRMetadataManager.exists(childId)) {
LOGGER.warn("Unable to remove not existing object {} of parent {}", childId, id);
continue;
}
MCRMetadataManager.deleteMCRObject(childId, (MCRObject o, MCRObjectID p) -> {
// Do nothing with the parent, because its removed anyway.
});
}
// remove all derivates
for (MCRMetaLinkID derivate : mcrObject.getStructure().getDerivates()) {
MCRObjectID derivateId = derivate.getXLinkHrefID();
if (!MCRMetadataManager.exists(derivateId)) {
LOGGER.warn("Unable to remove not existing derivate {} of object {}", derivateId, id);
continue;
}
MCRMetadataManager.deleteMCRDerivate(derivateId);
}
// handle events
fireEvent(mcrObject, null, MCREvent.DELETE_EVENT);
// remove mark
MCRMarkManager.instance().remove(id);
}
use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRMetadataManager method delete.
/**
* Deletes MCRDerivate.
*
* @param mcrDerivate
* to be deleted
* @throws MCRPersistenceException
* if persistence problem occurs
* @throws MCRAccessException
* if delete permission is missing
*/
public static void delete(final MCRDerivate mcrDerivate) throws MCRPersistenceException, MCRAccessException {
MCRObjectID id = mcrDerivate.getId();
if (!MCRAccessManager.checkPermission(id, PERMISSION_DELETE)) {
throw MCRAccessException.missingPermission("Delete derivate", id.toString(), PERMISSION_DELETE);
}
// mark for deletion
MCRMarkManager.instance().mark(id, Operation.DELETE);
// remove link
MCRObjectID metaId = null;
try {
metaId = mcrDerivate.getDerivate().getMetaLink().getXLinkHrefID();
if (MCRMetadataManager.removeDerivateFromObject(metaId, id)) {
LOGGER.info(MessageFormat.format("Link in MCRObject {0} to MCRDerivate {1} is deleted.", metaId, id));
} else {
LOGGER.warn(MessageFormat.format("Link in MCRObject {0} to MCRDerivate {1} could not be deleted.", metaId, id));
}
} catch (final Exception e) {
LOGGER.warn("Can't delete link for MCRDerivate {} from MCRObject {}. Error ignored.", id, metaId);
}
// delete data from IFS
if (mcrDerivate.getDerivate().getInternals() != null) {
try {
deleteDerivate(id.toString());
LOGGER.info("IFS entries for MCRDerivate {} are deleted.", id);
} catch (final Exception e) {
throw new MCRPersistenceException("Error while delete MCRDerivate " + id + " in IFS", e);
}
}
// handle events
fireEvent(mcrDerivate, null, MCREvent.DELETE_EVENT);
// remove mark
MCRMarkManager.instance().remove(id);
}
use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRMetadataManager method create.
/**
* Stores the derivate.
*
* @param mcrDerivate
* derivate instance to store
* @throws MCRPersistenceException
* if a persistence problem is occurred
* @throws MCRAccessException
* if write permission to object is missing
*/
public static void create(final MCRDerivate mcrDerivate) throws MCRPersistenceException, MCRAccessException {
if (exists(mcrDerivate.getId())) {
throw new MCRPersistenceException("The derivate " + mcrDerivate.getId() + " already exists, nothing done.");
}
try {
mcrDerivate.validate();
} catch (MCRException exc) {
throw new MCRPersistenceException("The derivate " + mcrDerivate.getId() + " is not valid.", exc);
}
final MCRObjectID objid = mcrDerivate.getDerivate().getMetaLink().getXLinkHrefID();
if (!MCRAccessManager.checkPermission(objid, PERMISSION_WRITE)) {
throw MCRAccessException.missingPermission("Add derivate " + mcrDerivate.getId() + " to object.", objid.toString(), PERMISSION_WRITE);
}
byte[] objectBackup;
try {
objectBackup = MCRXMLMetadataManager.instance().retrieveBLOB(objid);
} catch (IOException ioExc) {
throw new MCRPersistenceException("Unable to retrieve xml blob of " + objid);
}
if (objectBackup == null) {
throw new MCRPersistenceException("Cannot find " + objid + " to attach derivate " + mcrDerivate.getId() + " to it.");
}
// prepare the derivate metadata and store under the XML table
if (mcrDerivate.getService().getDate("createdate") == null || !mcrDerivate.isImportMode()) {
mcrDerivate.getService().setDate("createdate");
}
if (mcrDerivate.getService().getDate("modifydate") == null || !mcrDerivate.isImportMode()) {
mcrDerivate.getService().setDate("modifydate");
}
// handle events
fireEvent(mcrDerivate, null, MCREvent.CREATE_EVENT);
// add the link to metadata
final MCRMetaLinkID der = new MCRMetaLinkID();
der.setReference(mcrDerivate.getId().toString(), null, mcrDerivate.getLabel());
der.setSubTag("derobject");
try {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("adding Derivate in data store");
}
MCRMetadataManager.addOrUpdateDerivateToObject(objid, der);
} catch (final Exception e) {
MCRMetadataManager.restore(mcrDerivate, objid, objectBackup);
// throw final exception
throw new MCRPersistenceException("Error while creatlink to MCRObject " + objid + ".", e);
}
// create data in IFS
if (mcrDerivate.getDerivate().getInternals() != null) {
MCRObjectID derId = mcrDerivate.getId();
MCRPath rootPath = MCRPath.getPath(derId.toString(), "/");
if (mcrDerivate.getDerivate().getInternals().getSourcePath() == null) {
try {
rootPath.getFileSystem().createRoot(rootPath.getOwner());
BasicFileAttributes attrs = Files.readAttributes(rootPath, BasicFileAttributes.class);
if (!(attrs.fileKey() instanceof String)) {
throw new MCRPersistenceException("Cannot get ID from newely created directory, as it is not a String." + rootPath);
}
mcrDerivate.getDerivate().getInternals().setIFSID(attrs.fileKey().toString());
} catch (IOException ioExc) {
throw new MCRPersistenceException("Cannot create root of '" + rootPath.getOwner() + "' or read the file attributes.", ioExc);
}
} else {
final String sourcepath = mcrDerivate.getDerivate().getInternals().getSourcePath();
final File f = new File(sourcepath);
if (f.exists()) {
try {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Starting File-Import");
}
importDerivate(derId.toString(), f.toPath());
BasicFileAttributes attrs = Files.readAttributes(rootPath, BasicFileAttributes.class);
if (!(attrs.fileKey() instanceof String)) {
throw new MCRPersistenceException("Cannot get ID from newely created directory, as it is not a String." + rootPath);
}
mcrDerivate.getDerivate().getInternals().setIFSID(attrs.fileKey().toString());
} catch (final Exception e) {
if (Files.exists(rootPath)) {
deleteDerivate(derId.toString());
}
MCRMetadataManager.restore(mcrDerivate, objid, objectBackup);
throw new MCRPersistenceException("Can't add derivate to the IFS", e);
}
} else {
LOGGER.warn("Empty derivate, the File or Directory -->{}<-- was not found.", sourcepath);
}
}
}
}
Aggregations