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.TransportadoraDAO;
import com.rjconsultores.ventaboletos.entidad.Transportadora;

@Repository("transportadoraDAO")
public class TransportadoraHibernateDAO extends GenericHibernateDAO<Transportadora, Long> implements TransportadoraDAO {

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

    @SuppressWarnings("unchecked")
	@Override
    public List<Transportadora> obtenerTodos() {
        Criteria c = getSession().createCriteria(getPersistentClass());
        c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));

        return c.list();
    }

	@Override
	public boolean existe(String nit) {
		Criteria c = getSession().createCriteria(getPersistentClass());
		c.add(Restrictions.eq("nit", nit.trim()));
        c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));
        
        return !c.list().isEmpty();
	}
	
	@Override
	public Transportadora buscarPorNit(String nit) {
		Criteria c = getSession().createCriteria(getPersistentClass());
		c.add(Restrictions.eq("nit", nit.trim()));
        c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));
        
        List<Transportadora> ret = castList(Transportadora.class, c.list());
        if( ret.isEmpty()) {
        	return null;
        }else {
        	return ret.get(0);
        }
	}
}
