daoImp.PerfilDaoImp.java Source code

Java tutorial

Introduction

Here is the source code for daoImp.PerfilDaoImp.java

Source

/*
 * 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 daoImp;

import dao.PerfilDao;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import modelo.PerPerfil;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.primefaces.context.RequestContext;
import util.HibernateUtil;

/**
 *
 * @author Chris Allen
 */
public class PerfilDaoImp implements PerfilDao {

    private String msg;

    @Override
    public void guardar(PerPerfil perfil) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.getTransaction().begin();
            session.save(perfil);
            session.getTransaction().commit();
            msg = "Perfil guardado com sucesso!";
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, null);
            FacesContext.getCurrentInstance().addMessage(null, message);
            RequestContext.getCurrentInstance().execute("PF('dlgnovo').hide()");

        } catch (Exception e) {
            msg = "Erro ao guardar perfil!";
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, null);
            FacesContext.getCurrentInstance().addMessage(null, message);
            System.out.println("Erro ao Incluir o Item: " + e.getMessage());
            session.getTransaction().rollback();
            RequestContext.getCurrentInstance().execute("PF('dlgnovo').show()");

        } finally {
            try {
                session.isOpen();
                session.close();
                System.out.println("Sesso fechada");
            } catch (Exception e) {
                System.out.println("Erro: " + e.getMessage());

            }
        }
    }

    @Override
    public void apagar(PerPerfil perfil) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.getTransaction().begin();
            session.delete(perfil);
            session.getTransaction().commit();
            msg = "Perfil apagado com sucesso!";
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, null);
            FacesContext.getCurrentInstance().addMessage(null, message);

        } catch (Exception e) {
            msg = "Erro ao apagar perfil!";
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, null);
            FacesContext.getCurrentInstance().addMessage(null, message);
            System.out.println("Erro ao Incluir o Item: " + e.getMessage());
            session.getTransaction().rollback();

        } finally {
            try {
                session.isOpen();
                session.close();
                System.out.println("Sesso fechada");
            } catch (Exception e) {
                System.out.println("Erro: " + e.getMessage());

            }
        }
    }

    @Override
    public void alterar(PerPerfil perfil) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            Transaction tx = session.beginTransaction();
            session.merge(perfil);
            tx.commit();
            msg = "Perfil alterado com sucesso!";
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, null);
            FacesContext.getCurrentInstance().addMessage(null, message);
            RequestContext.getCurrentInstance().execute("PF('dlgEditar').hide()");

        } catch (Exception e) {
            msg = "Erro ao alterar perfil!";
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, null);
            FacesContext.getCurrentInstance().addMessage(null, message);
            System.out.println("Erro ao Incluir o Item: " + e.getMessage());
            session.getTransaction().rollback();
            RequestContext.getCurrentInstance().execute("PF('dlgEditar').show()");

        } finally {
            try {
                session.isOpen();
                session.close();
                System.out.println("Sesso fechada");
            } catch (Exception e) {
                System.out.println("Erro: " + e.getMessage());

            }
        }
    }

    @Override
    public PerPerfil buscaPorId(int id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return (PerPerfil) session.load(PerPerfil.class, id);
    }

    @Override
    public List<PerPerfil> buscarTodos() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            return session.createQuery("from PerPerfil where perDescricao <> 'admin'").list();
        } catch (Exception e) {

            System.out.println("Erro: " + e.getMessage());
            session.beginTransaction().rollback();
        } finally {
            try {
                session.isOpen();
                session.close();
                System.out.println("Sesso fechada");
            } catch (Exception e) {
                System.out.println("Erro: " + e.getMessage());
            }
        }
        return null;
    }

    @Override
    public List<PerPerfil> selectItems() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return session.createQuery("from PerPerfil where perDescricao <> 'admin'").list();
    }

}