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.IContinenteDAO;
|
||||||
import org.palomafp.aadd.ss.dao.impl.ContinenteDaoCSV;
|
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;
|
import org.palomafp.aadd.ss.model.Continente;
|
||||||
|
|
||||||
public class GestorMapaMundi {
|
public class GestorMapaMundi {
|
||||||
@@ -14,36 +16,46 @@ public class GestorMapaMundi {
|
|||||||
|
|
||||||
IContinenteDAO continenteDao = new ContinenteDaoCSV();
|
IContinenteDAO continenteDao = new ContinenteDaoCSV();
|
||||||
Continente continente = null;
|
Continente continente = null;
|
||||||
|
try {
|
||||||
|
switch(opcion) {
|
||||||
|
case 1:
|
||||||
|
List<Continente> continentes = continenteDao.obtenerTodos();
|
||||||
|
|
||||||
switch(opcion) {
|
for(Continente c : continentes) {
|
||||||
case 1:
|
System.out.println(c);
|
||||||
List<Continente> continentes = continenteDao.obtenerTodos();
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
for(Continente c : continentes) {
|
case 2:
|
||||||
System.out.println(c);
|
continente = continenteDao.obtenerPorCodigo(args[1]);
|
||||||
|
System.out.println("El continente es: " + continente);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
continente = new Continente(args[1], args[2]);
|
||||||
|
continenteDao.insertar(continente);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 4:
|
||||||
|
continenteDao.eliminar(args[1]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 5:
|
||||||
|
continente = new Continente(args[1], args[2]);
|
||||||
|
continenteDao.actualizar(continente);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
System.out.println("Opcion incorrecta...");
|
||||||
}
|
}
|
||||||
break;
|
//}catch(NegocioException e) {
|
||||||
|
// System.out.println("Error de Negocio: " + e.getMessage());
|
||||||
|
|
||||||
case 2:
|
}catch(AccesoDatosException e) {
|
||||||
continente = continenteDao.obtenerPorCodigo(args[1]);
|
System.out.println("Error de Acceso a datos " + e.getMessage());
|
||||||
System.out.println("El continente es: " + continente);
|
}catch(TecnicoException e) {
|
||||||
break;
|
System.out.println("Error de Tecnico " + e.getMessage());
|
||||||
|
}catch(Exception e){
|
||||||
case 3:
|
System.out.println("Error insperado " + e.getMessage());
|
||||||
continente = new Continente(args[1], args[2]);
|
|
||||||
continenteDao.insertar(continente);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 4:
|
|
||||||
continenteDao.eliminar(args[1]);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 5:
|
|
||||||
continente = new Continente(args[1], args[2]);
|
|
||||||
continenteDao.actualizar(continente);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
System.out.println("Opcion incorrecta...");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -61,8 +73,3 @@ public class GestorMapaMundi {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,12 +2,13 @@ package org.palomafp.aadd.ss.dao;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ss.excepciones.AccesoDatosException;
|
||||||
import org.palomafp.aadd.ss.model.Continente;
|
import org.palomafp.aadd.ss.model.Continente;
|
||||||
|
|
||||||
public interface IContinenteDAO {
|
public interface IContinenteDAO {
|
||||||
List<Continente> obtenerTodos();
|
List<Continente> obtenerTodos()throws AccesoDatosException;
|
||||||
Continente obtenerPorCodigo(String codigo);
|
Continente obtenerPorCodigo(String codigo)throws AccesoDatosException;
|
||||||
boolean insertar(Continente continente);
|
boolean insertar(Continente continente)throws AccesoDatosException;
|
||||||
boolean eliminar(String codigo);
|
boolean eliminar(String codigo)throws AccesoDatosException;
|
||||||
boolean actualizar(Continente continente);
|
boolean actualizar(Continente continente)throws AccesoDatosException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,13 @@ package org.palomafp.aadd.ss.dao;
|
|||||||
|
|
||||||
import java.util.List;
|
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;
|
import org.palomafp.aadd.ss.model.Pais;
|
||||||
|
|
||||||
public interface IPaisDAO {
|
public interface IPaisDAO {
|
||||||
|
|
||||||
Pais obtenerPaisPorIdentificador(int idPais);
|
Pais obtenerPaisPorIdentificador(int idPais)throws AccesoDatosException, NegocioException;
|
||||||
List<Pais> obtenerPaisesPorContinente(String codContinente);
|
List<Pais> obtenerPaisesPorContinente(String codContinente)throws AccesoDatosException, NegocioException;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.palomafp.aadd.ss.dao.IContinenteDAO;
|
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.model.Continente;
|
||||||
import org.palomafp.aadd.ss.gestores.GestorConfiguracion;
|
import org.palomafp.aadd.ss.gestores.GestorConfiguracion;
|
||||||
public class ContinenteDaoCSV implements IContinenteDAO {
|
public class ContinenteDaoCSV implements IContinenteDAO {
|
||||||
@@ -16,7 +17,7 @@ public class ContinenteDaoCSV implements IContinenteDAO {
|
|||||||
private String separador = GestorConfiguracion.getSeparadorCsvFicheroContinente();
|
private String separador = GestorConfiguracion.getSeparadorCsvFicheroContinente();
|
||||||
private Path rutaNio = Path.of(ruta);
|
private Path rutaNio = Path.of(ruta);
|
||||||
@Override
|
@Override
|
||||||
public List<Continente> obtenerTodos() {
|
public List<Continente> obtenerTodos() throws AccesoDatosException {
|
||||||
|
|
||||||
List<Continente> listaContinentes = new ArrayList<>();
|
List<Continente> listaContinentes = new ArrayList<>();
|
||||||
try{
|
try{
|
||||||
@@ -36,13 +37,13 @@ public class ContinenteDaoCSV implements IContinenteDAO {
|
|||||||
listaContinentes.add(new Continente(datos[1].trim(), datos[2].trim()));
|
listaContinentes.add(new Continente(datos[1].trim(), datos[2].trim()));
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} 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;
|
return listaContinentes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Continente obtenerPorCodigo(String codigo) {
|
public Continente obtenerPorCodigo(String codigo) throws AccesoDatosException {
|
||||||
|
|
||||||
List<Continente> todos = obtenerTodos();
|
List<Continente> todos = obtenerTodos();
|
||||||
for(Continente c : todos) {
|
for(Continente c : todos) {
|
||||||
@@ -54,7 +55,7 @@ public class ContinenteDaoCSV implements IContinenteDAO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean insertar(Continente continente) {
|
public boolean insertar(Continente continente) throws AccesoDatosException {
|
||||||
|
|
||||||
if(obtenerPorCodigo(continente.getCodigo()) != null) {
|
if(obtenerPorCodigo(continente.getCodigo()) != null) {
|
||||||
System.out.println("El continente con codigo " + continente.getCodigo() + " ya existe.");
|
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);
|
Files.write(rutaNio, List.of(lineaNueva),StandardCharsets.UTF_8 , StandardOpenOption.APPEND);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (IOException e) {
|
} catch (Exception e) {
|
||||||
System.err.println("Error al insertat el continente. " + e.getMessage());
|
throw new AccesoDatosException("Error al insertar el continente en el fichero CSV", e);
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean eliminar(String codigo) {
|
public boolean eliminar(String codigo) throws AccesoDatosException {
|
||||||
|
|
||||||
List<Continente> Lista = obtenerTodos();
|
List<Continente> Lista = obtenerTodos();
|
||||||
boolean encontrado = false;
|
boolean encontrado = false;
|
||||||
@@ -99,7 +99,7 @@ public class ContinenteDaoCSV implements IContinenteDAO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean actualizar(Continente continente) {
|
public boolean actualizar(Continente continente) throws AccesoDatosException {
|
||||||
List<Continente> lista = obtenerTodos();
|
List<Continente> lista = obtenerTodos();
|
||||||
boolean encontrado = false;
|
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{
|
try{
|
||||||
// 3. Usamos Files.write para sobreescribir el archivo entero
|
// 3. Usamos Files.write para sobreescribir el archivo entero
|
||||||
//Path rutaNio = Path.of(ruta);
|
//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!)
|
// 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);
|
Files.write(rutaNio, lineasAEscribir, StandardCharsets.UTF_8);
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (Exception e) {
|
||||||
System.out.println("Error al actualizar el fichero: " + e.getMessage());
|
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.IContinenteDAO;
|
||||||
import org.palomafp.aadd.ss.dao.IPaisDAO;
|
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.gestores.GestorConfiguracion;
|
||||||
import org.palomafp.aadd.ss.model.Continente;
|
import org.palomafp.aadd.ss.model.Continente;
|
||||||
import org.palomafp.aadd.ss.model.Pais;
|
import org.palomafp.aadd.ss.model.Pais;
|
||||||
@@ -26,7 +28,7 @@ public class PaisDaoFichero implements IPaisDAO {
|
|||||||
public IContinenteDAO continenteDAO = new ContinenteDaoCSV();
|
public IContinenteDAO continenteDAO = new ContinenteDaoCSV();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Pais obtenerPaisPorIdentificador(int idPais) {
|
public Pais obtenerPaisPorIdentificador(int idPais) throws AccesoDatosException, NegocioException {
|
||||||
Pais pais = null;
|
Pais pais = null;
|
||||||
try {
|
try {
|
||||||
List<String> todasLineas = Files.readAllLines(rutaNio, StandardCharsets.UTF_8);
|
List<String> todasLineas = Files.readAllLines(rutaNio, StandardCharsets.UTF_8);
|
||||||
@@ -51,6 +53,9 @@ public class PaisDaoFichero implements IPaisDAO {
|
|||||||
pais.setCapital(capital);
|
pais.setCapital(capital);
|
||||||
|
|
||||||
Continente continente = continenteDAO.obtenerPorCodigo(codigoContinente);
|
Continente continente = continenteDAO.obtenerPorCodigo(codigoContinente);
|
||||||
|
if(continente == null) {
|
||||||
|
throw new NegocioException("El pais " + nombre + "Esta asignada a continente no existente" + codigoContinente);
|
||||||
|
}
|
||||||
pais.setContinente(continente);
|
pais.setContinente(continente);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@@ -60,14 +65,14 @@ public class PaisDaoFichero implements IPaisDAO {
|
|||||||
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
} 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;
|
return pais;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Pais> obtenerPaisesPorContinente(String codContinente) {
|
public List<Pais> obtenerPaisesPorContinente(String codContinente) throws NegocioException, AccesoDatosException {
|
||||||
|
|
||||||
List<Pais> listaPaises =new ArrayList<>();
|
List<Pais> listaPaises =new ArrayList<>();
|
||||||
Pais pais = null;
|
Pais pais = null;
|
||||||
@@ -94,6 +99,9 @@ public class PaisDaoFichero implements IPaisDAO {
|
|||||||
pais.setCapital(capital);
|
pais.setCapital(capital);
|
||||||
|
|
||||||
continente = continenteDAO.obtenerPorCodigo(codigo);
|
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);
|
pais.setContinente(continente);
|
||||||
|
|
||||||
listaPaises.add(pais);
|
listaPaises.add(pais);
|
||||||
@@ -102,7 +110,8 @@ public class PaisDaoFichero implements IPaisDAO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} catch (IOException e) {
|
} 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;
|
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.io.InputStream;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ss.excepciones.TecnicoException;
|
||||||
|
|
||||||
public class GestorConfiguracion {
|
public class GestorConfiguracion {
|
||||||
// LÍNEA 1: Nombre del archivo dentro del Classpath.
|
// 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,
|
// 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).
|
// Retorna un InputStream (flujo de datos).
|
||||||
try(InputStream fichero = GestorConfiguracion.class.getClassLoader().getResourceAsStream(RUTA_FICHERO_CONFIGURACION)){
|
try(InputStream fichero = GestorConfiguracion.class.getClassLoader().getResourceAsStream(RUTA_FICHERO_CONFIGURACION)){
|
||||||
if (fichero == null) {
|
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).
|
// LÍNEA 7: La estantería PROPIEDADES_CONFIGURACION lee y organiza las claves y valores del inputStream (.load).
|
||||||
PROPIEDAD_CONFIGURACION.load(fichero);
|
PROPIEDAD_CONFIGURACION.load(fichero);
|
||||||
|
|
||||||
} catch (FileNotFoundException e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
throw new RuntimeException("Fallo crítico al cargar la configuración de la aplicación", e);
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public static String getRutaFicheroContinente() {
|
public static String getRutaFicheroContinente() {
|
||||||
|
|||||||
Reference in New Issue
Block a user