From bd16c14bee2fc167cde80ba08dc0f4bbde76dc42 Mon Sep 17 00:00:00 2001 From: gleimar Date: Mon, 27 Aug 2012 19:15:57 +0000 Subject: [PATCH] git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/AdmVenta/Model/trunk/modelWeb@20761 d1611594-4594-4d17-8e1d-87c2c4800839 --- .../ventaboletos/dao/TaxaEmbarqueDAO.java | 11 ++ .../hibernate/TaxaEmbarqueHibernateDAO.java | 38 ++++++ .../dao/sqlbuilder/SQLBuilder.java | 4 + .../dao/sqlbuilder/SQLFactory.java | 10 +- .../dao/sqlbuilder/impl/SQLBuilderOracle.java | 111 ++++++++++++++++++ .../sqlbuilder/impl/SQLBuilderSQLServer.java | 16 --- .../service/TaxaEmbarqueService.java | 21 ++++ .../service/impl/TaxaEmbarqueServiceImpl.java | 22 ++++ 8 files changed, 209 insertions(+), 24 deletions(-) create mode 100644 src/com/rjconsultores/ventaboletos/dao/TaxaEmbarqueDAO.java create mode 100644 src/com/rjconsultores/ventaboletos/dao/hibernate/TaxaEmbarqueHibernateDAO.java delete mode 100644 src/com/rjconsultores/ventaboletos/dao/sqlbuilder/impl/SQLBuilderSQLServer.java create mode 100644 src/com/rjconsultores/ventaboletos/service/TaxaEmbarqueService.java create mode 100644 src/com/rjconsultores/ventaboletos/service/impl/TaxaEmbarqueServiceImpl.java diff --git a/src/com/rjconsultores/ventaboletos/dao/TaxaEmbarqueDAO.java b/src/com/rjconsultores/ventaboletos/dao/TaxaEmbarqueDAO.java new file mode 100644 index 000000000..6d08dfb88 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/dao/TaxaEmbarqueDAO.java @@ -0,0 +1,11 @@ +package com.rjconsultores.ventaboletos.dao; + + +public interface TaxaEmbarqueDAO { + /** + * See {@link TaxaEmbarqueServic#atualizarTaxaEmbarque(Integer, Integer)} + * @param rutaId + * @param usuarioId TODO + */ + public void atualizarTaxaEmbarque(Integer rutaId, Integer usuarioId); +} diff --git a/src/com/rjconsultores/ventaboletos/dao/hibernate/TaxaEmbarqueHibernateDAO.java b/src/com/rjconsultores/ventaboletos/dao/hibernate/TaxaEmbarqueHibernateDAO.java new file mode 100644 index 000000000..b024bc437 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/dao/hibernate/TaxaEmbarqueHibernateDAO.java @@ -0,0 +1,38 @@ +package com.rjconsultores.ventaboletos.dao.hibernate; + +import org.hibernate.SQLQuery; +import org.hibernate.SessionFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Repository; + +import com.rjconsultores.ventaboletos.dao.TaxaEmbarqueDAO; +import com.rjconsultores.ventaboletos.dao.sqlbuilder.SQLBuilder; + +@Repository("taxaEmbarqueDAO") +public class TaxaEmbarqueHibernateDAO extends GenericHibernateDAO implements TaxaEmbarqueDAO { + + @Autowired + private SQLBuilder sqlBuilder; + + @Autowired + public TaxaEmbarqueHibernateDAO(@Qualifier("sessionFactory") SessionFactory factory) { + setSessionFactory(factory); + } + + @Override + public void atualizarTaxaEmbarque(Integer rutaId, Integer usuarioId) { + // Atualizo a taxa de embarque de acordo a parada e km + SQLQuery query = getSession().createSQLQuery(sqlBuilder.getSQLTaxaEmbarque1(rutaId, usuarioId)); + query.executeUpdate(); + + // Atualizo a taxa de embarque de acordo a km do orgao + query = getSession().createSQLQuery(sqlBuilder.getSQLTaxaEmbarque2(rutaId, usuarioId)); + query.executeUpdate(); + + // Atualizo a taxa de embarque de acordo a parada e valor fixo + query = getSession().createSQLQuery(sqlBuilder.getSQLTaxaEmbarque3(rutaId, usuarioId)); + query.executeUpdate(); + } + +} diff --git a/src/com/rjconsultores/ventaboletos/dao/sqlbuilder/SQLBuilder.java b/src/com/rjconsultores/ventaboletos/dao/sqlbuilder/SQLBuilder.java index ecafa278b..a816fe013 100644 --- a/src/com/rjconsultores/ventaboletos/dao/sqlbuilder/SQLBuilder.java +++ b/src/com/rjconsultores/ventaboletos/dao/sqlbuilder/SQLBuilder.java @@ -18,4 +18,8 @@ public interface SQLBuilder { public String getSQLTarifaOficial2(Integer rutaId, Integer usuarioId); + public String getSQLTaxaEmbarque1(Integer rutaId, Integer usuarioId); + public String getSQLTaxaEmbarque2(Integer rutaId, Integer usuarioId); + public String getSQLTaxaEmbarque3(Integer rutaId, Integer usuarioId); + } diff --git a/src/com/rjconsultores/ventaboletos/dao/sqlbuilder/SQLFactory.java b/src/com/rjconsultores/ventaboletos/dao/sqlbuilder/SQLFactory.java index 63a45ae30..f1a40bb2e 100644 --- a/src/com/rjconsultores/ventaboletos/dao/sqlbuilder/SQLFactory.java +++ b/src/com/rjconsultores/ventaboletos/dao/sqlbuilder/SQLFactory.java @@ -3,7 +3,6 @@ package com.rjconsultores.ventaboletos.dao.sqlbuilder; import org.apache.log4j.Logger; import com.rjconsultores.ventaboletos.dao.sqlbuilder.impl.SQLBuilderOracle; -import com.rjconsultores.ventaboletos.dao.sqlbuilder.impl.SQLBuilderSQLServer; import com.rjconsultores.ventaboletos.dao.util.DBUtil; public class SQLFactory { @@ -13,12 +12,7 @@ public class SQLFactory { if (DBUtil.getInstance().isOracle()) { return new SQLBuilderOracle(); } - if (DBUtil.getInstance().isSQLServer()) { - return new SQLBuilderSQLServer(); - } - - log.error("Não foi identificado a base de dados"); - - return new SQLBuilderOracle(); + + throw new RuntimeException("Não foi localizado a classe que traduz SQL Nativos"); } } diff --git a/src/com/rjconsultores/ventaboletos/dao/sqlbuilder/impl/SQLBuilderOracle.java b/src/com/rjconsultores/ventaboletos/dao/sqlbuilder/impl/SQLBuilderOracle.java index 556b9708a..061e44b81 100644 --- a/src/com/rjconsultores/ventaboletos/dao/sqlbuilder/impl/SQLBuilderOracle.java +++ b/src/com/rjconsultores/ventaboletos/dao/sqlbuilder/impl/SQLBuilderOracle.java @@ -147,4 +147,115 @@ public class SQLBuilderOracle implements SQLBuilder { return sb.toString(); } + @Override + public String getSQLTaxaEmbarque1(Integer rutaId, Integer usuarioId) { + StringBuilder sb = new StringBuilder(""); + sb.append("update tarifa_oficial set IMPORTETAXAEMBARQUE = "); + sb.append("( "); + sb.append("select "); + sb.append(" tepKm.valorTaxa "); + sb.append(" "); + sb.append(" "); + sb.append("from "); + sb.append(" tarifa_oficial tao "); + sb.append(" inner join tramo t on t.tramo_id = tao.tramo_id "); + sb.append(" inner join TAXA_EMBARQUE_PARADA tepKm on tepKm.parada_id = t.origen_id and tepKm.KMATE >= t.CANTKMREAL and tepKm.activo = 1 and tepKm.indtipo = 'K' "); + sb.append("where "); + sb.append(" tepKm.kmate= "); + sb.append(" (select min(tepKm1.KMATE) "); + sb.append(" from "); + sb.append(" TAXA_EMBARQUE_PARADA tepKm1 "); + sb.append(" where "); + sb.append(" tepKm1.parada_id = t.origen_id and tepKm1.KMATE >= t.CANTKMREAL and tepKm.activo = 1 "); + sb.append(" ) "); + sb.append(" and tao.marca_id = tarifa_oficial.marca_id "); + sb.append(" and tao.orgaoconcedente_id = tarifa_oficial.orgaoconcedente_id "); + sb.append(" and tao.claseservicio_id=tarifa_oficial.claseservicio_id "); + sb.append(" and tao.moneda_id = tarifa_oficial.moneda_id "); + sb.append(" and tao.ruta_id = tarifa_oficial.ruta_id "); + sb.append(" and tao.tramo_id = tarifa_oficial.tramo_id "); + sb.append(") "); + sb.append(", usuario_id = ").append(usuarioId).append(",fecmodif = sysdate "); + sb.append(" "); + sb.append("where "); + sb.append(" tarifa_oficial.activo = 1 "); + if (rutaId != null){ + sb.append(" and tarifa_oficial.ruta_id = ").append(rutaId); + } + + return sb.toString(); + } + + @Override + public String getSQLTaxaEmbarque2(Integer rutaId, Integer usuarioId) { + StringBuilder sb = new StringBuilder(""); + sb.append("update tarifa_oficial set IMPORTETAXAEMBARQUE = "); + sb.append("coalesce( "); + sb.append("( "); + sb.append("select "); + sb.append(" tek.valorTaxa "); + sb.append(" "); + sb.append("from "); + sb.append(" tarifa_oficial tao "); + sb.append(" inner join tramo t on t.tramo_id = tao.tramo_id "); + sb.append(" inner join taxa_embarque_km tek on tek.orgaoconcedente_id = tao.ORGAOCONCEDENTE_ID and tek.activo = 1 and t.CANTKMREAL <= tek.kmate "); + sb.append("where "); + sb.append(" tek.kmate= "); + sb.append(" (select min(tek1.KMATE) "); + sb.append(" from "); + sb.append(" taxa_embarque_km tek1 "); + sb.append(" where "); + sb.append(" tek1.orgaoconcedente_id = tao.ORGAOCONCEDENTE_ID and t.CANTKMREAL <= tek1.kmate and tek1.activo = 1 "); + sb.append(" ) "); + sb.append(" "); + sb.append(" and tao.marca_id = tarifa_oficial.marca_id "); + sb.append(" and tao.orgaoconcedente_id = tarifa_oficial.orgaoconcedente_id "); + sb.append(" and tao.claseservicio_id=tarifa_oficial.claseservicio_id "); + sb.append(" and tao.moneda_id = tarifa_oficial.moneda_id "); + sb.append(" and tao.ruta_id = tarifa_oficial.ruta_id "); + sb.append(" and tao.tramo_id = tarifa_oficial.tramo_id "); + sb.append("),IMPORTETAXAEMBARQUE) "); + sb.append(", usuario_id = ").append(usuarioId).append(",fecmodif = sysdate "); + sb.append(" "); + sb.append("where "); + sb.append(" tarifa_oficial.activo = 1 "); + + if (rutaId != null){ + sb.append(" and tarifa_oficial.ruta_id = ").append(rutaId); + } + return sb.toString(); + } + + @Override + public String getSQLTaxaEmbarque3(Integer rutaId, Integer usuarioId) { + StringBuilder sb = new StringBuilder(""); + + sb.append("update tarifa_oficial set IMPORTETAXAEMBARQUE = "); + sb.append("coalesce( "); + sb.append("( "); + sb.append("select "); + sb.append(" tepFixo.valorTaxa "); + sb.append(" "); + sb.append("from "); + sb.append(" tarifa_oficial tao "); + sb.append(" inner join tramo t on t.tramo_id = tao.tramo_id "); + sb.append(" inner join TAXA_EMBARQUE_PARADA tepFixo on tepFixo.parada_id = t.origen_id and tepFixo.indtipo = 'F' and tepFixo.activo = 1 "); + sb.append("where "); + sb.append(" tao.marca_id = tarifa_oficial.marca_id "); + sb.append(" and tao.orgaoconcedente_id = tarifa_oficial.orgaoconcedente_id "); + sb.append(" and tao.claseservicio_id=tarifa_oficial.claseservicio_id "); + sb.append(" and tao.moneda_id = tarifa_oficial.moneda_id "); + sb.append(" and tao.ruta_id = tarifa_oficial.ruta_id "); + sb.append(" and tao.tramo_id = tarifa_oficial.tramo_id "); + sb.append("),IMPORTETAXAEMBARQUE) "); + sb.append(", usuario_id = ").append(usuarioId).append(",fecmodif = sysdate "); + sb.append(" "); + sb.append("where "); + sb.append(" tarifa_oficial.activo = 1 "); + if (rutaId != null){ + sb.append(" and tarifa_oficial.ruta_id = ").append(rutaId); + } + return sb.toString(); + } + } \ No newline at end of file diff --git a/src/com/rjconsultores/ventaboletos/dao/sqlbuilder/impl/SQLBuilderSQLServer.java b/src/com/rjconsultores/ventaboletos/dao/sqlbuilder/impl/SQLBuilderSQLServer.java deleted file mode 100644 index 7ee1274f9..000000000 --- a/src/com/rjconsultores/ventaboletos/dao/sqlbuilder/impl/SQLBuilderSQLServer.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.rjconsultores.ventaboletos.dao.sqlbuilder.impl; - -import com.rjconsultores.ventaboletos.dao.sqlbuilder.SQLBuilder; - -public class SQLBuilderSQLServer implements SQLBuilder { - - @Override - public String getSQLTarifaOficial1(Integer codRuta, Integer usuarioId) { - throw new RuntimeException("SQL getSQLTarifaOficial1 não implementado"); - } - - @Override - public String getSQLTarifaOficial2(Integer rutaId, Integer usuarioId) { - throw new RuntimeException("SQL getSQLTarifaOficial2 não implementado"); - } -} diff --git a/src/com/rjconsultores/ventaboletos/service/TaxaEmbarqueService.java b/src/com/rjconsultores/ventaboletos/service/TaxaEmbarqueService.java new file mode 100644 index 000000000..60ad37f8a --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/service/TaxaEmbarqueService.java @@ -0,0 +1,21 @@ +package com.rjconsultores.ventaboletos.service; + +public interface TaxaEmbarqueService { + + /** + * Atualiza a taxa de embarque da entidade TarifaOficial.
+ * + * Para o cálculo, é usado a seguinte lógica:
+ * - Atualiza de acordo a tabela TAXA_EMBARQUE_PARADA por kilometragem
+ * - Atualiza de acordo a tabela TAXA_EMBARQUE por kilometragem
+ * - Atualiza de acordo a tabela TAXA_EMBARQUE_PARADA por valor fixo
+ * + * A sequencia acima indica a ordem de atualização da taxa de embarque.
+ * + * A taxa de embarque mais restritiva fica por último (TAXA_EMBARQUE_PARADA por valor fixo) + * + * @param rutaId - Se informado, será atualizado apenas a taxa de embarque da ruta informada + */ + public void atualizarTaxaEmbarque(Integer rutaId); + +} diff --git a/src/com/rjconsultores/ventaboletos/service/impl/TaxaEmbarqueServiceImpl.java b/src/com/rjconsultores/ventaboletos/service/impl/TaxaEmbarqueServiceImpl.java new file mode 100644 index 000000000..d5a3d5ee1 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/service/impl/TaxaEmbarqueServiceImpl.java @@ -0,0 +1,22 @@ +package com.rjconsultores.ventaboletos.service.impl; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import com.rjconsultores.ventaboletos.dao.TaxaEmbarqueDAO; +import com.rjconsultores.ventaboletos.service.TaxaEmbarqueService; +import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado; + +@Service("taxaEmbarqueService") +public class TaxaEmbarqueServiceImpl implements TaxaEmbarqueService{ + @Autowired + private TaxaEmbarqueDAO taxaEmbarqueDAO; + + @Override + @Transactional + public void atualizarTaxaEmbarque(Integer rutaId) { + taxaEmbarqueDAO.atualizarTaxaEmbarque(rutaId, UsuarioLogado.getUsuarioLogado().getUsuarioId()); + } + +}