@Named
@ViewScoped
public class CollectorView implements Serializable {
private Book book;
private List<Book> books;
@PostConstruct
public void init() {
book = new Book();
books = new ArrayList<Book>();
}
public void createNew() {
if(books.contains(book)) {
FacesMessage msg = new FacesMessage("Dublicated", "This book has already been added");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
else {
books.add(book);
book = new Book();
}
}
public String reinit() {
book = new Book();
return null;
}
public Book getBook() {
return book;
}
public List<Book> getBooks() {
return books;
}
}
public class Book implements Serializable {
private String title;
private String author;
private String publisher;
private Integer pages;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public Integer getPages() {
return pages;
}
public void setPages(Integer pages) {
this.pages = pages;
}
public boolean equals(Object obj) {
if(!(obj instanceof Book))
return false;
Book book = (Book) obj;
return (book.getTitle() != null && book.getTitle().equals(title)) && (book.getAuthor() != null && book.getAuthor().equals(author));
}
public int hashCode() {
int hash = 1;
if(title != null)
hash = hash * 31 + title.hashCode();
if(author != null)
hash = hash * 29 + author.hashCode();
return hash;
}
}