package com.eyrolles.model;

import java.util.Collection;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;

@Entity
@Table(name="categories")
public class Categorie {
	@Id @GeneratedValue(strategy=GenerationType.AUTO)
	private int id;
	private String nom;
	@ManyToOne
	@JsonBackReference
	private Theme theme;
	
	@OneToMany(mappedBy="categorie")
	@Cascade({CascadeType.ALL})
	@JsonBackReference
	private Collection<Livre> livres;
	
	@OneToMany(mappedBy="categorie")
	@Cascade({CascadeType.ALL})
	@JsonManagedReference
	private Collection<SousCategorie> sousCategories;
	
	
	public Categorie() {
		super();
	}
	public Categorie(String nom, Theme theme) {
		super();
		this.nom = nom;
		this.theme = theme;
		
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getNom() {
		return nom;
	}
	public void setNom(String nom) {
		this.nom = nom;
	}
	public Theme getTheme() {
		return theme;
	}
	public void setTheme(Theme theme) {
		this.theme = theme;
	}
	public Collection<Livre> getLivres() {
		return livres;
	}
	public void setLivres(Collection<Livre> livres) {
		this.livres = livres;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Categorie other = (Categorie) obj;
		if (nom == null) {
			if (other.nom != null)
				return false;
		} else if (!nom.equals(other.nom))
			return false;
		if (theme == null) {
			if (other.theme != null)
				return false;
		} else if (!theme.equals(other.theme))
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "Categorie [id=" + id + ", nom=" + nom + "]";
	}
	public Collection<SousCategorie> getSousCategories() {
		return sousCategories;
	}
	public void setSousCategories(Collection<SousCategorie> sousCategories) {
		this.sousCategories = sousCategories;
	}
	
	
}
