From b6cc0ac8875473d2b27bd469a32e488fd45c4f70 Mon Sep 17 00:00:00 2001 From: Pekka Helenius Date: Wed, 7 Oct 2020 14:32:37 +0300 Subject: [PATCH] Book hash IDs: consider potential hash collisions; move logic into BookEventHandler Signed-off-by: Pekka Helenius --- .../model/book/BookEventHandler.java | 23 ++++++++++++++++++ .../bookstore/web/BookController.java | 24 ++++++------------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookEventHandler.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookEventHandler.java index d21639a..8a2952e 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookEventHandler.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookEventHandler.java @@ -31,6 +31,12 @@ public class BookEventHandler { private BookHashRepository bookHashRepository; /* + * Generate hash id for the book. One-to-one unidirectional tables. + * Associate generated book hash object information + * to the book (table). + * Associate new book object information + * to the book hash (table). + * * When using REST API to add a new book, we need to add a corresponding * book hash Id to BOOK_HASH table, as well. * @@ -42,6 +48,23 @@ public class BookEventHandler { @HandleAfterCreate public void handleAfterCreate(Book book) { BookHash bookHash = new BookHash(); + + /* + * In a very unlikely scenario, we have hash collisions + * (same hash values generated for two different book entity objects). + * Therefore, we need to check that hash id is not already defined for + * some other book. + */ + int i = 0; + while (i < 5) { + if (bookHashRepository.findByHashId(bookHash.getHashId()) != null) { + bookHash = new BookHash(); + } else { + break; + } + i++; + } + book.setBookHash(bookHash); bookHash.setBook(book); 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 ac5e335..dc5f9b2 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java @@ -31,6 +31,7 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes; import com.fjordtek.bookstore.model.book.AuthorRepository; import com.fjordtek.bookstore.model.book.Book; +import com.fjordtek.bookstore.model.book.BookEventHandler; import com.fjordtek.bookstore.model.book.BookHash; import com.fjordtek.bookstore.model.book.BookHashRepository; import com.fjordtek.bookstore.model.book.BookRepository; @@ -81,6 +82,9 @@ public class BookController { @Autowired private BookAuthorHelper bookAuthorHelper; + @Autowired + private BookEventHandler bookEventHandler; + /* private Map globalModelMap = new HashMap() { private static final long serialVersionUID = 1L; @@ -211,26 +215,12 @@ public class BookController { httpServerLogger.log(requestData, responseData); - /* - * Generate hash id for the book. One-to-one unidirectional tables. - * Associate generated book hash object information - * to the book (table). - * Associate new book object information - * to the book hash (table). - */ - BookHash bookHash = new BookHash(); - book.setBookHash(bookHash); - bookHash.setBook(book); - - /* - * More sophisticated methods are required to handle - * user input with random letter cases etc. considered - */ - //authorRepository.save(book.getAuthor()); bookAuthorHelper.detectAndSaveUpdateAuthorForBook(book); bookRepository.save(book); - bookHashRepository.save(bookHash); + + // Manually call a book event handler. Is there a better way to do this? + bookEventHandler.handleAfterCreate(book); return "redirect:" + env.getProperty("page.url.list"); }