From fce63976f62211dddf611a901f06c3ff0dfa9472 Mon Sep 17 00:00:00 2001 From: gleimar Date: Thu, 14 Jul 2016 14:47:34 +0000 Subject: [PATCH] fixes bug#7722 git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/AdmVenta/Model/trunk/modelWeb@57889 d1611594-4594-4d17-8e1d-87c2c4800839 --- .../ventaboletos/dao/util/AidfGenerator.java | 44 +---------- .../ventaboletos/dao/util/IdAidfStore.java | 78 +++++++++++++++++++ 2 files changed, 82 insertions(+), 40 deletions(-) create mode 100644 src/com/rjconsultores/ventaboletos/dao/util/IdAidfStore.java diff --git a/src/com/rjconsultores/ventaboletos/dao/util/AidfGenerator.java b/src/com/rjconsultores/ventaboletos/dao/util/AidfGenerator.java index e3c4a3586..38e2ee451 100644 --- a/src/com/rjconsultores/ventaboletos/dao/util/AidfGenerator.java +++ b/src/com/rjconsultores/ventaboletos/dao/util/AidfGenerator.java @@ -1,10 +1,6 @@ package com.rjconsultores.ventaboletos.dao.util; import java.io.Serializable; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; import org.apache.log4j.Logger; import org.hibernate.HibernateException; @@ -26,44 +22,12 @@ public class AidfGenerator implements IdentifierGenerator { @Override public Serializable generate(SessionImplementor session, Object object) throws HibernateException { - int qtdMaxima = 999; - long idEncontrado = -1; + log.info("Inicio geração Idaidf"); + Serializable nextId = IdAidfStore.getInstance().getNextId(session.connection()); + log.info("Fim geração Idaidf:" + nextId); - Connection connection = session.connection(); - PreparedStatement ps; + return nextId; - int i=1; - - try { - - ps = connection.prepareStatement("select count(*) as qtd from aidf where aidf_id = :1"); - - for(; (idEncontrado == -1) && (i <= qtdMaxima);i++){ - ps.setInt(1, i); - ResultSet rs = ps.executeQuery(); - rs.next(); - int qtd = rs.getInt("qtd"); - - if (qtd == 0){ - idEncontrado = i; - } - } - - } catch (SQLException e) { - log.error("erro ao buscar id aidf",e); - throw new HibernateException(e); - } - log.info("i="+i); - log.info("idEncontrado="+idEncontrado); - - if (i > qtdMaxima && (idEncontrado != qtdMaxima) ){ - throw new HibernateException("A qtd de IDs permitidos para AIDF foi atingido:"+qtdMaxima); - } - - if (idEncontrado == -1){ - throw new HibernateException("Houve algum erro ao buscar um novo ID para AIDF"); - } - return idEncontrado; } } diff --git a/src/com/rjconsultores/ventaboletos/dao/util/IdAidfStore.java b/src/com/rjconsultores/ventaboletos/dao/util/IdAidfStore.java new file mode 100644 index 000000000..bada2933d --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/dao/util/IdAidfStore.java @@ -0,0 +1,78 @@ +package com.rjconsultores.ventaboletos.dao.util; + +import java.io.Serializable; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.apache.log4j.Logger; +import org.hibernate.HibernateException; +import org.hibernate.engine.SessionImplementor; +/** + * Classe que trata a geração do ID da tabela Aidf. + * + * Foi abandonado o uso da sequence pois existiam lacunas nos numeros e já havia atingido o tamanho máximo de 3 dígitos, com isso + * sendo necessário aproveitar os número que não haviam sido utilizados + * + * @author gleimar + * + */ +public class IdAidfStore { + + private static Logger log = Logger.getLogger(IdAidfStore.class); + + private static IdAidfStore INSTANCE = new IdAidfStore(); + + private int indiceAtual = 1; + + private IdAidfStore(){ + + } + + public static IdAidfStore getInstance(){ + return INSTANCE; + } + + + public synchronized Serializable getNextId(Connection connection) throws HibernateException { + + int qtdMaxima = 999; + long idEncontrado = -1; + + PreparedStatement ps; + + try { + + ps = connection.prepareStatement("select count(*) as qtd from aidf where aidf_id = :1"); + + for(; (idEncontrado == -1) && (indiceAtual <= qtdMaxima);indiceAtual++){ + ps.setInt(1, indiceAtual); + ResultSet rs = ps.executeQuery(); + rs.next(); + int qtd = rs.getInt("qtd"); + + if (qtd == 0){ + idEncontrado = indiceAtual; + } + } + + } catch (SQLException e) { + log.error("erro ao buscar id aidf",e); + throw new HibernateException(e); + } + + log.info("indiceAtual="+indiceAtual); + log.info("idEncontrado="+idEncontrado); + + if (indiceAtual > qtdMaxima && (idEncontrado != qtdMaxima) ){ + throw new HibernateException("A qtd de IDs permitidos para AIDF foi atingido:"+qtdMaxima); + } + + if (idEncontrado == -1){ + throw new HibernateException("Houve algum erro ao buscar um novo ID para AIDF"); + } + return idEncontrado; + } + +}