/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.rjconsultores.ventaboletos.service.impl; import java.util.Calendar; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.rjconsultores.ventaboletos.dao.EstacionDAO; import com.rjconsultores.ventaboletos.entidad.Estacion; import com.rjconsultores.ventaboletos.entidad.EstacionSitef; import com.rjconsultores.ventaboletos.entidad.PuntoVenta; import com.rjconsultores.ventaboletos.exception.BusinessException; import com.rjconsultores.ventaboletos.service.AutorizaFolioService; import com.rjconsultores.ventaboletos.service.EstacionService; import com.rjconsultores.ventaboletos.utilerias.ApplicationProperties; import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado; /** * * @author Administrador */ @Service("estacionService") public class EstacionServiceImpl implements EstacionService { @Autowired private EstacionDAO estacionDAO; @Autowired private AutorizaFolioService autorizaFolioService; public List obtenerTodos() { return estacionDAO.obtenerTodos(); } public Estacion obtenerID(Integer id) { return estacionDAO.obtenerID(id); } @Transactional(rollbackFor = BusinessException.class) public Estacion suscribirActualizar(Estacion estacion) throws BusinessException { validarEstoqueUpdate(estacion); Boolean esMacDuplicado = Boolean.FALSE; List lsEstacionMac = estacionDAO.buscar(estacion.getDescmac()); if (!lsEstacionMac.isEmpty()) { for (Estacion est : lsEstacionMac) { if (!est.getEstacionId().equals(estacion.getEstacionId())) { esMacDuplicado = Boolean.TRUE; } } } if (esMacDuplicado) { throw new BusinessException("estacionServiceImpl.msg.macDuplicado"); } boolean esCajaDuplicado = false; List lsEstacion = estacionDAO.buscar(estacion.getNumcaja(), estacion.getPuntoVenta()); if (!lsEstacion.isEmpty()) { if (estacion.getEstacionId() == null) { esCajaDuplicado = true; } else { esCajaDuplicado = !lsEstacion.get(0).getEstacionId().equals(estacion.getEstacionId()); } } if (esCajaDuplicado) { throw new BusinessException("estacionServiceImpl.msg.cajaDuplicado"); } estacion.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId()); estacion.setFecmodif(Calendar.getInstance().getTime()); estacion.setActivo(Boolean.TRUE); if (estacion.getEstacionId() == null) { estacion = estacionDAO.suscribir(estacion); } else { estacion = estacionDAO.actualizacion(estacion); } if (ApplicationProperties.getInstance().generarRotinaFolios()) { autorizaFolioService.noChequeFolioPreimpresos(estacion); } return estacion; } private void validarEstoqueUpdate(Estacion estacion) throws BusinessException { if (estacion.getEstacionId() == null) { return; } if (!ApplicationProperties.getInstance().generarRotinaFolios()) { Estacion estacionPuntoVentaAnterior = estacionDAO.obtenerID(estacion.getEstacionId()); if (!estacionPuntoVentaAnterior.getPuntoVenta().equals(estacion.getPuntoVenta())) { if (estacionDAO.temEstoque(estacionPuntoVentaAnterior.getPuntoVenta(), estacion)) { throw new BusinessException("estacionServiceImpl.msg.hayStock"); } } } } private void validarEstoqueBorrar(Estacion estacion) throws BusinessException { if (!ApplicationProperties.getInstance().generarRotinaFolios()) { if (estacionDAO.temEstoque(estacion.getPuntoVenta(), estacion)) { throw new BusinessException("estacionServiceImpl.msg.hayStock.borrar"); } } } @Transactional public void borrar(Estacion entidad) throws BusinessException { validarEstoqueBorrar(entidad); entidad.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId()); entidad.setFecmodif(Calendar.getInstance().getTime()); entidad.setActivo(Boolean.FALSE); if (entidad.getEstacionSitefList() != null) { for (EstacionSitef es : entidad.getEstacionSitefList()) { es.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId()); es.setFecmodif(Calendar.getInstance().getTime()); es.setActivo(Boolean.FALSE); } } estacionDAO.actualizacion(entidad); } public List buscar(String descEstacion, String descMac, Long nunCaja, PuntoVenta pv) { return estacionDAO.buscar(descEstacion, descMac, nunCaja, pv); } public List buscar(String descMac) { return estacionDAO.buscar(descMac); } @Override public Long getDecimalMAC(String mac) { mac = mac.trim(); mac = mac.replace("-", ""); return Long.parseLong(mac, 16); } @Override public List buscarEstaciones(PuntoVenta puntoVenta) { List estaciones = estacionDAO.buscarEstaciones(puntoVenta); return estaciones; } }