com.dominion.salud.nomenclator.negocio.service.impl.farmatools.InteracGgServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.dominion.salud.nomenclator.negocio.service.impl.farmatools.InteracGgServiceImpl.java

Source

/*
 * Copyright (C) 2015 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.nomenclator.negocio.service.impl.farmatools;

import com.dominion.salud.nomenclator.negocio.entities.aemps.AtcInteracciones;
import com.dominion.salud.nomenclator.negocio.entities.farmatools.InteracGg;
import com.dominion.salud.nomenclator.negocio.entities.farmatools.InteracGgPK;
import com.dominion.salud.nomenclator.negocio.repositories.farmatools.InteracGgRepository;
import com.dominion.salud.nomenclator.negocio.service.aemps.AtcInteraccionesService;
import com.dominion.salud.nomenclator.negocio.service.farmatools.InteracGgService;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.persistence.NoResultException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author jcgonzalez
 */
@Transactional
@Service("interacGgService")
public class InteracGgServiceImpl extends AbstractFarmatoolsServiceImpl<InteracGg, Long>
        implements InteracGgService {

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

    @Autowired
    private InteracGgRepository interacGgRepository;
    @Autowired
    private AtcInteraccionesService atcInteraccionesService;

    @Override
    public InteracGg findOne(InteracGg interacGg) {
        return interacGgRepository.findOne(interacGg);
    }

    @Override
    public void autoload() {
        logger.info("INICIANDO la carga de INTERACCIONES_GG automatizadas");

        Set<String> mensajes = new HashSet<>();
        List<AtcInteracciones> listaAtcInteracciones = atcInteraccionesService.findAll();

        if (listaAtcInteracciones != null && !listaAtcInteracciones.isEmpty()) {
            logger.debug("     Se han obtenido: " + listaAtcInteracciones.size() + " INTERACCIONES_GG");
            Iterator<AtcInteracciones> iteradorAtcInteracciones = listaAtcInteracciones.iterator();
            int contador = 1;

            while (iteradorAtcInteracciones.hasNext()) {
                logger.debug("          Cargando INTERACCION_GG (" + contador + " de "
                        + listaAtcInteracciones.size() + ")");
                AtcInteracciones atcInteracciones = iteradorAtcInteracciones.next();

                try {
                    InteracGg interacGg = new InteracGg();
                    InteracGgPK interacGgPK = new InteracGgPK();

                    interacGgPK.setGrupo1(atcInteracciones.getAtc().getCodAtc());
                    interacGgPK.setGrupo2(atcInteracciones.getInteraccion().getCodAtc());
                    interacGg.setInteracGgPK(interacGgPK);
                    interacGg.setDesInterac(atcInteracciones.getEfecto());
                    interacGg.setDesNaturaleza(StringUtils.substring(atcInteracciones.getOrientacion(), 0, 200));
                    interacGg.setObservacion(StringUtils.substring(atcInteracciones.getDescripcion(), 0, 60));

                    try {
                        interacGg = interacGgRepository.findOne(interacGg);
                        logger.debug("               INTERACCION_GG ya existe: " + interacGg);
                    } catch (NoResultException nre) {
                        logger.debug("               INTERACCION_GG NO existe. Almacenando: " + interacGg);
                        interacGgRepository.save(interacGg);
                    }

                    logger.debug("                    " + interacGg.toString());
                    logger.debug("               INTERACCION_GG creada correctamente");

                    contador++;
                } catch (Exception e) {
                    logger.error("               No se ha podido generar la INTERACCION_GG: " + e.toString());
                    mensajes.add("No se ha podido generar la INTERACCION_GG: " + e.toString());
                }
            }
        }

        if (!mensajes.isEmpty()) {
            logger.warn("     Se han producido los siguientes errores en la carga de registros: ");
            Iterator<String> iteradorMensajes = mensajes.iterator();
            while (iteradorMensajes.hasNext()) {
                logger.warn("          - " + iteradorMensajes.next());
            }
        }

        logger.info("FINALIZANDO la carga de INTERACCIONES_GG automatizadas");
    }
}