package cz.muni.fi.pa165.entity;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

/**
 * Entity representing a single book in a library
 *
 * @author Michael Simacek
 *
 */
@Entity
public class Book {

    @Id
    private Long id;

    @Column(nullable = false)
    private String name;

    @Column
    private String authorName;

    @Column(nullable = false)
    private Long isbn;

    @Column(nullable = false)
    private BookState state;

    @Column
    @ManyToMany(mappedBy = "books")
    private Set<BookCollection> collections = new HashSet<>();

    //<editor-fold defaultstate="collapsed" desc="GET/SET">
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthorName() {
        return authorName;
    }

    public void setAuthorName(String authorName) {
        this.authorName = authorName;
    }

    public Long getIsbn() {
        return isbn;
    }

    public void setIsbn(Long isbn) {
        this.isbn = isbn;
    }

    public BookState getState() {
        return state;
    }

    public void setState(BookState state) {
        this.state = state;
    }

    public Set<BookCollection> getCollections() {
        return Collections.unmodifiableSet(collections);
    }
    
    public void addCollection(BookCollection collection){
        this.collections.add(collection);
    }
    //</editor-fold>

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((authorName == null) ? 0 : authorName.hashCode());
        result = prime * result + ((isbn == null) ? 0 : isbn.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof Book)) {
            return false;
        }
        Book other = (Book) obj;
        if (authorName == null) {
            if (other.authorName != null) {
                return false;
            }
        } else if (!authorName.equals(other.authorName)) {
            return false;
        }
        if (isbn == null) {
            if (other.isbn != null) {
                return false;
            }
        } else if (!isbn.equals(other.isbn)) {
            return false;
        }
        if (name == null) {
            if (other.name != null) {
                return false;
            }
        } else if (!name.equals(other.name)) {
            return false;
        }
        return true;
    }
}

@@ -101,24 +101,25 @@
 a.util.Set;%0A
+%0A
 import javax
@@ -237,16 +237,52 @@
 yToMany;
+%0Aimport javax.persistence.ManyToOne;
 %0A%0A/**%0A *
@@ -759,63 +759,124 @@
     
-//%3Ceditor-fold defaultstate=%22collapsed%22 desc=%22GET/SET%22%3E
+@ManyToOne%0A    // ManyToOne because we want to keep history of loans%0A    private Set%3CLoan%3E loans = new HashSet%3C%3E();%0A
 %0A   
@@ -1663,20 +1663,16 @@
 ;%0A    %7D%0A
-    
 %0A    pub
@@ -1720,16 +1720,17 @@
 lection)
+ 
 %7B%0A      
@@ -1731,21 +1731,16 @@
         
-this.
 collecti
@@ -1770,28 +1770,262 @@
   %7D%0A
+%0A
     
-//%3C/editor-fold%3E
+public Set%3CLoan%3E getLoans() %7B%0A        return loans;%0A    %7D%0A%0A    public void setLoans(Set%3CLoan%3E loans) %7B%0A        this.loans = loans;%0A    %7D%0A%0A    public void setCollections(Set%3CBookCollection%3E collections) %7B%0A        this.collections = collections;%0A    %7D
 %0A%0A  
@@ -2472,26 +2472,24 @@
 this == obj)
- %7B
 %0A           
@@ -2502,26 +2502,16 @@
 n true;%0A
-        %7D%0A
         
@@ -2518,34 +2518,32 @@
 if (obj == null)
- %7B
 %0A            ret
@@ -2549,34 +2549,24 @@
 turn false;%0A
-        %7D%0A
         if (
@@ -2588,18 +2588,16 @@
 f Book))
- %7B
 %0A       
@@ -2611,34 +2611,24 @@
 turn false;%0A
-        %7D%0A
         Book
@@ -2715,34 +2715,32 @@
 horName != null)
- %7B
 %0A               
@@ -2750,38 +2750,24 @@
 turn false;%0A
-            %7D%0A
         %7D el
@@ -2806,26 +2806,24 @@
 authorName))
- %7B
 %0A           
@@ -2833,34 +2833,24 @@
 turn false;%0A
-        %7D%0A
         if (
@@ -2892,34 +2892,32 @@
 er.isbn != null)
- %7B
 %0A               
@@ -2927,38 +2927,24 @@
 turn false;%0A
-            %7D%0A
         %7D el
@@ -2975,18 +2975,16 @@
 r.isbn))
- %7B
 %0A       
@@ -2998,34 +2998,24 @@
 turn false;%0A
-        %7D%0A
         if (
@@ -3061,26 +3061,24 @@
 ame != null)
- %7B
 %0A           
@@ -3100,22 +3100,8 @@
 se;%0A
-            %7D%0A
     
@@ -3140,18 +3140,16 @@
 r.name))
- %7B
 %0A       
@@ -3171,18 +3171,8 @@
 se;%0A
-        %7D%0A
     

Add Book.loans