package it.micegroup.gestioneferie.configurazionepf.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.Id;
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.libs.entity.BaseEntity;
import it.micegroup.voila2runtime.utils.EntityUtils;

@NoArgsConstructor

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

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

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

	// ATTRIBUTES
	@Id

	@Column(name = "SEQU_ID_PERC_AUT_PIAN_FERIE_H", columnDefinition = "NUMBER(15)")
	private Long sequIdPercAutPianFerieH;
	@NotNull

	@Column(name = "FK_CONFIG_PA_PIANO_FERIE", columnDefinition = "NUMBER(15)")
	private Long fkConfigPaPianoFerie;
	@NotNull

	@Column(name = "FK_PERCORSO", columnDefinition = "NUMBER(15)")
	private Long fkPercorso;
	@NotNull

	@Column(name = "FK_PERC_AUT_PIAN_FERIE_PK", columnDefinition = "NUMBER(15)")
	private Long fkPercAutPianFeriePk;
	@NotNull

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

	@Column(name = "DATA_FINE_VALIDITA", columnDefinition = "DATE")
	private Date dataFineValidita;
	@NotNull

	@Column(name = "DATA_INIZIO_VALIDITA", columnDefinition = "DATE")
	private Date dataInizioValidita;
	@NotNull

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

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

	// IMPORTED PARENTS

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

	public DpercAutPianFerieH(Long fkConfigPaPianoFerie, Long fkPercorso, Long fkPercAutPianFeriePk,
			Date dataUltimaModifica, Date dataFineValidita, Date dataInizioValidita, String utenteUltimaModifica,
			String flagEliminato, Long sequIdPercAutPianFerieH) {
		super();

		this.fkConfigPaPianoFerie = fkConfigPaPianoFerie;
		this.fkPercorso = fkPercorso;
		this.fkPercAutPianFeriePk = fkPercAutPianFeriePk;
		this.dataUltimaModifica = dataUltimaModifica;
		this.dataFineValidita = dataFineValidita;
		this.dataInizioValidita = dataInizioValidita;
		this.utenteUltimaModifica = utenteUltimaModifica;
		this.flagEliminato = flagEliminato;
		this.sequIdPercAutPianFerieH = sequIdPercAutPianFerieH;
	}

	// CHILDREN

	// PARENTS
	@NotNull
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "FK_PERC_AUT_PIAN_FERIE_PK", referencedColumnName = "SEQU_ID_PERC_AUT_PIAN_FERIE_PK", nullable = false)
	@JsonBackReference("theDpercAutPianFeriePk")
	@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" }, allowSetters = true)
	private DpercAutPianFeriePk theDpercAutPianFeriePk;
	@NotNull
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "FK_CONFIG_PA_PIANO_FERIE", referencedColumnName = "SEQU_ID_CONFIG_PA_PIANO_FERIE", nullable = false)
	@JsonBackReference("theDconfigPaPianiFerie")
	@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" }, allowSetters = true)
	private DconfigPaPianiFerie theDconfigPaPianiFerie;

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

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

	/**
	 * @return the fkConfigPaPianoFerie
	 */
	public Long getFkConfigPaPianoFerie() {
		return this.fkConfigPaPianoFerie;
	}

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

	/**
	 * @return the fkPercorso
	 */
	public Long getFkPercorso() {
		return this.fkPercorso;
	}

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

	/**
	 * @return the fkPercAutPianFeriePk
	 */
	public Long getFkPercAutPianFeriePk() {
		return this.fkPercAutPianFeriePk;
	}

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

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

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

	/**
	 * @return the dataFineValidita
	 */
	public Date getDataFineValidita() {
		return this.dataFineValidita;
	}

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

	/**
	 * @return the dataInizioValidita
	 */
	public Date getDataInizioValidita() {
		return this.dataInizioValidita;
	}

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

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

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

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

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

	// CHILD GETTER/SETTER

	/// ADD CHILD

	// PARENT GETTER/SETTER
	/**
	 * @return the DpercAutPianFeriePk
	 */
	@JsonProperty(access = Access.WRITE_ONLY)
	public DpercAutPianFeriePk getTheDpercAutPianFeriePk() {
		return theDpercAutPianFeriePk;
	}

	/**
	 * @param aDpercAutPianFeriePkList to set
	 */
	public void setTheDpercAutPianFeriePk(DpercAutPianFeriePk aDpercAutPianFeriePk) {
		theDpercAutPianFeriePk = aDpercAutPianFeriePk;
	}

	/**
	 * @return the DconfigPaPianiFerie
	 */
	@JsonProperty(access = Access.WRITE_ONLY)
	public DconfigPaPianiFerie getTheDconfigPaPianiFerie() {
		return theDconfigPaPianiFerie;
	}

	/**
	 * @param aDconfigPaPianiFerieList to set
	 */
	public void setTheDconfigPaPianiFerie(DconfigPaPianiFerie aDconfigPaPianiFerie) {
		theDconfigPaPianiFerie = aDconfigPaPianiFerie;
	}

	// PARENT ID GETTER/SETTER
	/**
	 * Return the sequIdPercAutPianFeriePk from theDpercAutPianFeriePk.
	 * 
	 * @return sequIdPercAutPianFeriePk from theDpercAutPianFeriePk.
	 */
	@JsonProperty
	public Long getSequIdPercAutPianFeriePk() {
		// If the parent entity object is null, then return null
		if (getTheDpercAutPianFeriePk() == null) {
			return null;
		}
		// Return requested attribute
		return theDpercAutPianFeriePk.getSequIdPercAutPianFeriePk();
	}

	/**
	 * Return the sequIdConfigPaPianoFerie from theDconfigPaPianiFerie.
	 * 
	 * @return sequIdConfigPaPianoFerie from theDconfigPaPianiFerie.
	 */
	@JsonProperty
	public Long getSequIdConfigPaPianoFerie() {
		// If the parent entity object is null, then return null
		if (getTheDconfigPaPianiFerie() == null) {
			return null;
		}
		// Return requested attribute
		return theDconfigPaPianiFerie.getSequIdConfigPaPianoFerie();
	}

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

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

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

	/**
	 * Set object key of theDpercAutPianFeriePk.
	 * 
	 */
	@JsonProperty
	public void setTheDpercAutPianFeriePkObjectKey(String objectKey) {
		if (EntityUtils.isValueChanged(getTheDpercAutPianFeriePkObjectKey(), objectKey, false)) {
			DpercAutPianFeriePk dpercAutPianFeriePk = new DpercAutPianFeriePk();
			dpercAutPianFeriePk.setObjectKey(objectKey);
			setTheDpercAutPianFeriePk(dpercAutPianFeriePk);
		}
	}

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

	/**
	 * Set object key of theDconfigPaPianiFerie.
	 * 
	 */
	@JsonProperty
	public void setTheDconfigPaPianiFerieObjectKey(String objectKey) {
		if (EntityUtils.isValueChanged(getTheDconfigPaPianiFerieObjectKey(), objectKey, false)) {
			DconfigPaPianiFerie dconfigPaPianiFerie = new DconfigPaPianiFerie();
			dconfigPaPianiFerie.setObjectKey(objectKey);
			setTheDconfigPaPianiFerie(dconfigPaPianiFerie);
		}
	}

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

	public String getObjectKeyById(Long identification) {
		if (identification == null) {
			return null;
		}
		StringBuilder output = new StringBuilder();
		output.append(identification);
		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;

		setSequIdPercAutPianFerieH(getLongCheckedAgainstNullContent(array[ctr]));
	}

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

	// PREPERSIST

	@PrePersist
	public void prePersist() {
	}

	@PreUpdate
	public void PreUpdate() {
	}

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

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