Skip to content
Snippets Groups Projects
Commit 2ce79a0d authored by Hugues.Cazeaux's avatar Hugues.Cazeaux Committed by Nicolas.Rod
Browse files

feat: improve bulk delete (deleteList) when there are errors

parent 9dd3d40d
No related branches found
No related tags found
1 merge request!417feat: improve buld delete (deleteList) when there are errors
......@@ -25,6 +25,7 @@ package ch.unige.solidify.controller;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import java.util.HashMap;
import java.util.Map;
import org.springframework.hateoas.RepresentationModel;
......@@ -38,6 +39,7 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import ch.unige.solidify.SolidifyConstants;
import ch.unige.solidify.exception.SolidifyBulkActionException;
import ch.unige.solidify.exception.SolidifyRuntimeException;
import ch.unige.solidify.exception.SolidifyUndeletableException;
import ch.unige.solidify.exception.SolidifyUnmodifiableException;
......@@ -76,15 +78,24 @@ public abstract class ResourceController<T extends Resource> extends ResourceRea
@DeleteMapping
public ResponseEntity<Void> deleteList(@RequestBody String[] ids) {
Map<String, String> errorList = new HashMap<>();
// Remove each item
for (final String id : ids) {
final T t = this.itemService.findOne(id);
if (!t.isDeletable()) {
throw new SolidifyUndeletableException(
this.messageService.get("validation.resource.undeletable",
new Object[] { t.getClass().getSimpleName() + ": " + id }));
try {
final T t = this.itemService.findOne(id);
if (!t.isDeletable()) {
throw new SolidifyUndeletableException(
this.messageService.get("validation.resource.undeletable",
new Object[] { t.getClass().getSimpleName() + ": " + id }));
}
this.itemService.delete(id);
} catch (RuntimeException e) {
errorList.put(id, e.getMessage());
}
this.itemService.delete(id);
}
// Check if there is error
if (!errorList.isEmpty()) {
throw new SolidifyBulkActionException(this.messageService.get("validation.resource.bulkdelete"), errorList);
}
return new ResponseEntity<>(HttpStatus.OK);
}
......
......@@ -28,6 +28,9 @@ import static ch.unige.solidify.controller.SolidifyControllerAdvice.StackTraceDi
import java.util.NoSuchElementException;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
......@@ -57,10 +60,9 @@ import org.springframework.web.servlet.resource.NoResourceFoundException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import ch.unige.solidify.config.SolidifyProperties;
import ch.unige.solidify.exception.SolidifyBulkActionException;
import ch.unige.solidify.exception.SolidifyError;
import ch.unige.solidify.exception.SolidifyHttpErrorException;
import ch.unige.solidify.exception.SolidifyMfaNeededException;
......@@ -140,7 +142,7 @@ public class SolidifyControllerAdvice {
* Used to catch all exceptions not specifically catched by another ExceptionHandler
*
* @param exception
* @param request @returnlogException(exception, request)
* @param request @returnlogException(exception, request)
*/
@ExceptionHandler(Exception.class)
public ResponseEntity<SolidifyError> handleException(Exception exception, HttpServletRequest request) {
......@@ -227,6 +229,12 @@ public class SolidifyControllerAdvice {
this.badRequestStacktrace);
}
@ExceptionHandler({ SolidifyBulkActionException.class })
public ResponseEntity<SolidifyError> handleBulkActionException(SolidifyBulkActionException exception, HttpServletRequest request) {
return this.buildResponse(exception, request, exception.getDetailedMessage(), HttpStatus.BAD_REQUEST,
this.badRequestStacktrace);
}
@ExceptionHandler(HttpClientErrorException.class)
public ResponseEntity<SolidifyError> handleHttpClientErrorException(HttpClientErrorException exception, HttpServletRequest request) {
return this.buildResponse(exception, request, exception.getMessage(), exception.getStatusCode(), WITHOUT_STACK_TRACE);
......@@ -321,7 +329,7 @@ public class SolidifyControllerAdvice {
log.error(message, request.getMethod(), request.getRequestURL(), request.getRemoteAddr(), headerXFF, exception);
} else if (stackTraceDisplay == WITHOUT_STACK_TRACE) {
message += " {}";
log.info(message, request.getMethod(), request.getRequestURL(), request.getRemoteAddr(), headerXFF, exception.toString());
log.info(message, request.getMethod(), request.getRequestURL(), request.getRemoteAddr(), headerXFF, exception);
}
}
......
......@@ -7,6 +7,10 @@ message.validator.assert.another.field.has.value=Validation error: field {0} can
##############
# Validation #
##############
validation.resource.alreadyexist=The resource identified by ''{0}'' already exists
validation.resource.unmodifiable=The resource identified by ''{0}'' is not modifiable
validation.resource.undeletable=The resource identified by ''{0}'' is not deletable
validation.resource.bulkdelete=Some errors occurred during bulk deletion
validation.value.mustBeGreaterThanZero=The value must be greater than 0
validation.indexFieldAlias.facetValue.mandatory=The value is mandatory if the item is a facet
validation.indexFieldAlias.facetDefaultVisibleValues.largerThanLimit=The number of default visible values cannot be greater than the limit number
......
......@@ -50,6 +50,7 @@ public class SolidifyError implements Serializable {
private HttpStatus status;
private int statusCode;
private String timeStamp;
@JsonIgnore
private ValidationError validationError;
......@@ -153,9 +154,9 @@ public class SolidifyError implements Serializable {
}
private HttpStatus getStatus(Object status) {
if (status instanceof Integer) {
if (status instanceof Integer integer) {
try {
return HttpStatus.valueOf((Integer) status);
return HttpStatus.valueOf(integer);
} catch (final IllegalArgumentException e) {
// Unknown status code
return HttpStatus.INTERNAL_SERVER_ERROR;
......
/*-
* %%----------------------------------------------------------------------------------------------
* Solidify Framework - Solidify Util - SolidifyBulkActionException.java
* SPDX-License-Identifier: GPL-2.0-or-later
* %----------------------------------------------------------------------------------------------%
* Copyright (C) 2017 - 2024 University of Geneva
* %----------------------------------------------------------------------------------------------%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* ----------------------------------------------------------------------------------------------%%
*/
package ch.unige.solidify.exception;
import java.util.Map;
import java.util.Map.Entry;
public class SolidifyBulkActionException extends SolidifyRuntimeException {
private static final long serialVersionUID = 7823180563330529746L;
private final Map<String, String> errorList;
public SolidifyBulkActionException(String message, Map<String, String> errorList) {
super(message);
this.errorList = errorList;
}
public Map<String, String> getErrorList() {
return this.errorList;
}
public String getDetailedMessage() {
StringBuilder sb = new StringBuilder();
for (Entry<String, String> error : this.errorList.entrySet()) {
sb.append(error.getKey()).append(" : ").append(error.getValue()).append(System.lineSeparator());
}
return sb.toString();
}
}
......@@ -25,9 +25,12 @@ package ch.unige.solidify.exception;
public class SolidifyMfaNeededException extends SolidifyRuntimeException {
private static final long serialVersionUID = 7965916444189686673L;
public SolidifyMfaNeededException() {
super();
}
public SolidifyMfaNeededException(String message) {
super(message);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment