Skip to content
This repository has been archived by the owner on Apr 10, 2023. It is now read-only.

Commit

Permalink
fix shelf binding. Closes #29
Browse files Browse the repository at this point in the history
  • Loading branch information
knjk04 committed Jun 12, 2020
1 parent 46b1e82 commit 681bee7
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 61 deletions.
20 changes: 16 additions & 4 deletions src/main/java/com/karankumar/bookproject/backend/model/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ public class Book extends BaseEntity {
@JoinColumn(name = "author_id", referencedColumnName = "ID")
private Author author;

@ManyToMany(fetch = FetchType.EAGER)
private Set<Shelf> shelves;
// @ManyToMany(fetch = FetchType.EAGER)
// private Set<Shelf> shelves;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "shelf_id")
private Shelf shelves;

public Book() {
}
Expand Down Expand Up @@ -81,11 +85,19 @@ public void setRating(RatingScale rating) {
this.rating = rating;
}

public Set<Shelf> getShelves() {
// public Set<Shelf> getShelves() {
// return shelves;
// }
//
// public void setShelves(Set<Shelf> shelves) {
// this.shelves = shelves;
// }

public Shelf getShelves() {
return shelves;
}

public void setShelves(Set<Shelf> shelves) {
public void setShelves(Shelf shelves) {
this.shelves = shelves;
}

Expand Down
30 changes: 26 additions & 4 deletions src/main/java/com/karankumar/bookproject/backend/model/Shelf.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Set;

/**
* Represents a shelf (or a list) of books (e.g. books in a 'to read' shelf)
Expand All @@ -35,8 +37,20 @@ public class Shelf extends BaseEntity {
@NotEmpty
private String name;

@ManyToMany(fetch = FetchType.EAGER)
private List<Book> books;
// @ManyToMany(fetch = FetchType.EAGER)
// private List<Book> books;

// @OneToMany(fetch = FetchType.EAGER, mappedBy = "shelves")
// private List<Book> books;

// @OneToMany(fetch = FetchType.EAGER, mappedBy = "shelves")
// private Set<Book> books;

// @ManyToMany(fetch = FetchType.EAGER)
// private Set<Book> books;

@OneToMany(fetch = FetchType.EAGER, mappedBy = "shelves")
private Set<Book> books;

public Shelf() {
}
Expand All @@ -53,11 +67,19 @@ public void setName(String name) {
this.name = name;
}

public List<Book> getBooks() {
// public List<Book> getBooks() {
// return books;
// }
//
// public void setBooks(List<Book> books) {
// this.books = books;
// }

public Set<Book> getBooks() {
return books;
}

public void setBooks(List<Book> books) {
public void setBooks(Set<Book> books) {
this.books = books;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import javax.annotation.PostConstruct;
import java.time.LocalDate;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
Expand All @@ -45,7 +46,7 @@ public class ShelfService extends BaseService<Shelf, Long> {
private AuthorRepository authorRepository;

public ShelfService(BookRepository bookRepository, AuthorRepository authorRepository,
ShelfRepository shelfRepository ) {
ShelfRepository shelfRepository) {
this.bookRepository = bookRepository;
this.shelfRepository = shelfRepository;

Expand Down Expand Up @@ -78,74 +79,89 @@ public void delete(Shelf shelf) {
@PostConstruct
public void populateTestData() {
if (authorRepository.count() == 0) {
authorRepository.saveAll(
Stream.of(
"J.K. Rowling",
"Neil Gaiman",
"J.R.R Tolkien",
"Roald Dahl",
"Robert Galbraith",
"Dan Brown")
.map(
name -> {
String[] fullName = name.split(" ");


Author author = new Author();
author.setFirstName(fullName[0]);
author.setLastName(fullName[1]);
return author;
})
.collect(Collectors.toList()));
authorRepository.saveAll(
Stream.of(
"J.K. Rowling",
"Neil Gaiman",
"J.R.R Tolkien",
"Roald Dahl",
"Robert Galbraith",
"Dan Brown")
.map(
name -> {
String[] fullName = name.split(" ");


Author author = new Author();
author.setFirstName(fullName[0]);
author.setLastName(fullName[1]);
return author;
})
.collect(Collectors.toList()));
}

if (bookRepository.count() == 0) {
Random random = new Random(0);
List<Author> authors = authorRepository.findAll();

bookRepository.saveAll(
Stream.of(
"Harry Potter and the Philosopher's stone",
"Harry Potter and the Chamber of Secrets",
"Harry Potter and the Prisoner of Azkaban",
"Harry Potter and the Goblet of Fire",
"Harry Potter and the Order of Phoenix",
"Harry Potter and the Half-Blood Prince",
"Harry Potter and the Deathly Hallows")
.map(title -> {
int min = 300;
int max = 1000;
int range = (max-min) + 1;
int pages = (int) (Math.random() * range);
Stream.of(
"Harry Potter and the Philosopher's stone",
"Harry Potter and the Chamber of Secrets",
"Harry Potter and the Prisoner of Azkaban",
"Harry Potter and the Goblet of Fire",
"Harry Potter and the Order of Phoenix",
"Harry Potter and the Half-Blood Prince",
"Harry Potter and the Deathly Hallows")
.map(title -> {
int min = 300;
int max = 1000;
int range = (max - min) + 1;
int pages = (int) (Math.random() * range);

Book book = new Book();
book.setTitle(title);
Book book = new Book();
book.setTitle(title);

book.setAuthor(authors.get(0));
book.setAuthor(authors.get(0));

Genre genre = Genre.values()[random.nextInt(Genre.values().length)];
Genre genre = Genre.values()[random.nextInt(Genre.values().length)];

book.setGenre(genre);
book.setNumberOfPages(pages);
book.setGenre(genre);
book.setNumberOfPages(pages);

book.setDateStartedReading(LocalDate.now().minusDays(2));
book.setDateFinishedReading(LocalDate.now());
book.setDateStartedReading(LocalDate.now().minusDays(2));
book.setDateFinishedReading(LocalDate.now());

book.setRating(RatingScale.values()[random.nextInt(RatingScale.values().length)]);
book.setRating(RatingScale.values()[random.nextInt(RatingScale.values().length)]);

return book;
}).collect(Collectors.toList()));
return book;
}).collect(Collectors.toList()));
}

if (shelfRepository.count() == 0) {
List<Book> books = bookRepository.findAll();
shelfRepository.saveAll(
Stream.of("To read", "Reading", "Read")
.map(name -> {
Shelf shelf = new Shelf(name);
shelf.setBooks(books);
return shelf;
}).collect(Collectors.toList()));
.map(name -> {
Shelf shelf = new Shelf(name);
// shelf.setBooks(books);
shelf.setBooks(new HashSet<>(books));
return shelf;
}).collect(Collectors.toList()));
}

List<Book> books = bookRepository.findAll();
System.out.println("Books size = " + books.size());
List<Shelf> shelves = shelfRepository.findAll();
System.out.println("Shelf size = " + shelves.size());

Random random = new Random(0);

for (Book book : books) {
Shelf shelf = shelves.get(random.nextInt(shelves.size()));
book.setShelves(shelf);
System.out.println("Setting book " + book.getTitle() + " to shelf " + shelf.getName());
}
bookRepository.saveAll(books);
}
}
23 changes: 19 additions & 4 deletions src/main/java/com/karankumar/bookproject/ui/shelf/BookForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ private void configureBinder() {
// binder.forField(shelf)
// .withConverter(new StringToShelfConverter())
// .bind(Book::getShelves, Book::setShelves);
binder.forField(shelf)
.bind("shelves.name");
binder.forField(dateStartedReading)
.bind(Book::getDateStartedReading, Book::setDateStartedReading);
Binder.Binding<Book, LocalDate> bindingEndDate = binder.forField(dateFinishedReading)
Expand Down Expand Up @@ -170,27 +172,40 @@ private void validateOnSave() {

public void setBook(Book book) {
if (book == null) {
logger.log(Level.FINE, "Book is null");
logger.log(Level.SEVERE, "Book is null");
System.out.println("Book is null");
}
if (book.getRating() == null) {
logger.log(Level.FINE, "Rating is null");
System.out.println("Rating is null");
}
if (book.getAuthor().getFirstName() == null) {
logger.log(Level.FINE, "Author's first name is null");
logger.log(Level.SEVERE, "Author's first name is null");
System.out.println("Author's first name is null");
}
if (book.getAuthor().getLastName() == null) {
logger.log(Level.FINE, "Author's last name is null");
logger.log(Level.SEVERE, "Author's last name is null");
System.out.println("Author's last name is null");
}
if (book.getDateStartedReading() == null) {
logger.log(Level.FINE, "Date started reading is null");
System.out.println("Date started reading is null");
}
if (book.getDateFinishedReading() == null) {
logger.log(Level.FINE, "Date finished reading is null");
System.out.println("Date finished reading is null");
}
if (book.getGenre() == null) {
logger.log(Level.FINE, "Book genre is null");
System.out.println("Book genre is null");
}

if (binder == null) {
logger.log(Level.SEVERE, "Binder is null");
System.out.println("Binder is null");
} else {
binder.setBean(book);
}
binder.setBean(book);
}

private void configureTitle() {
Expand Down

0 comments on commit 681bee7

Please sign in to comment.