package com.rjconsultores.ventaboletos.dao.hibernate;


import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.MatchMode;
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.ContratoDAO;
import com.rjconsultores.ventaboletos.entidad.ContratoCorporativo;

@Repository("contratoDAO")
@SuppressWarnings("unchecked")
public class ContratoHibernateDAO extends GenericHibernateDAO<ContratoCorporativo, Long> implements ContratoDAO {

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

	@Override
	public List<ContratoCorporativo> buscaLike(String nome) {
		Criteria c = makeCriteria();
		c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));
		
		if( nome != null ) {
			c.add(Restrictions.like("numContrato", nome, MatchMode.ANYWHERE));	
		}
		
		return c.list();
	}

	@Override
	public boolean existe(ContratoCorporativo contrato) {
		Criteria c = makeCriteria();

		c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));
		c.add(Restrictions.eq("numContrato", contrato.getNumContrato()));	
		
		return c.uniqueResult() != null;
	}

}
