All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.somospnt.parametros.repository.ParametroRepository Maven / Gradle / Ivy

The newest version!
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package com.somospnt.parametros.repository;

import com.somospnt.parametros.domain.Parametro;
import com.somospnt.parametros.exception.ParametroException;
import static java.util.Arrays.asList;
import java.util.List;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;

/**
 * Repo de parametros
 *
 */
public class ParametroRepository {

    private final JdbcTemplate jdbcTemplate;

    public ParametroRepository(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public  T buscar(String nombre, Class clase) {
        try {
            return jdbcTemplate.queryForObject("SELECT valor FROM parametro WHERE nombre = ?", clase, nombre);
        } catch (IncorrectResultSizeDataAccessException e) {
            throw new ParametroException("Falta el parametro '" + nombre + "'");
        }
    }

    public Parametro guardar(Parametro parametro) {
        try {
            jdbcTemplate.update("UPDATE parametro SET valor = ? WHERE nombre = ?", parametro.getValor(), parametro.getNombre());
            return jdbcTemplate.queryForObject("SELECT * FROM parametro WHERE nombre = ?", new BeanPropertyRowMapper<>(Parametro.class), parametro.getNombre());
        } catch (EmptyResultDataAccessException e) {
            jdbcTemplate.update("INSERT INTO parametro (nombre, valor) VALUES (?, ?)", parametro.getNombre(), parametro.getValor());
            return jdbcTemplate.queryForObject("SELECT * FROM parametro WHERE nombre = ?", new BeanPropertyRowMapper<>(Parametro.class), parametro.getNombre());
        }
    }

    public boolean existe(String nombre) {
        try {
            this.buscar(nombre, String.class);
            return true;
        } catch (ParametroException e) {
            return false;
        }
    }

    public List buscarLista(String nombre) {
        return asList(StringUtils.commaDelimitedListToStringArray(buscar(nombre, String.class)));
    }

    public void borrar(String parametro) {
        if (existe(parametro)) {
            jdbcTemplate.update("DELETE FROM parametro WHERE nombre = ?", parametro);
        } else {
            throw new ParametroException("Falta el parametro '" + parametro + "'");
        }
    }

    public List buscarPorNombreLike(String parametro) {
        List listaDeParametros = jdbcTemplate.query("SELECT * FROM parametro WHERE nombre like ?", new BeanPropertyRowMapper<>(Parametro.class), parametro + ".%");
        if (listaDeParametros.isEmpty()) {
            throw new ParametroException("Falta el parametro '" + parametro + "'");
        } else {
            return listaDeParametros;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy