package com.jasamedika.medifirst2000.entities;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.jasamedika.medifirst2000.base.BaseMaster;
import com.jasamedika.medifirst2000.helper.Caption;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.NotFound;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static javax.persistence.CascadeType.ALL;
import static javax.persistence.FetchType.EAGER;
import static javax.persistence.FetchType.LAZY;
import static javax.persistence.GenerationType.SEQUENCE;
import static org.hibernate.annotations.NotFoundAction.IGNORE;

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

	private static final long serialVersionUID = -2915289408717017206L;

	@Id
	@GeneratedValue(strategy = SEQUENCE, generator = "public.modulaplikasi_m_id_seq")
	@SequenceGenerator(name = "public.modulaplikasi_m_id_seq", sequenceName = "public.modulaplikasi_m_id_seq", allocationSize = 1)
	@Column(name = "id")
	protected Integer id;

	@Column(name = "KdModulAplikasi", nullable = false, length = 2)
	@Caption(value = "Kode Modul Aplikasi")
	private String kdModulAplikasi;

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

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

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

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

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

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

}
