Java tutorial
/* * 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 com.bolao.persistencia.dao; import com.bolao.persistencia.connection.HibernateFactory; import com.bolao.persistencia.entidades.Time; import com.bolao.web.uteis.ConversorData; import com.bolao.web.uteis.ModoConsulta; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; public class TimeDAO implements Serializable { private List<Time> lista = new ArrayList<>(); private ConversorData conversorData = new ConversorData(); public String persist(Time time) { try { Session session = HibernateFactory.getSessionFactory(); session.beginTransaction(); session.persist(time); session.getTransaction().commit(); session.close(); } catch (Exception ex) { ex.printStackTrace(); return HibernateFactory.FALHA; } return HibernateFactory.SUCESSO; } public String refresh(Time time) { try { Session session = HibernateFactory.getSessionFactory(); session.beginTransaction(); session.merge(time); session.getTransaction().commit(); session.close(); } catch (Exception ex) { return HibernateFactory.FALHA; } return HibernateFactory.SUCESSO; } public String delete(Time time) { try { Session session = HibernateFactory.getSessionFactory(); session.beginTransaction(); session.delete(time); session.getTransaction().commit(); session.close(); } catch (Exception ex) { return HibernateFactory.FALHA; } return HibernateFactory.SUCESSO; } public List<Time> list(ModoConsulta modoConsulta, Object object, Object dataObject) { try { Session session = HibernateFactory.getSessionFactory(); session.beginTransaction(); if (modoConsulta == ModoConsulta.CIDADE && object != null && !object.toString().isEmpty()) { lista.clear(); Query query = session .createQuery(" from Time t where t.cidade like :XCIDADE order by t.codigo,t.cidade "); query.setString("XCIDADE", "%" + String.valueOf(object) + "%"); lista = query.list(); } else if (modoConsulta == ModoConsulta.CODIGO && object != null && !object.toString().isEmpty()) { lista.clear(); Query query = session .createQuery(" from Time t where t.codigo =:xcodigo order by t.cidade asc,t.codigo desc"); query.setInteger("xcodigo", Integer.parseInt(String.valueOf(object))); lista = query.list(); } else if (modoConsulta == ModoConsulta.DATA_CADASTRO && object != null && !object.toString().isEmpty() && dataObject != null && !dataObject.toString().isEmpty()) { lista.clear(); Query query = session.createQuery( " from Time t where t.datacadastro between :XDATAINI AND :XDATAFIM order by t.cidade asc,t.codigo desc"); query.setDate("XDATAINI", conversorData.converterData(String.valueOf(object))); query.setDate("XDATAFIM", conversorData.converterData(String.valueOf(dataObject))); lista = query.list(); } else if (modoConsulta == ModoConsulta.DATA_FUNDACAO && object != null && !object.toString().isEmpty() && dataObject != null && !dataObject.toString().isEmpty()) { lista.clear(); Query query = session.createQuery( " from Time t where t.datafundacao between :XDATAINI AND :XDATAFIM order by t.cidade asc,t.codigo desc"); query.setDate("XDATAINI", conversorData.converterData(String.valueOf(object))); query.setDate("XDATAFIM", conversorData.converterData(String.valueOf(dataObject))); lista = query.list(); } else if (modoConsulta == ModoConsulta.DEFAULT || object == null || object.toString().isEmpty()) { lista.clear(); Query query = session.createQuery(" from Time t order by t.codigo,t.cidade "); lista = query.list(); } else if (modoConsulta == ModoConsulta.EMAIL && object != null && !object.toString().isEmpty()) { lista.clear(); Query query = session .createQuery(" from Time t where t.email like :XEMAIL order by t.codigo,t.cidade"); query.setString("XEMAIL", "%" + String.valueOf(object) + "%"); lista = query.list(); } else if (modoConsulta == ModoConsulta.ENDERECO && object != null && !object.toString().isEmpty()) { lista.clear(); Query query = session .createQuery(" from Time t where t.endereco like :XENDERECO order by t.codigo,t.cidade"); query.setString("XENDERECO", "%" + String.valueOf(object) + "%"); lista = query.list(); } else if (modoConsulta == ModoConsulta.SITE && object != null && !object.toString().isEmpty()) { lista.clear(); Query query = session .createQuery(" from Time t where t.site like :XSITE order by t.codigo,t.cidade"); query.setString("XSITE", "%" + String.valueOf(object) + "%"); lista = query.list(); } else if (modoConsulta == ModoConsulta.TELEFONE && object != null && !object.toString().isEmpty()) { lista.clear(); Query query = session .createQuery(" from Time t where t.telefone like :XTELEFONE order by t.codigo,t.cidade"); query.setString("XTELEFONE", "%" + String.valueOf(object) + "%"); lista = query.list(); } else if (modoConsulta == ModoConsulta.TIME && object != null && !object.toString().isEmpty()) { lista.clear(); Query query = session .createQuery(" from Time t where t.nome like :XNOME order by t.codigo,t.cidade"); query.setString("XNOME", "%" + String.valueOf(object) + "%"); lista = query.list(); } else if (modoConsulta == ModoConsulta.DESCRICAO && object != null && !object.toString().isEmpty()) { lista.clear(); Query query = session .createQuery(" from Time t where t.descricao like :XNOME order by t.codigo,t.cidade"); query.setString("XNOME", "%" + String.valueOf(object) + "%"); lista = query.list(); } session.getTransaction().commit(); session.close(); } catch (Exception e) { } return lista; } }