package it.micegroup.gestioneferie.pianificazioneferie.entity;

import java.io.Serializable;

import javax.validation.constraints.NotNull;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import java.util.Collection;
import java.util.ArrayList;
import javax.persistence.OneToMany;
import javax.persistence.CascadeType;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import lombok.ToString;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonProperty.Access;

import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.Getter;
import lombok.AllArgsConstructor;

import javax.persistence.Table;

import org.apache.commons.lang3.StringUtils;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.Hibernate;
import java.util.Objects;

import it.micegroup.gestioneferie.libs.entity.BaseEntity;
import it.micegroup.voila2runtime.utils.EntityUtils;

@NoArgsConstructor

@Getter
@Setter
@Entity
@Table(name = "D_STATI_RICH_APPROV_PF")
public class DstatiRichApprovPf extends BaseEntity implements Serializable {

	// Generated SERIAL VERSION UID
	private static final long serialVersionUID = 2860986138L;

	@SuppressWarnings("unused")
	private static final Logger LOGGER = LogManager.getLogger(DstatiRichApprovPf.class);

	// ATTRIBUTES
	@NotNull

	@Column(name = "SEQU_ID_STATO_RICH_APPROV_PF", columnDefinition = "NUMBER(15)")
	private Long sequIdStatoRichApprovPf;
	@NotNull

	@Column(name = "CODI_STATO_RICHIESTA_APPROV", columnDefinition = "VARCHAR(50)")
	private String codiStatoRichiestaApprov;
	@NotNull

	@Column(name = "DESC_STATO_RICHIESTA_APPROV", columnDefinition = "VARCHAR(250)")
	private String descStatoRichiestaApprov;
	@NotNull

	@Column(name = "FLAG_ELIMINATO", columnDefinition = "VARCHAR(1)")
	private String flagEliminato;
	@NotNull

	@Column(name = "DATA_FINE_RECORD", columnDefinition = "DATE")
	private Date dataFineRecord;
	@NotNull

	@Column(name = "DATA_ULTIMA_MODIFICA", columnDefinition = "DATE")
	private Date dataUltimaModifica;
	@NotNull

	@Column(name = "DATA_INIZIO_RECORD", columnDefinition = "DATE")
	private Date dataInizioRecord;
	@NotNull

	@Column(name = "UTENTE_ULTIMA_MODIFICA", columnDefinition = "VARCHAR(50)")
	private String utenteUltimaModifica;

	// IMPORTED PARENTS

	// CONSTRUCTORS
	public DstatiRichApprovPf(String objectKey) {
		super();
		setObjectKey(objectKey);
	}

	public DstatiRichApprovPf(DstatiRichApprovPfKey dstatiRichApprovPfKey, Long sequIdStatoRichApprovPf,
			String codiStatoRichiestaApprov, String descStatoRichiestaApprov, String flagEliminato, Date dataFineRecord,
			Date dataUltimaModifica, Date dataInizioRecord, String utenteUltimaModifica) {
		super();

		this.dstatiRichApprovPfKey = dstatiRichApprovPfKey;

		this.sequIdStatoRichApprovPf = sequIdStatoRichApprovPf;
		this.codiStatoRichiestaApprov = codiStatoRichiestaApprov;
		this.descStatoRichiestaApprov = descStatoRichiestaApprov;
		this.flagEliminato = flagEliminato;
		this.dataFineRecord = dataFineRecord;
		this.dataUltimaModifica = dataUltimaModifica;
		this.dataInizioRecord = dataInizioRecord;
		this.utenteUltimaModifica = utenteUltimaModifica;
	}

	// CHILDREN
	@JsonProperty(access = Access.WRITE_ONLY)
	@JsonManagedReference("theDstatiRichApprovPf")
	@OneToMany(mappedBy = "theDstatiRichApprovPf", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	@ToString.Exclude
	private Collection<DrichiesteApprovazPf> theDrichiesteApprovazPf = new ArrayList<>();

	// PARENTS

	// GETTER/SETTER
	/**
	 * @return the sequIdStatoRichApprovPf
	 */
	public Long getSequIdStatoRichApprovPf() {
		return this.sequIdStatoRichApprovPf;
	}

	/**
	 * @param sequIdStatoRichApprovPf to set
	 */
	public void setSequIdStatoRichApprovPf(Long sequIdStatoRichApprovPf) {
		this.sequIdStatoRichApprovPf = sequIdStatoRichApprovPf;
	}

	/**
	 * @return the codiStatoRichiestaApprov
	 */
	public String getCodiStatoRichiestaApprov() {
		return this.codiStatoRichiestaApprov;
	}

	/**
	 * @param codiStatoRichiestaApprov to set
	 */
	public void setCodiStatoRichiestaApprov(String codiStatoRichiestaApprov) {
		this.codiStatoRichiestaApprov = codiStatoRichiestaApprov;
	}

	/**
	 * @return the descStatoRichiestaApprov
	 */
	public String getDescStatoRichiestaApprov() {
		return this.descStatoRichiestaApprov;
	}

	/**
	 * @param descStatoRichiestaApprov to set
	 */
	public void setDescStatoRichiestaApprov(String descStatoRichiestaApprov) {
		this.descStatoRichiestaApprov = descStatoRichiestaApprov;
	}

	/**
	 * @return the flagEliminato
	 */
	public String getFlagEliminato() {
		return this.flagEliminato;
	}

	/**
	 * @param flagEliminato to set
	 */
	public void setFlagEliminato(String flagEliminato) {
		this.flagEliminato = flagEliminato;
	}

	/**
	 * @return the dataFineRecord
	 */
	public Date getDataFineRecord() {
		return this.dataFineRecord;
	}

	/**
	 * @param dataFineRecord to set
	 */
	public void setDataFineRecord(Date dataFineRecord) {
		this.dataFineRecord = dataFineRecord;
	}

	/**
	 * @return the dataUltimaModifica
	 */
	public Date getDataUltimaModifica() {
		return this.dataUltimaModifica;
	}

	/**
	 * @param dataUltimaModifica to set
	 */
	public void setDataUltimaModifica(Date dataUltimaModifica) {
		this.dataUltimaModifica = dataUltimaModifica;
	}

	/**
	 * @return the dataInizioRecord
	 */
	public Date getDataInizioRecord() {
		return this.dataInizioRecord;
	}

	/**
	 * @param dataInizioRecord to set
	 */
	public void setDataInizioRecord(Date dataInizioRecord) {
		this.dataInizioRecord = dataInizioRecord;
	}

	/**
	 * @return the utenteUltimaModifica
	 */
	public String getUtenteUltimaModifica() {
		return this.utenteUltimaModifica;
	}

	/**
	 * @param utenteUltimaModifica to set
	 */
	public void setUtenteUltimaModifica(String utenteUltimaModifica) {
		this.utenteUltimaModifica = utenteUltimaModifica;
	}

	// CHILD GETTER/SETTER
	/**
	 * @return the DrichiesteApprovazPf
	 */
	public Collection<DrichiesteApprovazPf> getTheDrichiesteApprovazPf() {
		return theDrichiesteApprovazPf;
	}

	/**
	 * @param aDrichiesteApprovazPfList to set
	 */
	public void setTheDrichiesteApprovazPf(Collection<DrichiesteApprovazPf> aDrichiesteApprovazPfList) {
		theDrichiesteApprovazPf = aDrichiesteApprovazPfList;
	}

	/// ADD CHILD
	public void addDrichiesteApprovazPf(DrichiesteApprovazPf drichiesteApprovazPf) {
		theDrichiesteApprovazPf.add(drichiesteApprovazPf);
	}

	// PARENT GETTER/SETTER

	// PARENT ID GETTER/SETTER

	// PARENT OBJECT TITLE

	// PARENT OBJECT KEY

	// IMPORTED PARENT OBJECT KEY

	// OPERATIONS

	// OBJECT KEY
	/**
	 * Restituisce l'identificativo della chiave in formato stringa. Ritorna
	 * conveniente nelle selezioni da lista.
	 * 
	 * @return L'identificativo della chiave in formato pk1||pk2||pk3...
	 */
	public String getObjectKey() {
		return getObjectKeyById(getDstatiRichApprovPfKey());
	}

	public String getObjectKeyById(DstatiRichApprovPfKey identification) {
		if (identification == null) {
			return null;
		}
		StringBuilder output = new StringBuilder();
		return output.toString();
	}

	/**
	 * Inizializza la parte identificativa del bean in base alla stringa tokenizzata
	 * da "||" fornita in input.
	 * 
	 * @param key L'identificativo della chiave in formato pk1||pk2||pk3...
	 */
	public void setObjectKey(String key) {
		if (key == null || key.trim().length() == 0) {
			return;
		}
		String[] array = StringUtils.splitByWholeSeparatorPreserveAllTokens(key, getRowIdFieldDelimiter());
		int ctr = 0;

		dstatiRichApprovPfKey = new DstatiRichApprovPfKey();
	}

	// OBJECT TITLE
	public String getObjectTitle() {
		StringBuilder output = new StringBuilder();
		output.append(getCodiStatoRichiestaApprov());
		return output.toString();
	}

	// COMPOSITE PRIMARY KEY
	@EmbeddedId
	private DstatiRichApprovPfKey dstatiRichApprovPfKey;

	// GETTER/SETTER CK
	/**
	 * @return the dstatiRichApprovPfKey
	 */
	public DstatiRichApprovPfKey getDstatiRichApprovPfKey() {
		return this.dstatiRichApprovPfKey;
	}

	/**
	 * @param dstatiRichApprovPfKey to set
	 */
	public void setDstatiRichApprovPfKey(DstatiRichApprovPfKey dstatiRichApprovPfKey) {
		this.dstatiRichApprovPfKey = dstatiRichApprovPfKey;
	}

	// COMPOSITE KEY CLASS

	// PREPERSIST
	public void syncCK() {
		if (dstatiRichApprovPfKey == null) {
			dstatiRichApprovPfKey = new DstatiRichApprovPfKey();
		}
	}

	@PrePersist
	public void prePersist() {
		syncCK();
	}

	@PreUpdate
	public void PreUpdate() {
		syncCK();
	}

	// Equals / HashCode
	@Override
	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o))
			return false;
		DstatiRichApprovPf that = (DstatiRichApprovPf) o;
		return dstatiRichApprovPfKey != null && Objects.equals(dstatiRichApprovPfKey, that.dstatiRichApprovPfKey);
	}

	@Override
	public int hashCode() {
		return Objects.hash(dstatiRichApprovPfKey);
	}
}
