Tratamiento en PostgreSQL
This commit is contained in:
parent
21e005e3a2
commit
e0f8aa1698
@ -5,6 +5,7 @@ import java.util.List;
|
||||
import org.lapaloma.aadd.redmetro.dao.ILineaDAO;
|
||||
import org.lapaloma.aadd.redmetro.dao.jpa.LineaDaoJPA;
|
||||
import org.lapaloma.aadd.redmetro.dao.mgdb.LineaDaoMongoDB;
|
||||
import org.lapaloma.aadd.redmetro.gestores.GestorEntityManagerJPA;
|
||||
import org.lapaloma.aadd.redmetro.vo.Linea;
|
||||
|
||||
/**
|
||||
@ -16,7 +17,7 @@ public class AppRedMetro {
|
||||
public static void main(String[] args) {
|
||||
AppRedMetro app = new AppRedMetro();
|
||||
// Obtiene la Session de forma estática
|
||||
// GestorEntityManagerJPA.getEntityManager();
|
||||
GestorEntityManagerJPA.getEntityManager();
|
||||
|
||||
app.migrarMongoDBAPostgreSQL();
|
||||
}
|
||||
@ -27,7 +28,6 @@ public class AppRedMetro {
|
||||
if (listaLineas != null) {
|
||||
lineaDAO = new LineaDaoJPA();
|
||||
for (Linea linea : listaLineas) {
|
||||
|
||||
// Se comprueba si existe la línea en PostgreSQL
|
||||
Linea lineaBBDD = lineaDAO.obtenerLineaPorID(linea.getCodigo());
|
||||
|
||||
|
||||
@ -1,19 +1,19 @@
|
||||
package org.lapaloma.aadd.redmetro.dao.mgdb;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.lapaloma.aadd.redmetro.dao.ILineaDAO;
|
||||
import org.lapaloma.aadd.redmetro.gestores.GestorConexionMongoDB;
|
||||
import org.lapaloma.aadd.redmetro.util.UtilidadesRedMetro;
|
||||
import org.lapaloma.aadd.redmetro.vo.Color;
|
||||
import org.lapaloma.aadd.redmetro.vo.Linea;
|
||||
|
||||
import com.mongodb.client.FindIterable;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
|
||||
import es.palomafp.aadd.inm.vo.Pais;
|
||||
|
||||
/**
|
||||
*
|
||||
* ContinenteDaoMongoDB: Implentación MongoDB de las operaciones de
|
||||
@ -23,7 +23,7 @@ import es.palomafp.aadd.inm.vo.Pais;
|
||||
* @date 30 enero 2026
|
||||
*/
|
||||
public class LineaDaoMongoDB implements ILineaDAO {
|
||||
private final static String COLECCION_PAIS = "C_LINEA";
|
||||
private final static String COLECCION_PAIS = "c_linea";
|
||||
|
||||
@Override
|
||||
public Linea obtenerLineaPorID(int identificador) {
|
||||
@ -63,23 +63,38 @@ public class LineaDaoMongoDB implements ILineaDAO {
|
||||
private Linea getLineaFromDocumentoLinea(Document documentoLinea) {
|
||||
Linea linea = null;
|
||||
|
||||
/*
|
||||
* int idMongoDB = documentoPais.getInteger("_id"); int identificador =
|
||||
* documentoPais.getInteger("identificador"); String nombrePais =
|
||||
* documentoPais.getString("nombre"); String capital =
|
||||
* documentoPais.getString("capital");
|
||||
*
|
||||
* Document documentoColor= (Document) documentoPais.get("continente"); String
|
||||
* codigoContinente = documentoContinente.getString("codigo"); String
|
||||
* nombreContinente = documentoContinente.getString("nombre");
|
||||
*
|
||||
* Color color = new Color(); color.setCodigo(codigoContinente);
|
||||
* color.setNombreContinente(nombreContinente);
|
||||
*
|
||||
* linea = new Linea(); linea.setIdentificador(identificador);
|
||||
* linea.setNombrePais(nombrePais); linea.setCapital(capital);
|
||||
* linea.setColor(color);
|
||||
*/
|
||||
int codLinea = Integer.valueOf(documentoLinea.getString("cod_linea"));
|
||||
String urlImgLinea = documentoLinea.getString("url_img_tmp");
|
||||
byte[] imagenLinea= UtilidadesRedMetro.getBytesFromURL(urlImgLinea);
|
||||
Object kilometros = documentoLinea.get("kilometros");
|
||||
if (kilometros instanceof Double) {
|
||||
kilometros = BigDecimal.valueOf((Double) kilometros);
|
||||
} else if (kilometros instanceof Integer) {
|
||||
kilometros = BigDecimal.valueOf((Integer) kilometros);
|
||||
}
|
||||
// System.out.println("kilometros: " + kilometros);
|
||||
|
||||
Document documentoNombre = (Document) documentoLinea.get("nombre");
|
||||
String nombreCorto = documentoNombre.getString("nombre_corto");
|
||||
String nombreLargo = documentoNombre.getString("nombre_largo");
|
||||
|
||||
Document documentoColor = (Document) documentoLinea.get("color");
|
||||
int codigoColor = documentoColor.getInteger("cod_color");
|
||||
String nombreColor = documentoColor.getString("nombre");
|
||||
String codHexadecimal = documentoColor.getString("cod_hexadecimal");
|
||||
|
||||
Color color = new Color();
|
||||
color.setCodigo(codigoColor);
|
||||
color.setNombre(nombreColor);
|
||||
color.setCodHexadecimal(codHexadecimal);
|
||||
|
||||
linea = new Linea();
|
||||
linea.setCodigo(codLinea);
|
||||
linea.setNombreCorto(nombreCorto);
|
||||
linea.setNombreLargo(nombreLargo);
|
||||
linea.setKilometros((BigDecimal)kilometros);
|
||||
linea.setImagenLinea(imagenLinea);
|
||||
linea.setColor(color);
|
||||
|
||||
return linea;
|
||||
}
|
||||
|
||||
@ -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_POSTGRESQL");
|
||||
} catch (Throwable ex) {
|
||||
System.err.println("Error en EntityManagerFactory: " + ex);
|
||||
throw new ExceptionInInitializerError(ex);
|
||||
|
||||
@ -0,0 +1,51 @@
|
||||
package org.lapaloma.aadd.redmetro.util;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
/**
|
||||
*
|
||||
* UtilidadesCine: Clase que realiza ....
|
||||
*
|
||||
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||
* @date 23 ene 2026
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
public class UtilidadesRedMetro {
|
||||
public static byte[] getBytesFromURL(String textoURL) {
|
||||
byte[] bytesImagen = null;
|
||||
URI url = null;
|
||||
try {
|
||||
url = new URI(textoURL);
|
||||
HttpURLConnection urlConnection = (HttpURLConnection) url.toURL().openConnection();
|
||||
urlConnection.addRequestProperty("User-Agent", "Mozilla");
|
||||
InputStream inputStream = urlConnection.getInputStream();
|
||||
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[1024];
|
||||
int len = 0;
|
||||
while ((len = inputStream.read(buffer)) != -1) {
|
||||
outStream.write(buffer, 0, len);
|
||||
}
|
||||
inputStream.close();
|
||||
bytesImagen = outStream.toByteArray();
|
||||
} catch (IOException | URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return bytesImagen;
|
||||
}
|
||||
|
||||
public static LocalDate parsearFecha(String fechaStr, String formato) {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formato);
|
||||
|
||||
LocalDate fecha = LocalDate.parse(fechaStr, formatter);
|
||||
return fecha;
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,9 @@
|
||||
package org.lapaloma.aadd.redmetro.vo;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
/**
|
||||
* Entidad que representa la tabla T_COLOR.
|
||||
@ -9,7 +12,7 @@ import jakarta.persistence.*;
|
||||
@Table(name = "T_COLOR")
|
||||
public class Color {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
// @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "cod_color")
|
||||
private int codigo;
|
||||
|
||||
|
||||
@ -1,26 +1,27 @@
|
||||
package org.lapaloma.aadd.redmetro.vo;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Arrays;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.OneToOne;
|
||||
import jakarta.persistence.Table;
|
||||
import jakarta.persistence.UniqueConstraint;
|
||||
|
||||
/**
|
||||
* Entidad que representa la tabla T_LINEA.
|
||||
* Relación OneToOne con T_COLOR (código único).
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "T_LINEA")
|
||||
@Table(name = "T_LINEA", uniqueConstraints = @UniqueConstraint(columnNames = "cod_color"))
|
||||
public class Linea {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
//@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "cod_linea")
|
||||
private int codigo;
|
||||
|
||||
@ -30,14 +31,16 @@ public class Linea {
|
||||
@Column(name = "nombre_largo", nullable = false, length = 100)
|
||||
private String nombreLargo;
|
||||
|
||||
// Relación OneToOne con T_COLOR (clave única)
|
||||
@OneToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "cod_color", nullable = false, unique = true)
|
||||
private Color color;
|
||||
|
||||
@Column(name = "kilometros", nullable = false, precision = 5, scale = 2)
|
||||
private BigDecimal kilometros;
|
||||
|
||||
// Relación OneToOne con T_COLOR (clave única)
|
||||
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
|
||||
@JoinColumn(name = "cod_color", nullable = false)
|
||||
private Color color;
|
||||
|
||||
@Column(name = "imagen_linea", columnDefinition = "BYTEA")
|
||||
private byte[] imagenLinea;
|
||||
|
||||
public int getCodigo() {
|
||||
return codigo;
|
||||
@ -79,11 +82,18 @@ public class Linea {
|
||||
this.kilometros = kilometros;
|
||||
}
|
||||
|
||||
public byte[] getImagenLinea() {
|
||||
return imagenLinea;
|
||||
}
|
||||
|
||||
public void setImagenLinea(byte[] imagenLinea) {
|
||||
this.imagenLinea = imagenLinea;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Linea [codigo=" + codigo + ", nombreCorto=" + nombreCorto + ", nombreLargo=" + nombreLargo + ", color="
|
||||
+ color + ", kilometros=" + kilometros + "]\n";
|
||||
+ color + ", kilometros=" + kilometros + ", imagenLinea=" + Arrays.toString(imagenLinea) + "]\n";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -20,7 +20,7 @@
|
||||
|
||||
<!-- Configuración de propiedades del SGDB (PostgreSQL) -->
|
||||
<properties>
|
||||
<property name="jakarta.persistence.jdbc.url" value="jdbc:postgresql://192.168.1.36:5432/Mapamundi"/>
|
||||
<property name="jakarta.persistence.jdbc.url" value="jdbc:postgresql://192.168.1.36:5432/red_metro_aadd"/>
|
||||
<property name="jakarta.persistence.jdbc.user" value="vdlp"/>
|
||||
<property name="jakarta.persistence.jdbc.password" value="postgresql_123"/>
|
||||
<property name="jakarta.persistence.jdbc.driver" value="org.postgresql.Driver"/>
|
||||
|
||||
@ -1,2 +1,2 @@
|
||||
url.conexion.mongodb=mongodb://admin:mongodb_123@172.16.0.181:27017
|
||||
bbdd.mongodb=MapaMundi
|
||||
url.conexion.mongodb=mongodb://admin:mongodb_123@192.168.1.36:27017
|
||||
bbdd.mongodb=red_metro_aadd
|
||||
Loading…
Reference in New Issue
Block a user