package com.fp.persistence.pbpm.gene;

import java.lang.reflect.Field;
import java.util.List;

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

import com.fp.dto.hb.HibernateBean;

/**
 * Clase que implementa la entidad de Hibernate que hace referencia a la tabla TBPMGROUPSUSERS
 */
@Entity(name = "TbpmGroupsUsers")
@Table(name = "TBPMGROUPSUSERS")
public class TbpmGroupsUsers extends com.fp.dto.AbstractDataTransport implements HibernateBean {
    /**
     * 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 TbpmGroupsUsers
     */
    @EmbeddedId
    private TbpmGroupsUsersKey pk;

    @Column(name = "RECORDVERSION", nullable = true)
    /**
     * Optimistick locking del registro
     */
    private Integer recordversion;

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

    /**
     * Contructor de TbpmGroupsUsers
     * 
     * @param pPk Clave Primaria del entity
     */
    public TbpmGroupsUsers(TbpmGroupsUsersKey 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 TbpmGroupsUsers
     */
    public static TbpmGroupsUsers find(EntityManager pEntityManager, TbpmGroupsUsersKey pKey) throws Exception {
        TbpmGroupsUsers obj = pEntityManager.find(TbpmGroupsUsers.class, pKey);
        return obj;
    }

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

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

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

    @Override
    public boolean equals(Object rhs) {
        if (rhs == null) {
            return false;
        }
        if (!(rhs instanceof TbpmGroupsUsers)) {
            return false;
        }
        TbpmGroupsUsers that = (TbpmGroupsUsers) 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 TbpmGroupsUsers
     * 
     * @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 TbpmGroupsUsers */
    @Override
    public Object createInstance() {
        TbpmGroupsUsers instance = new TbpmGroupsUsers();
        instance.setPk(new TbpmGroupsUsersKey());
        return instance;
    }

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

    // Metodos manuales.

    /** Sentecia que entrega una lista de TbpmGroupsUsers dado un codigo de grupo. */
    private static final String HQL_USER = "from TbpmGroupsUsers t " + " where t.pk.groupcode = :groupcode order by t.pk.usercode ";

    /** Sentecia que entrega una lista de TbpmGroupsUsers dado un codigo de grupo. */
    private static final String HQL_GROUPS_BY_USER = "from TbpmGroupsUsers t " + " where t.pk.usercode = :user order by t.pk.usercode ";

    /**
     * Metodo que entrega una lista de registros de la tabla TbpmGroupsUsers dado un codigo de grupo.
     * 
     * @param pEntityManager Referencia a la session de la base de datos.
     * @param pGroupcode Codigo de grupo a obtener una lista de usuarios.
     * @return List<TbpmGroupsUsers>
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public static List<TbpmGroupsUsers> find(EntityManager pEntityManager, String pGroupcode) throws Exception {
        List<TbpmGroupsUsers> ldata = null;
        Query qry = pEntityManager.createQuery(TbpmGroupsUsers.HQL_USER);
        qry.setParameter("groupcode", pGroupcode);
        ldata = qry.getResultList();
        return ldata;
    }

    /**
     * Metodo que entrega una lista de registros de la tabla TbpmGroupsUsers dado un codigo de grupo.
     * 
     * @param pEntityManager Referencia a la session de la base de datos.
     * @param pGroupcode Codigo de grupo a obtener una lista de usuarios.
     * @return List<TbpmGroupsUsers>
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public static List<TbpmGroupsUsers> findGroupsByUser(EntityManager pEntityManager, String pUser) throws Exception {
        List<TbpmGroupsUsers> ldata = null;
        Query qry = pEntityManager.createQuery(TbpmGroupsUsers.HQL_GROUPS_BY_USER);
        qry.setParameter("user", pUser);
        ldata = qry.getResultList();
        return ldata;
    }
}
