fixes bug#7722
git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/AdmVenta/Model/trunk/modelWeb@57889 d1611594-4594-4d17-8e1d-87c2c4800839master
parent
fe17984137
commit
fce63976f6
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue