package org.codiki.core.entities.persistence;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.codiki.core.entities.dto.CategoryDTO;
import org.codiki.core.entities.dto.View;

import com.fasterxml.jackson.annotation.JsonView;
import org.hibernate.annotations.Proxy;

@Entity
@Table(name="category")
@Inheritance(strategy = InheritanceType.JOINED)
@Proxy(lazy = false)
public class Category implements Serializable {
	private static final long serialVersionUID = 1L;

	/* ******************* */
	/* Attributes */
	/* ******************* */
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@JsonView({View.PostDTO.class, View.CategoryDTO.class})
	private Long id;
	
	@JsonView({View.PostDTO.class, View.CategoryDTO.class})
	private String name;

	/* ******************* */
	/* Relations */
	/* ******************* */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "creator_id")
	protected User creator;

	@JsonView({View.CategoryDTO.class})
	@OneToMany(mappedBy = "mainCategory")
	private List<SubCategory> listSubCategories;
	
	@OneToMany(mappedBy = "category")
	protected List<Post> listPosts;

	/* ******************* */
	/* Constructors */
	/* ******************* */
	public Category() {
		super();
		listSubCategories = new LinkedList<>();
	}
	
	public Category(final CategoryDTO pCategory) {
		this();
		id = pCategory.getId();
		name = pCategory.getName();
	}
	
	/* ******************* */
	/* Getters & Setters */
	/* ******************* */
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		if(this.id != null) {
			throw new IllegalAccessError("It's not allowed to rewrite the id entity.");
		}
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public User getCreator() {
		return creator;
	}

	public void setCreator(User creator) {
		this.creator = creator;
	}
	
	public List<SubCategory> getListSubCategories() {
		return listSubCategories;
	}
}
