package com.rjconsultores.ventaboletos.dao.hibernate;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
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.OrgaoConcedenteDAO;
import com.rjconsultores.ventaboletos.entidad.OrgaoConcedente;

@SuppressWarnings("unchecked")
@Repository("orgaoConcedenteDAO")
public class OrgaoConcedenteHibernateDAO extends GenericHibernateDAO<OrgaoConcedente, Integer> implements OrgaoConcedenteDAO {

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

    @Override
    public List<OrgaoConcedente> obtenerTodos() {
        Criteria c = getSession().createCriteria(getPersistentClass());
        c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));
        c.addOrder(Order.asc("descOrgao"));

        return c.list();
    }
    
    public List<OrgaoConcedente> obtenerTodosExceto(Integer... id) {
        Criteria c = getSession().createCriteria(getPersistentClass());
        c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));
        c.addOrder(Order.asc("descOrgao"));
        c.add(Restrictions.not(Restrictions.in("orgaoConcedenteId", id)));

        return c.list();
    }
    
    @Override
    public List<Integer> obtenerTodosIds() {
    	Criteria c = getSession().createCriteria(getPersistentClass());
    	c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));
    	c.setProjection(Projections.property("orgaoConcedenteId"));
    	
    	return c.list();
    }

    @Override
    public List<OrgaoConcedente> buscar(String desc) {
        Criteria c = getSession().createCriteria(getPersistentClass());
        c.add(Restrictions.like("descOrgao", desc));
        c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));

        return c.list();
    }
    
    @Override
    public List<OrgaoConcedente> buscarListaPorIds(List<Integer> orgaoConcedenteIds) {
		Criteria c = this.makeCriteria();
		c.add(Restrictions.in("orgaoConcedenteId", orgaoConcedenteIds));
		c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));

		return c.list();
	}
}
