Search in sources :

Example 1 with MCRActiveLinkException

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);
}
Also used : MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Example 2 with MCRActiveLinkException

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
            }
        }
    }
}
Also used : MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID)

Example 3 with MCRActiveLinkException

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);
}
Also used : MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRHIBConnection(org.mycore.backend.hibernate.MCRHIBConnection) Test(org.junit.Test) Field(java.lang.reflect.Field) MCRPI(org.mycore.pi.backend.MCRPI) Stream(java.util.stream.Stream) Rule(org.junit.Rule) MCRAccessBaseImpl(org.mycore.access.MCRAccessBaseImpl) MCRStoreTestCase(org.mycore.common.MCRStoreTestCase) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) Map(java.util.Map) After(org.junit.After) MCRObject(org.mycore.datamodel.metadata.MCRObject) Optional(java.util.Optional) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) Assert(org.junit.Assert) MCRAccessException(org.mycore.access.MCRAccessException) TemporaryFolder(org.junit.rules.TemporaryFolder) Before(org.junit.Before) MCRObject(org.mycore.datamodel.metadata.MCRObject) Test(org.junit.Test)

Example 4 with MCRActiveLinkException

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();
}
Also used : MCRException(org.mycore.common.MCRException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) MCRAccessException(org.mycore.access.MCRAccessException) MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPersistentIdentifier(org.mycore.pi.MCRPersistentIdentifier) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 5 with MCRActiveLinkException

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);
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) SwordServerException(org.swordapp.server.SwordServerException) MCRAccessException(org.mycore.access.MCRAccessException) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRException(org.mycore.common.MCRException) SwordServerException(org.swordapp.server.SwordServerException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) MCRAccessException(org.mycore.access.MCRAccessException)

Aggregations

MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)8 MCRAccessException (org.mycore.access.MCRAccessException)5 MCRException (org.mycore.common.MCRException)4 IOException (java.io.IOException)3 MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 MCRHIBConnection (org.mycore.backend.hibernate.MCRHIBConnection)2 MCRPersistenceException (org.mycore.common.MCRPersistenceException)2 MCRObject (org.mycore.datamodel.metadata.MCRObject)2 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)2 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)2 MCRPersistentIdentifierException (org.mycore.pi.exceptions.MCRPersistentIdentifierException)2 SAXParseException (org.xml.sax.SAXParseException)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 Field (java.lang.reflect.Field)1 URL (java.net.URL)1 Paths (java.nio.file.Paths)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1