From d1615f458f4c898dccb2bed8900c8899884c464a Mon Sep 17 00:00:00 2001 From: alberto Date: Thu, 28 Mar 2019 14:55:38 +0000 Subject: [PATCH] =?UTF-8?q?S=C3=A9rie=20para=20venda=20embarcada=20bug#137?= =?UTF-8?q?48=20dev:trevezani=20qua:?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/AdmVenta/Model/trunk/modelWeb@91304 d1611594-4594-4d17-8e1d-87c2c4800839 --- .../ventaboletos/dao/CtrlSerieBPeDAO.java | 3 + .../hibernate/CtrlSerieBPeHibernateDAO.java | 84 +++++++++++++++++++ .../ControleSerieEmbarcadaServiceImpl.java | 23 +++-- 3 files changed, 101 insertions(+), 9 deletions(-) diff --git a/src/com/rjconsultores/ventaboletos/dao/CtrlSerieBPeDAO.java b/src/com/rjconsultores/ventaboletos/dao/CtrlSerieBPeDAO.java index b9972df02..b806e959b 100644 --- a/src/com/rjconsultores/ventaboletos/dao/CtrlSerieBPeDAO.java +++ b/src/com/rjconsultores/ventaboletos/dao/CtrlSerieBPeDAO.java @@ -4,6 +4,9 @@ import com.rjconsultores.ventaboletos.entidad.CtrlSerieBPe; public interface CtrlSerieBPeDAO extends GenericDAO { + public void gerarSeqSerieBPe(Integer empresaId, String estado, Integer minvalue) throws RuntimeException; + public Integer buscarSequencia(Integer empresaId, String estado); + public CtrlSerieBPe buscarPorEmpresaEstado(Integer empresaId, Integer estadoId); } diff --git a/src/com/rjconsultores/ventaboletos/dao/hibernate/CtrlSerieBPeHibernateDAO.java b/src/com/rjconsultores/ventaboletos/dao/hibernate/CtrlSerieBPeHibernateDAO.java index 6252d7ed1..12177595f 100644 --- a/src/com/rjconsultores/ventaboletos/dao/hibernate/CtrlSerieBPeHibernateDAO.java +++ b/src/com/rjconsultores/ventaboletos/dao/hibernate/CtrlSerieBPeHibernateDAO.java @@ -1,5 +1,12 @@ package com.rjconsultores.ventaboletos.dao.hibernate; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import javax.sql.DataSource; + import org.hibernate.Query; import org.hibernate.SessionFactory; import org.slf4j.Logger; @@ -15,11 +22,88 @@ import com.rjconsultores.ventaboletos.entidad.CtrlSerieBPe; public class CtrlSerieBPeHibernateDAO extends GenericHibernateDAO implements CtrlSerieBPeDAO { private static final Logger log = LoggerFactory.getLogger(CtrlSerieBPeHibernateDAO.class); + @Autowired + private DataSource dataSource; + @Autowired public CtrlSerieBPeHibernateDAO(@Qualifier("sessionFactory") SessionFactory factory) { setSessionFactory(factory); } + public void gerarSeqSerieBPe(Integer empresaId, String estado, Integer minvalue) throws RuntimeException { + Connection conn = null; + + try { + conn = dataSource.getConnection(); + + StringBuilder seq = new StringBuilder(); + seq.append("SERIE_BPE_"); + seq.append(estado).append("_"); + seq.append(empresaId).append("_"); + seq.append("SEQ"); + + if (!conn.createStatement().executeQuery("select SEQUENCE_NAME from DBA_SEQUENCES where SEQUENCE_NAME like '" + seq.toString() + "%'").next()) { + conn.createStatement().execute("CREATE SEQUENCE " + seq.toString() + " MINVALUE 1 MAXVALUE 999 INCREMENT BY 1 START WITH " + minvalue.toString() + " ORDER NOCACHE"); + + log.info("SEQUENCIA " + seq.toString() + " GERADA COM SUCESSO."); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + if (conn != null && !conn.isClosed()) { + conn.close(); + } + } catch (SQLException e) { + log.error(e.getMessage(), e); + } + } + } + + public Integer buscarSequencia(Integer empresaId, String estado) { + Integer retorno = null; + + Connection conn = null; + Statement st = null; + ResultSet rs = null; + + try { + conn = dataSource.getConnection(); + + StringBuilder seq = new StringBuilder(); + seq.append("SERIE_BPE_"); + seq.append(estado).append("_"); + seq.append(empresaId).append("_"); + seq.append("SEQ"); + + StringBuilder sql = new StringBuilder(); + sql.append("select ").append(seq.toString()).append(".nextval from dual"); + + st = conn.createStatement(); + rs = st.executeQuery(sql.toString()); + + rs.next(); + + retorno = rs.getInt(1); + + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { if (rs != null) rs.close(); } catch (Exception e) { log.error("", e); } + try { if (st != null) st.close(); } catch (Exception e) { log.error("", e); } + + try { + if (conn != null && !conn.isClosed()) { + conn.close(); + } + } catch (SQLException e) { + log.error(e.getMessage(), e); + } + } + + return retorno; + } + public CtrlSerieBPe buscarPorEmpresaEstado(Integer empresaId, Integer estadoId) { StringBuilder sb = new StringBuilder(); sb.append("SELECT ctrl "); diff --git a/src/com/rjconsultores/ventaboletos/service/impl/ControleSerieEmbarcadaServiceImpl.java b/src/com/rjconsultores/ventaboletos/service/impl/ControleSerieEmbarcadaServiceImpl.java index 590ceee38..30c8f9cbf 100644 --- a/src/com/rjconsultores/ventaboletos/service/impl/ControleSerieEmbarcadaServiceImpl.java +++ b/src/com/rjconsultores/ventaboletos/service/impl/ControleSerieEmbarcadaServiceImpl.java @@ -1,7 +1,5 @@ package com.rjconsultores.ventaboletos.service.impl; -import java.util.Date; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -49,16 +47,23 @@ public class ControleSerieEmbarcadaServiceImpl implements ControleSerieEmbarcada if (serieembarcada == null) { CtrlSerieBPe ctrlSerieBpe = ctrlSerieBPeDAO.buscarPorEmpresaEstado(controleSerieEmbarcada.getEmpresa().getEmpresaId(), controleSerieEmbarcada.getEstado().getEstadoId()); - Integer serie = 0; + Integer serie = 1; - if (ctrlSerieBpe != null) { - serie = Integer.valueOf(ctrlSerieBpe.getSerie()); + if (ctrlSerieBpe == null) { + try { + ctrlSerieBPeDAO.gerarSeqSerieBPe(controleSerieEmbarcada.getEmpresa().getEmpresaId(), controleSerieEmbarcada.getEstado().getCveestado(), serie); + } catch (Exception e) { + serie = ctrlSerieBPeDAO.buscarSequencia(controleSerieEmbarcada.getEmpresa().getEmpresaId(), controleSerieEmbarcada.getEstado().getCveestado()); + } + } else { + try { + ctrlSerieBPeDAO.gerarSeqSerieBPe(controleSerieEmbarcada.getEmpresa().getEmpresaId(), controleSerieEmbarcada.getEstado().getCveestado(), Integer.valueOf(ctrlSerieBpe.getSerie()) + 1); + } catch (Exception e) { + } + + serie = ctrlSerieBPeDAO.buscarSequencia(controleSerieEmbarcada.getEmpresa().getEmpresaId(), controleSerieEmbarcada.getEstado().getCveestado()); } - do { - serie++; - } while (ctrlSerieEmbarcadaDAO.validaPossuiDispositivo(controleSerieEmbarcada.getEmpresa().getEmpresaId(), controleSerieEmbarcada.getEstado().getEstadoId(), serie.toString())); - serieembarcada = new SerieEmbarcada(); serieembarcada.setSerie(serie.toString()); serieembarcada.setNumero("1");