com.dominion.salud.mpr.negocio.service.integracion.impl.BuzonErroresServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.dominion.salud.mpr.negocio.service.integracion.impl.BuzonErroresServiceImpl.java

Source

/*
 * Copyright (C) 2016 Dominion Global
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.dominion.salud.mpr.negocio.service.integracion.impl;

import com.dominion.salud.mpr.negocio.entities.acuerdos.AcuCentros;
import com.dominion.salud.mpr.negocio.entities.admin.Centros;
import com.dominion.salud.mpr.negocio.entities.integracion.BuzonErrores;
import com.dominion.salud.mpr.negocio.entities.integracion.BuzonIn;
import com.dominion.salud.mpr.negocio.entities.integracion.BuzonOut;
import com.dominion.salud.mpr.negocio.repositories.integracion.BuzonErroresRepository;
import com.dominion.salud.mpr.negocio.service.acuerdos.AcuCentrosService;
import com.dominion.salud.mpr.negocio.service.integracion.BuzonErroresService;
import com.dominion.salud.mpr.negocio.service.integracion.BuzonInService;
import com.dominion.salud.mpr.negocio.service.integracion.BuzonOutService;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author jcgonzalez
 */
@Service("buzonErroresService")
public class BuzonErroresServiceImpl extends AbstractIntegracionServiceImpl<BuzonErrores, Long>
        implements BuzonErroresService {

    private static final Logger logger = LoggerFactory.getLogger(BuzonErroresServiceImpl.class);

    @Autowired
    private BuzonErroresRepository buzonErroresRepository;
    @Autowired
    private BuzonInService buzonInService;
    @Autowired
    private BuzonOutService buzonOutService;

    @Autowired
    private AcuCentrosService acuCentrosService;

    @Override
    public List<BuzonErrores> findAllByIdCentro(Centros centros) {
        return buzonErroresRepository.findAllByIdCentro(centros);
    }

    @Override
    @Transactional
    public BuzonErrores procesar(BuzonErrores buzonErrores) {
        logger.debug("PROCESANDO EL REGISTRO DE BUZON_ERRORES: " + buzonErrores.toString());
        if (buzonErrores.getBuzonIn() != null) {
            BuzonIn buzonIn = buzonErrores.getBuzonIn();
            logger.debug("     Procesando mensaje de BUZON_IN: " + buzonErrores.getBuzonIn().toString());

            switch (buzonIn.getTipo()) {
            case "ZDS_O13": //Dispensacion
                buzonIn.setEstado(BuzonErrores.MENSAJE_NO_PROCESADO);
                buzonInService.save(buzonIn);
                delete(buzonErrores);
                break;
            case "ZFN_M13": //Evaluacion
                buzonIn.setEstado(BuzonErrores.MENSAJE_NO_PROCESADO);
                buzonInService.save(buzonIn);
                delete(buzonErrores);
                break;
            case "RAS_O17": //Administracion
                break;
            case "OMP_O09": //Prescripcion
                break;
            default:
                logger.error("No se reconoce el formato del mensaje: " + buzonIn.getTipo());
            }
        } else if (buzonErrores.getBuzonOut() != null) {
            BuzonOut buzonOut = buzonErrores.getBuzonOut();
            logger.debug("     Procesando mensaje de BUZON_OUT: " + buzonErrores.getBuzonOut().toString());

            switch (buzonOut.getTipo()) {
            case "ZFN_O13": //Acuerdo
                if (StringUtils.isNotBlank(buzonErrores.getParametros())) {
                    AcuCentros acuCentros = acuCentrosService
                            .findOne(NumberUtils.toLong(buzonErrores.getParametros()));
                    if (acuCentros != null) {
                        buzonOutService.delete(buzonOut);
                        delete(buzonErrores);
                        acuCentrosService.toBuzonOut(acuCentros);
                    }
                } else {
                    buzonOut.setEstado(BuzonErrores.MENSAJE_NO_PROCESADO);
                    buzonOutService.save(buzonOut);
                    delete(buzonErrores);
                }
                break;
            default:
                logger.error("No se reconoce el formato del mensaje: " + buzonOut.getTipo());
            }
        } else {
            logger.error("No se reconoce el formato del mensaje");
        }
        logger.debug("FINALIZA EL PROCESADO DEL REGISTRO DE BUZON_ERRORES: " + buzonErrores.toString());
        return buzonErrores;
    }

    @Override
    @Scheduled(cron = "${mpr.task.buzon.errores.clean}")
    public void clean() {
        logger.debug("Eliminando mensajes procesados de BUZON_ERRORES");
        delete(buzonErroresRepository.findAllProcesados());
        logger.debug("Mensajes procesados de BUZON_ERRORES eliminados correctamente");
    }
}