fixes bug#18139
qua: Juliane dev: Wilian Implementei a sequência com a mesma solução que tem na entidade AIDF. git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/AdmVenta/Model/trunk/modelWeb@100327 d1611594-4594-4d17-8e1d-87c2c4800839master
parent
cd51edea9b
commit
a29832e160
|
@ -0,0 +1,26 @@
|
||||||
|
package com.rjconsultores.ventaboletos.dao.util;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
import org.hibernate.HibernateException;
|
||||||
|
import org.hibernate.engine.SessionImplementor;
|
||||||
|
import org.hibernate.id.IdentifierGenerator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trata a geração do ID da forma de pagamento data tabela forma de pagamento, pois não pode ultrapassar 2 digitos
|
||||||
|
*
|
||||||
|
* @author Guilherme Lopes
|
||||||
|
*/
|
||||||
|
public class FormaPagoGenerator implements IdentifierGenerator {
|
||||||
|
|
||||||
|
private static Logger log = Logger.getLogger(FormaPagoGenerator.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Short generate(SessionImplementor session, Object object) throws HibernateException {
|
||||||
|
log.info("Inicio geração IdFormaPago");
|
||||||
|
Serializable nextId = IdFormaPagoStore.getInstance().getNextId(session.connection());
|
||||||
|
log.info("Fim geração IdFormaPago:" + nextId);
|
||||||
|
return Short.parseShort(nextId.toString());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,121 @@
|
||||||
|
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.commons.lang.StringUtils;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
import org.hibernate.HibernateException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trata a geração do ID da forma de pagamento data tabela forma de pagamento, pois não pode ultrapassar 2 digitos
|
||||||
|
*
|
||||||
|
* @author Guilherme Lopes
|
||||||
|
*/
|
||||||
|
public class IdFormaPagoStore {
|
||||||
|
|
||||||
|
private static Logger log = Logger.getLogger(IdFormaPagoStore.class);
|
||||||
|
|
||||||
|
private static final String LIMITE_ID_FORMA_PAGTO = "LIMITE_ID_FORMA_PAGTO";
|
||||||
|
|
||||||
|
private static IdFormaPagoStore INSTANCE = new IdFormaPagoStore();
|
||||||
|
|
||||||
|
private int indiceAtual = 1;
|
||||||
|
|
||||||
|
private IdFormaPagoStore() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IdFormaPagoStore getInstance() {
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized Serializable getNextId(Connection connection) throws HibernateException {
|
||||||
|
|
||||||
|
int qtdMaxima = this.buscarLimiteFormaPago(connection);
|
||||||
|
|
||||||
|
log.info(String.format("qtdMaxima:%s", qtdMaxima));
|
||||||
|
|
||||||
|
long idEncontrado = -1;
|
||||||
|
|
||||||
|
PreparedStatement ps;
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
ps = connection.prepareStatement("SELECT COUNT(*) AS qtd FROM FORMA_PAGO WHERE FORMAPAGO_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 IdFormaPago", 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 IdFormaPago foi atingido:" + qtdMaxima);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (idEncontrado == -1) {
|
||||||
|
throw new HibernateException("Houve algum erro ao buscar um novo ID para IdFormaPago !");
|
||||||
|
}
|
||||||
|
return idEncontrado;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Buscar novo limite de IdFormaPago.
|
||||||
|
*
|
||||||
|
* Por padrão é 99. Com o intuito de ativar de forma gradual, foi criado uma constante para essa função
|
||||||
|
*
|
||||||
|
* @param connection
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private int buscarLimiteFormaPago(Connection connection) {
|
||||||
|
int qtdMaxima = 99;
|
||||||
|
PreparedStatement ps;
|
||||||
|
|
||||||
|
log.info("buscando constante limite IdFormaPago ...");
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
ps = connection.prepareStatement("SELECT VALORCONSTANTE FROM CONSTANTE WHERE NOMBCONSTANTE = :1");
|
||||||
|
ps.setString(1, LIMITE_ID_FORMA_PAGTO);
|
||||||
|
|
||||||
|
ResultSet rs = ps.executeQuery();
|
||||||
|
|
||||||
|
if (rs.next()) {
|
||||||
|
|
||||||
|
String tmp = rs.getString("VALORCONSTANTE");
|
||||||
|
|
||||||
|
if (StringUtils.isNotBlank(tmp)) {
|
||||||
|
qtdMaxima = Integer.parseInt(tmp);
|
||||||
|
|
||||||
|
log.info(String.format("constante encontrada : %s", tmp));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.info("constante não parametrizada");
|
||||||
|
}
|
||||||
|
|
||||||
|
rs.close();
|
||||||
|
ps.close();
|
||||||
|
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error("erro buscar constante", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return qtdMaxima;
|
||||||
|
}
|
||||||
|
}
|
|
@ -23,6 +23,8 @@ import javax.persistence.Table;
|
||||||
import javax.persistence.Temporal;
|
import javax.persistence.Temporal;
|
||||||
import javax.persistence.TemporalType;
|
import javax.persistence.TemporalType;
|
||||||
|
|
||||||
|
import org.hibernate.annotations.GenericGenerator;
|
||||||
|
|
||||||
import com.rjconsultores.ventaboletos.enums.TipoFormapago;
|
import com.rjconsultores.ventaboletos.enums.TipoFormapago;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -38,7 +40,8 @@ public class FormaPago implements Serializable {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@Basic(optional = false)
|
@Basic(optional = false)
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO, generator = "FORMA_PAGO_SEQ")
|
@GenericGenerator(name = "FOR_PAG_GEN_ID", strategy = "com.rjconsultores.ventaboletos.dao.util.FormaPagoGenerator")
|
||||||
|
@GeneratedValue(generator = "FOR_PAG_GEN_ID")
|
||||||
@Column(name = "FORMAPAGO_ID")
|
@Column(name = "FORMAPAGO_ID")
|
||||||
private Short formapagoId;
|
private Short formapagoId;
|
||||||
@Column(name = "DESCPAGO")
|
@Column(name = "DESCPAGO")
|
||||||
|
|
Loading…
Reference in New Issue