Actividad 1.10 Incluir Excepción de proyecto en Mapamundi
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
# Ignorar la carpeta de compilados
|
||||||
|
bin/
|
||||||
|
*.class
|
||||||
|
|
||||||
|
# Ignorar archivos de configuración de Eclipse
|
||||||
|
.settings/
|
||||||
|
.project
|
||||||
|
.classpath
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
# config/application.properties
|
||||||
|
ruta.fichero.continente=C:\\Users\\lauuj\\aadd\\fic10\\informacion_continentes.csv
|
||||||
|
separador.csv.continente=#
|
||||||
|
ruta.fichero.pais=C:\\Users\\lauuj\\aadd\\fic10\\informacion_paises.txt
|
||||||
|
separador.txt.pais=,
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
module Ficheros_03 {
|
||||||
|
}
|
||||||
@@ -0,0 +1,167 @@
|
|||||||
|
package org.palomafp.aadd.ljl;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ljl.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.ljl.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.ljl.dao.impl.ContinenteDaoFichero;
|
||||||
|
import org.palomafp.aadd.ljl.dao.impl.PaisDaoFichero;
|
||||||
|
import org.palomafp.aadd.ljl.excepciones.AccesoDatosException;
|
||||||
|
import org.palomafp.aadd.ljl.excepciones.NegocioException;
|
||||||
|
import org.palomafp.aadd.ljl.excepciones.TecnicoException;
|
||||||
|
import org.palomafp.aadd.ljl.model.Continente;
|
||||||
|
import org.palomafp.aadd.ljl.model.Pais;
|
||||||
|
|
||||||
|
public class GestorMapamundi {
|
||||||
|
|
||||||
|
private final IContinenteDAO continenteDao;
|
||||||
|
private final IPaisDAO paisDao;
|
||||||
|
|
||||||
|
public GestorMapamundi() throws TecnicoException {
|
||||||
|
this.continenteDao = new ContinenteDaoFichero();
|
||||||
|
this.paisDao = new PaisDaoFichero(continenteDao);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
// lo que nos devuelve el array de los argumentos son Strings, asi que lo parseamos
|
||||||
|
int opcion = Integer.parseInt(args[0]);
|
||||||
|
|
||||||
|
GestorMapamundi gestor = new GestorMapamundi();
|
||||||
|
|
||||||
|
Continente continente = null;
|
||||||
|
|
||||||
|
switch(opcion) {
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
// Obtenemos una Lista de continentes
|
||||||
|
List<Continente> continentes = gestor.obtenerListaDeContinentes();
|
||||||
|
for (Continente cont : continentes) {
|
||||||
|
System.out.println(cont);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
// Obtenemos un Continente por su código
|
||||||
|
continente = gestor.obtenerContinenteDeCodigo(args[1]);
|
||||||
|
System.out.println("El continente que buscas es: " + continente);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
// Insertaremos un continente
|
||||||
|
continente = new Continente(args[1], args[2]);
|
||||||
|
gestor.insertarContinente(continente);
|
||||||
|
System.out.println("Continente insertado correctamente");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 4:
|
||||||
|
// Eliminaremos un continente
|
||||||
|
gestor.borrarContinente(args[1]);
|
||||||
|
System.out.println("Continente eliminado correctamente");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 5:
|
||||||
|
// Actualizaremos un continente
|
||||||
|
continente = new Continente(args[1], args[2]);
|
||||||
|
gestor.actualizarContinente(continente);
|
||||||
|
System.out.println("Continente actualizado correctamente");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 6:
|
||||||
|
// Obtenemos la lista de países de un continente
|
||||||
|
List<Pais> paises = gestor.obtenerPaisesDeContinente(args[1]);
|
||||||
|
if (paises.size() == 0) {
|
||||||
|
System.out.println("Ese continente no tiene paises asociados");
|
||||||
|
} else {
|
||||||
|
for (Pais pais : paises) {
|
||||||
|
System.out.println(pais);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 7:
|
||||||
|
// Obtenemos un pais por su id
|
||||||
|
int id = Integer.parseInt(args[1]);
|
||||||
|
Pais pais = gestor.obtenerPaisDeId(id);
|
||||||
|
System.out.println("El pais que buscas es: " + pais);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
System.out.println("Opción no esperada");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hay que ir en orden de especificidad, la más general debe ir al final
|
||||||
|
} catch (AccesoDatosException e) {
|
||||||
|
System.err.println("Error de acceso a datos: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (TecnicoException e) {
|
||||||
|
System.err.println("Error técnico: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (NegocioException e) {
|
||||||
|
System.err.println("Error de negocio: " + e.getMessage());
|
||||||
|
|
||||||
|
|
||||||
|
// son para cuando se introduzca una opcion no paseable a un numero | cuando no haya argumentos suficientes
|
||||||
|
// Estas dos son unchecked (heredan de RuntimeException), así que Java no te obliga a capturarlas ni a declararlas con throws
|
||||||
|
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
|
||||||
|
System.err.println("Argumentos no válidos: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private Pais obtenerPaisDeId(int num) throws AccesoDatosException, NegocioException {
|
||||||
|
|
||||||
|
Pais pais = paisDao.obtenerPaisPorId(num);
|
||||||
|
|
||||||
|
// puede devolver un null si no lo encuentra
|
||||||
|
if (pais == null) {
|
||||||
|
throw new NegocioException("El País con id " + num + " no existe");
|
||||||
|
}
|
||||||
|
|
||||||
|
return pais;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Pais> obtenerPaisesDeContinente(String codigoContinente) throws AccesoDatosException, NegocioException {
|
||||||
|
|
||||||
|
return paisDao.obtenerPaisesPorContinente(codigoContinente);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void actualizarContinente(Continente continente) throws AccesoDatosException, NegocioException {
|
||||||
|
|
||||||
|
obtenerContinenteDeCodigo(continente.getCodigo());
|
||||||
|
// aqui ya nunca me devolverá nulo porque obtenerContinenteDeCodigo ya contempla los nulos
|
||||||
|
continenteDao.actualizarContinente(continente);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void borrarContinente(String codContinente) throws AccesoDatosException, NegocioException {
|
||||||
|
|
||||||
|
obtenerContinenteDeCodigo(codContinente);
|
||||||
|
// aqui ya nunca me devolverá nulo porque obtenerContinenteDeCodigo ya contempla los nulos
|
||||||
|
continenteDao.eliminarContinente(codContinente);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void insertarContinente(Continente continente) throws AccesoDatosException, NegocioException {
|
||||||
|
|
||||||
|
// aqui no podemos usar obtenerContinenteDeCodigo porque si lanza una excepcion cuando no existe el continente
|
||||||
|
// como quiero insertar necesito que no este ese continente sin que se lance la excepcion
|
||||||
|
Continente continenteFichero = continenteDao.obtenerContinentePorCod(continente.getCodigo());
|
||||||
|
if (continenteFichero == null) {
|
||||||
|
continenteDao.insertarContinenteNuevo(continente);
|
||||||
|
} else {
|
||||||
|
throw new NegocioException("El codigo de continente " + continente.getCodigo() + " ya existe");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Continente obtenerContinenteDeCodigo(String codigoContinente) throws AccesoDatosException, NegocioException {
|
||||||
|
Continente continente = continenteDao.obtenerContinentePorCod(codigoContinente);
|
||||||
|
if (continente == null) {
|
||||||
|
throw new NegocioException("El codigo del continente " + codigoContinente + " no existe");
|
||||||
|
}
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Continente> obtenerListaDeContinentes() throws AccesoDatosException {
|
||||||
|
List<Continente> listaContinentes = continenteDao.obtenerListaContinentes();
|
||||||
|
return listaContinentes;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package org.palomafp.aadd.ljl.config;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ljl.excepciones.TecnicoException;
|
||||||
|
|
||||||
|
public class ConfigManager {
|
||||||
|
|
||||||
|
private static final String RUTA_FICHERO_CONFIGURACION = "application.properties";
|
||||||
|
private static final Properties PROPIEDADES_CONFIGURACION = new Properties();
|
||||||
|
private static boolean cargado = false;
|
||||||
|
|
||||||
|
private ConfigManager() {}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Un bloque static no puede lanzar excepciones checked, así que la carga pasa a ser perezosa (al pedir la primera propiedad).
|
||||||
|
Los getters declaran throws TecnicoException y llaman a cargarPropiedad (si ya se ha hecho antes no se vuelve a hacer -> boolean)
|
||||||
|
*/
|
||||||
|
private static void cargarPropiedades() throws TecnicoException {
|
||||||
|
if (!cargado) {
|
||||||
|
try (InputStream is = ConfigManager.class.getClassLoader().getResourceAsStream(RUTA_FICHERO_CONFIGURACION)) {
|
||||||
|
|
||||||
|
if (is == null) {
|
||||||
|
throw new TecnicoException("No se ha encontrado el fichero de configuración: " + RUTA_FICHERO_CONFIGURACION);
|
||||||
|
}
|
||||||
|
|
||||||
|
PROPIEDADES_CONFIGURACION.load(is);
|
||||||
|
cargado = true;
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new TecnicoException("No se ha podido cargar el fichero de configuración", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// estos métodos -> .getProperty() -> sirve para extraer la información del fichero
|
||||||
|
public static String getRutaFicheroContinente() throws TecnicoException {
|
||||||
|
cargarPropiedades();
|
||||||
|
return PROPIEDADES_CONFIGURACION.getProperty("ruta.fichero.continente", "C:\\Users\\lauuj\\aadd\\fic10\\informacion_continentes.csv");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getSeparadorCsv() throws TecnicoException {
|
||||||
|
cargarPropiedades();
|
||||||
|
return PROPIEDADES_CONFIGURACION.getProperty("separador.csv.continente", "#");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getRutaFicheroPais() throws TecnicoException {
|
||||||
|
cargarPropiedades();
|
||||||
|
return PROPIEDADES_CONFIGURACION.getProperty("ruta.fichero.pais", "C:\\Users\\lauuj\\aadd\\fic10\\informacion_paises.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getSeparadorTxt() throws TecnicoException {
|
||||||
|
cargarPropiedades();
|
||||||
|
return PROPIEDADES_CONFIGURACION.getProperty("separador.txt.pais", ",");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package org.palomafp.aadd.ljl.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ljl.excepciones.AccesoDatosException;
|
||||||
|
import org.palomafp.aadd.ljl.model.Continente;
|
||||||
|
|
||||||
|
public interface IContinenteDAO {
|
||||||
|
|
||||||
|
public List<Continente> obtenerListaContinentes() throws AccesoDatosException;
|
||||||
|
|
||||||
|
public Continente obtenerContinentePorCod(String codContinente) throws AccesoDatosException;
|
||||||
|
|
||||||
|
public void insertarContinenteNuevo(Continente continente) throws AccesoDatosException;
|
||||||
|
|
||||||
|
public void eliminarContinente(String codContinente) throws AccesoDatosException;
|
||||||
|
|
||||||
|
public void actualizarContinente(Continente continente) throws AccesoDatosException;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package org.palomafp.aadd.ljl.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ljl.excepciones.AccesoDatosException;
|
||||||
|
import org.palomafp.aadd.ljl.excepciones.NegocioException;
|
||||||
|
import org.palomafp.aadd.ljl.model.Pais;
|
||||||
|
|
||||||
|
public interface IPaisDAO {
|
||||||
|
|
||||||
|
public Pais obtenerPaisPorId(int idPais) throws AccesoDatosException, NegocioException;
|
||||||
|
|
||||||
|
public List<Pais> obtenerPaisesPorContinente(String codContinente) throws AccesoDatosException, NegocioException;
|
||||||
|
}
|
||||||
@@ -0,0 +1,216 @@
|
|||||||
|
package org.palomafp.aadd.ljl.dao.impl;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ljl.config.ConfigManager;
|
||||||
|
import org.palomafp.aadd.ljl.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.ljl.excepciones.AccesoDatosException;
|
||||||
|
import org.palomafp.aadd.ljl.excepciones.TecnicoException;
|
||||||
|
import org.palomafp.aadd.ljl.model.Continente;
|
||||||
|
|
||||||
|
public class ContinenteDaoFichero implements IContinenteDAO {
|
||||||
|
|
||||||
|
// les quitamos static (ahora son de instancia)
|
||||||
|
// porque ahora leer la configuración puede fallar y eso solo se puede gestionar desde un constructor
|
||||||
|
private final String rutaFichero;
|
||||||
|
private final String separador;
|
||||||
|
|
||||||
|
public ContinenteDaoFichero() throws TecnicoException {
|
||||||
|
this.rutaFichero = ConfigManager.getRutaFicheroContinente();
|
||||||
|
this.separador = ConfigManager.getSeparadorCsv();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Continente> obtenerListaContinentes() throws AccesoDatosException {
|
||||||
|
|
||||||
|
List<Continente> continentes = new ArrayList<>();
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(rutaFichero))) {
|
||||||
|
|
||||||
|
String linea;
|
||||||
|
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
if (validarLinea(linea)) {
|
||||||
|
|
||||||
|
String[] partes = dividirLinea(linea);
|
||||||
|
continentes.add(crearContinente(partes));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AccesoDatosException("Error al leer la lista de continentes", e);
|
||||||
|
}
|
||||||
|
return continentes;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Continente obtenerContinentePorCod(String codContinente) throws AccesoDatosException {
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(rutaFichero))) {
|
||||||
|
String linea;
|
||||||
|
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
if (validarLinea(linea)) {
|
||||||
|
|
||||||
|
String[] partes = dividirLinea(linea);
|
||||||
|
|
||||||
|
if (partes[1].equals(codContinente)) {
|
||||||
|
return crearContinente(partes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AccesoDatosException("Error al buscar el continente " + codContinente, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void insertarContinenteNuevo(Continente continente) throws AccesoDatosException {
|
||||||
|
|
||||||
|
try (BufferedWriter bw = new BufferedWriter(new FileWriter(rutaFichero, true))) {
|
||||||
|
// bajar el cursor (que no se apelotonen las lineas)
|
||||||
|
bw.newLine();
|
||||||
|
bw.write(formatearLinea(continente));
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AccesoDatosException("Error al insertar el continente " + continente.getCodigo(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void eliminarContinente(String codContinente) throws AccesoDatosException {
|
||||||
|
|
||||||
|
List<String> lineasAConservar = new ArrayList<>();
|
||||||
|
|
||||||
|
// 1. Leemos el fichero y guardamos todo EXCEPTO el continente a borrar
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(rutaFichero))) {
|
||||||
|
String linea;
|
||||||
|
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
|
||||||
|
// Conservamos las lineas que no empiecen por un numero y líneas vacías (las lineas no validas)
|
||||||
|
if (!validarLinea(linea)) {
|
||||||
|
lineasAConservar.add(linea);
|
||||||
|
// para que no se intente dividir en partes una linea no valida
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Conservamos todo lo que no coincida con el cod del continente que se quiere eliminar
|
||||||
|
String[] partes = dividirLinea(linea);
|
||||||
|
|
||||||
|
if(!partes[1].equals(codContinente)) {
|
||||||
|
lineasAConservar.add(linea);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AccesoDatosException("Error al leer el fichero para eliminar el continente " + codContinente, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Sobrescribimos el fichero con las líneas conservadas
|
||||||
|
try (BufferedWriter bw = new BufferedWriter(new FileWriter(rutaFichero, false))) {
|
||||||
|
|
||||||
|
for (int i = 0; i < lineasAConservar.size(); i++) {
|
||||||
|
bw.write(lineasAConservar.get(i));
|
||||||
|
|
||||||
|
// Salto de línea entre líneas para que no se apelotonen, y en la ultima linea ya no se salta (-1)
|
||||||
|
if (i < lineasAConservar.size() - 1) {
|
||||||
|
bw.newLine();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AccesoDatosException("Error al escribir el fichero para eliminar el continente " + codContinente, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actualizarContinente(Continente continente) throws AccesoDatosException {
|
||||||
|
|
||||||
|
List<String> lineasActualizadas = new ArrayList<>();
|
||||||
|
|
||||||
|
// 1. Leemos y sustituimos solo la línea que coincida
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(rutaFichero))) {
|
||||||
|
String linea;
|
||||||
|
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
|
||||||
|
if (validarLinea(linea)) {
|
||||||
|
|
||||||
|
String[] partes = dividirLinea(linea);
|
||||||
|
|
||||||
|
if (partes[1].equals(continente.getCodigo())) {
|
||||||
|
lineasActualizadas.add(formatearLinea(continente));
|
||||||
|
} else {
|
||||||
|
lineasActualizadas.add(linea);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Cabecera o línea vacía: se queda como estaba
|
||||||
|
lineasActualizadas.add(linea);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AccesoDatosException("Error al leer el fichero para actualizar el continente " + continente.getCodigo(), e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Sobrescribimos el fichero
|
||||||
|
try (BufferedWriter bw = new BufferedWriter(new FileWriter(rutaFichero, false))) {
|
||||||
|
|
||||||
|
for (int i = 0; i < lineasActualizadas.size(); i++) {
|
||||||
|
bw.write(lineasActualizadas.get(i));
|
||||||
|
|
||||||
|
if (i < lineasActualizadas.size() - 1) {
|
||||||
|
bw.newLine();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AccesoDatosException("Error al escribir el fichero para actualizar el continente " + continente.getCodigo(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MÉTODOS AUXILIARES
|
||||||
|
|
||||||
|
// Solo son datos las líneas que empiezan por el año (un dígito); descarta vacías y cabecera
|
||||||
|
private boolean validarLinea(String linea) {
|
||||||
|
String limpia = linea.trim();
|
||||||
|
return !limpia.isEmpty() && Character.isDigit(limpia.charAt(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String[] dividirLinea(String linea) throws AccesoDatosException {
|
||||||
|
String[] partes = linea.split(separador);
|
||||||
|
|
||||||
|
if (partes.length < 3) {
|
||||||
|
// antes era IllegalArgumentException; ahora es un problema de datos del fichero
|
||||||
|
throw new AccesoDatosException("Línea de continente no válida: " + linea);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < partes.length; i++) {
|
||||||
|
partes[i] = partes[i].trim();
|
||||||
|
}
|
||||||
|
return partes;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Continente crearContinente(String[] partes) {
|
||||||
|
Continente continente = new Continente();
|
||||||
|
|
||||||
|
continente.setCodigo(partes[1]);
|
||||||
|
continente.setNombre(partes[2]);
|
||||||
|
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String formatearLinea(Continente continente) {
|
||||||
|
String anioActual = String.valueOf(LocalDate.now().getYear());
|
||||||
|
return anioActual + separador + continente.getCodigo() + separador + continente.getNombre();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,140 @@
|
|||||||
|
package org.palomafp.aadd.ljl.dao.impl;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ljl.config.ConfigManager;
|
||||||
|
import org.palomafp.aadd.ljl.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.ljl.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.ljl.excepciones.AccesoDatosException;
|
||||||
|
import org.palomafp.aadd.ljl.excepciones.NegocioException;
|
||||||
|
import org.palomafp.aadd.ljl.excepciones.TecnicoException;
|
||||||
|
import org.palomafp.aadd.ljl.model.Continente;
|
||||||
|
import org.palomafp.aadd.ljl.model.Pais;
|
||||||
|
|
||||||
|
public class PaisDaoFichero implements IPaisDAO {
|
||||||
|
|
||||||
|
private final String rutaFichero;
|
||||||
|
private final String separador;
|
||||||
|
|
||||||
|
// No puede ser static porque no es un atributo de clase (será de la instancia del continenteDao que nos pasen)
|
||||||
|
// nota: continenteDao no es un Continente en concreto, es una manera de acceder a los datos de los continentes (métodos)
|
||||||
|
// y necesitamos poder acceder, ya que un Pais tiene un Continente como atributo (no solo el codContinente que hay en el fichero)
|
||||||
|
// continenteDao es "final" -> significa esta variable se asigna una sola vez (en el constructor)
|
||||||
|
private final IContinenteDAO continenteDao;
|
||||||
|
|
||||||
|
// Constructor: aquí "entra" la dependencia
|
||||||
|
public PaisDaoFichero(IContinenteDAO continenteDao) throws TecnicoException {
|
||||||
|
this.continenteDao = continenteDao;
|
||||||
|
this.rutaFichero = ConfigManager.getRutaFicheroPais();
|
||||||
|
this.separador = ConfigManager.getSeparadorTxt();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Pais obtenerPaisPorId(int idPais) throws AccesoDatosException, NegocioException {
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(rutaFichero))) {
|
||||||
|
String linea;
|
||||||
|
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
if (validarLinea(linea)) {
|
||||||
|
|
||||||
|
String[] partes = dividirLinea(linea);
|
||||||
|
|
||||||
|
if (Integer.parseInt(partes[1]) == idPais) {
|
||||||
|
|
||||||
|
Continente cont = obtenerContinente(partes[0]);
|
||||||
|
|
||||||
|
return crearPais(partes, cont);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AccesoDatosException("Error al buscar el país con id " + idPais, e);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
throw new AccesoDatosException("Identificador de país no numérico", e);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Pais> obtenerPaisesPorContinente(String codContinente) throws AccesoDatosException, NegocioException {
|
||||||
|
|
||||||
|
List<Pais> paises = new ArrayList<>();
|
||||||
|
|
||||||
|
// si el continente es nulo se trata en obtenerContinente -> no nos dará la lista de Paises porque no existe el continente
|
||||||
|
// nuestro negocio no dejara que se vea ["09",200, "Atlantis", "Poseidonia"]
|
||||||
|
Continente continente = obtenerContinente(codContinente);
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(rutaFichero))) {
|
||||||
|
String linea;
|
||||||
|
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
if (validarLinea(linea)) {
|
||||||
|
|
||||||
|
String[] partes = dividirLinea(linea);
|
||||||
|
|
||||||
|
if (partes[0].equals(codContinente)) {
|
||||||
|
paises.add(crearPais(partes, continente));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AccesoDatosException("Error al leer los países del continente " + codContinente, e);
|
||||||
|
}catch (NumberFormatException e) {
|
||||||
|
throw new AccesoDatosException("Identificador de país no numérico", e);
|
||||||
|
}
|
||||||
|
return paises;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// MÉTODOS AUXILIARES (para no repetir código)
|
||||||
|
|
||||||
|
private boolean validarLinea(String linea) {
|
||||||
|
String limpia = linea.trim();
|
||||||
|
return !limpia.isEmpty() && !limpia.startsWith("#") && !limpia.startsWith("--");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private String[] dividirLinea(String linea) throws AccesoDatosException {
|
||||||
|
|
||||||
|
String[] partes = linea.replace("\"", "").split(separador);
|
||||||
|
|
||||||
|
if (partes.length < 4) {
|
||||||
|
throw new AccesoDatosException("Línea de país no válida: " + linea);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < partes.length; i++) {
|
||||||
|
partes[i] = partes[i].trim();
|
||||||
|
}
|
||||||
|
return partes;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Continente obtenerContinente(String codContinente) throws AccesoDatosException, NegocioException {
|
||||||
|
|
||||||
|
Continente continente = continenteDao.obtenerContinentePorCod(codContinente);
|
||||||
|
|
||||||
|
if (continente == null) {
|
||||||
|
// si el continente no existe no es un error en el acceso a datos, la lectura/escritura es correcta
|
||||||
|
// si alguien falla poniendo el codigo del continente es porque el dato incumple una regla del negocio
|
||||||
|
// en el fichero pais te dan un codigo de continente y todo país debe pertenecer a un continente existente (integridad referencial)
|
||||||
|
throw new NegocioException("El codigo del continente " + codContinente + " no existe");
|
||||||
|
}
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Pais crearPais(String[] partes, Continente continente) {
|
||||||
|
Pais pais = new Pais();
|
||||||
|
|
||||||
|
pais.setContinente(continente);
|
||||||
|
pais.setIdentificador(Integer.parseInt(partes[1]));
|
||||||
|
pais.setNombre(partes[2]);
|
||||||
|
pais.setCapital(partes[3]);
|
||||||
|
|
||||||
|
return pais;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package org.palomafp.aadd.ljl.excepciones;
|
||||||
|
|
||||||
|
public class AccesoDatosException extends TecnicoException {
|
||||||
|
|
||||||
|
public AccesoDatosException(String mensaje) {
|
||||||
|
super(mensaje);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AccesoDatosException(String mensaje, Throwable causa) {
|
||||||
|
super(mensaje, causa);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package org.palomafp.aadd.ljl.excepciones;
|
||||||
|
|
||||||
|
public abstract class MapaMundiException extends Exception {
|
||||||
|
|
||||||
|
protected MapaMundiException(String mensaje) {
|
||||||
|
super(mensaje);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected MapaMundiException(String mensaje, Throwable causa) {
|
||||||
|
super(mensaje, causa);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package org.palomafp.aadd.ljl.excepciones;
|
||||||
|
|
||||||
|
public class NegocioException extends MapaMundiException {
|
||||||
|
|
||||||
|
public NegocioException(String mensaje) {
|
||||||
|
super(mensaje);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package org.palomafp.aadd.ljl.excepciones;
|
||||||
|
|
||||||
|
public class TecnicoException extends MapaMundiException {
|
||||||
|
|
||||||
|
public TecnicoException(String mensaje) {
|
||||||
|
super(mensaje);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TecnicoException(String mensaje, Throwable causa) {
|
||||||
|
super(mensaje, causa);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package org.palomafp.aadd.ljl.model;
|
||||||
|
|
||||||
|
public class Continente {
|
||||||
|
|
||||||
|
private String codigo;
|
||||||
|
private String nombre;
|
||||||
|
|
||||||
|
// Constructores
|
||||||
|
public Continente() {
|
||||||
|
|
||||||
|
}
|
||||||
|
public Continente(String codigo, String nombre) {
|
||||||
|
super();
|
||||||
|
this.codigo = codigo;
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Getters y setters
|
||||||
|
public String getCodigo() {
|
||||||
|
return codigo;
|
||||||
|
}
|
||||||
|
public String getNombre() {
|
||||||
|
return nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCodigo(String codigo) {
|
||||||
|
this.codigo = codigo;
|
||||||
|
}
|
||||||
|
public void setNombre(String nombre) {
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generamos el método toString
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Continente [codigo=" + codigo + ", nombre=" + nombre + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
package org.palomafp.aadd.ljl.model;
|
||||||
|
|
||||||
|
public class Pais {
|
||||||
|
|
||||||
|
private int identificador;
|
||||||
|
private String nombre;
|
||||||
|
private String nombreCapital;
|
||||||
|
private Continente continente;
|
||||||
|
|
||||||
|
// Constructores
|
||||||
|
public Pais() {
|
||||||
|
|
||||||
|
}
|
||||||
|
public Pais(int identificador, String nombre, String capital, Continente continente) {
|
||||||
|
super();
|
||||||
|
this.identificador = identificador;
|
||||||
|
this.nombre = nombre;
|
||||||
|
this.nombreCapital = capital;
|
||||||
|
this.continente = continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Getters y Setters
|
||||||
|
public int getIdentificador() {
|
||||||
|
return identificador;
|
||||||
|
}
|
||||||
|
public String getNombre() {
|
||||||
|
return nombre;
|
||||||
|
}
|
||||||
|
public String getCapital() {
|
||||||
|
return nombreCapital;
|
||||||
|
}
|
||||||
|
public Continente getContinente() {
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIdentificador(int identificador) {
|
||||||
|
this.identificador = identificador;
|
||||||
|
}
|
||||||
|
public void setNombre(String nombre) {
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
public void setCapital(String capital) {
|
||||||
|
this.nombreCapital = capital;
|
||||||
|
}
|
||||||
|
public void setContinente(Continente continente) {
|
||||||
|
this.continente = continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Método toString
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.nombre + " (" + this.identificador + ") con capital en " + this.nombreCapital +
|
||||||
|
" pertenece al continente " + this.continente.getNombre() + " (" + this.continente.getCodigo() + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user