Browse Source

Create BookAuthorHelper class for handling author-related operations

Signed-off-by: Pekka Helenius <fincer89@hotmail.com>
v0.0.2-alpha
Pekka Helenius 4 years ago
parent
commit
b16266eb69
2 changed files with 110 additions and 34 deletions
  1. +87
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/web/BookAuthorHelper.java
  2. +23
    -34
      bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java

+ 87
- 0
bookstore/src/main/java/com/fjordtek/bookstore/web/BookAuthorHelper.java View File

@ -0,0 +1,87 @@
//Pekka Helenius <fincer89@hotmail.com>, Fjordtek 2020
package com.fjordtek.bookstore.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.fjordtek.bookstore.model.Author;
import com.fjordtek.bookstore.model.AuthorRepository;
import com.fjordtek.bookstore.model.Book;
/**
*
* This class implements special methods for handling book-related author
* saving and updating operations.
* <p>
* Since the bookstore implements a separate AUTHOR table for BOOK with
* a relation between both, sophisticated methods are required in
* some controller operations.
*
* @author Pekka Helenius
*/
@Component
public class BookAuthorHelper {
@Autowired
private AuthorRepository authorRepository;
public void detectAndSaveUpdateAuthorForBook(Book book) {
/*
* Find an author from the current AUTHOR table by his/her first and last name,
* included in the Book entity object. In CrudRepository, if a matching Id
* value is not found, it is stored as a new Author entity object. Therefore,
* it's crucial to identify whether author row values already exist in
* AUTHOR table.
*/
try {
Author authorI = authorRepository.findByFirstNameIgnoreCaseContainingAndLastNameIgnoreCaseContaining(
book.getAuthor().getFirstName(),book.getAuthor().getLastName())
.get(0);
/*
* When author is found, use it's Id attribute for book's author Id
*
* NOTE: This step is required by Hibernate, otherwise we get
* TransientPropertyValueException
*/
book.getAuthor().setId(authorI.getId());
/*
* Otherwise, consider this a new author and store it appropriately.
* Actually, when author is not found, we get IndexOutOfBoundsException.
*/
} catch (IndexOutOfBoundsException e) {
authorRepository.save(book.getAuthor());
}
}
public Author detectAndSaveAuthorByName(String firstName, String lastName) {
/*
* Find an author from the current AUTHOR table by his/her first and last name.
* In CrudRepository, if a matching Id value is not found, it is stored
* as a new Author entity object. Therefore, it's crucial to identify
* whether author row values already exist in AUTHOR table.
*/
try {
return authorRepository.findByFirstNameIgnoreCaseContainingAndLastNameIgnoreCaseContaining(
firstName,lastName).get(0);
/*
* Otherwise, consider this a new author and store it appropriately.
* Actually, when author is not found, we get IndexOutOfBoundsException.
*/
} catch (IndexOutOfBoundsException e) {
return authorRepository.save(new Author(firstName,lastName));
}
}
// TODO implement methods to save author even if either his/her first or last name is missing
}

+ 23
- 34
bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java View File

@ -26,7 +26,6 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import com.fjordtek.bookstore.model.Author;
import com.fjordtek.bookstore.model.AuthorRepository;
import com.fjordtek.bookstore.model.Book;
import com.fjordtek.bookstore.model.BookHash;
@ -34,6 +33,13 @@ import com.fjordtek.bookstore.model.BookHashRepository;
import com.fjordtek.bookstore.model.BookRepository;
import com.fjordtek.bookstore.model.CategoryRepository;
/**
*
* This class implements the default Spring Model View controller for the bookstore.
*
* @author Pekka Helenius
*/
@Controller
public class BookController {
@ -57,6 +63,8 @@ public class BookController {
@Autowired
private BookHashRepository bookHashRepository;
private BookAuthorHelper bookAuthorHelper;
private static final String RestJSONPageView = "json";
private static final String RestAPIRefPageView = "apiref";
@ -66,6 +74,18 @@ public class BookController {
private static final String bookDeletePageView = "bookdelete";
private static final String bookEditPageView = "bookedit";
/*
* This method MUST exist with Autowired annotation. Handles autowiring of external classes.
* If this method is not defined, they are not found by this controller class (are null).
*/
@Autowired
private void instanceAttributeController(
BookAuthorHelper bookAuthorHelper
) {
this.bookAuthorHelper = bookAuthorHelper;
}
private Map<String,String> globalModelMap = new HashMap<String,String>() {
private static final long serialVersionUID = 1L;
{
@ -90,37 +110,6 @@ public class BookController {
dataModel.addAttribute("authors", authorRepository.findAll());
}
//////////////////////////////
// Private methods
private void detectAndSaveBookAuthor(Book book) {
/*
* Find an author from the current AUTHOR table by his/her first and last name.
* In CrudRepository, if Id attribute is not found, it is stored
* as a new row value. Therefore, it's crucial to identify whether row value already
* exists in AUTHOR table.
*/
try {
Author authorI = authorRepository.findByFirstNameIgnoreCaseContainingAndLastNameIgnoreCaseContaining(
book.getAuthor().getFirstName(),book.getAuthor().getLastName())
.get(0);
/*
* When author is found, use it's Id attribute for book's author Id...
*/
book.getAuthor().setId(authorI.getId());
/*
* ...Otherwise, consider this a new author and store it appropriately.
* Actually, when author is not found, we get IndexOutOfBoundsException.
*/
} catch (IndexOutOfBoundsException e) {
authorRepository.save(book.getAuthor());
}
}
//////////////////////////////
// LIST PAGE
@RequestMapping(
@ -187,7 +176,7 @@ public class BookController {
httpServerLogger.log(requestData, responseData);
detectAndSaveBookAuthor(book);
bookAuthorHelper.detectAndSaveUpdateAuthorForBook(book);
/*
* Generate hash id for the book. One-to-one unidirectional tables.
@ -304,7 +293,7 @@ public class BookController {
*/
book.setId(bookId);
detectAndSaveBookAuthor(book);
bookAuthorHelper.detectAndSaveUpdateAuthorForBook(book);
bookRepository.save(book);
httpServerLogger.log(requestData, responseData);


Loading…
Cancel
Save