daoImp.ComentarioDaoImp.java Source code

Java tutorial

Introduction

Here is the source code for daoImp.ComentarioDaoImp.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.ComentarioDao;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import modelo.ComComentario;
import modelo.OrdOrdem;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.HibernateUtil;

/**
 *
 * @author Chris Allen
 */
public class ComentarioDaoImp implements ComentarioDao {

    private String msg;
    OrdOrdem ordem = new OrdOrdem();

    @Override
    public void guardar(ComComentario comentario) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            Transaction tx = session.beginTransaction();
            session.save(comentario);
            tx.commit();
            msg = "Comentario criado com sucesso!";
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, null);
            FacesContext.getCurrentInstance().addMessage(null, message);

        } catch (Exception e) {
            msg = "Erro ao criar o comentario!";
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, null);
            FacesContext.getCurrentInstance().addMessage(null, message);
            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());

            }
        }
    }

    @Override
    public void apagar(ComComentario comentario) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.delete(comentario);
            session.beginTransaction().commit();
            msg = "Comentario apagado com sucesso!";
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, null);
            FacesContext.getCurrentInstance().addMessage(null, message);

        } catch (Exception e) {
            msg = "Erro ao apagar a comentario!";
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, null);
            FacesContext.getCurrentInstance().addMessage(null, message);
            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());

            }
        }
    }

    @Override
    public void alterar(ComComentario comentario) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.merge(comentario);
            session.beginTransaction().commit();
            msg = "Comentario alterado com sucesso!";
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, null);
            FacesContext.getCurrentInstance().addMessage(null, message);

        } catch (Exception e) {
            msg = "Erro ao alterar o comentario!";
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, null);
            FacesContext.getCurrentInstance().addMessage(null, message);
            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());

            }
        }
    }

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

    @Override
    public List<ComComentario> buscarTodos() {
        Session session = HibernateUtil.getSessionFactory().openSession();

        try {
            return session.createQuery("from ComComentario ORDER BY com_dt_alteracao DESC").list();
        } catch (Exception e) {
            msg = "Erro ao listar a finalidade!";
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, null);
            FacesContext.getCurrentInstance().addMessage(null, message);
            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<ComComentario> buscarTodosDoAtest() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
                .getRequest();
        HttpServletRequest request = (HttpServletRequest) req;
        HttpSession sess = (HttpSession) request.getSession();
        ordem = (OrdOrdem) sess.getAttribute("ord");
        try {
            return session.createQuery("from ComComentario o left join fetch o.ordOrdem where o.ordOrdem ='"
                    + ordem.getOrdId() + "'ORDER BY com_dt_alteracao DESC").list();
        } catch (Exception e) {
            //e.printStackTrace();
            msg = "Erro ao listar comentrios!";
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, null);
            FacesContext.getCurrentInstance().addMessage(null, message);
            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;
    }

    public Set<ComComentario> buscaComentariosPorOrdemId(int id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            List<ComComentario> result = session
                    .createQuery("from ComComentario o left join fetch o.ordOrdem where o.ordOrdem.id = " + id
                            + " ORDER BY com_dt_alteracao DESC")
                    .list();
            return new HashSet<>(result);
        } catch (Exception e) {
            e.printStackTrace();
            msg = "Erro ao listar comentrios!";
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, null);
            FacesContext.getCurrentInstance().addMessage(null, message);
            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;
    }

}