diff --git a/src/com/rjconsultores/ventaboletos/dao/AliasServicoDAO.java b/src/com/rjconsultores/ventaboletos/dao/AliasServicoDAO.java index be110a826..07ef56c68 100644 --- a/src/com/rjconsultores/ventaboletos/dao/AliasServicoDAO.java +++ b/src/com/rjconsultores/ventaboletos/dao/AliasServicoDAO.java @@ -1,7 +1,29 @@ package com.rjconsultores.ventaboletos.dao; +import java.util.List; + import com.rjconsultores.ventaboletos.entidad.AliasServico; public interface AliasServicoDAO extends GenericDAO{ + /** + * Realiza a buscar com os filtros informados e restringindo o corridaId para is null + * + * @param origenId + * @param destinoId + * @param rutaId + * @return + */ + public List buscarAliasSemCorrida(Integer origenId,Integer destinoId,Integer rutaId); + + /** + * Realiza a busca com os filtro informados. + * @param origenId + * @param destinoId + * @param rutaId + * @param corridaId + * @return + */ + public List buscar(Integer origenId,Integer destinoId,Integer rutaId,Integer corridaId); + } diff --git a/src/com/rjconsultores/ventaboletos/dao/hibernate/AliasServicoHibernateDAO.java b/src/com/rjconsultores/ventaboletos/dao/hibernate/AliasServicoHibernateDAO.java index 267f68673..46a661073 100644 --- a/src/com/rjconsultores/ventaboletos/dao/hibernate/AliasServicoHibernateDAO.java +++ b/src/com/rjconsultores/ventaboletos/dao/hibernate/AliasServicoHibernateDAO.java @@ -1,6 +1,10 @@ package com.rjconsultores.ventaboletos.dao.hibernate; +import java.util.List; + +import org.hibernate.Criteria; import org.hibernate.SessionFactory; +import org.hibernate.criterion.Restrictions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Repository; @@ -16,4 +20,29 @@ public class AliasServicoHibernateDAO extends GenericHibernateDAO buscarAliasSemCorrida(Integer origenId, Integer destinoId, Integer rutaId) { + + Criteria c = makeCriteria(); + c.add(Restrictions.eq("activo", Boolean.TRUE)); + c.add(Restrictions.eq("origen.paradaId",origenId)); + c.add(Restrictions.eq("destino.paradaId",destinoId)); + c.add(Restrictions.eq("ruta.rutaId",rutaId)); + c.add(Restrictions.isNull("corridaId")); + + return c.list(); + } + + @Override + public List buscar(Integer origenId, Integer destinoId, Integer rutaId, Integer corridaId) { + Criteria c = makeCriteria(); + c.add(Restrictions.eq("activo", Boolean.TRUE)); + c.add(Restrictions.eq("origen.paradaId",origenId)); + c.add(Restrictions.eq("destino.paradaId",destinoId)); + c.add(Restrictions.eq("ruta.rutaId",rutaId)); + c.add(Restrictions.eq("corridaId",corridaId)); + + return c.list(); + } } diff --git a/src/com/rjconsultores/ventaboletos/exception/BusinessException.java b/src/com/rjconsultores/ventaboletos/exception/BusinessException.java index 72ee06334..9a318446b 100644 --- a/src/com/rjconsultores/ventaboletos/exception/BusinessException.java +++ b/src/com/rjconsultores/ventaboletos/exception/BusinessException.java @@ -6,6 +6,10 @@ import org.zkoss.util.resource.Labels; public class BusinessException extends Exception { + /** + * + */ + private static final long serialVersionUID = 1L; /** * * @param message diff --git a/src/com/rjconsultores/ventaboletos/service/AliasServicoService.java b/src/com/rjconsultores/ventaboletos/service/AliasServicoService.java index cc1c8ff36..2cefabeff 100644 --- a/src/com/rjconsultores/ventaboletos/service/AliasServicoService.java +++ b/src/com/rjconsultores/ventaboletos/service/AliasServicoService.java @@ -1,7 +1,17 @@ package com.rjconsultores.ventaboletos.service; +import java.util.List; + import com.rjconsultores.ventaboletos.entidad.AliasServico; +import com.rjconsultores.ventaboletos.exception.BusinessException; -public interface AliasServicoService extends GenericService{ +public interface AliasServicoService{ + public List obtenerTodos(); + + public AliasServico obtenerID(Integer id); + + public AliasServico suscribirActualizar(AliasServico entidad) throws BusinessException; + + public void borrar(AliasServico entidad); } diff --git a/src/com/rjconsultores/ventaboletos/service/impl/AliasServicoServiceImpl.java b/src/com/rjconsultores/ventaboletos/service/impl/AliasServicoServiceImpl.java index 0fd32d932..fd397af20 100644 --- a/src/com/rjconsultores/ventaboletos/service/impl/AliasServicoServiceImpl.java +++ b/src/com/rjconsultores/ventaboletos/service/impl/AliasServicoServiceImpl.java @@ -1,14 +1,18 @@ package com.rjconsultores.ventaboletos.service.impl; import java.util.Calendar; +import java.util.Collections; import java.util.List; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.Predicate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.rjconsultores.ventaboletos.dao.AliasServicoDAO; import com.rjconsultores.ventaboletos.entidad.AliasServico; +import com.rjconsultores.ventaboletos.exception.BusinessException; import com.rjconsultores.ventaboletos.service.AliasServicoService; import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado; @@ -27,21 +31,43 @@ public class AliasServicoServiceImpl implements AliasServicoService { } @Transactional - public AliasServico suscribir(AliasServico entidad) { + public AliasServico suscribirActualizar(AliasServico entidad) throws BusinessException { + + if ( (entidad.getOrigen() == null) || (entidad.getDestino() == null) || (entidad.getAliasDestino()== null) || (entidad.getAliasOrigen()== null) || (entidad.getRuta()== null)){ + throw new BusinessException("editarAliasServicoController.MSG.camposObrigatorios"); + } + + List lsBusca = Collections.emptyList(); + + if (entidad.getCorridaId() != null){ + lsBusca = aliasServicoDAO.buscar(entidad.getOrigen().getParadaId(), entidad.getDestino().getParadaId(), entidad.getRuta().getRutaId(), entidad.getCorridaId()); + }else{ + lsBusca = aliasServicoDAO.buscarAliasSemCorrida(entidad.getOrigen().getParadaId(), entidad.getDestino().getParadaId(), entidad.getRuta().getRutaId()); + } + + if (!lsBusca.isEmpty()){ + boolean existe = false; + + for(AliasServico as : lsBusca){ + if (as.equals(entidad)){ + existe = true; + } + } + + if (!existe){ + throw new BusinessException("editarAliasServicoController.MSG.registroYaExiste"); + } + } + entidad.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId()); entidad.setFecmodif(Calendar.getInstance().getTime()); entidad.setActivo(Boolean.TRUE); - return aliasServicoDAO.suscribir(entidad); - } - - @Transactional - public AliasServico actualizacion(AliasServico entidad) { - entidad.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId()); - entidad.setFecmodif(Calendar.getInstance().getTime()); - entidad.setActivo(Boolean.TRUE); - - return aliasServicoDAO.actualizacion(entidad); + if (entidad.getAliasServicoId() == null){ + return aliasServicoDAO.suscribir(entidad); + }else{ + return aliasServicoDAO.actualizacion(entidad); + } } @Transactional