package com.fp.persistence.pgeneral.acco;

import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Column;
import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
import javax.persistence.Query;

import java.io.Serializable;
import javax.persistence.Table;

import com.fp.common.exception.CommonException;
import com.fp.dto.hb.HibernateBean;
import java.lang.reflect.Field;
import javax.persistence.Transient;

/**Clase que implementa la entidad de Hibernate que hace referencia a la tabla TGENEACCOUNTNUMBER*/
@Entity(name="TgeneAccountNumber")
@Table(name="TGENEACCOUNTNUMBER")
public class TgeneAccountNumber extends com.fp.dto.AbstractDataTransport implements Serializable,HibernateBean,Cloneable{
/**
* 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 TgeneAccountNumber
*/
@EmbeddedId
private TgeneAccountNumberKey pk;
@Column(name="PRODUCTCODE", nullable=true)

/**
* Codigo de producto
*/
private String productcode;

@Column(name="CURRENCYCODE", nullable=true)

/**
* Codigo de moneda
*/
private String currencycode;

@Column(name="ACCOUNTNUMBER", nullable=false)

/**
* Consecutivo de cuenta
*/
private String accountnumber;

/**Contructor por defecto*/
public TgeneAccountNumber(){
}
/**Contructor de TgeneAccountNumber
@param pPk Clave Primaria del entity
@param pAccountnumber Consecutivo de cuenta
*/
public TgeneAccountNumber(TgeneAccountNumberKey pPk,String pAccountnumber){
 this();
 pk=pPk;
 accountnumber=pAccountnumber;
}
/**
* 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 TgeneAccountNumber
*/
public static TgeneAccountNumber find(EntityManager pEntityManager,TgeneAccountNumberKey pKey) throws Exception{
    TgeneAccountNumber obj = pEntityManager.find(TgeneAccountNumber.class,pKey);
    return obj;
}
/**Entrega la Clave primaria de TgeneAccountNumber
@return El objeto que referencia a la Clave primaria de TgeneAccountNumber
*/
public TgeneAccountNumberKey getPk(){
  return pk;
}
/**Fija un nuevo valor a la Clave primaria de TgeneAccountNumber
@param pPk El objeto que referencia a la nueva Clave primaria de TgeneAccountNumber
*/
public void setPk(TgeneAccountNumberKey pPk){
  pk=pPk;
}
/**Obtiene el valor de productcode
@return valor de productcode*/
public String getProductcode(){
  return productcode;
}
/**Fija el valor de productcode
@param pProductcode nuevo Valor de productcode*/
public void setProductcode(String pProductcode){
  productcode=pProductcode;
}

/**Obtiene el valor de currencycode
@return valor de currencycode*/
public String getCurrencycode(){
  return currencycode;
}
/**Fija el valor de currencycode
@param pCurrencycode nuevo Valor de currencycode*/
public void setCurrencycode(String pCurrencycode){
  currencycode=pCurrencycode;
}

/**Obtiene el valor de accountnumber
@return valor de accountnumber*/
public String getAccountnumber(){
  return accountnumber;
}
/**Fija el valor de accountnumber
@param pAccountnumber nuevo Valor de accountnumber*/
public void setAccountnumber(String pAccountnumber){
  accountnumber=pAccountnumber;
}

public boolean equals(Object rhs){
    if (rhs == null)return false;
    if (! (rhs instanceof TgeneAccountNumber))return false;
    TgeneAccountNumber that = (TgeneAccountNumber) 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 TgeneAccountNumber
@return el hashCode la instancia
*/
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
*/
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 TgeneAccountNumber
*/
public Object createInstance(){
 TgeneAccountNumber instance=new TgeneAccountNumber();	
 instance.setPk(new TgeneAccountNumberKey());
 return instance;
}
/**Clona la entidad TgeneAccountNumber
@see com.fp.dto.hb.HibernateBean#cloneMe()
*/
public Object cloneMe() throws CloneNotSupportedException{
  TgeneAccountNumber p=(TgeneAccountNumber)this.clone();
  p.setPk((TgeneAccountNumberKey)this.pk.cloneMe());
  return p;
}

	//METODOS MANUALES
	/**Sentencia que obtiene un registro de TgeneAccountNumber con la secuencia de numeros de cuenta.*/
	private static final String HQL = "from com.fp.persistence.pgeneral.acco.TgeneAccountNumber t "+
	" where t.pk.modulecode = :modulecode "+
	" and coalesce(productcode,:productcode) = :productcode "+
	" and coalesce(currencycode,:currencycode) = :currencycode ";
	
	/**
	* Metodo que entrega datos de la tabla dada la clave primaria.
	* @param pEntityManager referencia de la session a obtener datos del bean.
	* @param pModule Codigo de modulo
	* @param pProduct Codigo de producto
	* @param pCurrency Codigo de moneda
	* @return TgeneAccountNumber
	*/
	public static TgeneAccountNumber findWithhold(EntityManager pEntityManager,String pModule,
			String pProduct,String pCurrency) throws Exception{
		TgeneAccountNumber tgeneAccountNumber = null;
		Query qry = pEntityManager.createQuery(HQL);
		qry.setParameter("modulecode", pModule);
		qry.setParameter("productcode", pProduct); 
		qry.setParameter("currencycode", pCurrency); 
		qry.setLockMode(LockModeType.PESSIMISTIC_READ);
		tgeneAccountNumber = (TgeneAccountNumber)qry.getSingleResult();
		if(tgeneAccountNumber == null){
			throw new CommonException("CORE-0025","SECUENCIA DE CUENTA NO DEFINIDA EN TGENEACCOUNTNUMBER MODULO: {}",pModule);
		}
		return tgeneAccountNumber;
	}

}
