controller.CategoriaEmpresaController.java Source code

Java tutorial

Introduction

Here is the source code for controller.CategoriaEmpresaController.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 controller;

import java.awt.HeadlessException;
import java.util.List;
import javax.swing.JOptionPane;
import model.CategoriaEmpresa;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import util.HibernateUtil;
import view.CadCategoriaEmpresa;

/**
 *
 * @author thales
 */
public class CategoriaEmpresaController {

    private CategoriaEmpresa cat;
    private CadCategoriaEmpresa view;

    public CategoriaEmpresaController(CadCategoriaEmpresa view) {
        this.view = view;
    }

    public void criarCategoria(String nome) {
        this.cat = new CategoriaEmpresa();
        this.cat.setNome(nome);
    }

    public void criarCategoria(int id, String nome) {
        this.cat = new CategoriaEmpresa();
        this.cat.setIdCategoriaEmpresa(id);
        this.cat.setNome(nome);
    }

    public void salvar() {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();

        try {
            s.save(this.cat);
            tx.commit();
            JOptionPane.showMessageDialog(null, "Dado Inserido com Sucesso!");
        } catch (HeadlessException e) {
            JOptionPane.showMessageDialog(null, "Erro ao inserir os dados: " + e.getMessage());
            tx.rollback();
        }

        view.loadTable();
        s.close();

    }

    public void atualizar() {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();

        try {
            s.merge(this.cat);
            tx.commit();
            JOptionPane.showMessageDialog(null, "Dado Atualizado com Sucesso!");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Erro ao atualizar os dados: " + e.getMessage());
            tx.rollback();
        }

        view.loadTable();
        s.close();
    }

    public List listar() {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session s = sf.openSession();

        try {

            List<CategoriaEmpresa> list = s.createCriteria(CategoriaEmpresa.class).list();

            return list;

        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "Erro ao listar os dados: " + e.getMessage());

        }
        s.close();
        return null;
    }

    public void buscar() {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session s = sf.openSession();

        Criteria c = s.createCriteria(CategoriaEmpresa.class);
        c.addOrder(Order.desc("idCategoriaEmpresa"));
        c.add(Restrictions.sqlRestriction("limit 1"));

    }

    public void remover() {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();

        try {
            s.delete(this.cat);
            tx.commit();
            JOptionPane.showMessageDialog(null, "Dado Removido com Sucesso!");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Erro ao remover os dados: " + e.getMessage());
            tx.rollback();
        }

        view.loadTable();
        s.close();
    }

}