package com.rjconsultores.ventaboletos.dao.hibernate;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
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.ItemDescontoDAO;
import com.rjconsultores.ventaboletos.entidad.ItemDesconto;

@Repository("itemDescontoDAO")
public class ItemDescontoHibernateDAO extends GenericHibernateDAO<ItemDesconto, Long> implements ItemDescontoDAO {
	
	@Autowired
    public ItemDescontoHibernateDAO(@Qualifier("sessionFactory") SessionFactory factory) {
        setSessionFactory(factory);
    }

	@Override
	@SuppressWarnings("unchecked")
	public List<ItemDesconto> buscarPorNome(String nomitemdesconto) {
		StringBuilder sb = new StringBuilder();
		sb.append(" select id ")
		  .append(" from ItemDesconto id ")
		  .append(" where id.activo = 1 ");
		
		if(StringUtils.isNotBlank(nomitemdesconto)) {
			sb.append("and id.nomitemdesconto = :nomitemdesconto ");
		}
		
		Query query = getSession().createQuery(sb.toString());
		
		if(StringUtils.isNotBlank(nomitemdesconto)) {
			query.setString("nomitemdesconto", nomitemdesconto);
		}

		return query.list();
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<ItemDesconto> obtenerTodos() {
		Criteria c = this.makeCriteria();
		c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));
		return c.list();
	}

}
