Skip to content

Commit b6cc0ac

Browse files
committed
Book hash IDs: consider potential hash collisions; move logic into
BookEventHandler Signed-off-by: Pekka Helenius <[email protected]>
1 parent 0450a8f commit b6cc0ac

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookEventHandler.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ public class BookEventHandler {
3131
private BookHashRepository bookHashRepository;
3232

3333
/*
34+
* Generate hash id for the book. One-to-one unidirectional tables.
35+
* Associate generated book hash object information
36+
* to the book (table).
37+
* Associate new book object information
38+
* to the book hash (table).
39+
*
3440
* When using REST API to add a new book, we need to add a corresponding
3541
* book hash Id to BOOK_HASH table, as well.
3642
*
@@ -42,6 +48,23 @@ public class BookEventHandler {
4248
@HandleAfterCreate
4349
public void handleAfterCreate(Book book) {
4450
BookHash bookHash = new BookHash();
51+
52+
/*
53+
* In a very unlikely scenario, we have hash collisions
54+
* (same hash values generated for two different book entity objects).
55+
* Therefore, we need to check that hash id is not already defined for
56+
* some other book.
57+
*/
58+
int i = 0;
59+
while (i < 5) {
60+
if (bookHashRepository.findByHashId(bookHash.getHashId()) != null) {
61+
bookHash = new BookHash();
62+
} else {
63+
break;
64+
}
65+
i++;
66+
}
67+
4568
book.setBookHash(bookHash);
4669
bookHash.setBook(book);
4770

bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import com.fjordtek.bookstore.model.book.AuthorRepository;
3333
import com.fjordtek.bookstore.model.book.Book;
34+
import com.fjordtek.bookstore.model.book.BookEventHandler;
3435
import com.fjordtek.bookstore.model.book.BookHash;
3536
import com.fjordtek.bookstore.model.book.BookHashRepository;
3637
import com.fjordtek.bookstore.model.book.BookRepository;
@@ -81,6 +82,9 @@ public void initBinder(WebDataBinder binder) {
8182
@Autowired
8283
private BookAuthorHelper bookAuthorHelper;
8384

85+
@Autowired
86+
private BookEventHandler bookEventHandler;
87+
8488
/*
8589
private Map<String,String> globalModelMap = new HashMap<String,String>() {
8690
private static final long serialVersionUID = 1L;
@@ -211,26 +215,12 @@ public String webFormSaveNewBook(
211215

212216
httpServerLogger.log(requestData, responseData);
213217

214-
/*
215-
* Generate hash id for the book. One-to-one unidirectional tables.
216-
* Associate generated book hash object information
217-
* to the book (table).
218-
* Associate new book object information
219-
* to the book hash (table).
220-
*/
221-
BookHash bookHash = new BookHash();
222-
book.setBookHash(bookHash);
223-
bookHash.setBook(book);
224-
225-
/*
226-
* More sophisticated methods are required to handle
227-
* user input with random letter cases etc. considered
228-
*/
229-
//authorRepository.save(book.getAuthor());
230218
bookAuthorHelper.detectAndSaveUpdateAuthorForBook(book);
231219

232220
bookRepository.save(book);
233-
bookHashRepository.save(bookHash);
221+
222+
// Manually call a book event handler. Is there a better way to do this?
223+
bookEventHandler.handleAfterCreate(book);
234224

235225
return "redirect:" + env.getProperty("page.url.list");
236226
}

0 commit comments

Comments
 (0)