package com.rjconsultores.ventaboletos.dao.hibernate;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
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.SeguroTarifaDAO;
import com.rjconsultores.ventaboletos.entidad.OrgaoConcedente;
import com.rjconsultores.ventaboletos.entidad.SeguroTarifa;
import java.util.List;

@Repository("seguroTarifaDAO")
public class SeguroTarifaHibernateDAO extends GenericHibernateDAO<SeguroTarifa, Integer> implements SeguroTarifaDAO {

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

    @Override
    public boolean existe(Integer orgaoConcedenteId) {
        Criteria c = makeCriteria();
        c.add(Restrictions.eq("orgaoconcedente.orgaoConcedenteId", orgaoConcedenteId));
        c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));
        c.setProjection(Projections.rowCount());


        return HibernateFix.count(c.list()) > 0;
    }

    public List<SeguroTarifa> buscarPorOrgao(OrgaoConcedente orgao) {
        Criteria c = makeCriteria();
        c.add(Restrictions.eq("orgaoconcedente", orgao));
        c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));

        return c.list();
    }
}
