From 28afed3c9f4b63eca53b2eb07173eb1d2a981b4c Mon Sep 17 00:00:00 2001 From: Pekka Helenius Date: Wed, 7 Oct 2020 01:36:06 +0300 Subject: [PATCH] Un-hardcode more messages Signed-off-by: Pekka Helenius --- .../bookstore/web/BookController.java | 37 +++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java index e1b413c..20fb064 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java @@ -10,6 +10,7 @@ import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.MessageSource; import org.springframework.core.env.Environment; import org.springframework.http.HttpStatus; import org.springframework.security.access.prepost.PreAuthorize; @@ -59,6 +60,9 @@ public class BookController { @Autowired private Environment env; + @Autowired + private MessageSource msg; + @Autowired private CategoryRepository categoryRepository; @@ -194,7 +198,16 @@ public class BookController { // TODO consider better solution. Add custom Hibernate annotation for Book class? if (bookRepository.existsByIsbn(book.getIsbn())) { - bindingResult.rejectValue("isbn", "error.user", "ISBN code already exists"); + bindingResult.rejectValue( + "isbn", + "error.user", + msg.getMessage( + "book.error.isbn.exists", + null, + "book.error.isbn.exists [placeholder]", + requestData.getLocale() + ) + ); } if (bindingResult.hasErrors()) { @@ -355,13 +368,31 @@ public class BookController { Long bookId = bookHash.getBookId(); if (bookId == null) { - bindingResultBook.rejectValue("name", "error.user", "Unknown book"); + bindingResultBook.rejectValue( + "name", + "error.user", + msg.getMessage( + "book.error.unknownbook", + null, + "book.error.unknownbook [placeholder]", + requestData.getLocale() + ) + ); } else { // If existing ISBN value is not attached to the current book... if (bookI != null) { if (bookI.getId() != bookId) { - bindingResultBook.rejectValue("isbn", "error.user", "ISBN code already exists"); + bindingResultBook.rejectValue( + "isbn", + "error.user", + msg.getMessage( + "book.error.isbn.exists", + null, + "book.error.isbn.exists [placeholder]", + requestData.getLocale() + ) + ); } }