From d954733bba418add9d6c2b44decaf11c3827abd0 Mon Sep 17 00:00:00 2001 From: Bogdan Kostov Date: Fri, 13 Sep 2024 00:31:26 +0200 Subject: [PATCH] [#82] Refactor default messages of generic entity validation errors. - Generate entity type specific bad request response messages - response message is a concatenation of entity type name and validation erro default message. --- .../kbss/analysis/controller/util/RestExceptionHandler.java | 2 +- src/main/java/cz/cvut/kbss/analysis/dao/BaseDao.java | 5 +++++ src/main/java/cz/cvut/kbss/analysis/dao/GenericDao.java | 5 +++++ src/main/java/cz/cvut/kbss/analysis/model/NamedEntity.java | 2 +- .../cvut/kbss/analysis/service/BaseRepositoryService.java | 3 ++- .../analysis/service/validation/NamedEntityValidator.java | 6 +++--- 6 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main/java/cz/cvut/kbss/analysis/controller/util/RestExceptionHandler.java b/src/main/java/cz/cvut/kbss/analysis/controller/util/RestExceptionHandler.java index 365b9659..58cc3397 100755 --- a/src/main/java/cz/cvut/kbss/analysis/controller/util/RestExceptionHandler.java +++ b/src/main/java/cz/cvut/kbss/analysis/controller/util/RestExceptionHandler.java @@ -48,7 +48,7 @@ public ErrorInfo handleValidationException(HttpServletRequest request, Validatio String errorMessage = e.getErrors() .stream() - .map(DefaultMessageSourceResolvable::getDefaultMessage) + .map(err -> "%s %s".formatted(err.getObjectName(), err.getDefaultMessage())) .collect(Collectors.joining(",", "[", "]")); return new ErrorInfo(errorMessage, request.getRequestURI()); diff --git a/src/main/java/cz/cvut/kbss/analysis/dao/BaseDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/BaseDao.java index 4da95a7e..ff8488ec 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/BaseDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/BaseDao.java @@ -302,4 +302,9 @@ public void removeAll(URI subjectURI, URI propertyURI, URI context){ .setParameter("context", context) .executeUpdate(); } + + @Override + public Class getType() { + return type; + } } \ No newline at end of file diff --git a/src/main/java/cz/cvut/kbss/analysis/dao/GenericDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/GenericDao.java index 05b62e4d..402c6fc2 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/GenericDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/GenericDao.java @@ -127,4 +127,9 @@ public interface GenericDao { * @param context */ void deleteContext(URI context); + + /** + * @return the type of the entity managed by this class. + */ + Class getType(); } diff --git a/src/main/java/cz/cvut/kbss/analysis/model/NamedEntity.java b/src/main/java/cz/cvut/kbss/analysis/model/NamedEntity.java index d0e25ac9..0ae0c239 100644 --- a/src/main/java/cz/cvut/kbss/analysis/model/NamedEntity.java +++ b/src/main/java/cz/cvut/kbss/analysis/model/NamedEntity.java @@ -16,7 +16,7 @@ @Setter public class NamedEntity extends AbstractEntity{ - @NotEmpty(message = "Name must not be empty") + @NotEmpty(message = "name is empty.") @ParticipationConstraints(nonEmpty = true) @OWLDataProperty(iri = Vocabulary.s_p_name) private String name; diff --git a/src/main/java/cz/cvut/kbss/analysis/service/BaseRepositoryService.java b/src/main/java/cz/cvut/kbss/analysis/service/BaseRepositoryService.java index 31d6b523..3e83fcdb 100755 --- a/src/main/java/cz/cvut/kbss/analysis/service/BaseRepositoryService.java +++ b/src/main/java/cz/cvut/kbss/analysis/service/BaseRepositoryService.java @@ -262,7 +262,8 @@ public boolean existsInPersistenceContext(URI id) { * @throws ValidationException In case the instance is not valid */ public void validate(T instance, Object ... groups) { - DataBinder binder = new DataBinder(instance); + DataBinder binder = new DataBinder(instance, getPrimaryDao().getType().getSimpleName()); + binder.setValidator(validator); BindingResult bindingResult = binder.getBindingResult(); diff --git a/src/main/java/cz/cvut/kbss/analysis/service/validation/NamedEntityValidator.java b/src/main/java/cz/cvut/kbss/analysis/service/validation/NamedEntityValidator.java index 763dd82b..58c78552 100644 --- a/src/main/java/cz/cvut/kbss/analysis/service/validation/NamedEntityValidator.java +++ b/src/main/java/cz/cvut/kbss/analysis/service/validation/NamedEntityValidator.java @@ -48,13 +48,13 @@ protected void customValidation(Object target, Errors errors, Object... validati NamedEntity entity = (NamedEntity) target; if(existsWithName(entity)) - errors.rejectValue("name", "name.duplicate", "Duplicate entity name"); + errors.rejectValue("name", "name.duplicate", "uri is not null nor unique."); if(groups.isCreateGroup() && exists(entity)){ - errors.rejectValue("uri", "uri.exists", "The uri should be null or unique"); + errors.rejectValue("uri", "uri.exists", "name already exists."); } if(groups.isUpdateGroup() && !exists(entity)){ - errors.rejectValue("uri", "uri.not-exists", "Uri does not refer to an existing entity"); + errors.rejectValue("uri", "uri.not-exists", "uri does not refer to an existing entity."); } }