package com.fp.persistence.pcustomer.gene;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.Query;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

import com.fp.dto.hb.HibernateBean;
import com.fp.dto.hb.Log;

/**
 * Clase que implementa la entidad de Hibernate que hace referencia a la tabla
 * TCUSTPERSONPHONE
 */
@Entity(name = "TcustPersonPhone")
@Table(name = "TCUSTPERSONPHONE")
public class TcustPersonPhone extends com.fp.dto.AbstractDataTransport implements Serializable, HibernateBean, Cloneable, Log {

    /**
     * HashCode asociado con la Instancia
     */
    @Transient
    private int hashValue = 0;
    /**
     * Version de la Clase
     */
    private static final long serialVersionUID = 1L;
    /**
     * Clave primaria de la Entidad TcustPersonPhone
     */
    @EmbeddedId
    @GenericGenerator(name = "seq_id", strategy = "com.fp.general.keygen.SubSequenceKey", parameters = {
        @Parameter(name = "sql", value = "select coalesce(max(PHONESEQUENCE),0)+1 from TCUSTPERSONPHONE where personcode=:code"),
        @Parameter(name = "type", value = "java.lang.Integer"),
        @Parameter(name = "param", value = "code,pk.personcode"),
        @Parameter(name = "field", value = "phonesequence")})
    @GeneratedValue(generator = "seq_id")
    private TcustPersonPhoneKey pk;
    @Column(name = "DATEFROM", nullable = true)
    /**
     * Fecha desde la cual esta vigente el registro del telefono
     */
    private Timestamp datefrom;
    @Version
    @Column(name = "RECORDVERSION", nullable = true)
    /**
     * Optimistic locking del registro
     */
    private Integer recordversion;
    @Column(name = "PHONETYPECATALOG", nullable = true)
    /**
     * Codigo de catalogo 1 Celular, 2 Fijo
     */
    private String phonetypecatalog;
    @Column(name = "PHONETYPECATALOGCODE", nullable = true)
    /**
     * Codigo de tabla de catalogo PHONETYPE
     */
    private String phonetypecatalogcode;
    @Column(name = "INGRESSUSERCODE", nullable = true)
    /**
     * Codigo de usuario que crea el registro
     */
    private String ingressusercode;
    @Column(name = "MODIFYUSERCODE", nullable = true)
    /**
     * Codigo de usuario que modifica el registro
     */
    private String modifyusercode;
    @Column(name = "ADDRESSNUMBER", nullable = true)
    /**
     * Numero de direccion asociada al telefono
     */
    private Integer addressnumber;
    @Column(name = "AREACODE", nullable = true)
    /**
     * Codigo de area del telefono
     */
    private Integer areacode;
    @Column(name = "PHONENUMBER", nullable = true)
    /**
     * Numero de telefono
     */
    private String phonenumber;
    @Column(name = "EXTENSION", nullable = true)
    /**
     * Codigo de extension del telefono
     */
    private Integer extension;

    /**
     * Contructor por defecto
     */
    public TcustPersonPhone() {
    }

    /**
     * Contructor de TcustPersonPhone
     *
     * @param pPk Clave Primaria del entity
     */
    public TcustPersonPhone(TcustPersonPhoneKey pPk) {
        this();
        this.pk = pPk;
    }

    /**
     * Metodo que entrega datos de la tabla dada la clave primaria.
     *
     * @param pEntityManager referencia de la session a obtener datos del bean.
     * @param pKey Caleve primaria del bean.
     * @return TcustPersonPhone
     */
    public static TcustPersonPhone find(EntityManager pEntityManager, TcustPersonPhoneKey pKey) throws Exception {
        TcustPersonPhone obj = pEntityManager.find(TcustPersonPhone.class, pKey);
        return obj;
    }

    /**
     * Entrega la Clave primaria de TcustPersonPhone
     *
     * @return El objeto que referencia a la Clave primaria de TcustPersonPhone
     */
    public TcustPersonPhoneKey getPk() {
        return this.pk;
    }

    /**
     * Fija un nuevo valor a la Clave primaria de TcustPersonPhone
     *
     * @param pPk El objeto que referencia a la nueva Clave primaria de
     * TcustPersonPhone
     */
    public void setPk(TcustPersonPhoneKey pPk) {
        this.pk = pPk;
    }

    /**
     * Obtiene el valor de datefrom
     *
     * @return valor de datefrom
     */
    public Timestamp getDatefrom() {
        return this.datefrom;
    }

    /**
     * Fija el valor de datefrom
     *
     * @param pDatefrom nuevo Valor de datefrom
     */
    public void setDatefrom(Timestamp pDatefrom) {
        this.datefrom = pDatefrom;
    }

    /**
     * Obtiene el valor de recordversion
     *
     * @return valor de recordversion
     */
    public Integer getRecordversion() {
        return this.recordversion;
    }

    /**
     * Fija el valor de recordversion
     *
     * @param pRecordversion nuevo Valor de recordversion
     */
    public void setRecordversion(Integer pRecordversion) {
        this.recordversion = pRecordversion;
    }

    /**
     * Obtiene el valor de phonetypecatalog
     *
     * @return valor de phonetypecatalog
     */
    public String getPhonetypecatalog() {
        return this.phonetypecatalog;
    }

    /**
     * Fija el valor de phonetypecatalog
     *
     * @param pPhonetypecatalog nuevo Valor de phonetypecatalog
     */
    public void setPhonetypecatalog(String pPhonetypecatalog) {
        this.phonetypecatalog = pPhonetypecatalog;
    }

    /**
     * Obtiene el valor de phonetypecatalogcode
     *
     * @return valor de phonetypecatalogcode
     */
    public String getPhonetypecatalogcode() {
        return this.phonetypecatalogcode;
    }

    /**
     * Fija el valor de phonetypecatalogcode
     *
     * @param pPhonetypecatalogcode nuevo Valor de phonetypecatalogcode
     */
    public void setPhonetypecatalogcode(String pPhonetypecatalogcode) {
        this.phonetypecatalogcode = pPhonetypecatalogcode;
    }

    /**
     * Obtiene el valor de ingressusercode
     *
     * @return valor de ingressusercode
     */
    public String getIngressusercode() {
        return this.ingressusercode;
    }

    /**
     * Fija el valor de ingressusercode
     *
     * @param pIngressusercode nuevo Valor de ingressusercode
     */
    public void setIngressusercode(String pIngressusercode) {
        this.ingressusercode = pIngressusercode;
    }

    /**
     * Obtiene el valor de modifyusercode
     *
     * @return valor de modifyusercode
     */
    public String getModifyusercode() {
        return this.modifyusercode;
    }

    /**
     * Fija el valor de modifyusercode
     *
     * @param pModifyusercode nuevo Valor de modifyusercode
     */
    public void setModifyusercode(String pModifyusercode) {
        this.modifyusercode = pModifyusercode;
    }

    /**
     * Obtiene el valor de addressnumber
     *
     * @return valor de addressnumber
     */
    public Integer getAddressnumber() {
        return this.addressnumber;
    }

    /**
     * Fija el valor de addressnumber
     *
     * @param pAddressnumber nuevo Valor de addressnumber
     */
    public void setAddressnumber(Integer pAddressnumber) {
        this.addressnumber = pAddressnumber;
    }

    /**
     * Obtiene el valor de areacode
     *
     * @return valor de areacode
     */
    public Integer getAreacode() {
        return this.areacode;
    }

    /**
     * Fija el valor de areacode
     *
     * @param pAreacode nuevo Valor de areacode
     */
    public void setAreacode(Integer pAreacode) {
        this.areacode = pAreacode;
    }

    /**
     * Obtiene el valor de phonenumber
     *
     * @return valor de phonenumber
     */
    public String getPhonenumber() {
        return this.phonenumber;
    }

    /**
     * Fija el valor de phonenumber
     *
     * @param pPhonenumber nuevo Valor de phonenumber
     */
    public void setPhonenumber(String pPhonenumber) {
        this.phonenumber = pPhonenumber;
    }

    /**
     * Obtiene el valor de extension
     *
     * @return valor de extension
     */
    public Integer getExtension() {
        return this.extension;
    }

    /**
     * Fija el valor de extension
     *
     * @param pExtension nuevo Valor de extension
     */
    public void setExtension(Integer pExtension) {
        this.extension = pExtension;
    }

    @Override
    public boolean equals(Object rhs) {
        if (rhs == null) {
            return false;
        }
        if (!(rhs instanceof TcustPersonPhone)) {
            return false;
        }
        TcustPersonPhone that = (TcustPersonPhone) rhs;
        if ((this.getPk() == null) || (that.getPk() == null)) {
            return false;
        }
        return (this.getPk().equals(that.getPk()));
    }

    /**
     * Implementaci�n del metodo hashCode de la la entidad TcustPersonPhone
     *
     * @return el hashCode la instancia
     */
    @Override
    public int hashCode() {
        if (this.hashValue == 0) {
            int result = 17;
            if (this.getPk() == null) {
                result = super.hashCode();
            } else {
                result = this.getPk().hashCode();
            }
            this.hashValue = result;
        }
        return this.hashValue;
    }

    /**
     * Implementaci�n toString
     */
    @Override
    public String toString() {
        Field[] fs = this.getClass().getDeclaredFields();
        String data = "";
        for (Field f : fs) {
            try {
                String name = f.getName();
                if (f.getType().getName().compareTo("java.util.Set") == 0) {
                    continue;
                }
                if ((name.compareTo("hashValue") == 0) || (name.compareTo("serialVersionUID") == 0)) {
                    continue;
                }
                data += name + "=" + f.get(this) + ";";
            } catch (Exception e) {
                continue;
            }
        }
        if (data.compareTo("") == 0) {
            data = super.toString();
        }
        return data;
    }

    /**
     * Implementaci�n de la creaci�n de un bean en blanco TcustPersonPhone
     */
    @Override
    public Object createInstance() {
        TcustPersonPhone instance = new TcustPersonPhone();
        instance.setPk(new TcustPersonPhoneKey());
        return instance;
    }

    /**
     * Clona la entidad TcustPersonPhone
     *
     * @see com.fp.dto.hb.HibernateBean#cloneMe()
     */
    @Override
    public Object cloneMe() throws CloneNotSupportedException {
        TcustPersonPhone p = (TcustPersonPhone) this.clone();
        p.setPk((TcustPersonPhoneKey) this.pk.cloneMe());
        return p;
    }

    public Object getId() {
        return this.pk;
    }
    /**
     * jpql Que retorna un objeto TcustPersonPhone a partir de un parametros
     * espec�ficos
     */
    public static final String JPQL_GET_ACCOUNT_PARAMETER = "from TcustPersonPhone t" + " where t.pk.personcode = :personcode "
            + "and t.pk.dateto = :dateto ";

    public static List<TcustPersonPhone> findPhones(EntityManager pEntityManager, Integer personcode, Timestamp dateto) throws Exception {
        List<TcustPersonPhone> lTcustPersonPhone = new ArrayList<TcustPersonPhone>();
        Query qry = pEntityManager.createQuery(TcustPersonPhone.JPQL_GET_ACCOUNT_PARAMETER);
        qry.setParameter("personcode", personcode);
        qry.setParameter("dateto", dateto);
        return lTcustPersonPhone = qry.getResultList();
    }
}
