fixes bug#0014751
dev: thiago qua: Correção efetuada. Foi feito uma alteração para que as sequences não existentes no banco sejam criadas quando a empresa for salva no cadastro. git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/AdmVenta/Model/trunk/modelWeb@94993 d1611594-4594-4d17-8e1d-87c2c4800839master
parent
a23a8f1d9b
commit
b81e10558a
|
@ -6,6 +6,7 @@ package com.rjconsultores.ventaboletos.dao.hibernate;
|
|||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
@ -176,47 +177,70 @@ public class EmpresaHibernateDAO extends GenericHibernateDAO<Empresa, Integer> i
|
|||
}
|
||||
|
||||
@Override
|
||||
public Empresa suscribir(Empresa entity) throws RuntimeException {
|
||||
public Empresa suscribir(Empresa entity) {
|
||||
|
||||
entity = super.suscribir(entity);
|
||||
|
||||
getSession().flush();
|
||||
|
||||
gerarSeqNumFolioSistema(entity.getEmpresaId());
|
||||
try {
|
||||
gerarSeqNumFolioSistema(entity.getEmpresaId());
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
private void gerarSeqNumFolioSistema(Integer idEmpresa) throws RuntimeException {
|
||||
Connection conn = null;
|
||||
try {
|
||||
conn = dataSource.getConnection();
|
||||
String[] sequences = { "AC", "AL", "AM", "AP", "BA", "CE", "DF", "ES", "GO", "MA", "MG", "MS", "MT", "PA", "PB", "PE", "PI", "PR", "RJ", "RN", "RO", "RR", "RS", "SC", "SE", "SP", "TO" };
|
||||
if (!conn.createStatement().executeQuery("select SEQUENCE_NAME from DBA_SEQUENCES where SEQUENCE_NAME like 'FOLIO_SISTEMA_" + idEmpresa + "_SEQ%'").next()) {
|
||||
for (String sequence : sequences) {
|
||||
conn.createStatement().execute("CREATE SEQUENCE FOLIO_SISTEMA_" + (sequence == "" ? sequence : (sequence + "_") + idEmpresa + "_SEQ INCREMENT BY 1 START WITH 1 NOCACHE ORDER NOCYCLE"));
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public Empresa actualizacion(Empresa entity) {
|
||||
entity = super.actualizacion(entity);
|
||||
|
||||
/*
|
||||
* Cria as sequences de numeração dos bilhetes do BPe
|
||||
*/
|
||||
if (!conn.createStatement().executeQuery("select SEQUENCE_NAME from DBA_SEQUENCES where SEQUENCE_NAME like 'FOLIO_SISTEMA_BPE_" + idEmpresa + "_SEQ%'").next()) {
|
||||
for (String sequence : sequences) {
|
||||
conn.createStatement().execute("CREATE SEQUENCE FOLIO_SISTEMA_BPE_" + (sequence == "" ? sequence : (sequence + "_") + idEmpresa + "_SEQ MINVALUE 1 MAXVALUE 999999999 INCREMENT BY 1 START WITH 1 ORDER NOCACHE CYCLE"));
|
||||
}
|
||||
}
|
||||
try {
|
||||
gerarSeqNumFolioSistema(entity.getEmpresaId());
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}finally {
|
||||
try {
|
||||
if(conn != null && !conn.isClosed()){
|
||||
conn.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
private void gerarSeqNumFolioSistema(Integer idEmpresa) throws SQLException {
|
||||
final Connection conn = dataSource.getConnection();
|
||||
try {
|
||||
geraSequence(conn, idEmpresa, false);
|
||||
geraSequence(conn, idEmpresa, true);
|
||||
} finally {
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void geraSequence(final Connection conn, final Integer idEmpresa, final Boolean isFolioBpe) throws SQLException {
|
||||
final Statement statement = conn.createStatement();
|
||||
try {
|
||||
List<String> lista = getSession().createSQLQuery(obtemSqlSequence(idEmpresa, isFolioBpe)).list();
|
||||
for (String instrucao : lista) {
|
||||
statement.execute(instrucao);
|
||||
}
|
||||
} finally {
|
||||
statement.close();
|
||||
}
|
||||
}
|
||||
|
||||
private String obtemSqlSequence(Integer idEmpresa, Boolean isFolioBpe) {
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append("SELECT 'CREATE SEQUENCE ' || SEQUENCIA || ' INCREMENT BY 1 START WITH 1 NOCACHE ORDER NOCYCLE' FROM (");
|
||||
sql.append(" SELECT DISTINCT '" + obtemNomeFolio(isFolioBpe) +"'||CVEESTADO||'_" + idEmpresa + "_SEQ' SEQUENCIA ");
|
||||
sql.append(" FROM ESTADO WHERE ACTIVO = 1) TMP ");
|
||||
sql.append("WHERE TMP.SEQUENCIA NOT IN( ");
|
||||
sql.append(" SELECT SEQUENCE_NAME FROM DBA_SEQUENCES ");
|
||||
sql.append(" WHERE SEQUENCE_NAME LIKE '" + obtemNomeFolio(isFolioBpe) + "%_" + idEmpresa + "_SEQ')");
|
||||
return sql.toString();
|
||||
}
|
||||
|
||||
private String obtemNomeFolio(Boolean isFolioBpe) {
|
||||
return isFolioBpe ? "FOLIO_SISTEMA_BPE_" : "FOLIO_SISTEMA_";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue