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.Tgenecanton;
import com.fp.armas.portal.util.RegistroException;

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

	public CantonDao() {
		super(Tgenecanton.class);
	}

	/**
	 * Busca los cantones en base a los criterios ingresados
	 * @param countryCode
	 * @param provinceCode
	 * @return
	 */
	public Collection<Tgenecanton> buscarCantonesxPaisyProvincia(String countryCode, String provinceCode) {
		try {
			CriteriaBuilder cb = this.getEntityManager().getCriteriaBuilder();
			CriteriaQuery<Tgenecanton> query = cb.createQuery(Tgenecanton.class);
			Root<Tgenecanton> tcantonRoot = query.from(Tgenecanton.class);
			query.where(cb.equal(tcantonRoot.get("id").get("countrycode"), countryCode), cb.equal(tcantonRoot.get("id").get("provincecode"), provinceCode));
			query.orderBy(cb.asc(tcantonRoot.get("description")));
			return this.getEntityManager().createQuery(query).getResultList();
		} catch (Throwable e) {
			throw new RegistroException(e);
		}
	}
}
