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_CONFIG_PA_PF_UO_H")
public class DconfigPaPfUoH extends BaseEntity implements Serializable {

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

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

	// ATTRIBUTES
	@Id

	@Column(name = "SEQU_ID_CONFIG_PA_PF_UO_H", columnDefinition = "NUMBER(15)")
	private Long sequIdConfigPaPfUoH;
	@NotNull

	@Column(name = "FLAG_VALIDO_UO_SUBORDINATE", columnDefinition = "VARCHAR(1)")
	private String flagValidoUoSubordinate;
	@NotNull

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

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

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

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

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

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

	// IMPORTED PARENTS

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

	public DconfigPaPfUoH(String flagValidoUoSubordinate, Long fkConfigPaPianoFerie, Long fkUnitaFunzionale,
			Long fkUnitaOrganizzativaPk, String utenteUltimaModifica, String flagEliminato, Date dataInizioValidita,
			Date dataFineValidita, Date dataUltimaModifica, Long sequIdConfigPaPfUoH) {
		super();

		this.flagValidoUoSubordinate = flagValidoUoSubordinate;
		this.fkConfigPaPianoFerie = fkConfigPaPianoFerie;
		this.fkUnitaFunzionale = fkUnitaFunzionale;
		this.fkUnitaOrganizzativaPk = fkUnitaOrganizzativaPk;
		this.utenteUltimaModifica = utenteUltimaModifica;
		this.flagEliminato = flagEliminato;
		this.dataInizioValidita = dataInizioValidita;
		this.dataFineValidita = dataFineValidita;
		this.dataUltimaModifica = dataUltimaModifica;
		this.sequIdConfigPaPfUoH = sequIdConfigPaPfUoH;
	}

	// CHILDREN

	// PARENTS
	@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 sequIdConfigPaPfUoH
	 */
	public Long getSequIdConfigPaPfUoH() {
		return this.sequIdConfigPaPfUoH;
	}

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

	/**
	 * @return the flagValidoUoSubordinate
	 */
	public String getFlagValidoUoSubordinate() {
		return this.flagValidoUoSubordinate;
	}

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

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

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

	/**
	 * @return the fkUnitaFunzionale
	 */
	public Long getFkUnitaFunzionale() {
		return this.fkUnitaFunzionale;
	}

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

	/**
	 * @return the fkUnitaOrganizzativaPk
	 */
	public Long getFkUnitaOrganizzativaPk() {
		return this.fkUnitaOrganizzativaPk;
	}

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

	/**
	 * @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;
	}

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

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

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

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

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

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

	// CHILD GETTER/SETTER

	/// ADD CHILD

	// PARENT GETTER/SETTER
	/**
	 * @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 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 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 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(getSequIdConfigPaPfUoH());
	}

	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;

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

	// OBJECT TITLE
	public String getObjectTitle() {
		StringBuilder output = new StringBuilder();
		output.append(getFlagValidoUoSubordinate());
		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;
		DconfigPaPfUoH that = (DconfigPaPfUoH) o;
		return sequIdConfigPaPfUoH != null && Objects.equals(sequIdConfigPaPfUoH, that.sequIdConfigPaPfUoH);
	}

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