package com.rjconsultores.ventaboletos.dao.hibernate;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.rjconsultores.ventaboletos.dao.OrgaoEmpParamDAO;
import com.rjconsultores.ventaboletos.entidad.OrgaoEmpParam;

@Repository("orgaoEmpParamDAO")
public class OrgaoEmpParamHibernateDAO extends GenericHibernateDAO<OrgaoEmpParam, Integer> implements OrgaoEmpParamDAO {

    @Autowired
    public OrgaoEmpParamHibernateDAO(@Qualifier("sessionFactory") SessionFactory factory) {
        setSessionFactory(factory);
    }

	@SuppressWarnings("unchecked")
	@Override
	public List<OrgaoEmpParam> obterPorOrgao(Integer orgaoConcedenteId) {
		Criteria criteria = createCriteria();
		criteria.add(Restrictions.eq("orgaoConcedente.orgaoConcedenteId", orgaoConcedenteId));

        return criteria.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<OrgaoEmpParam> obterPorEmpresa(Integer empresaId) {
		Criteria criteria = createCriteria();
		criteria.add(Restrictions.eq("empresa.empresaId", empresaId));

        return criteria.list();
	}

	@Override
	public OrgaoEmpParam obterPorOrgaoEmpresa(Integer orgaoConcedenteId, Integer empresaId) {
		Criteria criteria = createCriteria();
		criteria.add(Restrictions.eq("orgaoConcedente.orgaoConcedenteId", orgaoConcedenteId));
		criteria.add(Restrictions.eq("empresa.empresaId", empresaId));
		
        return (OrgaoEmpParam) criteria.uniqueResult();
	}

	private Criteria createCriteria() {
		Criteria criteria = getSession().createCriteria(getPersistentClass());
		criteria.add(Restrictions.eq(ACTIVO, Boolean.TRUE));
		return criteria;
	}

}
