Primer commit 10
This commit is contained in:
@@ -4,6 +4,8 @@ import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.ss.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.ss.dao.impl.ContinenteDaoCSV;
|
||||
import org.palomafp.aadd.ss.excepciones.AccesoDatosException;
|
||||
import org.palomafp.aadd.ss.excepciones.TecnicoException;
|
||||
import org.palomafp.aadd.ss.model.Continente;
|
||||
|
||||
public class GestorMapaMundi {
|
||||
@@ -14,7 +16,7 @@ public class GestorMapaMundi {
|
||||
|
||||
IContinenteDAO continenteDao = new ContinenteDaoCSV();
|
||||
Continente continente = null;
|
||||
|
||||
try {
|
||||
switch(opcion) {
|
||||
case 1:
|
||||
List<Continente> continentes = continenteDao.obtenerTodos();
|
||||
@@ -45,6 +47,16 @@ public class GestorMapaMundi {
|
||||
default:
|
||||
System.out.println("Opcion incorrecta...");
|
||||
}
|
||||
//}catch(NegocioException e) {
|
||||
// System.out.println("Error de Negocio: " + e.getMessage());
|
||||
|
||||
}catch(AccesoDatosException e) {
|
||||
System.out.println("Error de Acceso a datos " + e.getMessage());
|
||||
}catch(TecnicoException e) {
|
||||
System.out.println("Error de Tecnico " + e.getMessage());
|
||||
}catch(Exception e){
|
||||
System.out.println("Error insperado " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,8 +73,3 @@ public class GestorMapaMundi {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -2,12 +2,13 @@ package org.palomafp.aadd.ss.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.ss.excepciones.AccesoDatosException;
|
||||
import org.palomafp.aadd.ss.model.Continente;
|
||||
|
||||
public interface IContinenteDAO {
|
||||
List<Continente> obtenerTodos();
|
||||
Continente obtenerPorCodigo(String codigo);
|
||||
boolean insertar(Continente continente);
|
||||
boolean eliminar(String codigo);
|
||||
boolean actualizar(Continente continente);
|
||||
List<Continente> obtenerTodos()throws AccesoDatosException;
|
||||
Continente obtenerPorCodigo(String codigo)throws AccesoDatosException;
|
||||
boolean insertar(Continente continente)throws AccesoDatosException;
|
||||
boolean eliminar(String codigo)throws AccesoDatosException;
|
||||
boolean actualizar(Continente continente)throws AccesoDatosException;
|
||||
}
|
||||
|
||||
@@ -2,11 +2,13 @@ package org.palomafp.aadd.ss.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.ss.excepciones.AccesoDatosException;
|
||||
import org.palomafp.aadd.ss.excepciones.NegocioException;
|
||||
import org.palomafp.aadd.ss.model.Pais;
|
||||
|
||||
public interface IPaisDAO {
|
||||
|
||||
Pais obtenerPaisPorIdentificador(int idPais);
|
||||
List<Pais> obtenerPaisesPorContinente(String codContinente);
|
||||
Pais obtenerPaisPorIdentificador(int idPais)throws AccesoDatosException, NegocioException;
|
||||
List<Pais> obtenerPaisesPorContinente(String codContinente)throws AccesoDatosException, NegocioException;
|
||||
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.ss.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.ss.excepciones.AccesoDatosException;
|
||||
import org.palomafp.aadd.ss.model.Continente;
|
||||
import org.palomafp.aadd.ss.gestores.GestorConfiguracion;
|
||||
public class ContinenteDaoCSV implements IContinenteDAO {
|
||||
@@ -16,7 +17,7 @@ public class ContinenteDaoCSV implements IContinenteDAO {
|
||||
private String separador = GestorConfiguracion.getSeparadorCsvFicheroContinente();
|
||||
private Path rutaNio = Path.of(ruta);
|
||||
@Override
|
||||
public List<Continente> obtenerTodos() {
|
||||
public List<Continente> obtenerTodos() throws AccesoDatosException {
|
||||
|
||||
List<Continente> listaContinentes = new ArrayList<>();
|
||||
try{
|
||||
@@ -36,13 +37,13 @@ public class ContinenteDaoCSV implements IContinenteDAO {
|
||||
listaContinentes.add(new Continente(datos[1].trim(), datos[2].trim()));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error de leer el fichero " + e.getMessage());
|
||||
throw new AccesoDatosException("Error al consultar el fichero CSV de continentes", e);
|
||||
}
|
||||
return listaContinentes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Continente obtenerPorCodigo(String codigo) {
|
||||
public Continente obtenerPorCodigo(String codigo) throws AccesoDatosException {
|
||||
|
||||
List<Continente> todos = obtenerTodos();
|
||||
for(Continente c : todos) {
|
||||
@@ -54,7 +55,7 @@ public class ContinenteDaoCSV implements IContinenteDAO {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean insertar(Continente continente) {
|
||||
public boolean insertar(Continente continente) throws AccesoDatosException {
|
||||
|
||||
if(obtenerPorCodigo(continente.getCodigo()) != null) {
|
||||
System.out.println("El continente con codigo " + continente.getCodigo() + " ya existe.");
|
||||
@@ -68,14 +69,13 @@ public class ContinenteDaoCSV implements IContinenteDAO {
|
||||
Files.write(rutaNio, List.of(lineaNueva),StandardCharsets.UTF_8 , StandardOpenOption.APPEND);
|
||||
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error al insertat el continente. " + e.getMessage());
|
||||
} catch (Exception e) {
|
||||
throw new AccesoDatosException("Error al insertar el continente en el fichero CSV", e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean eliminar(String codigo) {
|
||||
public boolean eliminar(String codigo) throws AccesoDatosException {
|
||||
|
||||
List<Continente> Lista = obtenerTodos();
|
||||
boolean encontrado = false;
|
||||
@@ -99,7 +99,7 @@ public class ContinenteDaoCSV implements IContinenteDAO {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean actualizar(Continente continente) {
|
||||
public boolean actualizar(Continente continente) throws AccesoDatosException {
|
||||
List<Continente> lista = obtenerTodos();
|
||||
boolean encontrado = false;
|
||||
|
||||
@@ -119,7 +119,7 @@ public class ContinenteDaoCSV implements IContinenteDAO {
|
||||
}
|
||||
|
||||
|
||||
private void reescribirFichero(List<Continente> lista) {
|
||||
private void reescribirFichero(List<Continente> lista) throws AccesoDatosException {
|
||||
try{
|
||||
// 3. Usamos Files.write para sobreescribir el archivo entero
|
||||
//Path rutaNio = Path.of(ruta);
|
||||
@@ -132,8 +132,8 @@ public class ContinenteDaoCSV implements IContinenteDAO {
|
||||
// Si no le ponemos APPEND, Files.write borra el archivo y lo escribe entero de nuevo (¡Justo lo que queremos!)
|
||||
Files.write(rutaNio, lineasAEscribir, StandardCharsets.UTF_8);
|
||||
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error al actualizar el fichero: " + e.getMessage());
|
||||
} catch (Exception e) {
|
||||
throw new AccesoDatosException("Error al reescribir el fichero CSV de continentes", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@ import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.ss.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.ss.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.ss.excepciones.AccesoDatosException;
|
||||
import org.palomafp.aadd.ss.excepciones.NegocioException;
|
||||
import org.palomafp.aadd.ss.gestores.GestorConfiguracion;
|
||||
import org.palomafp.aadd.ss.model.Continente;
|
||||
import org.palomafp.aadd.ss.model.Pais;
|
||||
@@ -26,7 +28,7 @@ public class PaisDaoFichero implements IPaisDAO {
|
||||
public IContinenteDAO continenteDAO = new ContinenteDaoCSV();
|
||||
|
||||
@Override
|
||||
public Pais obtenerPaisPorIdentificador(int idPais) {
|
||||
public Pais obtenerPaisPorIdentificador(int idPais) throws AccesoDatosException, NegocioException {
|
||||
Pais pais = null;
|
||||
try {
|
||||
List<String> todasLineas = Files.readAllLines(rutaNio, StandardCharsets.UTF_8);
|
||||
@@ -51,6 +53,9 @@ public class PaisDaoFichero implements IPaisDAO {
|
||||
pais.setCapital(capital);
|
||||
|
||||
Continente continente = continenteDAO.obtenerPorCodigo(codigoContinente);
|
||||
if(continente == null) {
|
||||
throw new NegocioException("El pais " + nombre + "Esta asignada a continente no existente" + codigoContinente);
|
||||
}
|
||||
pais.setContinente(continente);
|
||||
|
||||
break;
|
||||
@@ -60,14 +65,14 @@ public class PaisDaoFichero implements IPaisDAO {
|
||||
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error al leer el fichero de países: " + e.getMessage());
|
||||
throw new AccesoDatosException("Error al consultar el fichero de texto de países", e);
|
||||
}
|
||||
|
||||
return pais;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Pais> obtenerPaisesPorContinente(String codContinente) {
|
||||
public List<Pais> obtenerPaisesPorContinente(String codContinente) throws NegocioException, AccesoDatosException {
|
||||
|
||||
List<Pais> listaPaises =new ArrayList<>();
|
||||
Pais pais = null;
|
||||
@@ -94,6 +99,9 @@ public class PaisDaoFichero implements IPaisDAO {
|
||||
pais.setCapital(capital);
|
||||
|
||||
continente = continenteDAO.obtenerPorCodigo(codigo);
|
||||
if (continente == null) {
|
||||
throw new NegocioException("No se puede cargar el país '" + nombre + "' porque el continente con código '" + codigo + "' no existe.");
|
||||
}
|
||||
pais.setContinente(continente);
|
||||
|
||||
listaPaises.add(pais);
|
||||
@@ -102,7 +110,8 @@ public class PaisDaoFichero implements IPaisDAO {
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error al leer el fichero de países: " + e.getMessage()); }
|
||||
throw new AccesoDatosException("Error al consultar la lista de países por continente", e);
|
||||
}
|
||||
|
||||
return listaPaises;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.palomafp.aadd.ss.excepciones;
|
||||
|
||||
/**
|
||||
* NegocioException: Clase que representa la excepción técnica ocurrida durante el acceso a datos del proyecto MapaMundi
|
||||
*/
|
||||
|
||||
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,22 @@
|
||||
package org.palomafp.aadd.ss.excepciones;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* MapaMundoException: Clase abstracta que representa la excepción base del proyecto MapaMundi
|
||||
*/
|
||||
|
||||
public abstract class MapaMundiException extends Exception {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
// Constructor protegido en clase abstracta
|
||||
// Conseguimos que esta clase no se puede "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,18 @@
|
||||
package org.palomafp.aadd.ss.excepciones;
|
||||
|
||||
/**
|
||||
* NegocioException: Clase que representa la excepción de negocio del proyecto MapaMundi *
|
||||
*/
|
||||
|
||||
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,19 @@
|
||||
package org.palomafp.aadd.ss.excepciones;
|
||||
|
||||
/**
|
||||
* NegocioException: Clase que representa la excepción técnica del proyecto MapaMundi
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.palomafp.aadd.ss.excepciones.TecnicoException;
|
||||
|
||||
public class GestorConfiguracion {
|
||||
// LÍNEA 1: Nombre del archivo dentro del Classpath.
|
||||
// Como marcamos la carpeta 'config' como Source Folder, el archivo se encuentra en la raíz del Classpath,
|
||||
@@ -26,15 +28,13 @@ public class GestorConfiguracion {
|
||||
// Retorna un InputStream (flujo de datos).
|
||||
try(InputStream fichero = GestorConfiguracion.class.getClassLoader().getResourceAsStream(RUTA_FICHERO_CONFIGURACION)){
|
||||
if (fichero == null) {
|
||||
throw new RuntimeException("No se encuentra " + RUTA_FICHERO_CONFIGURACION + " en el classpath");
|
||||
throw new TecnicoException("No se encuentra " + RUTA_FICHERO_CONFIGURACION + " en el classpath");
|
||||
}
|
||||
// LÍNEA 7: La estantería PROPIEDADES_CONFIGURACION lee y organiza las claves y valores del inputStream (.load).
|
||||
PROPIEDAD_CONFIGURACION.load(fichero);
|
||||
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Fallo crítico al cargar la configuración de la aplicación", e);
|
||||
}
|
||||
}
|
||||
public static String getRutaFicheroContinente() {
|
||||
|
||||
Reference in New Issue
Block a user