Commit por procesado de URLs
This commit is contained in:
parent
cc22926d49
commit
f1a6ddf58c
@ -1,6 +1,5 @@
|
||||
package org.lapaloma.aadd.redmetro;
|
||||
|
||||
import org.lapaloma.aadd.redmetro.gestores.GestorEntityManagerJPA;
|
||||
import org.lapaloma.aadd.redmetro.procesador.ProcesadorURLs;
|
||||
|
||||
/**
|
||||
@ -14,11 +13,15 @@ public class AppRedMetro {
|
||||
// Obtiene la Session de forma estática
|
||||
// GestorEntityManagerJPA.getEntityManager();
|
||||
|
||||
app.procesasrURLs();
|
||||
app.procesarURLs();
|
||||
}
|
||||
|
||||
private void procesasrURLs() {
|
||||
private void procesarURLs() {
|
||||
ProcesadorURLs procesadorURLs = new ProcesadorURLs();
|
||||
// Procesar URL de estaciones (XML)
|
||||
procesadorURLs.procesarURLEstacionesXML();
|
||||
|
||||
// Procesar URL de líneas y estaciones (JSON)
|
||||
procesadorURLs.procesarURLLineasEstacionesJSON();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,9 @@
|
||||
package org.lapaloma.aadd.redmetro.dao;
|
||||
|
||||
public interface IEstacionDAO {
|
||||
import org.lapaloma.aadd.redmetro.vo.Estacion;
|
||||
|
||||
public interface IEstacionDAO {
|
||||
Estacion obtenerEstacionPorCodigo(int codigo);
|
||||
void crearEstacion(Estacion estacion);
|
||||
void actualizarEstacion(Estacion estacion);
|
||||
}
|
||||
|
||||
@ -0,0 +1,7 @@
|
||||
package org.lapaloma.aadd.redmetro.dao;
|
||||
|
||||
import org.lapaloma.aadd.redmetro.vo.Linea;
|
||||
|
||||
public interface ILineaDAO {
|
||||
Linea obtenerLineaPorCodigo(int codigo);
|
||||
}
|
||||
@ -1,5 +1,12 @@
|
||||
package org.lapaloma.aadd.redmetro.dao;
|
||||
|
||||
public interface ILineaEstacionDAO {
|
||||
import org.lapaloma.aadd.redmetro.vo.LineaEstacion;
|
||||
import org.lapaloma.aadd.redmetro.vo.LineaEstacionId;
|
||||
|
||||
public interface ILineaEstacionDAO {
|
||||
LineaEstacion obtenerLineaEstacionPorCodigo(LineaEstacionId id);
|
||||
|
||||
void crearLineaEstacion(LineaEstacion lineaEstacion);
|
||||
|
||||
void actualizarLineaEstacion(LineaEstacion lineaEstacion);
|
||||
}
|
||||
|
||||
@ -1,7 +1,76 @@
|
||||
package org.lapaloma.aadd.redmetro.dao.jpa;
|
||||
|
||||
import org.lapaloma.aadd.redmetro.dao.IEstacionDAO;
|
||||
import org.lapaloma.aadd.redmetro.gestores.GestorEntityManagerJPA;
|
||||
import org.lapaloma.aadd.redmetro.vo.Estacion;
|
||||
|
||||
public class EstacionDaoJPA implements IEstacionDAO{
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityTransaction;
|
||||
|
||||
public class EstacionDaoJPA implements IEstacionDAO {
|
||||
|
||||
@Override
|
||||
public Estacion obtenerEstacionPorCodigo(int codigo) {
|
||||
Estacion estacion = null;
|
||||
|
||||
// try con recursos "cerrables": Session
|
||||
try (EntityManager gestorEntidades = GestorEntityManagerJPA.getEntityManager()) {
|
||||
estacion = gestorEntidades.find(Estacion.class, codigo);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return estacion;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void crearEstacion(Estacion estacion) {
|
||||
EntityManager gestorEntidades = null;
|
||||
EntityTransaction transaccion = null;
|
||||
|
||||
try {
|
||||
gestorEntidades = GestorEntityManagerJPA.getEntityManager();
|
||||
transaccion = gestorEntidades.getTransaction();
|
||||
transaccion.begin();
|
||||
|
||||
gestorEntidades.persist(estacion);
|
||||
|
||||
transaccion.commit();
|
||||
} catch (Exception e) {
|
||||
if (transaccion != null && transaccion.isActive()) {
|
||||
transaccion.rollback();
|
||||
}
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (gestorEntidades != null) {
|
||||
gestorEntidades.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actualizarEstacion(Estacion estacion) {
|
||||
EntityManager gestorEntidades = null;
|
||||
EntityTransaction transaccion = null;
|
||||
|
||||
try {
|
||||
gestorEntidades = GestorEntityManagerJPA.getEntityManager();
|
||||
transaccion = gestorEntidades.getTransaction();
|
||||
transaccion.begin();
|
||||
|
||||
if (!gestorEntidades.contains(estacion))
|
||||
gestorEntidades.merge(estacion);
|
||||
|
||||
transaccion.commit();
|
||||
} catch (Exception e) {
|
||||
if (transaccion != null && transaccion.isActive()) {
|
||||
transaccion.rollback();
|
||||
}
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (gestorEntidades != null) {
|
||||
gestorEntidades.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,23 @@
|
||||
package org.lapaloma.aadd.redmetro.dao.jpa;
|
||||
|
||||
import org.lapaloma.aadd.redmetro.dao.ILineaDAO;
|
||||
import org.lapaloma.aadd.redmetro.gestores.GestorEntityManagerJPA;
|
||||
import org.lapaloma.aadd.redmetro.vo.Linea;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
|
||||
public class LineaDaoJPA implements ILineaDAO {
|
||||
|
||||
@Override
|
||||
public Linea obtenerLineaPorCodigo(int codigo) {
|
||||
Linea linea = null;
|
||||
|
||||
// try con recursos "cerrables": Session
|
||||
try (EntityManager gestorEntidades = GestorEntityManagerJPA.getEntityManager()) {
|
||||
linea = gestorEntidades.find(Linea.class, codigo);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return linea;
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,77 @@
|
||||
package org.lapaloma.aadd.redmetro.dao.jpa;
|
||||
|
||||
import org.lapaloma.aadd.redmetro.dao.ILineaEstacionDAO;
|
||||
import org.lapaloma.aadd.redmetro.gestores.GestorEntityManagerJPA;
|
||||
import org.lapaloma.aadd.redmetro.vo.LineaEstacion;
|
||||
import org.lapaloma.aadd.redmetro.vo.LineaEstacionId;
|
||||
|
||||
public class LineaEstacionDaoJPA implements ILineaEstacionDAO{
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityTransaction;
|
||||
|
||||
public class LineaEstacionDaoJPA implements ILineaEstacionDAO {
|
||||
|
||||
@Override
|
||||
public LineaEstacion obtenerLineaEstacionPorCodigo(LineaEstacionId id) {
|
||||
LineaEstacion lineaEstacion = null;
|
||||
|
||||
// try con recursos "cerrables": Session
|
||||
try (EntityManager gestorEntidades = GestorEntityManagerJPA.getEntityManager()) {
|
||||
lineaEstacion = gestorEntidades.find(LineaEstacion.class, id);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return lineaEstacion;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void crearLineaEstacion(LineaEstacion lineaEstacion) {
|
||||
EntityManager gestorEntidades = null;
|
||||
EntityTransaction transaccion = null;
|
||||
|
||||
try {
|
||||
gestorEntidades = GestorEntityManagerJPA.getEntityManager();
|
||||
transaccion = gestorEntidades.getTransaction();
|
||||
transaccion.begin();
|
||||
|
||||
gestorEntidades.persist(lineaEstacion);
|
||||
|
||||
transaccion.commit();
|
||||
} catch (Exception e) {
|
||||
if (transaccion != null && transaccion.isActive()) {
|
||||
transaccion.rollback();
|
||||
}
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (gestorEntidades != null) {
|
||||
gestorEntidades.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actualizarLineaEstacion(LineaEstacion lineaEstacion) {
|
||||
EntityManager gestorEntidades = null;
|
||||
EntityTransaction transaccion = null;
|
||||
|
||||
try {
|
||||
gestorEntidades = GestorEntityManagerJPA.getEntityManager();
|
||||
transaccion = gestorEntidades.getTransaction();
|
||||
transaccion.begin();
|
||||
|
||||
if (!gestorEntidades.contains(lineaEstacion))
|
||||
gestorEntidades.merge(lineaEstacion);
|
||||
|
||||
transaccion.commit();
|
||||
} catch (Exception e) {
|
||||
if (transaccion != null && transaccion.isActive()) {
|
||||
transaccion.rollback();
|
||||
}
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (gestorEntidades != null) {
|
||||
gestorEntidades.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -23,7 +23,7 @@ public class GestorEntityManagerJPA {
|
||||
// Carga la configuración desde META-INF/persistence.xml
|
||||
static {
|
||||
try {
|
||||
entityManagerFactory = Persistence.createEntityManagerFactory("UP_PROYECTOSIES_ODB");
|
||||
entityManagerFactory = Persistence.createEntityManagerFactory("UP_REDMETRO_MYSQL");
|
||||
} catch (Throwable ex) {
|
||||
System.err.println("Error en EntityManagerFactory: " + ex);
|
||||
throw new ExceptionInInitializerError(ex);
|
||||
|
||||
@ -7,8 +7,17 @@ import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
|
||||
import org.lapaloma.aadd.redmetro.dao.IEstacionDAO;
|
||||
import org.lapaloma.aadd.redmetro.dao.ILineaDAO;
|
||||
import org.lapaloma.aadd.redmetro.dao.ILineaEstacionDAO;
|
||||
import org.lapaloma.aadd.redmetro.dao.jpa.EstacionDaoJPA;
|
||||
import org.lapaloma.aadd.redmetro.dao.jpa.LineaDaoJPA;
|
||||
import org.lapaloma.aadd.redmetro.dao.jpa.LineaEstacionDaoJPA;
|
||||
import org.lapaloma.aadd.redmetro.gestores.GestorFicheroConfiguracion;
|
||||
import org.lapaloma.aadd.redmetro.vo.Estacion;
|
||||
import org.lapaloma.aadd.redmetro.vo.Linea;
|
||||
import org.lapaloma.aadd.redmetro.vo.LineaEstacion;
|
||||
import org.lapaloma.aadd.redmetro.vo.LineaEstacionId;
|
||||
|
||||
import tools.jackson.databind.JsonNode;
|
||||
import tools.jackson.databind.ObjectMapper;
|
||||
@ -41,15 +50,83 @@ public class ProcesadorURLs {
|
||||
String direccion = nodoEstacion.path("direccion").asString();
|
||||
int id = nodoEstacion.path("id").asInt();
|
||||
|
||||
System.out.println("Nombre: " + nombre);
|
||||
System.out.println("Dirección: " + direccion);
|
||||
System.out.println("ID: " + id);
|
||||
|
||||
Estacion estacion = new Estacion();
|
||||
estacion.setCodigo(id);
|
||||
estacion.setNombre(nombre);
|
||||
estacion.setDireccion(direccion);
|
||||
System.out.println(estacion);
|
||||
|
||||
IEstacionDAO estacionDAO = new EstacionDaoJPA();
|
||||
Estacion estacionBBDD = estacionDAO.obtenerEstacionPorCodigo(id);
|
||||
|
||||
// Si no existe la estación, la creamos. Si existe, la actualizamos.
|
||||
if (estacionBBDD == null) {
|
||||
estacionDAO.crearEstacion(estacion);
|
||||
} else {
|
||||
estacionDAO.actualizarEstacion(estacion);
|
||||
}
|
||||
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void procesarURLLineasEstacionesJSON() {
|
||||
|
||||
String urlJSONLienasEstaciones = GestorFicheroConfiguracion.getValorfromClave("url.json.lineaestacion");
|
||||
|
||||
URL url = null;
|
||||
try {
|
||||
url = new URI(urlJSONLienasEstaciones).toURL();
|
||||
} catch (MalformedURLException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (URISyntaxException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Procesar con Jackson XML
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
try (InputStream is = url.openStream()) {
|
||||
JsonNode nodosLineasEstaciones = mapper.readTree(is);
|
||||
for (JsonNode nodoLineaEstacion : nodosLineasEstaciones) {
|
||||
int idLinea = nodoLineaEstacion.path("idLinea").asInt();
|
||||
int idEstacion = nodoLineaEstacion.path("idEstacion").asInt();
|
||||
int orden = nodoLineaEstacion.path("orden").asInt();
|
||||
|
||||
ILineaDAO lineaDAO = new LineaDaoJPA();
|
||||
Linea lineaBBDD = lineaDAO.obtenerLineaPorCodigo(idLinea);
|
||||
|
||||
IEstacionDAO estacionDAO = new EstacionDaoJPA();
|
||||
Estacion estacionBBDD = estacionDAO.obtenerEstacionPorCodigo(idEstacion);
|
||||
|
||||
// Si existen la línea y la estación, creamos la relación.
|
||||
if (lineaBBDD != null && estacionBBDD != null) {
|
||||
LineaEstacion lineaEstacion = new LineaEstacion();
|
||||
LineaEstacionId idLineaEstacion = new LineaEstacionId(idLinea, idEstacion);
|
||||
|
||||
lineaEstacion.setId(idLineaEstacion);
|
||||
lineaEstacion.setLinea(lineaBBDD);
|
||||
lineaEstacion.setEstacion(estacionBBDD);
|
||||
lineaEstacion.setOrden(orden);
|
||||
|
||||
System.out.println(lineaEstacion);
|
||||
|
||||
ILineaEstacionDAO lineaEstacionDAO = new LineaEstacionDaoJPA();
|
||||
LineaEstacion lineaEstacionBBDD = lineaEstacionDAO.obtenerLineaEstacionPorCodigo(idLineaEstacion);
|
||||
|
||||
// Si no existe la estación, la creamos. Si existe, la actualizamos.
|
||||
if (lineaEstacionBBDD == null) {
|
||||
lineaEstacionDAO.crearLineaEstacion(lineaEstacion);
|
||||
} else {
|
||||
lineaEstacionDAO.actualizarLineaEstacion(lineaEstacionBBDD);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
} catch (IOException e) {
|
||||
|
||||
@ -9,7 +9,7 @@ import jakarta.persistence.*;
|
||||
@Table(name = "T_ESTACION")
|
||||
public class Estacion {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
// @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "cod_estacion")
|
||||
private int codigo;
|
||||
|
||||
|
||||
@ -31,7 +31,7 @@ public class Linea {
|
||||
private String nombreLargo;
|
||||
|
||||
// Relación OneToOne con T_COLOR (clave única)
|
||||
@OneToOne(fetch = FetchType.LAZY)
|
||||
@OneToOne(fetch = FetchType.EAGER)
|
||||
@JoinColumn(name = "cod_color", nullable = false, unique = true)
|
||||
private Color color;
|
||||
|
||||
|
||||
@ -38,4 +38,9 @@ public class LineaEstacionId implements Serializable {
|
||||
public int hashCode() {
|
||||
return Objects.hash(codigoLinea, codigoEstacion);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "LineaEstacionId [codigoLinea=" + codigoLinea + ", codigoEstacion=" + codigoEstacion + "]";
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,25 +6,28 @@
|
||||
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
|
||||
version="2.1">
|
||||
|
||||
<persistence-unit name="UP_PROYECTOSIES_ODB" transaction-type="RESOURCE_LOCAL">
|
||||
<persistence-unit name="UP_REDMETRO_MYSQL" transaction-type="RESOURCE_LOCAL">
|
||||
<!-- Proveedor de persistencia JPA -->
|
||||
<provider>com.objectdb.jpa.Provider</provider>
|
||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||
|
||||
<!-- Mapeo de clases -->
|
||||
<class>es.palomafp.aadd.inm.vo.Concepto</class>
|
||||
<class>es.palomafp.aadd.inm.vo.Patrocinador</class>
|
||||
<class>es.palomafp.aadd.inm.vo.Proyecto</class>
|
||||
<class>es.palomafp.aadd.inm.vo.CursoAcademico</class>
|
||||
<class>es.palomafp.aadd.inm.vo.CursoProyecto</class>
|
||||
<class>es.palomafp.aadd.inm.vo.Gasto</class>
|
||||
<class>es.palomafp.aadd.inm.vo.Ingreso</class>
|
||||
<class>org.lapaloma.aadd.redmetro.vo.Color</class>
|
||||
<class>org.lapaloma.aadd.redmetro.vo.Linea</class>
|
||||
<class>org.lapaloma.aadd.redmetro.vo.Estacion</class>
|
||||
<class>org.lapaloma.aadd.redmetro.vo.LineaEstacion</class>
|
||||
|
||||
<!-- Configuración de propiedades del SGDB (MySQL) -->
|
||||
<properties>
|
||||
<property name="jakarta.persistence.jdbc.url" value="jdbc:mysql://192.168.1.36:3306/red_metro"/>
|
||||
<property name="jakarta.persistence.jdbc.user" value="root"/>
|
||||
<property name="jakarta.persistence.jdbc.password" value="mysql_123"/>
|
||||
<property name="jakarta.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
|
||||
<!-- Configuración Hibernate -->
|
||||
<property name="hibernate.hbm2ddl.auto" value="update"/>
|
||||
<property name="hibernate.show_sql" value="true"/>
|
||||
<property name="format_sql" value="true" />
|
||||
</properties>
|
||||
|
||||
<!-- Configuración de propiedades del SGDB (ObjectDB)-->
|
||||
<properties>
|
||||
<property name="jakarta.persistence.jdbc.url" value="objectdb://localhost/proyectos_ies.odb" />
|
||||
<property name="jakarta.persistence.jdbc.user" value="admin"/>
|
||||
<property name="jakarta.persistence.jdbc.password" value="admin"/>
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
|
||||
</persistence>
|
||||
Loading…
Reference in New Issue
Block a user