/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rjconsultores.ventaboletos.dao.hibernate;

import com.rjconsultores.ventaboletos.dao.MercadoCompetidoDAO;
import com.rjconsultores.ventaboletos.entidad.ClaseServicio;
import com.rjconsultores.ventaboletos.entidad.MercadoCompetido;
import com.rjconsultores.ventaboletos.entidad.Parada;
import java.util.List;
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;

/**
 *
 * @author rodrigo
 */
@Repository("mercadoCompetidoDAO")
public class MercadoCompetidoHibernateDAO
        extends GenericHibernateDAO<MercadoCompetido, Integer>
        implements MercadoCompetidoDAO {

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

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

        return c.list();
    }

    public boolean existe(ClaseServicio claseServicio, Parada origem,
            Parada destino) {
        Criteria c = getSession().createCriteria(getPersistentClass());
        c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));

        c.add(Restrictions.eq("claseServicio", claseServicio));
        c.add(Restrictions.eq("origem", origem));
        c.add(Restrictions.eq("destino", destino));
        
        c.setProjection(Projections.rowCount());

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

    public List<MercadoCompetido> buscar(Parada origem, Parada destino) {
        Criteria c = getSession().createCriteria(getPersistentClass());
        c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));

        c.add(Restrictions.eq("origem", origem));
        c.add(Restrictions.eq("destino", destino));

        return c.list();
    }
}
