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.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.FetchType;

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

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.annotation.JsonBackReference;
import javax.persistence.ManyToOne;
import javax.validation.constraints.NotNull;

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.pianificazioneferie.entity.DstatiRichApprovPfKey;

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

@NoArgsConstructor

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

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

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

	// ATTRIBUTES
	@NotNull

	@Column(name = "SEQU_ID_RICHIESTA_APPROVAZ_PF", columnDefinition = "NUMBER(15)")
	private Long sequIdRichiestaApprovazPf;
	@NotNull

	@Column(name = "CODI_RICHIESTA_APPROVAZIONE", columnDefinition = "VARCHAR(50)")
	private String codiRichiestaApprovazione;
	@NotNull

	@Column(name = "DATA_RICHIESTA_APPROVAZIONE", columnDefinition = "DATE")
	private Date dataRichiestaApprovazione;
	@NotNull

	@Column(name = "TEXT_NOTE", columnDefinition = "LONGTEXT")
	private String textNote;
	@NotNull

	@Column(name = "FK_PIANO_FERIE", columnDefinition = "NUMBER(15)")
	private Long fkPianoFerie;
	@NotNull

	@Column(name = "FK_STATO_RICH_APPROV_PF", columnDefinition = "NUMBER(15)")
	private Long fkStatoRichApprovPf;
	@NotNull

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

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

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

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

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

	// IMPORTED PARENTS

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

	public DrichiesteApprovazPf(DrichiesteApprovazPfKey drichiesteApprovazPfKey, Long sequIdRichiestaApprovazPf,
			String codiRichiestaApprovazione, Date dataRichiestaApprovazione, String textNote, Long fkPianoFerie,
			Long fkStatoRichApprovPf, Date dataInizioRecord, Date dataFineRecord, String flagEliminato,
			Date dataUltimaModifica, String utenteUltimaModifica) {
		super();

		this.drichiesteApprovazPfKey = drichiesteApprovazPfKey;

		this.sequIdRichiestaApprovazPf = sequIdRichiestaApprovazPf;
		this.codiRichiestaApprovazione = codiRichiestaApprovazione;
		this.dataRichiestaApprovazione = dataRichiestaApprovazione;
		this.textNote = textNote;
		this.fkPianoFerie = fkPianoFerie;
		this.fkStatoRichApprovPf = fkStatoRichApprovPf;
		this.dataInizioRecord = dataInizioRecord;
		this.dataFineRecord = dataFineRecord;
		this.flagEliminato = flagEliminato;
		this.dataUltimaModifica = dataUltimaModifica;
		this.utenteUltimaModifica = utenteUltimaModifica;
	}

	// CHILDREN

	// PARENTS
	@NotNull
	@ManyToOne(fetch = FetchType.LAZY)
	@JsonBackReference("theDstatiRichApprovPf")
	@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" }, allowSetters = true)
	private DstatiRichApprovPf theDstatiRichApprovPf;
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "FK_PIANO_FERIE", referencedColumnName = "SEQU_ID_PIANO_FERIE", nullable = true)
	@JsonBackReference("theDpianiFerie")
	@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" }, allowSetters = true)
	private DpianiFerie theDpianiFerie;

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

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

	/**
	 * @return the codiRichiestaApprovazione
	 */
	public String getCodiRichiestaApprovazione() {
		return this.codiRichiestaApprovazione;
	}

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

	/**
	 * @return the dataRichiestaApprovazione
	 */
	public Date getDataRichiestaApprovazione() {
		return this.dataRichiestaApprovazione;
	}

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

	/**
	 * @return the textNote
	 */
	public String getTextNote() {
		return this.textNote;
	}

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

	/**
	 * @return the fkPianoFerie
	 */
	public Long getFkPianoFerie() {
		return this.fkPianoFerie;
	}

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

	/**
	 * @return the fkStatoRichApprovPf
	 */
	public Long getFkStatoRichApprovPf() {
		return this.fkStatoRichApprovPf;
	}

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

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

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

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

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

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

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

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

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

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

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

	// CHILD GETTER/SETTER

	/// ADD CHILD

	// PARENT GETTER/SETTER
	/**
	 * @return the DstatiRichApprovPf
	 */
	@JsonProperty(access = Access.WRITE_ONLY)
	public DstatiRichApprovPf getTheDstatiRichApprovPf() {
		return theDstatiRichApprovPf;
	}

	/**
	 * @param aDstatiRichApprovPfList to set
	 */
	public void setTheDstatiRichApprovPf(DstatiRichApprovPf aDstatiRichApprovPf) {
		theDstatiRichApprovPf = aDstatiRichApprovPf;
	}

	/**
	 * @return the DpianiFerie
	 */
	@JsonProperty(access = Access.WRITE_ONLY)
	public DpianiFerie getTheDpianiFerie() {
		return theDpianiFerie;
	}

	/**
	 * @param aDpianiFerieList to set
	 */
	public void setTheDpianiFerie(DpianiFerie aDpianiFerie) {
		theDpianiFerie = aDpianiFerie;
	}

	// PARENT ID GETTER/SETTER
	/**
	 * Return the dstatiRichApprovPfKey from theDstatiRichApprovPf.
	 * 
	 * @return dstatiRichApprovPfKey from theDstatiRichApprovPf.
	 */
	@JsonProperty
	public DstatiRichApprovPfKey getDstatiRichApprovPfKey() {
		// If the parent entity object is null, then return null
		if (getTheDstatiRichApprovPf() == null) {
			return null;
		}
		// Return requested attribute
		return theDstatiRichApprovPf.getDstatiRichApprovPfKey();
	}

	/**
	 * Return the sequIdPianoFerie from theDpianiFerie.
	 * 
	 * @return sequIdPianoFerie from theDpianiFerie.
	 */
	@JsonProperty
	public Long getSequIdPianoFerie() {
		// If the parent entity object is null, then return null
		if (getTheDpianiFerie() == null) {
			return null;
		}
		// Return requested attribute
		return theDpianiFerie.getSequIdPianoFerie();
	}

	// PARENT OBJECT TITLE
	/**
	 * Return the object title of theDstatiRichApprovPf.
	 * 
	 * @return the object title of theDstatiRichApprovPf.
	 */
	@JsonProperty
	public String getTheDstatiRichApprovPfObjectTitle() {
		return getTheDstatiRichApprovPf() != null ? getTheDstatiRichApprovPf().getObjectTitle() : null;
	}

	/**
	 * Return the object title of theDpianiFerie.
	 * 
	 * @return the object title of theDpianiFerie.
	 */
	@JsonProperty
	public String getTheDpianiFerieObjectTitle() {
		return getTheDpianiFerie() != null ? getTheDpianiFerie().getObjectTitle() : null;
	}

	// PARENT OBJECT KEY
	/**
	 * Return the object key of theDstatiRichApprovPf.
	 * 
	 * @return the object key of theDstatiRichApprovPf.
	 */
	@JsonProperty
	public String getTheDstatiRichApprovPfObjectKey() {
		return getTheDstatiRichApprovPf() != null ? getTheDstatiRichApprovPf().getObjectKey() : null;
	}

	/**
	 * Set object key of theDstatiRichApprovPf.
	 * 
	 */
	@JsonProperty
	public void setTheDstatiRichApprovPfObjectKey(String objectKey) {
		if (EntityUtils.isValueChanged(getTheDstatiRichApprovPfObjectKey(), objectKey, false)) {
			DstatiRichApprovPf dstatiRichApprovPf = new DstatiRichApprovPf();
			dstatiRichApprovPf.setObjectKey(objectKey);
			setTheDstatiRichApprovPf(dstatiRichApprovPf);
		}
	}

	/**
	 * Return the object key of theDpianiFerie.
	 * 
	 * @return the object key of theDpianiFerie.
	 */
	@JsonProperty
	public String getTheDpianiFerieObjectKey() {
		return getTheDpianiFerie() != null ? getTheDpianiFerie().getObjectKey() : null;
	}

	/**
	 * Set object key of theDpianiFerie.
	 * 
	 */
	@JsonProperty
	public void setTheDpianiFerieObjectKey(String objectKey) {
		if (EntityUtils.isValueChanged(getTheDpianiFerieObjectKey(), objectKey, false)) {
			DpianiFerie dpianiFerie = new DpianiFerie();
			dpianiFerie.setObjectKey(objectKey);
			setTheDpianiFerie(dpianiFerie);
		}
	}

	// 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(getDrichiesteApprovazPfKey());
	}

	public String getObjectKeyById(DrichiesteApprovazPfKey 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;

		drichiesteApprovazPfKey = new DrichiesteApprovazPfKey();
	}

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

	// COMPOSITE PRIMARY KEY
	@EmbeddedId
	private DrichiesteApprovazPfKey drichiesteApprovazPfKey;

	// GETTER/SETTER CK
	/**
	 * @return the drichiesteApprovazPfKey
	 */
	public DrichiesteApprovazPfKey getDrichiesteApprovazPfKey() {
		return this.drichiesteApprovazPfKey;
	}

	/**
	 * @param drichiesteApprovazPfKey to set
	 */
	public void setDrichiesteApprovazPfKey(DrichiesteApprovazPfKey drichiesteApprovazPfKey) {
		this.drichiesteApprovazPfKey = drichiesteApprovazPfKey;
	}

	// COMPOSITE KEY CLASS

	// PREPERSIST
	public void syncCK() {
		if (drichiesteApprovazPfKey == null) {
			drichiesteApprovazPfKey = new DrichiesteApprovazPfKey();
		}
	}

	@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;
		DrichiesteApprovazPf that = (DrichiesteApprovazPf) o;
		return drichiesteApprovazPfKey != null && Objects.equals(drichiesteApprovazPfKey, that.drichiesteApprovazPfKey);
	}

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