daoImp.DepartamentoDaoImp.java Source code

Java tutorial

Introduction

Here is the source code for daoImp.DepartamentoDaoImp.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.DepartamentoDao;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import modelo.DepDepartamento;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.HibernateUtil;

/**
 *
 * @author Chris Allen
 */
public class DepartamentoDaoImp implements DepartamentoDao {

    private String msg;

    @Override
    public void guardar(DepDepartamento departamento) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.getTransaction().begin();
            session.save(departamento);
            session.getTransaction().commit();
            msg = "Departamento guardado com sucesso!";
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, null);
            FacesContext.getCurrentInstance().addMessage(null, message);

        } catch (Exception e) {
            msg = "Erro ao guardar departamento!";
            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 apagar(DepDepartamento departamento) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.getTransaction().begin();
            session.delete(departamento);
            session.getTransaction().commit();
            msg = "Departamento apagado com sucesso!";
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, null);
            FacesContext.getCurrentInstance().addMessage(null, message);

        } catch (Exception e) {
            msg = "Erro ao apagar departamento!";
            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(DepDepartamento departamento) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            Transaction tx = session.beginTransaction();
            session.merge(departamento);
            tx.commit();
            msg = "Departamento alterado com sucesso!";
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, null);
            FacesContext.getCurrentInstance().addMessage(null, message);

        } catch (Exception e) {
            msg = "Erro ao alterar departamento!";
            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 DepDepartamento buscaPorId(int id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return (DepDepartamento) session.load(DepDepartamento.class, id);
    }

    @Override
    public List<DepDepartamento> buscarTodos() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            return session.createQuery("from DepDepartamento").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<DepDepartamento> selectItems() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return session.createQuery("from DepDepartamento").list();
    }

    @Override
    public List<DepDepartamento> buscarDir(int id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String a = "Secretaria";
        try {
            return session
                    .createQuery(
                            "from DepDepartamentoo where o.dirDirecao= '" + id + "' and o.dirDirecao= '" + a + "'")
                    .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;
    }

}