Browse Source

Improve controller logic

Signed-off-by: Pekka Helenius <fincer89@hotmail.com>
v0.0.2-alpha
Pekka Helenius 4 years ago
parent
commit
01769f8ad6
3 changed files with 87 additions and 56 deletions
  1. +7
    -1
      bookstore/src/main/java/com/fjordtek/bookstore/web/BookBasePathAwareController.java
  2. +67
    -51
      bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java
  3. +13
    -4
      bookstore/src/main/java/com/fjordtek/bookstore/web/BookRestController.java

+ 7
- 1
bookstore/src/main/java/com/fjordtek/bookstore/web/BookBasePathAwareController.java View File

@ -70,7 +70,7 @@ public class BookBasePathAwareController {
String authorFirstName = null, authorLastName = null; String authorFirstName = null, authorLastName = null;
String categoryName = null; String categoryName = null;
// We keep going even if some of these is still null
// We keep going even if some of these are still null
try { authorFirstName = bookNode.get("author").get("firstname").textValue(); } catch (NullPointerException e) {}; try { authorFirstName = bookNode.get("author").get("firstname").textValue(); } catch (NullPointerException e) {};
try { authorLastName = bookNode.get("author").get("lastname").textValue(); } catch (NullPointerException e) {}; try { authorLastName = bookNode.get("author").get("lastname").textValue(); } catch (NullPointerException e) {};
try { categoryName = bookNode.get("category").get("name").textValue(); } catch (NullPointerException e) {}; try { categoryName = bookNode.get("category").get("name").textValue(); } catch (NullPointerException e) {};
@ -139,7 +139,10 @@ public class BookBasePathAwareController {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
responseData.setStatus(HttpServletResponse.SC_BAD_REQUEST);
httpServerLogger.log(requestData, responseData); httpServerLogger.log(requestData, responseData);
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
} }
} }
@ -180,7 +183,10 @@ public class BookBasePathAwareController {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
responseData.setStatus(HttpServletResponse.SC_BAD_REQUEST);
httpServerLogger.log(requestData, responseData); httpServerLogger.log(requestData, responseData);
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
} }


+ 67
- 51
bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java View File

@ -209,18 +209,23 @@ public class BookController {
HttpServletResponse responseData HttpServletResponse responseData
) { ) {
Long bookId = new Long(bookHashRepository.findByHashId(bookHashId).getBookId());
try {
Long bookId = new Long(bookHashRepository.findByHashId(bookHashId).getBookId());
/*
* Delete associated book id foreign key (+ other row data) from book hash table
* at first, after which delete the book.
*/
bookHashRepository.deleteByBookId(bookId);
bookRepository.deleteById(bookId);
/*
* Delete associated book id foreign key (+ other row data) from book hash table
* at first, after which delete the book.
*/
bookHashRepository.deleteByBookId(bookId);
bookRepository.deleteById(bookId);
} catch (NullPointerException e) {
responseData.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}
httpServerLogger.log(requestData, responseData); httpServerLogger.log(requestData, responseData);
return "redirect:../" + bookListPageView;
return "redirect:/" + bookListPageView;
} }
////////////////////////////// //////////////////////////////
@ -237,14 +242,21 @@ public class BookController {
HttpServletResponse responseData HttpServletResponse responseData
) { ) {
Long bookId = new Long(bookHashRepository.findByHashId(bookHashId).getBookId());
try {
Long bookIdFromHash = bookHashRepository.findByHashId(bookHashId).getBookId();
Book book = bookRepository.findById(bookIdFromHash).get();
Book book = bookRepository.findById(bookId).get();
dataModel.addAttribute("book", book);
dataModel.addAttribute("book", book);
httpServerLogger.log(requestData, responseData);
httpServerLogger.log(requestData, responseData);
return bookEditPageView;
} catch (NullPointerException e) {
responseData.setStatus(HttpServletResponse.SC_BAD_REQUEST);
httpServerLogger.log(requestData, responseData);
return "redirect:/" + bookListPageView;
}
return bookEditPageView;
} }
/* NOTE: We keep Id here for the sake of proper URL formatting. /* NOTE: We keep Id here for the sake of proper URL formatting.
@ -259,54 +271,56 @@ public class BookController {
) )
public String webFormUpdateBook( public String webFormUpdateBook(
@Valid @ModelAttribute("book") Book book, @Valid @ModelAttribute("book") Book book,
BindingResult bindingResult,
@PathVariable("hash_id") String bookHashId,
BindingResult bindingResultBook,
@ModelAttribute ("hash_id") String bookHashId,
HttpServletRequest requestData, HttpServletRequest requestData,
HttpServletResponse responseData HttpServletResponse responseData
) { ) {
BookHash bookHash = bookHashRepository.findByHashId(bookHashId); BookHash bookHash = bookHashRepository.findByHashId(bookHashId);
if (bookHash == null) { if (bookHash == null) {
bindingResult.rejectValue("name", "error.user", "Unknown book");
} else {
responseData.setStatus(HttpServletResponse.SC_BAD_REQUEST);
httpServerLogger.log(requestData, responseData);
return "redirect:/" + bookListPageView;
}
// 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);
// 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();
// 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 (bookId == null) {
bindingResultBook.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");
}
// If existing ISBN value is not attached to the current book...
if (bookI != null) {
if (bookI.getId() != bookId) {
bindingResultBook.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);
} }
/*
* This is necessary so that Hibernate does not attempt to INSERT data
* but UPDATEs current table data.
*/
book.setId(bookId);
} }
if (bindingResult.hasErrors()) {
if (bindingResultBook.hasErrors()) {
responseData.setStatus(HttpServletResponse.SC_BAD_REQUEST); responseData.setStatus(HttpServletResponse.SC_BAD_REQUEST);
httpServerLogger.log(requestData, responseData); httpServerLogger.log(requestData, responseData);
return bookEditPageView; return bookEditPageView;
@ -316,8 +330,7 @@ public class BookController {
bookRepository.save(book); bookRepository.save(book);
httpServerLogger.log(requestData, responseData); httpServerLogger.log(requestData, responseData);
return "redirect:../" + bookListPageView;
return "redirect:/" + bookListPageView;
} }
////////////////////////////// //////////////////////////////
@ -338,7 +351,7 @@ public class BookController {
// REDIRECTS // REDIRECTS
@RequestMapping( @RequestMapping(
value = { "*" }
value = { "*", "error" }
) )
@ResponseStatus(HttpStatus.FOUND) @ResponseStatus(HttpStatus.FOUND)
public String redirectToDefaultWebForm( public String redirectToDefaultWebForm(
@ -346,8 +359,11 @@ public class BookController {
HttpServletResponse responseData HttpServletResponse responseData
) )
{ {
if (requestData.getRequestURI().matches("error")) {
responseData.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}
httpServerLogger.log(requestData, responseData); httpServerLogger.log(requestData, responseData);
return "redirect:" + bookListPageView;
return "redirect:/" + bookListPageView;
} }
@RequestMapping( @RequestMapping(


+ 13
- 4
bookstore/src/main/java/com/fjordtek/bookstore/web/BookRestController.java View File

@ -72,11 +72,20 @@ public class BookRestController {
HttpServletResponse responseData HttpServletResponse responseData
) { ) {
Long bookId = new Long(bookHashRepository.findByHashId(bookHashId).getBookId());
try {
httpServerLogger.log(requestData, responseData);
Long bookId = new Long(bookHashRepository.findByHashId(bookHashId).getBookId());
httpServerLogger.log(requestData, responseData);
return bookRepository.findById(bookId);
} catch (NullPointerException e) {
responseData.setStatus(HttpServletResponse.SC_BAD_REQUEST);
httpServerLogger.log(requestData, responseData);
return bookRepository.findById(bookId);
this.redirectToDefaultWebForm(requestData, responseData);
return null;
}
} }
////////////////////////////// //////////////////////////////
@ -90,9 +99,9 @@ public class BookRestController {
HttpServletRequest requestData, HttpServletRequest requestData,
HttpServletResponse responseData HttpServletResponse responseData
) { ) {
httpServerLogger.log(requestData, responseData);
responseData.setHeader("Location", "/" + bookListPageView); responseData.setHeader("Location", "/" + bookListPageView);
responseData.setStatus(302); responseData.setStatus(302);
httpServerLogger.log(requestData, responseData);
} }
} }

Loading…
Cancel
Save