Primer commit
This commit is contained in:
+11
@@ -0,0 +1,11 @@
|
|||||||
|
# Compilados de Java
|
||||||
|
*.class
|
||||||
|
bin/
|
||||||
|
|
||||||
|
# Compilados de Maven
|
||||||
|
target/
|
||||||
|
|
||||||
|
# Configuración de Eclipse
|
||||||
|
.project
|
||||||
|
.classpath
|
||||||
|
.settings/
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
sgdb.conexion.url=jdbc:mysql://localhost:3306/
|
||||||
|
bbdd.nombre=Mapa_Mundi
|
||||||
|
bbdd.usuario=root
|
||||||
|
bbdd.clave=root1234
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
package org.palomafp.aadd.smmb;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.smmb.exception.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.smmb.gestores.GestorConfiguracion;
|
||||||
|
import org.palomafp.aadd.smmb.model.Continente;
|
||||||
|
import org.palomafp.aadd.smmb.service.ContinenteService;
|
||||||
|
|
||||||
|
public class GestorMapaMundi {
|
||||||
|
|
||||||
|
private final ContinenteService continenteService;
|
||||||
|
|
||||||
|
// Recibimos la configuración ya cargada y la asignamos
|
||||||
|
public GestorMapaMundi(GestorConfiguracion configuracion) {
|
||||||
|
this.continenteService = new ContinenteService(configuracion);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
GestorConfiguracion configuracion = new GestorConfiguracion("application.properties");
|
||||||
|
GestorMapaMundi gmm = new GestorMapaMundi(configuracion);
|
||||||
|
|
||||||
|
gmm.ejecutarPasos();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error de configuración: " + e.getMessage());
|
||||||
|
} catch (MapaMundiException e) {
|
||||||
|
System.err.println("Error en el acceso: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ejecutarPasos() throws MapaMundiException {
|
||||||
|
|
||||||
|
System.out.println("1. Lista de continentes:");
|
||||||
|
List<Continente> continentes = continenteService.obtenerListaContinentes();
|
||||||
|
|
||||||
|
for (Continente c : continentes) {
|
||||||
|
System.out.println(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("2. Continente con código 04:");
|
||||||
|
Continente continente = continenteService.obtenerContinentePorCodigo("04");
|
||||||
|
|
||||||
|
if (continente == null) {
|
||||||
|
System.out.println("No existe el continente 04");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(continente);
|
||||||
|
|
||||||
|
System.out.println("3. Borramos el continente 04 y sus países");
|
||||||
|
continenteService.borrarContinente("04");
|
||||||
|
|
||||||
|
System.out.println("4. Insertamos de nuevo el continente");
|
||||||
|
continenteService.insertarContinente(continente);
|
||||||
|
|
||||||
|
System.out.println("5. Cambiamos el nombre a Paneuropa");
|
||||||
|
continente.setNombre("Paneuropa");
|
||||||
|
continenteService.actualizarContinente(continente);
|
||||||
|
|
||||||
|
Continente actualizado = continenteService.obtenerContinentePorCodigo("04");
|
||||||
|
System.out.println("Resultado final: " + actualizado);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package org.palomafp.aadd.smmb.dao;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.smmb.model.Continente;
|
||||||
|
|
||||||
|
public interface IContinenteDAO {
|
||||||
|
|
||||||
|
Continente obtenerContinentePorCodigo(String idContinente) throws SQLException;
|
||||||
|
List<Continente> obtenerListaContinentes() throws SQLException;
|
||||||
|
void insertarContinente(Continente continente) throws SQLException;
|
||||||
|
void borrarContinente(String codigoContinente) throws SQLException;
|
||||||
|
void actualizarContinente(Continente continente) throws SQLException;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package org.palomafp.aadd.smmb.dao;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.smmb.model.Pais;
|
||||||
|
|
||||||
|
public interface IPaisDAO {
|
||||||
|
|
||||||
|
public List<Pais> obtenerPaises() throws SQLException;
|
||||||
|
public Pais obtenerPaisPorID(int idPais) throws SQLException;
|
||||||
|
void insertarPais(Pais pais) throws SQLException;
|
||||||
|
void borrarPais(int idPais) throws SQLException;
|
||||||
|
void actualizarPais(Pais pais) throws SQLException;
|
||||||
|
void borrarPaisesPorContinente(String codContinente) throws SQLException;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
package org.palomafp.aadd.smmb.dao.impl;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.smmb.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.smmb.model.Continente;
|
||||||
|
|
||||||
|
public class ContinenteDaoMySql implements IContinenteDAO {
|
||||||
|
|
||||||
|
private final Connection conexion;
|
||||||
|
|
||||||
|
public ContinenteDaoMySql(Connection conexion) {
|
||||||
|
this.conexion = conexion;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Continente> obtenerListaContinentes() throws SQLException {
|
||||||
|
|
||||||
|
String sql = "SELECT * FROM T_CONTINENTE";
|
||||||
|
List<Continente> lista = new ArrayList<>();
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql); ResultSet rs = ps.executeQuery()) {
|
||||||
|
|
||||||
|
while (rs.next()) {
|
||||||
|
|
||||||
|
Continente continente = construirContinente(rs);
|
||||||
|
lista.add(continente);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lista;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Continente obtenerContinentePorCodigo(String idContinente) throws SQLException {
|
||||||
|
|
||||||
|
String sql = "SELECT * FROM T_CONTINENTE WHERE codigo = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
ps.setString(1, idContinente);
|
||||||
|
|
||||||
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
|
|
||||||
|
if (rs.next()) {
|
||||||
|
Continente continente = construirContinente(rs);
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void insertarContinente(Continente continente) throws SQLException {
|
||||||
|
|
||||||
|
String sql = "INSERT INTO T_CONTINENTE (codigo, nombre_continente) VALUES (?, ?)";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
ps.setString(1, continente.getCodigo());
|
||||||
|
ps.setString(2, continente.getNombre());
|
||||||
|
|
||||||
|
ps.executeUpdate();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void borrarContinente(String codigoContinente) throws SQLException {
|
||||||
|
|
||||||
|
String sql = "DELETE FROM T_CONTINENTE WHERE codigo = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
ps.setString(1, codigoContinente);
|
||||||
|
|
||||||
|
ps.executeUpdate();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actualizarContinente(Continente continente) throws SQLException {
|
||||||
|
|
||||||
|
String sql = "UPDATE T_CONTINENTE SET nombre_continente = ? WHERE codigo = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
ps.setString(1, continente.getNombre());
|
||||||
|
ps.setString(2, continente.getCodigo());
|
||||||
|
|
||||||
|
ps.executeUpdate();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private Continente construirContinente(ResultSet rs) throws SQLException {
|
||||||
|
|
||||||
|
Continente continente = new Continente();
|
||||||
|
continente.setCodigo(rs.getString("codigo"));
|
||||||
|
continente.setNombre(rs.getString("nombre_continente"));
|
||||||
|
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,156 @@
|
|||||||
|
package org.palomafp.aadd.smmb.dao.impl;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.smmb.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.smmb.model.Continente;
|
||||||
|
import org.palomafp.aadd.smmb.model.Pais;
|
||||||
|
|
||||||
|
public class PaisDaoMySql implements IPaisDAO{
|
||||||
|
|
||||||
|
private final Connection conexion;
|
||||||
|
|
||||||
|
public PaisDaoMySql(Connection conexion) {
|
||||||
|
this.conexion = conexion;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Pais> obtenerPaises() throws SQLException {
|
||||||
|
|
||||||
|
String sql = "SELECT * FROM T_PAIS p JOIN T_CONTINENTE c ON p.cod_continente = c.codigo";
|
||||||
|
List<Pais> lista = new ArrayList<>();
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql);
|
||||||
|
ResultSet rs = ps.executeQuery()) {
|
||||||
|
|
||||||
|
while (rs.next()) {
|
||||||
|
Pais pais = construirPais(rs);
|
||||||
|
lista.add(pais);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return lista;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Pais obtenerPaisPorID(int idPais) throws SQLException {
|
||||||
|
|
||||||
|
String sql = "SELECT * FROM T_PAIS p JOIN T_CONTINENTE c ON p.cod_continente = c.codigo WHERE p.identificador = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
ps.setInt(1, idPais);
|
||||||
|
|
||||||
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
|
|
||||||
|
// Vemos si la encuentra o no para devolver el pais o null
|
||||||
|
if (rs.next()) {
|
||||||
|
|
||||||
|
Pais pais = construirPais(rs);
|
||||||
|
return pais;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void insertarPais(Pais pais) throws SQLException {
|
||||||
|
|
||||||
|
String codContinente = pais.getContinente().getCodigo();
|
||||||
|
int identificador = pais.getIdentificador();
|
||||||
|
String nombre = pais.getNombre();
|
||||||
|
String capital = pais.getNombreCapital();
|
||||||
|
|
||||||
|
String sql = "INSERT INTO T_PAIS (cod_continente, identificador, nombre_pais, capital) VALUES (?, ?, ?, ?)";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
ps.setString(1, codContinente);
|
||||||
|
ps.setInt(2, identificador);
|
||||||
|
ps.setString(3, nombre);
|
||||||
|
ps.setString(4, capital);
|
||||||
|
|
||||||
|
ps.executeUpdate();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void borrarPais(int idPais) throws SQLException {
|
||||||
|
|
||||||
|
String sql = "DELETE FROM T_PAIS WHERE identificador = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
ps.setInt(1, idPais);
|
||||||
|
ps.executeUpdate();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actualizarPais(Pais pais) throws SQLException {
|
||||||
|
|
||||||
|
String codContinente = pais.getContinente().getCodigo();
|
||||||
|
int identificador = pais.getIdentificador();
|
||||||
|
String nombre = pais.getNombre();
|
||||||
|
String capital = pais.getNombreCapital();
|
||||||
|
|
||||||
|
String sql = "UPDATE T_PAIS SET cod_continente = ?, nombre_pais = ?, capital = ? WHERE identificador = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
ps.setString(1, codContinente);
|
||||||
|
ps.setString(2, nombre);
|
||||||
|
ps.setString(3, capital);
|
||||||
|
ps.setInt(4, identificador);
|
||||||
|
|
||||||
|
ps.executeUpdate();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private Pais construirPais(ResultSet rs) throws SQLException {
|
||||||
|
|
||||||
|
Continente continente = new Continente();
|
||||||
|
continente.setCodigo(rs.getString("codigo"));
|
||||||
|
continente.setNombre(rs.getString("nombre_continente"));
|
||||||
|
|
||||||
|
Pais pais = new Pais();
|
||||||
|
pais.setIdentificador(rs.getInt("identificador"));
|
||||||
|
pais.setNombre(rs.getString("nombre_pais"));
|
||||||
|
pais.setNombreCapital(rs.getString("capital"));
|
||||||
|
pais.setContinente(continente);
|
||||||
|
|
||||||
|
return pais;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void borrarPaisesPorContinente(String codContinente) throws SQLException {
|
||||||
|
|
||||||
|
String sql = "DELETE FROM T_PAIS WHERE cod_continente = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
ps.setString(1, codContinente);
|
||||||
|
ps.executeUpdate();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package org.palomafp.aadd.smmb.exception;
|
||||||
|
|
||||||
|
public class MapaMundiException extends Exception{
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
public MapaMundiException(String mensaje) {
|
||||||
|
super(mensaje);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MapaMundiException(String mensaje, Throwable causa) {
|
||||||
|
super(mensaje, causa);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package org.palomafp.aadd.smmb.gestores;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class GestorConexionesBBDD {
|
||||||
|
|
||||||
|
private final String url;
|
||||||
|
private final String bbdd;
|
||||||
|
private final String usuario;
|
||||||
|
private final String clave;
|
||||||
|
|
||||||
|
public GestorConexionesBBDD(GestorConfiguracion gestor) {
|
||||||
|
this.url = gestor.getUrl();
|
||||||
|
this.bbdd = gestor.getBaseDatos();
|
||||||
|
this.usuario = gestor.getUsuario();
|
||||||
|
this.clave = gestor.getClave();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Connection obtenerConexion() throws SQLException {
|
||||||
|
|
||||||
|
Connection conexion = DriverManager.getConnection(url + bbdd, usuario, clave);
|
||||||
|
|
||||||
|
return conexion;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package org.palomafp.aadd.smmb.gestores;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public class GestorConfiguracion {
|
||||||
|
|
||||||
|
private final Properties propiedades = new Properties();
|
||||||
|
|
||||||
|
public GestorConfiguracion(String rutaFicheroConfig) throws IOException {
|
||||||
|
|
||||||
|
try (InputStream is = GestorConfiguracion.class.getClassLoader().getResourceAsStream(rutaFicheroConfig)) {
|
||||||
|
|
||||||
|
if (is == null) {
|
||||||
|
throw new IOException("No se ha encontrado el fichero de configuración: " + rutaFicheroConfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
propiedades.load(is);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDriver() {
|
||||||
|
return propiedades.getProperty("sgdb.clase.driver");
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUrl() {
|
||||||
|
return propiedades.getProperty("sgdb.conexion.url");
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBaseDatos() {
|
||||||
|
return propiedades.getProperty("bbdd.nombre");
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsuario() {
|
||||||
|
return propiedades.getProperty("bbdd.usuario");
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getClave() {
|
||||||
|
return propiedades.getProperty("bbdd.clave");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package org.palomafp.aadd.smmb.model;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class Continente implements Serializable{
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
private String codigo;
|
||||||
|
private String nombre;
|
||||||
|
|
||||||
|
public Continente() {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String getCodigo() {
|
||||||
|
return codigo;
|
||||||
|
}
|
||||||
|
public void setCodigo(String codigo) {
|
||||||
|
this.codigo = codigo;
|
||||||
|
}
|
||||||
|
public String getNombre() {
|
||||||
|
return nombre;
|
||||||
|
}
|
||||||
|
public void setNombre(String nombre) {
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.nombre + " (" + this.codigo + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package org.palomafp.aadd.smmb.model;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class Pais implements Serializable{
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
private int identificador;
|
||||||
|
private String nombre;
|
||||||
|
private String nombreCapital;
|
||||||
|
private Continente continente;
|
||||||
|
|
||||||
|
public Pais() {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public int getIdentificador() {
|
||||||
|
return identificador;
|
||||||
|
}
|
||||||
|
public void setIdentificador(int identificador) {
|
||||||
|
this.identificador = identificador;
|
||||||
|
}
|
||||||
|
public String getNombre() {
|
||||||
|
return nombre;
|
||||||
|
}
|
||||||
|
public void setNombre(String nombre) {
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
public String getNombreCapital() {
|
||||||
|
return nombreCapital;
|
||||||
|
}
|
||||||
|
public void setNombreCapital(String capital) {
|
||||||
|
this.nombreCapital = capital;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContinente(Continente continente) {
|
||||||
|
this.continente = continente;
|
||||||
|
}
|
||||||
|
public Continente getContinente() {
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.nombre + " (" + this.identificador + ") con capital en " + this.nombreCapital +
|
||||||
|
" pertenece al continente " + this.continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
package org.palomafp.aadd.smmb.service;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.smmb.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.smmb.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.smmb.dao.impl.ContinenteDaoMySql;
|
||||||
|
import org.palomafp.aadd.smmb.dao.impl.PaisDaoMySql;
|
||||||
|
import org.palomafp.aadd.smmb.exception.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.smmb.gestores.GestorConexionesBBDD;
|
||||||
|
import org.palomafp.aadd.smmb.gestores.GestorConfiguracion;
|
||||||
|
import org.palomafp.aadd.smmb.model.Continente;
|
||||||
|
|
||||||
|
public class ContinenteService {
|
||||||
|
|
||||||
|
private final GestorConexionesBBDD gestorConexiones;
|
||||||
|
|
||||||
|
public ContinenteService(GestorConfiguracion configuracion) {
|
||||||
|
this.gestorConexiones = new GestorConexionesBBDD(configuracion);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Continente obtenerContinentePorCodigo(String idContinente) throws MapaMundiException {
|
||||||
|
|
||||||
|
try (Connection conexion = gestorConexiones.obtenerConexion()) {
|
||||||
|
|
||||||
|
IContinenteDAO continenteDao = new ContinenteDaoMySql(conexion);
|
||||||
|
Continente continente = continenteDao.obtenerContinentePorCodigo(idContinente);
|
||||||
|
|
||||||
|
return continente;
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("No se pudo obtener el continente " + idContinente, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Continente> obtenerListaContinentes() throws MapaMundiException {
|
||||||
|
|
||||||
|
try (Connection conexion = gestorConexiones.obtenerConexion()) {
|
||||||
|
|
||||||
|
IContinenteDAO continenteDao = new ContinenteDaoMySql(conexion);
|
||||||
|
List<Continente> lista = continenteDao.obtenerListaContinentes();
|
||||||
|
|
||||||
|
return lista;
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("No se pudieron obtener los continentes", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insertarContinente(Continente continente) throws MapaMundiException {
|
||||||
|
|
||||||
|
try (Connection conexion = gestorConexiones.obtenerConexion()) {
|
||||||
|
|
||||||
|
IContinenteDAO continenteDao = new ContinenteDaoMySql(conexion);
|
||||||
|
continenteDao.insertarContinente(continente);
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("No se pudo insertar el continente " + continente.getNombre(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void borrarContinente(String codigoContinente) throws MapaMundiException {
|
||||||
|
|
||||||
|
try (Connection conexion = gestorConexiones.obtenerConexion()) {
|
||||||
|
|
||||||
|
IContinenteDAO continenteDao = new ContinenteDaoMySql(conexion);
|
||||||
|
IPaisDAO paisDao = new PaisDaoMySql(conexion);
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
// Desactivamos el guardado automático
|
||||||
|
conexion.setAutoCommit(false);
|
||||||
|
|
||||||
|
paisDao.borrarPaisesPorContinente(codigoContinente);
|
||||||
|
continenteDao.borrarContinente(codigoContinente);
|
||||||
|
|
||||||
|
// Guardamos
|
||||||
|
conexion.commit();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
|
||||||
|
// Rompe el borrador. Se deshace lo pendiente
|
||||||
|
conexion.rollback();
|
||||||
|
throw e;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("No se pudo borrar el continente " + codigoContinente, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void actualizarContinente(Continente continente) throws MapaMundiException {
|
||||||
|
|
||||||
|
try (Connection conexion = gestorConexiones.obtenerConexion()) {
|
||||||
|
|
||||||
|
IContinenteDAO continenteDao = new ContinenteDaoMySql(conexion);
|
||||||
|
continenteDao.actualizarContinente(continente);
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("No se pudo actualizar el continente " + continente.getNombre(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
package org.palomafp.aadd.smmb.service;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.smmb.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.smmb.dao.impl.PaisDaoMySql;
|
||||||
|
import org.palomafp.aadd.smmb.exception.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.smmb.gestores.GestorConexionesBBDD;
|
||||||
|
import org.palomafp.aadd.smmb.gestores.GestorConfiguracion;
|
||||||
|
import org.palomafp.aadd.smmb.model.Pais;
|
||||||
|
|
||||||
|
public class PaisService {
|
||||||
|
|
||||||
|
private final GestorConexionesBBDD gestorConexiones;
|
||||||
|
|
||||||
|
public PaisService(GestorConfiguracion configuracion) {
|
||||||
|
this.gestorConexiones = new GestorConexionesBBDD(configuracion);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public List<Pais> obtenerPaises() throws MapaMundiException {
|
||||||
|
|
||||||
|
try (Connection conexion = gestorConexiones.obtenerConexion()) {
|
||||||
|
|
||||||
|
IPaisDAO paisDao = new PaisDaoMySql(conexion);
|
||||||
|
List<Pais> lista = paisDao.obtenerPaises();
|
||||||
|
|
||||||
|
return lista;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("No se pudieron obtener los países", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pais obtenerPaisPorID(int idPais) throws MapaMundiException {
|
||||||
|
|
||||||
|
try (Connection conexion = gestorConexiones.obtenerConexion()) {
|
||||||
|
|
||||||
|
IPaisDAO paisDao = new PaisDaoMySql(conexion);
|
||||||
|
Pais pais = paisDao.obtenerPaisPorID(idPais);
|
||||||
|
|
||||||
|
return pais;
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("No se pudo obtener el país " + idPais, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insertarPais(Pais pais) throws MapaMundiException {
|
||||||
|
|
||||||
|
try (Connection conexion = gestorConexiones.obtenerConexion()) {
|
||||||
|
|
||||||
|
IPaisDAO paisDao = new PaisDaoMySql(conexion);
|
||||||
|
paisDao.insertarPais(pais);
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("No se pudo insertar el país " + pais.getNombre(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void borrarPais(int idPais) throws MapaMundiException {
|
||||||
|
|
||||||
|
try (Connection conexion = gestorConexiones.obtenerConexion()) {
|
||||||
|
|
||||||
|
IPaisDAO paisDao = new PaisDaoMySql(conexion);
|
||||||
|
paisDao.borrarPais(idPais);
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("No se pudo borrar el país " + idPais, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void actualizarPais(Pais pais) throws MapaMundiException {
|
||||||
|
|
||||||
|
try (Connection conexion = gestorConexiones.obtenerConexion()) {
|
||||||
|
|
||||||
|
IPaisDAO paisDao = new PaisDaoMySql(conexion);
|
||||||
|
paisDao.actualizarPais(pais);
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("No se pudo actualizar el país " + pais.getNombre(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user