Se gestionaron los nuevos tipos de Excepciones
This commit is contained in:
@@ -6,35 +6,56 @@ import org.palomafp.aadd.jaec.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.jaec.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.jaec.dao.impl.ContinenteDaoCsv;
|
||||
import org.palomafp.aadd.jaec.dao.impl.PaisDaoText;
|
||||
import java.io.*;
|
||||
import org.palomafp.aadd.jaec.excepciones.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class GestorMapaMundi {
|
||||
|
||||
public static void main(String[] args) {
|
||||
GestorMapaMundi app = new GestorMapaMundi();
|
||||
app.menu(args);
|
||||
try {
|
||||
GestorMapaMundi app = new GestorMapaMundi();
|
||||
app.menu(args);
|
||||
|
||||
System.out.println("----------------------------------------------------");
|
||||
System.out.println("----------------------------------------------------");
|
||||
|
||||
int identificadorPais = 1;
|
||||
Pais pais = app.obtenerPaisPorIdentificador(identificadorPais);
|
||||
System.out.println(pais);
|
||||
System.out.println(textoAImprimir(pais));
|
||||
int identificadorPais = 1;
|
||||
Pais pais = app.obtenerPaisPorIdentificador(identificadorPais);
|
||||
if (pais != null) {
|
||||
System.out.println(pais);
|
||||
System.out.println(textoAImprimir(pais));
|
||||
}
|
||||
|
||||
System.out.println("----------------------------------------------------");
|
||||
System.out.println("----------------------------------------------------");
|
||||
|
||||
String codigoContinente = "04";
|
||||
List<Pais> listaPaises = app.obtenerListaPaisesPorContinente("04");
|
||||
for (Pais p2 : listaPaises) {
|
||||
System.out.println(p2);
|
||||
System.out.println(textoAImprimir(p2));
|
||||
List<Pais> listaPaises = app.obtenerListaPaisesPorContinente("04");
|
||||
for (Pais p2 : listaPaises) {
|
||||
System.out.println(p2);
|
||||
System.out.println(textoAImprimir(p2));
|
||||
}
|
||||
|
||||
} catch (AccesoDatosException e) {
|
||||
System.err.println("[ERROR DE ACCESO A DATOS]: " + e.getMessage());
|
||||
if (e.getCause() != null) {
|
||||
System.err.println(" -> Causa raíz: " + e.getCause().getMessage());
|
||||
}
|
||||
} catch (NegocioException e) {
|
||||
System.err.println("[ERROR DE REGLA DE NEGOCIO]: " + e.getMessage());
|
||||
} catch (TecnicoException e) {
|
||||
System.err.println("[ERROR TÉCNICO]: " + e.getMessage());
|
||||
if (e.getCause() != null) {
|
||||
System.err.println(" -> Causa raíz: " + e.getCause().getMessage());
|
||||
}
|
||||
} catch (MapaMundiException e) {
|
||||
System.err.println("[ERROR GENERAL MAPAMUNDI]: " + e.getMessage());
|
||||
} catch (Exception e) {
|
||||
System.err.println("[ERROR INESPERADO]: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// METODOS PARA LA ENCAPSULACION DEL DAO
|
||||
private Pais obtenerPaisPorIdentificador(int identificador) {
|
||||
private Pais obtenerPaisPorIdentificador(int identificador) throws AccesoDatosException, NegocioException {
|
||||
Pais pais = null;
|
||||
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
@@ -44,7 +65,7 @@ public class GestorMapaMundi {
|
||||
return pais;
|
||||
}
|
||||
|
||||
private List<Pais> obtenerListaPaisesPorContinente(String codContinente) {
|
||||
private List<Pais> obtenerListaPaisesPorContinente(String codContinente) throws AccesoDatosException, NegocioException {
|
||||
List<Pais> listaPaises = null;
|
||||
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
@@ -54,7 +75,7 @@ public class GestorMapaMundi {
|
||||
return listaPaises;
|
||||
}
|
||||
|
||||
private List<Continente> obtenerTodosContinentes() {
|
||||
private List<Continente> obtenerTodosContinentes() throws AccesoDatosException {
|
||||
List<Continente> todosContinentes = null;
|
||||
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
@@ -63,7 +84,7 @@ public class GestorMapaMundi {
|
||||
return todosContinentes;
|
||||
}
|
||||
|
||||
private Continente obtenerContinentePorIdentificador(String codContinente) {
|
||||
private Continente obtenerContinentePorIdentificador(String codContinente) throws AccesoDatosException {
|
||||
Continente continente = null;
|
||||
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
@@ -72,17 +93,17 @@ public class GestorMapaMundi {
|
||||
return continente;
|
||||
}
|
||||
|
||||
private void insertarContinente(String codContinente, String nombreNuevoContinente) {
|
||||
private void insertarContinente(String codContinente, String nombreNuevoContinente) throws AccesoDatosException {
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
continenteDAO.insertarContinente(codContinente, nombreNuevoContinente);
|
||||
}
|
||||
|
||||
private void eliminarContinente(String codContinente) {
|
||||
private void eliminarContinente(String codContinente) throws AccesoDatosException {
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
continenteDAO.borrarContinente(codContinente);
|
||||
}
|
||||
|
||||
private void actualizarContinente(String codContinente, String nuevoNombre) {
|
||||
private void actualizarContinente(String codContinente, String nuevoNombre) throws AccesoDatosException {
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
continenteDAO.actualizarContinente(codContinente, nuevoNombre);
|
||||
}
|
||||
@@ -108,82 +129,86 @@ public class GestorMapaMundi {
|
||||
// El primer argumento es args[0] y indicará que opción del menu se ejecuta
|
||||
String opcion = args[0];
|
||||
|
||||
switch (opcion) {
|
||||
case "1":
|
||||
List<Continente> lista = obtenerTodosContinentes();
|
||||
if (lista != null && !lista.isEmpty()) {
|
||||
for (Continente c : lista) {
|
||||
System.out.println(c);
|
||||
}
|
||||
} else {
|
||||
System.out.println("La lista de Continentes esta vacia o no existe");
|
||||
}
|
||||
break;
|
||||
|
||||
case "2":
|
||||
if (args.length >= 2) {
|
||||
String cod = args[1];
|
||||
Continente c = obtenerContinentePorIdentificador(cod);
|
||||
if (c != null) {
|
||||
System.out.println(c);
|
||||
try {
|
||||
switch (opcion) {
|
||||
case "1":
|
||||
List<Continente> lista = obtenerTodosContinentes();
|
||||
if (lista != null && !lista.isEmpty()) {
|
||||
for (Continente c : lista) {
|
||||
System.out.println(c);
|
||||
}
|
||||
} else {
|
||||
System.out.println("No se ha encontrado al continente con código: " + cod);
|
||||
System.out.println("La lista de Continentes esta vacia o no existe");
|
||||
}
|
||||
} else {
|
||||
System.out.println("Faltan parámetros.");
|
||||
}
|
||||
break;
|
||||
break;
|
||||
|
||||
case "3":
|
||||
if (args.length >= 3) {
|
||||
String cod = args[1];
|
||||
String nombre = args[2];
|
||||
Continente c = obtenerContinentePorIdentificador(cod);
|
||||
if (c == null) {
|
||||
insertarContinente(cod, nombre);
|
||||
System.out.println("Continente insertado correctamente.");
|
||||
case "2":
|
||||
if (args.length >= 2) {
|
||||
String cod = args[1];
|
||||
Continente c = obtenerContinentePorIdentificador(cod);
|
||||
if (c != null) {
|
||||
System.out.println(c);
|
||||
} else {
|
||||
System.out.println("No se ha encontrado al continente con código: " + cod);
|
||||
}
|
||||
} else {
|
||||
System.out.println("Error: No se pudo insertar.");
|
||||
System.out.println("Faltan parámetros.");
|
||||
}
|
||||
} else {
|
||||
System.out.println("Faltan parámetros.");
|
||||
}
|
||||
break;
|
||||
break;
|
||||
|
||||
case "4":
|
||||
if (args.length >= 2) {
|
||||
String cod = args[1];
|
||||
Continente c = obtenerContinentePorIdentificador(cod);
|
||||
if (c != null) {
|
||||
eliminarContinente(cod);
|
||||
System.out.println("Continente eliminado.");
|
||||
case "3":
|
||||
if (args.length >= 3) {
|
||||
String cod = args[1];
|
||||
String nombre = args[2];
|
||||
Continente c = obtenerContinentePorIdentificador(cod);
|
||||
if (c == null) {
|
||||
insertarContinente(cod, nombre);
|
||||
System.out.println("Continente insertado correctamente.");
|
||||
} else {
|
||||
System.out.println("Error: No se pudo insertar.");
|
||||
}
|
||||
} else {
|
||||
System.out.println("Error: No se pudo eliminar.");
|
||||
System.out.println("Faltan parámetros.");
|
||||
}
|
||||
} else {
|
||||
System.out.println("Faltan parámetros.");
|
||||
}
|
||||
break;
|
||||
break;
|
||||
|
||||
case "5":
|
||||
if (args.length >= 3) {
|
||||
String cod = args[1];
|
||||
String nombre = args[2];
|
||||
Continente c = obtenerContinentePorIdentificador(cod);
|
||||
if (c != null) {
|
||||
actualizarContinente(cod, nombre);
|
||||
System.out.println("Se actualizó el nombre del continente.");
|
||||
case "4":
|
||||
if (args.length >= 2) {
|
||||
String cod = args[1];
|
||||
Continente c = obtenerContinentePorIdentificador(cod);
|
||||
if (c != null) {
|
||||
eliminarContinente(cod);
|
||||
System.out.println("Continente eliminado.");
|
||||
} else {
|
||||
System.out.println("Error: No se pudo eliminar.");
|
||||
}
|
||||
} else {
|
||||
System.out.println("Error: No se pudo modificar el nombre.");
|
||||
System.out.println("Faltan parámetros.");
|
||||
}
|
||||
} else {
|
||||
System.out.println("Faltan parámetros.");
|
||||
}
|
||||
break;
|
||||
break;
|
||||
|
||||
default:
|
||||
System.out.println("Opción no válida.");
|
||||
break;
|
||||
case "5":
|
||||
if (args.length >= 3) {
|
||||
String cod = args[1];
|
||||
String nombre = args[2];
|
||||
Continente c = obtenerContinentePorIdentificador(cod);
|
||||
if (c != null) {
|
||||
actualizarContinente(cod, nombre);
|
||||
System.out.println("Se actualizó el nombre del continente.");
|
||||
} else {
|
||||
System.out.println("Error: No se pudo modificar el nombre.");
|
||||
}
|
||||
} else {
|
||||
System.out.println("Faltan parámetros.");
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
System.out.println("Opción no válida.");
|
||||
break;
|
||||
}
|
||||
} catch (MapaMundiException e) {
|
||||
System.err.println("Error en la opción del menú: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package org.palomafp.aadd.jaec.config;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
import org.palomafp.aadd.jaec.excepciones.TecnicoException;
|
||||
|
||||
public class Configuracion {
|
||||
|
||||
@@ -19,13 +20,13 @@ public class Configuracion {
|
||||
try (InputStream input = Configuracion.class.getClassLoader().getResourceAsStream(RUTA_FICHERO_CONFIGURACION)) {
|
||||
|
||||
if (input == null) {
|
||||
throw new IOException("No se encuentra " + RUTA_FICHERO_CONFIGURACION + " en el classpath");
|
||||
throw new TecnicoException("No se encuentra " + RUTA_FICHERO_CONFIGURACION + " en el classpath");
|
||||
}
|
||||
|
||||
PROPIEDADES.load(input);
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("No se pudo cargar el fichero de configuración", e);
|
||||
} catch (IOException | TecnicoException e) {
|
||||
throw new RuntimeException(new TecnicoException("No se pudo cargar el fichero de configuración", e));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,18 @@
|
||||
package org.palomafp.aadd.jaec.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.jaec.model.Continente;
|
||||
import org.palomafp.aadd.jaec.excepciones.AccesoDatosException;
|
||||
|
||||
public interface IContinenteDAO {
|
||||
List<Continente> obtenerListaContinentes() throws AccesoDatosException;
|
||||
|
||||
List<Continente> obtenerListaContinentes();
|
||||
Continente obtenerContinentePorCodigo(String codContinente) throws AccesoDatosException;
|
||||
|
||||
Continente obtenerContinentePorCodigo(String codContinente);
|
||||
void insertarContinente(String codContinente, String nombre) throws AccesoDatosException;
|
||||
|
||||
void insertarContinente(String codContinente, String nombre);
|
||||
void borrarContinente(String codContinente) throws AccesoDatosException;
|
||||
|
||||
void borrarContinente(String codContinente);
|
||||
|
||||
void actualizarContinente(String codContinente, String nuevoNombre);
|
||||
void actualizarContinente(String codContinente, String nuevoNombre) throws AccesoDatosException;
|
||||
|
||||
}
|
||||
@@ -1,13 +1,15 @@
|
||||
package org.palomafp.aadd.jaec.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.jaec.model.Continente;
|
||||
import org.palomafp.aadd.jaec.model.Pais;
|
||||
import org.palomafp.aadd.jaec.excepciones.AccesoDatosException;
|
||||
import org.palomafp.aadd.jaec.excepciones.NegocioException;
|
||||
|
||||
public interface IPaisDAO {
|
||||
Pais obtenerPaisPorID(int identificador);
|
||||
List<Pais> obtenerPaisesPorContinente(String codigoContinenteBusqueda)
|
||||
throws AccesoDatosException, NegocioException;
|
||||
|
||||
List<Pais> obtenerPaisesPorContinente(String codContinente);
|
||||
Pais obtenerPaisPorID(int idPaisBusqueda) throws AccesoDatosException, NegocioException;
|
||||
|
||||
List<Pais> leerListaPaises() throws AccesoDatosException, NegocioException;
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import java.util.regex.Pattern;
|
||||
import org.palomafp.aadd.jaec.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.jaec.model.Continente;
|
||||
import org.palomafp.aadd.jaec.config.Configuracion;
|
||||
import org.palomafp.aadd.jaec.excepciones.AccesoDatosException;
|
||||
|
||||
public class ContinenteDaoCsv implements IContinenteDAO {
|
||||
private static final String RUTA_FICHERO = Configuracion.get("continente.ruta");
|
||||
@@ -24,7 +25,7 @@ public class ContinenteDaoCsv implements IContinenteDAO {
|
||||
// Parametro 1: 1
|
||||
// Metodo para obtener una lista List<Continente>
|
||||
@Override
|
||||
public List<Continente> obtenerListaContinentes() {
|
||||
public List<Continente> obtenerListaContinentes() throws AccesoDatosException {
|
||||
List<Continente> listaContinentes = new ArrayList<Continente>();
|
||||
|
||||
Path ficheroContinentes = Path.of(RUTA_FICHERO);
|
||||
@@ -32,7 +33,8 @@ public class ContinenteDaoCsv implements IContinenteDAO {
|
||||
if (!Files.exists(ficheroContinentes)) {
|
||||
System.out.println("Directorio de trabajo: " + System.getProperty("user.dir"));
|
||||
System.out.println("Ruta del CSV buscado: " + ficheroContinentes.toAbsolutePath());
|
||||
return listaContinentes;
|
||||
throw new AccesoDatosException(
|
||||
"El fichero de continentes no existe en: " + ficheroContinentes.toAbsolutePath());
|
||||
}
|
||||
|
||||
String linea = "";
|
||||
@@ -57,7 +59,7 @@ public class ContinenteDaoCsv implements IContinenteDAO {
|
||||
}
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
System.err.println(ioe.getMessage());
|
||||
throw new AccesoDatosException("Error al leer el fichero de continentes: " + ioe.getMessage(), ioe);
|
||||
}
|
||||
|
||||
return listaContinentes;
|
||||
@@ -66,7 +68,7 @@ public class ContinenteDaoCsv implements IContinenteDAO {
|
||||
// Parametro 1: 2
|
||||
// Metodo para obtener un Continente por codigo
|
||||
@Override
|
||||
public Continente obtenerContinentePorCodigo(String codContinente) {
|
||||
public Continente obtenerContinentePorCodigo(String codContinente) throws AccesoDatosException {
|
||||
Continente continenteEncontrado = null;
|
||||
|
||||
Path ficheroContinentes = Path.of(RUTA_FICHERO);
|
||||
@@ -74,7 +76,8 @@ public class ContinenteDaoCsv implements IContinenteDAO {
|
||||
if (!Files.exists(ficheroContinentes)) {
|
||||
System.out.println("Directorio de trabajo: " + System.getProperty("user.dir"));
|
||||
System.out.println("Ruta del CSV buscado: " + ficheroContinentes.toAbsolutePath());
|
||||
return null;
|
||||
throw new AccesoDatosException(
|
||||
"El fichero de continentes no existe en: " + ficheroContinentes.toAbsolutePath());
|
||||
}
|
||||
|
||||
String linea = "";
|
||||
@@ -98,7 +101,7 @@ public class ContinenteDaoCsv implements IContinenteDAO {
|
||||
}
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
System.err.println(ioe);
|
||||
throw new AccesoDatosException("Error al buscar continente por código: " + ioe.getMessage(), ioe);
|
||||
}
|
||||
|
||||
return continenteEncontrado;
|
||||
@@ -107,13 +110,14 @@ public class ContinenteDaoCsv implements IContinenteDAO {
|
||||
// Parametro 1: 3
|
||||
// Metodo para INSERTAR un Continente
|
||||
@Override
|
||||
public void insertarContinente(String codContinente, String nombre) {
|
||||
public void insertarContinente(String codContinente, String nombre) throws AccesoDatosException {
|
||||
Path ficheroContinentes = Path.of(RUTA_FICHERO);
|
||||
|
||||
if (!Files.exists(ficheroContinentes)) {
|
||||
System.out.println("Directorio de trabajo: " + System.getProperty("user.dir"));
|
||||
System.out.println("Ruta del CSV buscado: " + ficheroContinentes.toAbsolutePath());
|
||||
return;
|
||||
throw new AccesoDatosException(
|
||||
"El fichero de continentes no existe en: " + ficheroContinentes.toAbsolutePath());
|
||||
}
|
||||
|
||||
// Inserto el Continente al fichero Csv
|
||||
@@ -122,14 +126,14 @@ public class ContinenteDaoCsv implements IContinenteDAO {
|
||||
bw.write("2026" + SEPARADOR + codContinente + SEPARADOR + nombre);
|
||||
bw.newLine();
|
||||
} catch (IOException ioe) {
|
||||
System.err.println(ioe.getMessage());
|
||||
throw new AccesoDatosException("Error al insertar continente: " + ioe.getMessage(), ioe);
|
||||
}
|
||||
}
|
||||
|
||||
// Parametro 1: 4
|
||||
// Metodo para ELIMINAR un Continente
|
||||
@Override
|
||||
public void borrarContinente(String codContinente) {
|
||||
public void borrarContinente(String codContinente) throws AccesoDatosException {
|
||||
Continente continenteEncontrado = null;
|
||||
|
||||
Path ficheroContinentes = Path.of(RUTA_FICHERO);
|
||||
@@ -138,7 +142,8 @@ public class ContinenteDaoCsv implements IContinenteDAO {
|
||||
if (!Files.exists(ficheroContinentes)) {
|
||||
System.out.println("Directorio de trabajo: " + System.getProperty("user.dir"));
|
||||
System.out.println("Ruta del CSV buscado: " + ficheroContinentes.toAbsolutePath());
|
||||
return;
|
||||
throw new AccesoDatosException(
|
||||
"El fichero de continentes no existe en: " + ficheroContinentes.toAbsolutePath());
|
||||
}
|
||||
|
||||
String linea = "";
|
||||
@@ -168,8 +173,7 @@ public class ContinenteDaoCsv implements IContinenteDAO {
|
||||
}
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
System.err.println(ioe);
|
||||
return;
|
||||
throw new AccesoDatosException("Error al procesar el borrado del continente: " + ioe.getMessage(), ioe);
|
||||
}
|
||||
|
||||
// Por defecto continenteEncontrado es null, pero
|
||||
@@ -178,14 +182,16 @@ public class ContinenteDaoCsv implements IContinenteDAO {
|
||||
try {
|
||||
Files.move(ficheroTemporal, ficheroContinentes, StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (IOException ioe) {
|
||||
System.err.println(ioe);
|
||||
throw new AccesoDatosException("Error al reemplazar fichero temporal tras borrado: " + ioe.getMessage(),
|
||||
ioe);
|
||||
}
|
||||
} else {
|
||||
// Si no se encuentra el continente, eliminamos el fichero temporal no utilizado
|
||||
try {
|
||||
Files.deleteIfExists(ficheroTemporal);
|
||||
} catch (IOException ioe) {
|
||||
System.err.println(ioe);
|
||||
throw new AccesoDatosException("Error al eliminar fichero temporal no utilizado: " + ioe.getMessage(),
|
||||
ioe);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -193,7 +199,7 @@ public class ContinenteDaoCsv implements IContinenteDAO {
|
||||
// Parametro 1: 5
|
||||
// Metodo para cambiar el nombre de un continente
|
||||
@Override
|
||||
public void actualizarContinente(String codContinente, String nuevoNombre) {
|
||||
public void actualizarContinente(String codContinente, String nuevoNombre) throws AccesoDatosException {
|
||||
Continente continenteEncontrado = null;
|
||||
|
||||
Path ficheroContinentes = Path.of(RUTA_FICHERO);
|
||||
@@ -202,7 +208,8 @@ public class ContinenteDaoCsv implements IContinenteDAO {
|
||||
if (!Files.exists(ficheroContinentes)) {
|
||||
System.out.println("Directorio de trabajo: " + System.getProperty("user.dir"));
|
||||
System.out.println("Ruta del CSV buscado: " + ficheroContinentes.toAbsolutePath());
|
||||
return;
|
||||
throw new AccesoDatosException(
|
||||
"El fichero de continentes no existe en: " + ficheroContinentes.toAbsolutePath());
|
||||
}
|
||||
|
||||
String linea = "";
|
||||
@@ -239,8 +246,8 @@ public class ContinenteDaoCsv implements IContinenteDAO {
|
||||
}
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
System.err.println(ioe);
|
||||
return;
|
||||
throw new AccesoDatosException("Error al procesar la actualización del continente: " + ioe.getMessage(),
|
||||
ioe);
|
||||
}
|
||||
|
||||
// Por defecto continenteEncontrado es null, pero
|
||||
@@ -249,14 +256,16 @@ public class ContinenteDaoCsv implements IContinenteDAO {
|
||||
try {
|
||||
Files.move(ficheroTemporal, ficheroContinentes, StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (IOException ioe) {
|
||||
System.err.println(ioe);
|
||||
throw new AccesoDatosException(
|
||||
"Error al reemplazar fichero temporal tras actualización: " + ioe.getMessage(), ioe);
|
||||
}
|
||||
} else {
|
||||
// Si no se encuentra el continente, eliminamos el fichero temporal no utilizado
|
||||
try {
|
||||
Files.deleteIfExists(ficheroTemporal);
|
||||
} catch (IOException ioe) {
|
||||
System.err.println(ioe);
|
||||
throw new AccesoDatosException("Error al eliminar fichero temporal no utilizado: " + ioe.getMessage(),
|
||||
ioe);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
package org.palomafp.aadd.jaec.dao.impl;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
@@ -8,14 +7,15 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.palomafp.aadd.jaec.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.jaec.model.Continente;
|
||||
import org.palomafp.aadd.jaec.model.Pais;
|
||||
import org.palomafp.aadd.jaec.dao.IContinenteDAO;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
import org.palomafp.aadd.jaec.config.Configuracion;
|
||||
import org.palomafp.aadd.jaec.excepciones.AccesoDatosException;
|
||||
import org.palomafp.aadd.jaec.excepciones.NegocioException;
|
||||
|
||||
public class PaisDaoText implements IPaisDAO {
|
||||
private static final String RUTA_FICHERO = Configuracion.get("pais.ruta");
|
||||
@@ -29,7 +29,8 @@ public class PaisDaoText implements IPaisDAO {
|
||||
|
||||
// Metodo para obtener un List<Pais> con objetos Pais por codigo de Continente
|
||||
@Override
|
||||
public List<Pais> obtenerPaisesPorContinente(String codigoContinenteBusqueda) {
|
||||
public List<Pais> obtenerPaisesPorContinente(String codigoContinenteBusqueda)
|
||||
throws AccesoDatosException, NegocioException {
|
||||
Path fichero = Path.of(RUTA_FICHERO);
|
||||
List<Pais> paises = new ArrayList<Pais>();
|
||||
|
||||
@@ -51,6 +52,12 @@ public class PaisDaoText implements IPaisDAO {
|
||||
if (codigoContinenteBusqueda.compareTo(codContinente) == 0) {
|
||||
Continente continente = continenteDAO.obtenerContinentePorCodigo(codContinente);
|
||||
|
||||
// Pregunta 5: Si el continente devuelto es null, se viola la regla de negocio
|
||||
if (continente == null) {
|
||||
throw new NegocioException("Error de negocio: El país '" + nombrePais
|
||||
+ "' referencia a un continente inexistente (" + codContinente + ").");
|
||||
}
|
||||
|
||||
Pais pais = new Pais();
|
||||
pais.setIdentificador(idPais);
|
||||
pais.setNombre(nombrePais);
|
||||
@@ -63,7 +70,7 @@ public class PaisDaoText implements IPaisDAO {
|
||||
}
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
System.out.println(ioe.getMessage());
|
||||
throw new AccesoDatosException("Error al leer países por continente: " + ioe.getMessage(), ioe);
|
||||
}
|
||||
|
||||
// En caso no se encuentre nada se devuelve la lista paises vacio
|
||||
@@ -72,7 +79,7 @@ public class PaisDaoText implements IPaisDAO {
|
||||
|
||||
// Metodo para obtener un Pais por ID
|
||||
@Override
|
||||
public Pais obtenerPaisPorID(int idPaisBusqueda) {
|
||||
public Pais obtenerPaisPorID(int idPaisBusqueda) throws AccesoDatosException, NegocioException {
|
||||
// Leo la lista de TODOS los paises
|
||||
List<Pais> listaPaises = leerListaPaises();
|
||||
|
||||
@@ -84,12 +91,12 @@ public class PaisDaoText implements IPaisDAO {
|
||||
}
|
||||
// Si no encuentro nada devuelvo nulo
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
// Metodo para leer una lista de paises a partir de un fichero
|
||||
// Devuelve un List<Pais> con varios objetos <Pais>
|
||||
public List<Pais> leerListaPaises() {
|
||||
@Override
|
||||
public List<Pais> leerListaPaises() throws AccesoDatosException, NegocioException {
|
||||
Path fichero = Path.of(RUTA_FICHERO);
|
||||
List<Pais> paises = new ArrayList<Pais>();
|
||||
|
||||
@@ -110,6 +117,12 @@ public class PaisDaoText implements IPaisDAO {
|
||||
|
||||
Continente continente = continenteDAO.obtenerContinentePorCodigo(codContinente);
|
||||
|
||||
// Pregunta 5: Si el continente devuelto es null, se viola la regla de negocio
|
||||
if (continente == null) {
|
||||
throw new NegocioException("Error de negocio: El país '" + nombrePais
|
||||
+ "' referencia a un continente inexistente (" + codContinente + ").");
|
||||
}
|
||||
|
||||
Pais pais = new Pais();
|
||||
pais.setIdentificador(idPais);
|
||||
pais.setNombre(nombrePais);
|
||||
@@ -121,11 +134,10 @@ public class PaisDaoText implements IPaisDAO {
|
||||
}
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
System.out.println(ioe.getMessage());
|
||||
throw new AccesoDatosException("Error al leer la lista general de países: " + ioe.getMessage(), ioe);
|
||||
}
|
||||
|
||||
// En caso no se encuentre nada se devuelve la lista paises vacio
|
||||
return paises;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user