package com.jasamedika.medifirst2000.entities;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.jasamedika.medifirst2000.base.BaseMaster;
import com.jasamedika.medifirst2000.helper.Caption;

@Entity
@Table(name = "ModulAplikasi_S")
public class ModulAplikasi extends BaseMaster {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2915289408717017206L;
	
	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "public.modulaplikasi_m_id_seq")
	@javax.persistence.SequenceGenerator(
			name = "public.modulaplikasi_m_id_seq", 
			sequenceName = "public.modulaplikasi_m_id_seq", 
			allocationSize = 1
	)
	@Column(name = "id")
	protected Integer id;

	public Integer getId() {
		return id;
	}

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

	// @NotNull(message = "Kd Modul Aplikasi tidak boleh kosong")
	@Column(name = "KdModulAplikasi", nullable = false, length = 2)
	@Caption(value = "Kode Modul Aplikasi")
	private String kdModulAplikasi;

	public String getKdModulAplikasi() {
		return kdModulAplikasi;
	}

	public void setKdModulAplikasi(String kdModulAplikasi) {
		this.kdModulAplikasi = kdModulAplikasi;
	}

	@NotNull(message = "Modul Aplikasi tidak boleh kosong")
	@Column(name = "ModulAplikasi", nullable = false, length = 50)
	@Caption(value = "Modul Aplikasi")
	private String modulAplikasi;

	public String getModulAplikasi() {
		return modulAplikasi;
	}

	public void setModulAplikasi(String modulAplikasi) {
		this.modulAplikasi = modulAplikasi;
	}

	@Column(name = "NoUrut")
	private Integer noUrut;

	public Integer getNoUrut() {
		return noUrut;
	}

	public void setNoUrut(Integer noUrut) {
		this.noUrut = noUrut;
	}

	@Column(name = "IconImage")
	private String iconImage;

	public String getIconImage() {
		return iconImage;
	}

	public void setIconImage(String iconImage) {
		this.iconImage = iconImage;
	}

	/////////////////////
	// Many To Many Relationship
	////////////////////

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "MapObjekModulAplikasiToModulAplikasi_S", 
			joinColumns = @JoinColumn(name = "modulAplikasiId"), 
			inverseJoinColumns = @JoinColumn(name = "objekModulAplikasiId"))
	@JsonIgnore
	private List<ObjekModulAplikasi> objekModulAplikasi = new ArrayList<>();
	

	public List<ObjekModulAplikasi> getObjekModulAplikasi() {
		return objekModulAplikasi;
	}

	public void setObjekModulAplikasi(List<ObjekModulAplikasi> objekModulAplikasi) {
		this.objekModulAplikasi = objekModulAplikasi;
	}

	/////////////////////
	// Link Back Keys
	////////////////////


	@Column(name = "KdModulAplikasiHead", insertable = false, updatable = false, nullable = true)
	private Integer kdModulAplikasiHead;

	public Integer getKdModulAplikasiHead() {
		return kdModulAplikasiHead;
	}

	public void setKdModulAplikasiHead(Integer kdModulAplikasiHead) {
		this.kdModulAplikasiHead = kdModulAplikasiHead;
	}

	@NotFound(action = NotFoundAction.IGNORE)
	@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true, targetEntity = ModulAplikasi.class)
	@JoinColumn(name = "KdModulAplikasiHead")
	@OrderBy("noUrut")
	private Set<ModulAplikasi> children = new HashSet<>();

	public Set<ModulAplikasi> getChildren() {
		return children;
	}

	public void setChildren(Set<ModulAplikasi> children) {
		this.children = children;
	}

}
