use of org.mycore.datamodel.common.MCRActiveLinkException 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.datamodel.common.MCRActiveLinkException in project mycore by MyCoRe-Org.
the class MCRObject method checkLinkTargets.
public void checkLinkTargets() {
for (int i = 0; i < getMetadata().size(); i++) {
MCRMetaElement elm = getMetadata().getMetadataElement(i);
for (int j = 0; j < elm.size(); j++) {
MCRMetaInterface inf = elm.getElement(j);
if (inf instanceof MCRMetaClassification) {
String classID = ((MCRMetaClassification) inf).getClassId();
String categID = ((MCRMetaClassification) inf).getCategId();
boolean exists = MCRCategoryDAOFactory.getInstance().exist(new MCRCategoryID(classID, categID));
if (exists) {
continue;
}
MCRActiveLinkException activeLink = new MCRActiveLinkException("Failure while adding link!. Destination does not exist.");
String destination = classID + "##" + categID;
activeLink.addLink(getId().toString(), destination);
// throw activeLink;
// TODO: should trigger undo-Event
}
if (inf instanceof MCRMetaLinkID) {
MCRObjectID destination = ((MCRMetaLinkID) inf).getXLinkHrefID();
if (MCRMetadataManager.exists(destination)) {
continue;
}
MCRActiveLinkException activeLink = new MCRActiveLinkException("Failure while adding link!. Destination does not exist.");
activeLink.addLink(getId().toString(), destination.toString());
// throw activeLink;
// TODO: should trigger undo-Event
}
}
}
}
use of org.mycore.datamodel.common.MCRActiveLinkException in project mycore by MyCoRe-Org.
the class MCRPersistentIdentifierManagerTest method testRegistrationService.
@Test
public void testRegistrationService() throws MCRAccessException, MCRActiveLinkException, MCRPersistentIdentifierException {
MockMetadataManager mockMetadataManager = new MockMetadataManager();
MCRPIRegistrationService<MCRMockIdentifier> registrationService = MCRPIRegistrationServiceManager.getInstance().getRegistrationService(MOCK_SERVICE);
MCRMockIdentifierRegistrationService casted = (MCRMockIdentifierRegistrationService) registrationService;
Assert.assertFalse("Delete should not have been called!", casted.isDeleteCalled());
Assert.assertFalse("Register should not have been called!", casted.isRegisterCalled());
Assert.assertFalse("Update should not have been called!", casted.isUpdatedCalled());
MCRObject mcrObject = buildMockObject();
mockMetadataManager.put(mcrObject.getId(), mcrObject);
MCRMockIdentifier identifier = registrationService.register(mcrObject, "", true);
Assert.assertFalse("Delete should not have been called!", casted.isDeleteCalled());
Assert.assertTrue("The identifier " + identifier.asString() + " should be registered now!", registrationService.isCreated(mcrObject.getId(), ""));
registrationService.onUpdate(identifier, mcrObject, "");
Assert.assertFalse("Delete should not have been called!", casted.isDeleteCalled());
Assert.assertTrue("The identifier " + identifier.asString() + " should have been updated!", casted.isUpdatedCalled());
registrationService.onDelete(identifier, mcrObject, "");
Assert.assertFalse("The identifier " + identifier.asString() + " should not be registered now!", registrationService.isCreated(mcrObject.getId(), ""));
Assert.assertTrue("There should be one resolver", MCRPersistentIdentifierManager.getInstance().getResolvers().stream().filter(r -> r.getName().equals(MCRMockResolver.NAME)).count() > 0);
}
use of org.mycore.datamodel.common.MCRActiveLinkException in project mycore by MyCoRe-Org.
the class MCRPersistentIdentifierRegistrationResource method register.
@POST
@Path("service/{serviceName}/{mycoreId}")
@Produces(MediaType.APPLICATION_JSON)
public Response register(@PathParam("serviceName") String serviceName, @PathParam("mycoreId") String mycoreId, @DefaultValue("") @QueryParam("additional") String additional) {
if (!MCRPIRegistrationServiceManager.getInstance().getServiceIDList().contains(serviceName)) {
return Response.status(Response.Status.BAD_REQUEST).entity(buildErrorJSON("No Registration Service found for " + serviceName)).build();
}
MCRPIRegistrationService<MCRPersistentIdentifier> registrationService = MCRPIRegistrationServiceManager.getInstance().getRegistrationService(serviceName);
MCRObjectID mycoreIDObject;
try {
mycoreIDObject = MCRObjectID.getInstance(mycoreId);
} catch (MCRException e) {
LOGGER.error(e);
return Response.status(Response.Status.BAD_REQUEST).entity(buildErrorJSON("The provided id " + mycoreId + " seems to be invalid!", e)).build();
}
MCRPersistentIdentifier identifier;
MCRBase object = MCRMetadataManager.retrieve(mycoreIDObject);
try {
identifier = registrationService.register(object, additional, true);
} catch (MCRPersistentIdentifierException | MCRActiveLinkException e) {
LOGGER.error(e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(buildErrorJSON("Error while register new identifier!", e)).build();
} catch (MCRAccessException e) {
LOGGER.error(e);
return Response.status(Response.Status.FORBIDDEN).entity(buildErrorJSON("Error while register new identifier!", e)).build();
}
return Response.status(Response.Status.CREATED).entity(buildIdentifierObject(identifier)).build();
}
use of org.mycore.datamodel.common.MCRActiveLinkException in project mycore by MyCoRe-Org.
the class MCRSwordContainerHandler method deleteObject.
public void deleteObject(MCRObject object) throws SwordServerException {
try {
object.getStructure().getDerivates().stream().map(MCRMetaLinkID::getXLinkHrefID).forEach(id -> {
try {
MCRMetadataManager.deleteMCRDerivate(id);
} catch (Exception e) {
throw new MCRException(e);
}
});
MCRMetadataManager.delete(object);
} catch (MCRActiveLinkException | MCRAccessException | MCRException e) {
Throwable ex = e;
if (e instanceof MCRException && Optional.ofNullable(e.getCause()).map(Object::getClass).filter(MCRAccessException.class::isAssignableFrom).isPresent()) {
// unwrapp
ex = e.getCause();
}
throw new SwordServerException("Error while deleting Object.", ex);
}
}
Aggregations