Browse Source

Book hash IDs: consider potential hash collisions; move logic into

BookEventHandler
Signed-off-by: Pekka Helenius <fincer89@hotmail.com>
v0.0.3-alpha v0.0.3-alpha
Pekka Helenius 4 years ago
parent
commit
b6cc0ac887
2 changed files with 30 additions and 17 deletions
  1. +23
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookEventHandler.java
  2. +7
    -17
      bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java

+ 23
- 0
bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookEventHandler.java View File

@ -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);


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

@ -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<String,String> globalModelMap = new HashMap<String,String>() {
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");
}


Loading…
Cancel
Save