Browse Source

Update edit form code. Handle error situations better

Signed-off-by: Pekka Helenius <fincer89@hotmail.com>
v0.0.2-alpha
Pekka Helenius 4 years ago
parent
commit
d358dbcace
1 changed files with 36 additions and 17 deletions
  1. +36
    -17
      bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java

+ 36
- 17
bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java View File

@ -267,17 +267,42 @@ public class BookController {
BookHash bookHash = bookHashRepository.findByHashId(bookHashId); BookHash bookHash = bookHashRepository.findByHashId(bookHashId);
if (bookHash == null) { if (bookHash == null) {
bindingResult.rejectValue("name", "error.user", "Wrong book");
}
Long bookId = bookHash.getBookId();
// TODO consider better solution. Add custom Hibernate annotation for Book class?
Book bookI = bookRepository.findByIsbn(book.getIsbn());
// If existing ISBN value is not attached to the current book...
if (bookI != null) {
if (bookI.getId() != bookId) {
bindingResult.rejectValue("isbn", "error.user", "ISBN code already exists");
bindingResult.rejectValue("name", "error.user", "Unknown book");
} else {
// One-to-one unidirectional relationship handling
/*
* Must be set. Otherwise, we get TemplateProcessingException
* when user puts invalid field values
* (Thymeleaf template engine can't handle book.bookHash.hashId).
*/
book.setBookHash(bookHash);
/*
* Must be set. Otherwise, we may get merge errors when user
* has preceding error inputs in this view.
*/
bookHash.setBook(book);
// TODO consider better solution. Add custom Hibernate annotation for Book class?
Book bookI = bookRepository.findByIsbn(book.getIsbn());
Long bookId = bookHash.getBookId();
if (bookId == null) {
bindingResult.rejectValue("name", "error.user", "Unknown book");
} else {
// If existing ISBN value is not attached to the current book...
if (bookI != null) {
if (bookI.getId() != bookId) {
bindingResult.rejectValue("isbn", "error.user", "ISBN code already exists");
}
}
/*
* This is necessary so that Hibernate does not attempt to INSERT data
* but UPDATEs current table data.
*/
book.setId(bookId);
} }
} }
@ -287,12 +312,6 @@ public class BookController {
return bookEditPageView; return bookEditPageView;
} }
/*
* This is necessary so that Hibernate does not attempt to INSERT data
* but UPDATEs current table data.
*/
book.setId(bookId);
bookAuthorHelper.detectAndSaveUpdateAuthorForBook(book); bookAuthorHelper.detectAndSaveUpdateAuthorForBook(book);
bookRepository.save(book); bookRepository.save(book);


Loading…
Cancel
Save