first commit
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
.settings/
|
||||||
|
bin/
|
||||||
|
.classpath
|
||||||
|
.project
|
||||||
|
*.class
|
||||||
|
|
||||||
|
|
||||||
Binary file not shown.
@@ -0,0 +1,59 @@
|
|||||||
|
# aadd_fic10 - Mapamundi (Actividad 1.10: Excepciones)
|
||||||
|
|
||||||
|
Proyecto que gestiona continentes y países almacenados en ficheros, incorporando tratamiento de excepciones.
|
||||||
|
|
||||||
|
## Estructura
|
||||||
|
|
||||||
|
- `org.palomafp.aadd.dta` → clase principal `GestorMapaMundi`
|
||||||
|
- `org.palomafp.aadd.dta.dao` → interfaces `IContinenteDAO` e `IPaisDAO`
|
||||||
|
- `org.palomafp.aadd.dta.dao.impl` → `ContinenteDaoFichero` y `PaisDaoFichero`
|
||||||
|
- `org.palomafp.aadd.dta.gestores` → `GestorConfiguracion` (lee `application.properties`)
|
||||||
|
- `org.palomafp.aadd.dta.excepciones` → excepciones de aplicación
|
||||||
|
- `org.palomafp.aadd.dta.model` → `Continente` y `Pais`
|
||||||
|
|
||||||
|
## Excepciones
|
||||||
|
|
||||||
|
| Excepción | Cuándo se lanza |
|
||||||
|
|---|---|
|
||||||
|
| `TecnicoException` | Error al leer el fichero `application.properties` |
|
||||||
|
| `AccesoDatosException` | Error técnico al leer o escribir los ficheros de datos |
|
||||||
|
| `NegocioException` | Un país referencia un continente que no existe |
|
||||||
|
|
||||||
|
Jerarquía: `MapaMundiException` (abstracta) → `TecnicoException` → `AccesoDatosException`, y `MapaMundiException` → `NegocioException`.
|
||||||
|
|
||||||
|
Todas son *checked*. Las excepciones se lanzan en las implementaciones
|
||||||
|
y se recogen en el `main`.
|
||||||
|
|
||||||
|
## Configuración
|
||||||
|
|
||||||
|
El fichero `application.properties` debe estar en el classpath con estas claves:
|
||||||
|
|
||||||
|
```properties
|
||||||
|
# información de Continente
|
||||||
|
continente.ruta.fichero=C:\\Docs\\02dam\\accesoadatos\\ud01_manejoficheros\\06fic\\informacion_continentes.csv
|
||||||
|
continente.csv.separador=#
|
||||||
|
continente.implementacion.clase=org.palomafp.aadd.dta.dao.impl.ContinenteDaoFichero
|
||||||
|
|
||||||
|
# información de Pais
|
||||||
|
pais.ruta.fichero=C:\\Docs\\02dam\\accesoadatos\\ud01_manejoficheros\\05fic\\informacion_paises.txt
|
||||||
|
pais.txt.separador=,
|
||||||
|
pais.implementacion.clase=org.palomafp.aadd.dta.dao.impl.PaisDaoFichero
|
||||||
|
```
|
||||||
|
|
||||||
|
## Ejecución
|
||||||
|
|
||||||
|
El programa recibe la operación como primer argumento:
|
||||||
|
|
||||||
|
| Argumentos | Operación |
|
||||||
|
|---|---|
|
||||||
|
| `1` | Listar continentes |
|
||||||
|
| `2 <codigo>` | Consultar continente |
|
||||||
|
| `3 <codigo> <nombre>` | Insertar continente |
|
||||||
|
| `4 <codigo>` | Eliminar continente |
|
||||||
|
| `5 <codigo> <nombre>` | Actualizar continente |
|
||||||
|
| `6 <id>` | Consultar país por ID |
|
||||||
|
| `7 <codigo>` | Listar países de un continente |
|
||||||
|
|
||||||
|
##### Pregunta: Quieres lanzar una excepción en el caso de un país contenga la referencia a un continente que no exista (la operación devuelve null). ¿Cuál de las excepciones anteriores utilizarías?
|
||||||
|
|
||||||
|
Se usa `NegocioException`: la lectura del fichero funciona correctamente y no hay fallo técnico; lo que se incumple es una regla del dominio (todo país debe pertenecer a un continente existente).
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
# información de Continente
|
||||||
|
continente.ruta.fichero=C:\\Docs\\02dam\\accesoadatos\\ud01_manejoficheros\\06fic\\informacion_continentes.csv
|
||||||
|
continente.csv.separador=#
|
||||||
|
continente.implementacion.clase=org.palomafp.aadd.dta.dao.impl.ContinenteDaoFichero
|
||||||
|
|
||||||
|
# información de Pais
|
||||||
|
pais.ruta.fichero=C:\\Docs\\02dam\\accesoadatos\\ud01_manejoficheros\\05fic\\informacion_paises.txt
|
||||||
|
pais.txt.separador=,
|
||||||
|
pais.implementacion.clase=org.palomafp.aadd.dta.dao.impl.PaisDaoFichero
|
||||||
@@ -0,0 +1,149 @@
|
|||||||
|
package org.palomafp.aadd.dta;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.dta.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.dta.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.dta.dao.impl.ContinenteDaoFichero;
|
||||||
|
import org.palomafp.aadd.dta.dao.impl.PaisDaoFichero;
|
||||||
|
import org.palomafp.aadd.dta.excepciones.AccesoDatosException;
|
||||||
|
import org.palomafp.aadd.dta.excepciones.NegocioException;
|
||||||
|
import org.palomafp.aadd.dta.excepciones.TecnicoException;
|
||||||
|
import org.palomafp.aadd.dta.model.Continente;
|
||||||
|
import org.palomafp.aadd.dta.model.Pais;
|
||||||
|
|
||||||
|
public class GestorMapaMundi {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
if (args.length == 0) {
|
||||||
|
System.err.println("Error: Debes indicar el parametro de la operacion (1 al 7).");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int operacion;
|
||||||
|
try {
|
||||||
|
operacion = Integer.parseInt(args[0]);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
System.err.println("Error: El primer parametro debe ser un numero entero.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
IContinenteDAO continenteDao = new ContinenteDaoFichero();
|
||||||
|
IPaisDAO paisDao = new PaisDaoFichero(continenteDao);
|
||||||
|
|
||||||
|
switch (operacion) {
|
||||||
|
case 1 -> listarContinentes(continenteDao);
|
||||||
|
case 2 -> consultarContinente(continenteDao, args);
|
||||||
|
case 3 -> insertarContinente(continenteDao, args);
|
||||||
|
case 4 -> eliminarContinente(continenteDao, args);
|
||||||
|
case 5 -> actualizarContinente(continenteDao, args);
|
||||||
|
case 6 -> consultarPais(paisDao, args);
|
||||||
|
case 7 -> listarPaisesDeContinente(paisDao, args);
|
||||||
|
default -> System.err.println("Operacion no reconocida. Valores validos del 1 al 7.");
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (AccesoDatosException e) {
|
||||||
|
System.err.println("Error de acceso a datos: " + e.getMessage());
|
||||||
|
} catch (TecnicoException e) {
|
||||||
|
System.err.println("Error tecnico: " + e.getMessage());
|
||||||
|
} catch (NegocioException e) {
|
||||||
|
System.err.println("Error de negocio: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Operaciones de continentes
|
||||||
|
|
||||||
|
private static void listarContinentes(IContinenteDAO dao) throws AccesoDatosException {
|
||||||
|
for (Continente c : dao.obtenerTodos()) {
|
||||||
|
System.out.println(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void consultarContinente(IContinenteDAO dao, String[] args) throws AccesoDatosException {
|
||||||
|
if (args.length < 2) {
|
||||||
|
System.err.println("Error: Para la operacion 2 se necesita el Codigo del Continente.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String codigo = args[1];
|
||||||
|
Continente c = dao.obtenerPorCodigo(codigo);
|
||||||
|
if (c != null) {
|
||||||
|
System.out.println(c);
|
||||||
|
} else {
|
||||||
|
System.out.println("No se ha encontrado el continente con codigo: " + codigo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void insertarContinente(IContinenteDAO dao, String[] args) throws AccesoDatosException {
|
||||||
|
if (args.length < 3) {
|
||||||
|
System.err.println("Error: Faltan parametros (Codigo y Nombre) para insertar.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String codigo = args[1];
|
||||||
|
String nombre = args[2];
|
||||||
|
if (dao.insertar(new Continente(codigo, nombre))) {
|
||||||
|
System.out.println("El continente se ha insertado correctamente.");
|
||||||
|
} else {
|
||||||
|
System.out.println("Error al insertar: El continente con codigo " + codigo + " ya existe.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void eliminarContinente(IContinenteDAO dao, String[] args) throws AccesoDatosException {
|
||||||
|
if (args.length < 2) {
|
||||||
|
System.err.println("Error: Para la operacion 4 se necesita el Codigo del Continente a eliminar.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String codigo = args[1];
|
||||||
|
if (dao.eliminar(codigo)) {
|
||||||
|
System.out.println("El continente ha sido eliminado correctamente.");
|
||||||
|
} else {
|
||||||
|
System.out.println("Error: No existe el continente con el codigo " + codigo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void actualizarContinente(IContinenteDAO dao, String[] args) throws AccesoDatosException {
|
||||||
|
if (args.length < 3) {
|
||||||
|
System.err.println("Error: Faltan parametros (Codigo y Nuevo Nombre) para actualizar.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String codigo = args[1];
|
||||||
|
String nombre = args[2];
|
||||||
|
if (dao.actualizar(new Continente(codigo, nombre))) {
|
||||||
|
System.out.println("El continente se ha actualizado correctamente.");
|
||||||
|
} else {
|
||||||
|
System.out.println("Error al actualizar: No se encontro el continente con codigo " + codigo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Operaciones de países
|
||||||
|
|
||||||
|
private static void consultarPais(IPaisDAO dao, String[] args)
|
||||||
|
throws AccesoDatosException, NegocioException {
|
||||||
|
if (args.length < 2) {
|
||||||
|
System.err.println("Error: Para la operacion 6 se necesita el identificador del pais.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int id;
|
||||||
|
try {
|
||||||
|
id = Integer.parseInt(args[1]);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
System.err.println("Error: El identificador del pais debe ser un numero entero.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Pais pais = dao.obtenerPaisPorID(id);
|
||||||
|
if (pais != null) {
|
||||||
|
System.out.println(pais);
|
||||||
|
} else {
|
||||||
|
System.out.println("No se ha encontrado el pais con identificador: " + id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void listarPaisesDeContinente(IPaisDAO dao, String[] args)
|
||||||
|
throws AccesoDatosException, NegocioException {
|
||||||
|
if (args.length < 2) {
|
||||||
|
System.err.println("Error: Para la operacion 7 se necesita el Codigo del Continente.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (Pais p : dao.obtenerPaisesPorContinente(args[1])) {
|
||||||
|
System.out.println(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package org.palomafp.aadd.dta.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.dta.excepciones.AccesoDatosException;
|
||||||
|
import org.palomafp.aadd.dta.model.Continente;
|
||||||
|
|
||||||
|
public interface IContinenteDAO {
|
||||||
|
|
||||||
|
List<Continente> obtenerTodos() throws AccesoDatosException;
|
||||||
|
Continente obtenerPorCodigo(String codigo) throws AccesoDatosException;
|
||||||
|
boolean insertar(Continente continente) throws AccesoDatosException;
|
||||||
|
boolean actualizar(Continente continente) throws AccesoDatosException;
|
||||||
|
boolean eliminar(String codigo) throws AccesoDatosException;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package org.palomafp.aadd.dta.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.dta.excepciones.AccesoDatosException;
|
||||||
|
import org.palomafp.aadd.dta.excepciones.NegocioException;
|
||||||
|
import org.palomafp.aadd.dta.model.Pais;
|
||||||
|
|
||||||
|
public interface IPaisDAO {
|
||||||
|
|
||||||
|
Pais obtenerPaisPorID(int idPais) throws AccesoDatosException, NegocioException;
|
||||||
|
List<Pais> obtenerPaisesPorContinente(String codContinente) throws AccesoDatosException, NegocioException;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,163 @@
|
|||||||
|
package org.palomafp.aadd.dta.dao.impl;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.StandardOpenOption;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.dta.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.dta.excepciones.AccesoDatosException;
|
||||||
|
import org.palomafp.aadd.dta.excepciones.TecnicoException;
|
||||||
|
import org.palomafp.aadd.dta.gestores.GestorConfiguracion;
|
||||||
|
import org.palomafp.aadd.dta.model.Continente;
|
||||||
|
|
||||||
|
public class ContinenteDaoFichero implements IContinenteDAO {
|
||||||
|
|
||||||
|
private Path ruta;
|
||||||
|
private String separador;
|
||||||
|
|
||||||
|
public ContinenteDaoFichero() throws TecnicoException {
|
||||||
|
this.ruta = GestorConfiguracion.getRutaFicheroContinente();
|
||||||
|
this.separador = GestorConfiguracion.getSeparadorCsvFicheroContinente();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Continente> obtenerTodos() throws AccesoDatosException {
|
||||||
|
List<Continente> lista = new ArrayList<>();
|
||||||
|
try {
|
||||||
|
List<String> lineas = Files.readAllLines(ruta);
|
||||||
|
for(int i = 1; i < lineas.size(); i++) {
|
||||||
|
Continente c = parsearAContinente(lineas.get(i));
|
||||||
|
if (c != null) {
|
||||||
|
lista.add(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AccesoDatosException("Error al leer el fichero de continentes", e);
|
||||||
|
}
|
||||||
|
return lista;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Continente obtenerPorCodigo(String codigo) throws AccesoDatosException {
|
||||||
|
try {
|
||||||
|
List<String> lineas = Files.readAllLines(ruta);
|
||||||
|
for(int i = 1; i < lineas.size(); i++) {
|
||||||
|
Continente c = parsearAContinente(lineas.get(i));
|
||||||
|
if (c != null && c.getCodigo().equals(codigo.trim())) {
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AccesoDatosException("Error al leer el fichero de continentes", e);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean insertar(Continente continente) throws AccesoDatosException {
|
||||||
|
if (obtenerPorCodigo(continente.getCodigo()) != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
String nuevaLinea = System.lineSeparator() + convertirACsv(continente, "2026");
|
||||||
|
Files.writeString(ruta, nuevaLinea, StandardOpenOption.APPEND);
|
||||||
|
return true;
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AccesoDatosException("Error al insertar en el fichero de continentes", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean actualizar(Continente continente) throws AccesoDatosException {
|
||||||
|
try {
|
||||||
|
boolean actualizado = false;
|
||||||
|
List<String> lineas = Files.readAllLines(ruta);
|
||||||
|
|
||||||
|
for(int i = 1; i < lineas.size(); i++) {
|
||||||
|
String linea = lineas.get(i);
|
||||||
|
|
||||||
|
if (linea.trim().isEmpty() || !linea.contains(separador) || linea.startsWith("anyo")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String[] datos = linea.split(separador);
|
||||||
|
if (datos.length >= 3 && datos[1].trim().equals(continente.getCodigo().trim())) {
|
||||||
|
String nuevaLinea = convertirACsv(continente, datos[0].trim());
|
||||||
|
lineas.set(i, nuevaLinea);
|
||||||
|
actualizado = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (actualizado) {
|
||||||
|
reescribirFicheroCompleto(lineas);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AccesoDatosException("Error al leer el fichero de continentes para actualizar", e);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean eliminar(String codigo) throws AccesoDatosException {
|
||||||
|
try {
|
||||||
|
boolean eliminado = false;
|
||||||
|
List<String> lineas = Files.readAllLines(ruta);
|
||||||
|
|
||||||
|
for(int i = 1; i < lineas.size(); i++) {
|
||||||
|
String linea = lineas.get(i);
|
||||||
|
|
||||||
|
if (linea.trim().isEmpty() || !linea.contains(separador) || linea.startsWith("anyo")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String[] datos = linea.split(separador);
|
||||||
|
if (datos.length >= 3 && datos[1].trim().equals(codigo.trim())) {
|
||||||
|
lineas.remove(i);
|
||||||
|
eliminado = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (eliminado) {
|
||||||
|
reescribirFicheroCompleto(lineas);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AccesoDatosException("Error al leer el fichero de continentes para eliminar", e);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void reescribirFicheroCompleto(List<String> lineas) throws AccesoDatosException {
|
||||||
|
try {
|
||||||
|
Files.write(ruta, lineas, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AccesoDatosException("Error al sobreescribir el fichero de continentes", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// cambio recomendado por la IA: MÉTODOS AUXILIARES PARA EVITAR DUPLICIDAD
|
||||||
|
|
||||||
|
private Continente parsearAContinente(String linea) {
|
||||||
|
if (linea.trim().isEmpty() || !linea.contains(separador) || linea.startsWith("anyo")) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String[] datos = linea.split(separador);
|
||||||
|
if (datos.length >= 3) {
|
||||||
|
return new Continente(datos[1].trim(), datos[2].trim());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String convertirACsv(Continente continente, String anyo) {
|
||||||
|
return anyo + separador + continente.getCodigo().trim() + separador + continente.getNombre().trim();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
package org.palomafp.aadd.dta.dao.impl;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.dta.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.dta.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.dta.excepciones.AccesoDatosException;
|
||||||
|
import org.palomafp.aadd.dta.excepciones.NegocioException;
|
||||||
|
import org.palomafp.aadd.dta.excepciones.TecnicoException;
|
||||||
|
import org.palomafp.aadd.dta.gestores.GestorConfiguracion;
|
||||||
|
import org.palomafp.aadd.dta.model.Continente;
|
||||||
|
import org.palomafp.aadd.dta.model.Pais;
|
||||||
|
|
||||||
|
public class PaisDaoFichero implements IPaisDAO {
|
||||||
|
|
||||||
|
private Path ruta;
|
||||||
|
private String separador;
|
||||||
|
private IContinenteDAO continenteDAO;
|
||||||
|
|
||||||
|
public PaisDaoFichero(IContinenteDAO continenteDAO) throws TecnicoException {
|
||||||
|
this.ruta = GestorConfiguracion.getRutaFicheroPais();
|
||||||
|
this.separador = GestorConfiguracion.getSeparadorTxtFicheroPais();
|
||||||
|
this.continenteDAO = continenteDAO;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Pais obtenerPaisPorID(int idPais) throws AccesoDatosException, NegocioException {
|
||||||
|
try {
|
||||||
|
List<String> lineas = Files.readAllLines(ruta);
|
||||||
|
|
||||||
|
for (String linea : lineas) {
|
||||||
|
Pais pais = parsearAPais(linea);
|
||||||
|
|
||||||
|
if (pais != null && pais.getIdentificador() == idPais) {
|
||||||
|
return pais;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AccesoDatosException("Error al leer el fichero de países", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Pais> obtenerPaisesPorContinente(String codContinente) throws AccesoDatosException, NegocioException {
|
||||||
|
List<Pais> listaPaises = new ArrayList<>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
List<String> lineas = Files.readAllLines(ruta);
|
||||||
|
|
||||||
|
for (String linea : lineas) {
|
||||||
|
Pais pais = parsearAPais(linea);
|
||||||
|
|
||||||
|
if (pais != null && pais.getContinente().getCodigo().equals(codContinente)) {
|
||||||
|
listaPaises.add(pais);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AccesoDatosException("Error al leer el fichero de países", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return listaPaises;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Pais parsearAPais(String linea) throws AccesoDatosException, NegocioException {
|
||||||
|
if (linea.trim().isEmpty() || linea.startsWith("#") || linea.startsWith("--")) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
String lineaLimpia = linea.replace("\"", "");
|
||||||
|
String[] datos = lineaLimpia.split(separador);
|
||||||
|
|
||||||
|
if (datos.length >= 4) {
|
||||||
|
try {
|
||||||
|
String codigoContinente = datos[0].trim();
|
||||||
|
int identificador = Integer.parseInt(datos[1].trim());
|
||||||
|
String nombrePais = datos[2].trim();
|
||||||
|
String capital = datos[3].trim();
|
||||||
|
|
||||||
|
Continente continente = continenteDAO.obtenerPorCodigo(codigoContinente);
|
||||||
|
|
||||||
|
if (continente == null) {
|
||||||
|
throw new NegocioException("El país " + nombrePais
|
||||||
|
+ " referencia un continente inexistente: " + codigoContinente);
|
||||||
|
}
|
||||||
|
|
||||||
|
Pais pais = new Pais();
|
||||||
|
pais.setIdentificador(identificador);
|
||||||
|
pais.setNombre(nombrePais);
|
||||||
|
pais.setCapital(capital);
|
||||||
|
pais.setContinente(continente);
|
||||||
|
|
||||||
|
return pais;
|
||||||
|
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
throw new AccesoDatosException("Identificador de país no válido en la línea: " + linea, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package org.palomafp.aadd.dta.excepciones;
|
||||||
|
|
||||||
|
public class AccesoDatosException extends TecnicoException {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
public AccesoDatosException(String mensaje) {
|
||||||
|
super(mensaje);
|
||||||
|
}
|
||||||
|
public AccesoDatosException(String mensaje, Throwable causa) {
|
||||||
|
super(mensaje, causa);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package org.palomafp.aadd.dta.excepciones;
|
||||||
|
|
||||||
|
public abstract class MapaMundiException extends Exception {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
// Constructor protegido en clase abstracta
|
||||||
|
// Conseguimos que esta clase no se pueda "instanciar directamente", solo a través de sus subclases.
|
||||||
|
protected MapaMundiException(String mensaje) {
|
||||||
|
super(mensaje);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Constructor protegido en clase abstracta
|
||||||
|
protected MapaMundiException(String mensaje, Throwable causa) {
|
||||||
|
super(mensaje, causa);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package org.palomafp.aadd.dta.excepciones;
|
||||||
|
|
||||||
|
public class NegocioException extends MapaMundiException {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
// Constructor con mensaje como parámetro.
|
||||||
|
public NegocioException(String mensaje) {
|
||||||
|
super(mensaje);
|
||||||
|
}
|
||||||
|
|
||||||
|
// No se incluye el constructor con Throwable; se asume que una excepción de negocio no tiene asociada una excepción.
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package org.palomafp.aadd.dta.excepciones;
|
||||||
|
|
||||||
|
public class TecnicoException extends MapaMundiException {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
// Error de tipo "técnico" que no está asociado a una excepción concreta.
|
||||||
|
public TecnicoException(String mensaje) {
|
||||||
|
super(mensaje);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TecnicoException(String mensaje, Throwable causa) {
|
||||||
|
super(mensaje, causa);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
package org.palomafp.aadd.dta.gestores;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.dta.excepciones.TecnicoException;
|
||||||
|
|
||||||
|
public class GestorConfiguracion {
|
||||||
|
|
||||||
|
private static final String RUTA_FICHERO_CONFIGURACION = "application.properties";
|
||||||
|
private static final Properties PROPIEDADES_CONFIGURACION = new Properties();
|
||||||
|
private static boolean cargado = false;
|
||||||
|
|
||||||
|
// constructor privado: de esta manera conseguimos que esta clase no se puede instanciar.
|
||||||
|
private GestorConfiguracion() { }
|
||||||
|
|
||||||
|
public static void cargar() throws TecnicoException {
|
||||||
|
|
||||||
|
if (cargado) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try (var fichero = GestorConfiguracion.class.getClassLoader().getResourceAsStream(RUTA_FICHERO_CONFIGURACION)) {
|
||||||
|
|
||||||
|
if (fichero == null) {
|
||||||
|
throw new TecnicoException("No se ha encontrado el fichero "
|
||||||
|
+ RUTA_FICHERO_CONFIGURACION + " en el classpath.");
|
||||||
|
}
|
||||||
|
|
||||||
|
PROPIEDADES_CONFIGURACION.load(fichero);
|
||||||
|
cargado = true;
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new TecnicoException("No se ha podido cargar el fichero de configuración", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// configuración de continentes
|
||||||
|
|
||||||
|
public static Path getRutaFicheroContinente() throws TecnicoException {
|
||||||
|
cargar();
|
||||||
|
String rutaStr = PROPIEDADES_CONFIGURACION.getProperty(
|
||||||
|
"continente.ruta.fichero",
|
||||||
|
"C:\\Docs\\02dam\\accesoadatos\\ud01_manejoficheros\\06fic\\informacion_continentes.csv"
|
||||||
|
);
|
||||||
|
return Path.of(rutaStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getSeparadorCsvFicheroContinente() throws TecnicoException {
|
||||||
|
cargar();
|
||||||
|
return PROPIEDADES_CONFIGURACION.getProperty("continente.csv.separador", "#");
|
||||||
|
}
|
||||||
|
|
||||||
|
// configuración de países
|
||||||
|
|
||||||
|
public static Path getRutaFicheroPais() throws TecnicoException {
|
||||||
|
cargar();
|
||||||
|
String rutaStr = PROPIEDADES_CONFIGURACION.getProperty(
|
||||||
|
"pais.ruta.fichero",
|
||||||
|
"C:\\Docs\\02dam\\accesoadatos\\ud01_manejoficheros\\05fic\\informacion_paises.txt"
|
||||||
|
);
|
||||||
|
return Path.of(rutaStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getSeparadorTxtFicheroPais() throws TecnicoException {
|
||||||
|
cargar();
|
||||||
|
return PROPIEDADES_CONFIGURACION.getProperty("pais.txt.separador", ",");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package org.palomafp.aadd.dta.model;
|
||||||
|
|
||||||
|
public class Continente {
|
||||||
|
|
||||||
|
private String codigo;
|
||||||
|
private String nombre;
|
||||||
|
|
||||||
|
public Continente() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Continente(String codigo, String nombre) {
|
||||||
|
this.codigo = codigo;
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 "Continente [codigo=" + codigo + ", nombre=" + nombre + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package org.palomafp.aadd.dta.model;
|
||||||
|
|
||||||
|
public class Pais {
|
||||||
|
|
||||||
|
private int identificador;
|
||||||
|
private String nombre;
|
||||||
|
private String capital;
|
||||||
|
private Continente continente;
|
||||||
|
|
||||||
|
public Pais() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pais(int identificador, String nombre, String capital, Continente continente) {
|
||||||
|
this.identificador = identificador;
|
||||||
|
this.nombre = nombre;
|
||||||
|
this.capital = capital;
|
||||||
|
this.continente = continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 getCapital() {
|
||||||
|
return capital;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCapital(String capital) {
|
||||||
|
this.capital = capital;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Continente getContinente() {
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContinente(Continente continente) {
|
||||||
|
this.continente = continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return nombre + " (" + identificador + ") con capital en " + capital +
|
||||||
|
" pertenece al continente " + continente.getNombre() + " (" + continente.getCodigo() + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user