package com.fp.armas.portal.dao.registro;

import java.util.Collection;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import com.fp.armas.portal.dao.general.GenericDaoImpl;
import com.fp.armas.portal.model.Tgenecatalogdetail;
import com.fp.armas.portal.util.RegistroException;

/**
 * Objeto de acceso a datos de la tabla {@link Tgenecatalogdetail}
 * @author dcruz
 *
 */
public class CatalogoDao extends GenericDaoImpl<Tgenecatalogdetail> {

	public CatalogoDao(){
		super(Tgenecatalogdetail.class);
	}
	
	/**
	 * Busca los detalles del cat&aacute;logo en base el catalogcode
	 * @param codigoTipo
	 * @return
	 * @throws RegistroException
	 */
	public Collection<Tgenecatalogdetail> buscarCatalogo(String codigoTipo) throws RegistroException{
		CriteriaBuilder cb = this.getEntityManager().getCriteriaBuilder();
		CriteriaQuery<Tgenecatalogdetail> query = cb.createQuery(Tgenecatalogdetail.class);
		Root<Tgenecatalogdetail> rootTgenecatalog = query.from(Tgenecatalogdetail.class);
		query.select(rootTgenecatalog).where(cb.equal(rootTgenecatalog.get("id").get("catalogcode"), 
                    codigoTipo)).orderBy(cb.asc(rootTgenecatalog.get("description")));
		return this.getEntityManager().createQuery(query).getResultList();
	}
	
	public Collection<Tgenecatalogdetail> buscarCatalogo(String codigoTipo, String legalCode) throws RegistroException{
		CriteriaBuilder cb = this.getEntityManager().getCriteriaBuilder();
		CriteriaQuery<Tgenecatalogdetail> query = cb.createQuery(Tgenecatalogdetail.class);
		Root<Tgenecatalogdetail> rootTgenecatalog = query.from(Tgenecatalogdetail.class);
		query.select(rootTgenecatalog).where(cb.and(cb.equal(rootTgenecatalog.get("id").get("catalogcode"), codigoTipo), 
                    cb.equal(rootTgenecatalog.get("legalcode"), legalCode))).orderBy(cb.asc(rootTgenecatalog.get("description")));
		return this.getEntityManager().createQuery(query).getResultList();
	}
}
