Adaptación para el procesamiento de URL
This commit is contained in:
parent
3c45a7e1a9
commit
dc1a09632b
@ -6,20 +6,16 @@ package org.comunidadmadrid.fp.dam2;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.comunidadmadrid.fp.dam2.dao.ICicloFormativoDAO;
|
import org.comunidadmadrid.fp.dam2.dao.ICicloFormativoDAO;
|
||||||
import org.comunidadmadrid.fp.dam2.dao.IFormacionDAO;
|
|
||||||
import org.comunidadmadrid.fp.dam2.dao.IInstitutoDAO;
|
import org.comunidadmadrid.fp.dam2.dao.IInstitutoDAO;
|
||||||
import org.comunidadmadrid.fp.dam2.dao.hibernate.CicloFormativoDaoHibernate;
|
import org.comunidadmadrid.fp.dam2.dao.hibernate.CicloFormativoDaoHibernate;
|
||||||
import org.comunidadmadrid.fp.dam2.dao.hibernate.FormacionDaoHibernate;
|
|
||||||
import org.comunidadmadrid.fp.dam2.dao.jdbc.CicloFormativoDaoJDBC;
|
import org.comunidadmadrid.fp.dam2.dao.jdbc.CicloFormativoDaoJDBC;
|
||||||
import org.comunidadmadrid.fp.dam2.dao.jpa.InstitutoDaoJPA;
|
import org.comunidadmadrid.fp.dam2.dao.jpa.InstitutoDaoJPA;
|
||||||
import org.comunidadmadrid.fp.dam2.dao.mongodb.InstitutoDaoMongoDB;
|
import org.comunidadmadrid.fp.dam2.dao.mongodb.InstitutoDaoMongoDB;
|
||||||
import org.comunidadmadrid.fp.dam2.excepcion.FormacionException;
|
import org.comunidadmadrid.fp.dam2.excepcion.FormacionException;
|
||||||
import org.comunidadmadrid.fp.dam2.gestores.GestorFicheroConfiguracion;
|
|
||||||
import org.comunidadmadrid.fp.dam2.gestores.GestorSesionHibernate;
|
import org.comunidadmadrid.fp.dam2.gestores.GestorSesionHibernate;
|
||||||
import org.comunidadmadrid.fp.dam2.procesamiento.ProcesadorDatos;
|
import org.comunidadmadrid.fp.dam2.procesamiento.ProcesadorDatos;
|
||||||
import org.comunidadmadrid.fp.dam2.utilidades.GestorURLs;
|
import org.comunidadmadrid.fp.dam2.utilidades.GestorURLs;
|
||||||
import org.comunidadmadrid.fp.dam2.vo.CicloFormativo;
|
import org.comunidadmadrid.fp.dam2.vo.CicloFormativo;
|
||||||
import org.comunidadmadrid.fp.dam2.vo.Formacion;
|
|
||||||
import org.comunidadmadrid.fp.dam2.vo.Instituto;
|
import org.comunidadmadrid.fp.dam2.vo.Instituto;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -110,22 +106,9 @@ public class ClasePrincipal {
|
|||||||
* @throws FormacionException
|
* @throws FormacionException
|
||||||
*/
|
*/
|
||||||
private void tratarFormaciones() throws FormacionException {
|
private void tratarFormaciones() throws FormacionException {
|
||||||
|
|
||||||
// Se obtiene la lista de Formaciones a partir de una URL
|
|
||||||
String urlFormaciones = GestorFicheroConfiguracion.obtenerValor("url.formaciones");
|
|
||||||
ProcesadorDatos procesador = new ProcesadorDatos();
|
ProcesadorDatos procesador = new ProcesadorDatos();
|
||||||
List<Formacion> listaFormaciones = procesador.obtenerFormacionesDeURL(urlFormaciones);
|
procesador.procesarFormacionesDeURL();
|
||||||
|
|
||||||
if (listaFormaciones != null && listaFormaciones.size() > 0) {
|
|
||||||
// Se prepara el AADD de Formación para que se realice mediante Hibernate
|
|
||||||
IFormacionDAO iFormacionDAO = new FormacionDaoHibernate();
|
|
||||||
|
|
||||||
for (Formacion formacion : listaFormaciones) {
|
|
||||||
|
|
||||||
System.out.println(formacion);
|
|
||||||
iFormacionDAO.insertarFormacion(formacion);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -167,7 +150,7 @@ public class ClasePrincipal {
|
|||||||
private void tratarInstitutoFormacion() throws FormacionException {
|
private void tratarInstitutoFormacion() throws FormacionException {
|
||||||
|
|
||||||
ProcesadorDatos procesador = new ProcesadorDatos();
|
ProcesadorDatos procesador = new ProcesadorDatos();
|
||||||
procesador.crearFormacionesInstitutoCSV();
|
procesador.procesarFormacionesInstitutoCSV();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,7 +5,6 @@ package org.comunidadmadrid.fp.dam2.dao;
|
|||||||
|
|
||||||
import org.comunidadmadrid.fp.dam2.excepcion.FormacionException;
|
import org.comunidadmadrid.fp.dam2.excepcion.FormacionException;
|
||||||
import org.comunidadmadrid.fp.dam2.vo.Formacion;
|
import org.comunidadmadrid.fp.dam2.vo.Formacion;
|
||||||
import org.comunidadmadrid.fp.dam2.vo.Instituto;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Organización: Virgen de la Paloma
|
Organización: Virgen de la Paloma
|
||||||
@ -26,4 +25,11 @@ public interface IFormacionDAO {
|
|||||||
* @throws FormacionException
|
* @throws FormacionException
|
||||||
*/
|
*/
|
||||||
void insertarFormacion(Formacion formacion) throws FormacionException;
|
void insertarFormacion(Formacion formacion) throws FormacionException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param formacion
|
||||||
|
* @throws FormacionException
|
||||||
|
*/
|
||||||
|
void actualizarFormacion(Formacion formacion) throws FormacionException;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -55,4 +55,30 @@ public class FormacionDaoHibernate implements IFormacionDAO{
|
|||||||
}
|
}
|
||||||
return formacion;
|
return formacion;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actualizarFormacion(Formacion formacion) throws FormacionException {
|
||||||
|
Transaction transaccion = null;
|
||||||
|
Session sesion = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
sesion = GestorSesionHibernate.getSession();
|
||||||
|
transaccion = sesion.beginTransaction();
|
||||||
|
|
||||||
|
if (!sesion.contains(formacion))
|
||||||
|
sesion.merge(formacion);
|
||||||
|
|
||||||
|
transaccion.commit();
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (transaccion != null && transaccion.isActive()) {
|
||||||
|
transaccion.rollback();
|
||||||
|
}
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
if (sesion != null) {
|
||||||
|
sesion.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,8 +6,6 @@ package org.comunidadmadrid.fp.dam2.procesamiento;
|
|||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.comunidadmadrid.fp.dam2.dao.IFormacionDAO;
|
import org.comunidadmadrid.fp.dam2.dao.IFormacionDAO;
|
||||||
import org.comunidadmadrid.fp.dam2.dao.IInstitutoDAO;
|
import org.comunidadmadrid.fp.dam2.dao.IInstitutoDAO;
|
||||||
@ -26,6 +24,7 @@ import org.comunidadmadrid.fp.dam2.vo.InstitutoFormacion.InstitutoFormacionID;
|
|||||||
|
|
||||||
import tools.jackson.databind.JsonNode;
|
import tools.jackson.databind.JsonNode;
|
||||||
import tools.jackson.databind.ObjectMapper;
|
import tools.jackson.databind.ObjectMapper;
|
||||||
|
import tools.jackson.databind.node.ArrayNode;
|
||||||
import tools.jackson.dataformat.xml.XmlMapper;
|
import tools.jackson.dataformat.xml.XmlMapper;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -34,8 +33,8 @@ Programador: Isidoro Nevares Martín
|
|||||||
Fecha: 1 mar 2025
|
Fecha: 1 mar 2025
|
||||||
*/
|
*/
|
||||||
public class ProcesadorDatos {
|
public class ProcesadorDatos {
|
||||||
public List<Formacion> obtenerFormacionesDeURL(String urlFormaciones) {
|
public void procesarFormacionesDeURL() {
|
||||||
List<Formacion> listaFormaciones = null;
|
String urlFormaciones = GestorFicheroConfiguracion.obtenerValor("url.formaciones");
|
||||||
|
|
||||||
String informacionFormacionesXML = GestorURLs.getTextoFromURL(urlFormaciones);
|
String informacionFormacionesXML = GestorURLs.getTextoFromURL(urlFormaciones);
|
||||||
ObjectMapper mapeadorXML = new XmlMapper();
|
ObjectMapper mapeadorXML = new XmlMapper();
|
||||||
@ -45,28 +44,27 @@ public class ProcesadorDatos {
|
|||||||
JsonNode nodoRaiz = mapeadorXML.readTree(informacionFormacionesXML);
|
JsonNode nodoRaiz = mapeadorXML.readTree(informacionFormacionesXML);
|
||||||
|
|
||||||
if (nodoRaiz != null) {
|
if (nodoRaiz != null) {
|
||||||
listaFormaciones = new ArrayList<Formacion>();
|
|
||||||
|
|
||||||
JsonNode elementos = nodoRaiz.has("formacion") ? nodoRaiz.get("formacion") : nodoRaiz;
|
JsonNode nodosFormacion = nodoRaiz.has("formacion") ? nodoRaiz.get("formacion") : nodoRaiz;
|
||||||
|
|
||||||
if (elementos.isArray()) {
|
IFormacionDAO iFormacionDAO = new FormacionDaoHibernate();
|
||||||
for (JsonNode nodo : elementos) {
|
JsonNode arrayNodosFormacion = convertirJsonNodeEnArrayNode(nodosFormacion);
|
||||||
Formacion formacion = obtenerFormacionDeJsonNode(nodo);
|
for (JsonNode nodoFormacion : arrayNodosFormacion) {
|
||||||
if (formacion != null)
|
Formacion formacion = obtenerFormacionDeJsonNode(nodoFormacion);
|
||||||
listaFormaciones.add(formacion);
|
if (formacion != null) {
|
||||||
|
Formacion formacionBBDD = iFormacionDAO.obtenerFormacionPorID(formacion.getIdentificador());
|
||||||
|
if (formacionBBDD == null) {
|
||||||
|
iFormacionDAO.insertarFormacion(formacion);
|
||||||
|
} else {
|
||||||
|
iFormacionDAO.actualizarFormacion(formacion);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (!elementos.isMissingNode() && !elementos.isNull()) {
|
|
||||||
Formacion formacion = obtenerFormacionDeJsonNode(elementos);
|
|
||||||
if (formacion != null)
|
|
||||||
listaFormaciones.add(formacion);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println("Error procesando XML con JsonNode: " + e.getMessage());
|
System.err.println("Error procesando XML con JsonNode: " + e.getMessage());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
return listaFormaciones;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -75,7 +73,7 @@ public class ProcesadorDatos {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public void crearFormacionesInstitutoCSV() throws FormacionException {
|
public void procesarFormacionesInstitutoCSV() throws FormacionException {
|
||||||
String rutaFicheroCSV = GestorFicheroConfiguracion.obtenerValor("ruta.csv");
|
String rutaFicheroCSV = GestorFicheroConfiguracion.obtenerValor("ruta.csv");
|
||||||
|
|
||||||
try (BufferedReader br = new BufferedReader(new FileReader(rutaFicheroCSV))) {
|
try (BufferedReader br = new BufferedReader(new FileReader(rutaFicheroCSV))) {
|
||||||
@ -162,4 +160,16 @@ public class ProcesadorDatos {
|
|||||||
return formacion;
|
return formacion;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ArrayNode convertirJsonNodeEnArrayNode(JsonNode node) {
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
if (node.isArray()) {
|
||||||
|
return (ArrayNode) node;
|
||||||
|
} else {
|
||||||
|
ArrayNode arrayNode = mapper.createArrayNode();
|
||||||
|
if (!node.isMissingNode()) {
|
||||||
|
arrayNode.add(node);
|
||||||
|
}
|
||||||
|
return arrayNode;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user