diff --git a/dspace-api/src/main/java/org/dspace/eperson/EPersonServiceImpl.java b/dspace-api/src/main/java/org/dspace/eperson/EPersonServiceImpl.java index d4f34344fb..846261d390 100644 --- a/dspace-api/src/main/java/org/dspace/eperson/EPersonServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/eperson/EPersonServiceImpl.java @@ -283,9 +283,7 @@ public class EPersonServiceImpl extends DSpaceObjectServiceImpl impleme for (Group group: workFlowGroups) { List ePeople = groupService.allMembers(context, group); if (ePeople.size() == 1 && ePeople.contains(ePerson)) { - throw new IllegalStateException( - "Refused to delete user " + ePerson.getID() + " because it the only member of the workflow group" - + group.getID() + ". Delete the tasks and group first if you want to remove this user."); + throw new EmptyWorkflowGroupException(ePerson.getID(), group.getID()); } } // check for presence of eperson in tables that diff --git a/dspace-api/src/main/java/org/dspace/eperson/EmptyWorkflowGroupException.java b/dspace-api/src/main/java/org/dspace/eperson/EmptyWorkflowGroupException.java new file mode 100644 index 0000000000..5105e54588 --- /dev/null +++ b/dspace-api/src/main/java/org/dspace/eperson/EmptyWorkflowGroupException.java @@ -0,0 +1,24 @@ +package org.dspace.eperson; + +import java.util.UUID; + +/** + *

This exception class is used to distinguish the following condition: + * EPerson cannot be deleted because that would lead to one (or more) + * workflow groups being empty.

+ * + *

The message of this exception can be disclosed in the REST response to + * provide more granular feedback to the user.

+ * + * @author Bruno Roemers (bruno.roemers at atmire.com) + */ +public class EmptyWorkflowGroupException extends IllegalStateException { + + public EmptyWorkflowGroupException(UUID ePersonId, UUID groupId) { + super(String.format( + "Refused to delete user %s because it is the only member of the workflow group %s. " + + "Delete the tasks and group first if you want to remove this user.", ePersonId, groupId + )); + } + +} diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/exception/DSpaceApiExceptionControllerAdvice.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/exception/DSpaceApiExceptionControllerAdvice.java index 04aa626153..29f0730efe 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/exception/DSpaceApiExceptionControllerAdvice.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/exception/DSpaceApiExceptionControllerAdvice.java @@ -98,6 +98,14 @@ public class DSpaceApiExceptionControllerAdvice extends ResponseEntityExceptionH HttpStatus.UNPROCESSABLE_ENTITY.value()); } + @ExceptionHandler(RESTEmptyWorkflowGroupException.class) + protected void handleEmptyWorkflowGroupException(HttpServletRequest request, HttpServletResponse response, + Exception ex) throws IOException { + sendErrorResponse( + request, response, null, ex.getLocalizedMessage(), HttpStatus.UNPROCESSABLE_ENTITY.value() + ); + } + @ExceptionHandler(QueryMethodParameterConversionException.class) protected void ParameterConversionException(HttpServletRequest request, HttpServletResponse response, Exception ex) throws IOException { diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/exception/RESTEmptyWorkflowGroupException.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/exception/RESTEmptyWorkflowGroupException.java new file mode 100644 index 0000000000..cdfb0d1622 --- /dev/null +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/exception/RESTEmptyWorkflowGroupException.java @@ -0,0 +1,29 @@ +/** + * The contents of this file are subject to the license and copyright + * detailed in the LICENSE and NOTICE files at the root of the source + * tree and available online at + * + * http://www.dspace.org/license/ + */ +package org.dspace.app.rest.exception; + +/** + *

Extend {@link UnprocessableEntityException} to provide a specific error message + * in the REST response. The error message is added to the response in + * {@link DSpaceApiExceptionControllerAdvice#handleEmptyWorkflowGroupException}.

+ * + *

Note there is a similarly named error in the DSpace API module.

+ * + * @author Bruno Roemers (bruno.roemers at atmire.com) + */ +public class RESTEmptyWorkflowGroupException extends UnprocessableEntityException { + + public RESTEmptyWorkflowGroupException(String message, Throwable cause) { + super(message, cause); + } + + public RESTEmptyWorkflowGroupException(String message) { + super(message); + } + +} diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/EPersonRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/EPersonRestRepository.java index b6c82221aa..1cba63dc90 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/EPersonRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/EPersonRestRepository.java @@ -22,6 +22,7 @@ import org.dspace.app.rest.Parameter; import org.dspace.app.rest.SearchRestMethod; import org.dspace.app.rest.authorization.AuthorizationFeatureService; import org.dspace.app.rest.exception.DSpaceBadRequestException; +import org.dspace.app.rest.exception.RESTEmptyWorkflowGroupException; import org.dspace.app.rest.exception.UnprocessableEntityException; import org.dspace.app.rest.model.EPersonRest; import org.dspace.app.rest.model.MetadataRest; @@ -34,6 +35,7 @@ import org.dspace.authorize.service.AuthorizeService; import org.dspace.content.service.SiteService; import org.dspace.core.Context; import org.dspace.eperson.EPerson; +import org.dspace.eperson.EmptyWorkflowGroupException; import org.dspace.eperson.RegistrationData; import org.dspace.eperson.service.AccountService; import org.dspace.eperson.service.EPersonService; @@ -313,8 +315,10 @@ public class EPersonRestRepository extends DSpaceObjectRestRepository