/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package backend.repo.impl;

import backend.estrutura.loc.Pais;
import backend.estrutura.loc.Regiao;
import backend.repo.api.PaisDAO;
import java.util.Collection;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author mgomes
 */
@Repository
public class PaisDAOImpl extends BaseHibernateDAO implements PaisDAO
{

    @Override
    public Collection<Pais> listar() throws Exception 
    {
        try
        {
            DetachedCriteria criterios = DetachedCriteria.forClass(Pais.class);
            return (Collection<Pais>) getHibernateTemplate().findByCriteria(criterios);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    
    @Override
    public Collection<Pais> filtrar(String codigo, String nome, Regiao regiao) throws Exception 
    {
        try
        {
            DetachedCriteria criterios = DetachedCriteria.forClass(Pais.class);
            
            if((codigo != null)&&(codigo.length() > 0))
            {
                criterios.add(Restrictions.like("codigo", codigo));
            }
            
            if((nome != null)&&(nome.length() > 0))
            {
                criterios.add(Restrictions.like("nome", nome));
            }
            
            if((regiao != null))
            {
                criterios.add(Restrictions.like("regiao", regiao));
            }
            
            return (Collection<Pais>) getHibernateTemplate().findByCriteria(criterios);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    @Override
    public Pais filtrarByPk(String pkobj) throws Exception 
    {
        try
        {
            DetachedCriteria criterios = DetachedCriteria.forClass(Pais.class);
            
            if((pkobj != null)&&(pkobj.length() > 0))
            {
                criterios.add(Restrictions.eq("codigo", pkobj));
            }
            
            return (Pais) getHibernateTemplate().findByCriteria(criterios).iterator().next();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    @Override @Transactional(readOnly = false, propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public Pais salvar(Pais tobj) throws Exception 
    {
        try
        {
            return (Pais) getHibernateTemplate().merge(tobj);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    @Override @Transactional(readOnly = false, propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public void excluir(Pais tobj) throws Exception 
    {
        try
        {
            getHibernateTemplate().delete(tobj);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    
}
