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.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_PERC_AUT_PIAN_FERIE_PK")
public class DpercAutPianFeriePk extends BaseEntity implements Serializable {

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

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

	// ATTRIBUTES
	@Id

	@Column(name = "SEQU_ID_PERC_AUT_PIAN_FERIE_PK", columnDefinition = "NUMBER(15)")
	private Long sequIdPercAutPianFeriePk;
	@NotNull

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

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

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

	// IMPORTED PARENTS

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

	public DpercAutPianFeriePk(String flagEliminato, String utenteUltimaModifica, Date dataUltimaModifica,
			Long sequIdPercAutPianFeriePk) {
		super();

		this.flagEliminato = flagEliminato;
		this.utenteUltimaModifica = utenteUltimaModifica;
		this.dataUltimaModifica = dataUltimaModifica;
		this.sequIdPercAutPianFeriePk = sequIdPercAutPianFeriePk;
	}

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

	// PARENTS

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

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

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

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

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

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

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

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

	// CHILD GETTER/SETTER
	/**
	 * @return the DpercAutPianFerieH
	 */
	public Collection<DpercAutPianFerieH> getTheDpercAutPianFerieH() {
		return theDpercAutPianFerieH;
	}

	/**
	 * @param aDpercAutPianFerieHList to set
	 */
	public void setTheDpercAutPianFerieH(Collection<DpercAutPianFerieH> aDpercAutPianFerieHList) {
		theDpercAutPianFerieH = aDpercAutPianFerieHList;
	}

	/// ADD CHILD
	public void addDpercAutPianFerieH(DpercAutPianFerieH dpercAutPianFerieH) {
		theDpercAutPianFerieH.add(dpercAutPianFerieH);
	}

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

	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;

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

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

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