commit 7563c2f8e750c0296d194d61734c6e1143af10a9 Author: Isidoro Nevares Martín Date: Mon Nov 10 09:54:38 2025 +0100 primer commit diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..89051b0 --- /dev/null +++ b/.classpath @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f1d3ca --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target/ +*.class \ No newline at end of file diff --git a/.project b/.project new file mode 100644 index 0000000..51e1a44 --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + proyectos_lapaloma + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/.settings/org.eclipse.jdt.apt.core.prefs b/.settings/org.eclipse.jdt.apt.core.prefs new file mode 100644 index 0000000..d4313d4 --- /dev/null +++ b/.settings/org.eclipse.jdt.apt.core.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.apt.aptEnabled=false diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..56f4efb --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=22 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=22 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.processAnnotations=disabled +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=22 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..e29f308 --- /dev/null +++ b/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + ies.lapaloma.proyectos + ies_lapaloma_proyectos + 1.0.1 + Acceso_Datos_Proyecto_IES_La_Paloma + + + + + + com.mysql + mysql-connector-j + 9.2.0 + + + + + + org.hibernate.orm + hibernate-core + 6.6.3.Final + + + + + org.projectlombok + lombok + 1.18.36 + provided + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 + + + + org.projectlombok + lombok + 1.18.36 + + + 23 + 23 + + + + + + diff --git a/src/main/java/org/palomafp/dam2/aadd/cgf/ClasePrincipal.java b/src/main/java/org/palomafp/dam2/aadd/cgf/ClasePrincipal.java new file mode 100644 index 0000000..268ac7f --- /dev/null +++ b/src/main/java/org/palomafp/dam2/aadd/cgf/ClasePrincipal.java @@ -0,0 +1,91 @@ +package org.palomafp.dam2.aadd.cgf; + +public class ClasePrincipal { + + public static void main(String[] args) { +/* + IOperacionesCRUDEntidad implCursoAcademico = new CursoAcademicoDaoJPA(); + CursoAcademico curso= implCursoAcademico.obtenerEntidadPorID("2022-2023"); + System.out.println(curso); + System.out.println("\n"); + + + IOperacionesCRUDEntidad implProyecto = new ProyectoDaoJPA(); + Proyecto proyecto = implProyecto.obtenerEntidadPorNombre("Speedster"); + System.out.println(proyecto); + System.out.println("\n"); + + + IOperacionesCRUDMasConsultasEntidad implPatrocinador = new PatrocinadorDaoJPA(); + System.out.println("Total pratrocinadores: " + implPatrocinador.obtenerListaEntidades().size()); + System.out.println("\n"); + + System.out.println("Total pratrocinadores del curso " + curso.getCodigo() + ": " + implPatrocinador.obtenerListaEntidadesPorCursoAcademico(curso).size()); + System.out.println("\n"); + + System.out.println("Total pratrocinadores del proyecto " + proyecto.getNombre() + ": " + implPatrocinador.obtenerListaEntidadesPorProyecto(proyecto).size()); + System.out.println("\n"); + + System.out.println("Total pratrocinadores del proyecto/cruso " + proyecto.getNombre() + "/" + curso.getCodigo() + ": " + implPatrocinador.obtenerListaEntidadesPorProyectoCurso(proyecto, curso).size()); + System.out.println("\n"); + + + + IOperacionesCRUDMasConsultasEntidad implConcepto = new ConceptoDaoJPA(); + + System.out.println("Total Conceptos: " + implConcepto.obtenerListaEntidades().size()); + System.out.println("\n"); + + System.out.println("Total Conceptos del curso " + curso.getCodigo() + ": " + implConcepto.obtenerListaEntidadesPorCursoAcademico(curso).size()); + System.out.println("\n"); + + System.out.println("Total Conceptos del proyecto " + proyecto.getNombre() + ": " + implConcepto.obtenerListaEntidadesPorProyecto(proyecto).size()); + System.out.println("\n"); + + System.out.println("Total Conceptos del proyecto/cruso " + proyecto.getNombre() + "/" + curso.getCodigo() + ": " + implConcepto.obtenerListaEntidadesPorProyectoCurso(proyecto, curso).size()); + System.out.println("\n"); + + + + + IOperacionesCRUDMasConsultasEntidad implGasto = new GastoDaoJPA(); + + System.out.println("Total Gastos: " + implGasto.obtenerListaEntidades().size()); + System.out.println("\n"); + + System.out.println("Total Gastos del curso " + curso.getCodigo() + ": " + implGasto.obtenerListaEntidadesPorCursoAcademico(curso).size()); + System.out.println("\n"); + + System.out.println("Total Gastos del proyecto " + proyecto.getNombre() + ": " + implGasto.obtenerListaEntidadesPorProyecto(proyecto).size()); + System.out.println("\n"); + + System.out.println("Total Gastos del proyecto/cruso " + proyecto.getNombre() + "/" + curso.getCodigo() + ": " + implGasto.obtenerListaEntidadesPorProyectoCurso(proyecto, curso).size()); + System.out.println("\n"); + + + IOperacionesCRUDMasConsultasEntidad implIngreso = new IngresoDaoJPA(); + System.out.println("Total Ingresos: " + implIngreso.obtenerListaEntidades().size()); + System.out.println("\n"); + + System.out.println("Total Ingresos del curso " + curso.getCodigo() + ": " + implIngreso.obtenerListaEntidadesPorCursoAcademico(curso).size()); + System.out.println("\n"); + + System.out.println("Total Ingresos del proyecto " + proyecto.getNombre() + ": " + implIngreso.obtenerListaEntidadesPorProyecto(proyecto).size()); + System.out.println("\n"); + + System.out.println("Total Ingresos del proyecto/cruso " + proyecto.getNombre() + "/" + curso.getCodigo() + ": " + implIngreso.obtenerListaEntidadesPorProyectoCurso(proyecto, curso).size()); + System.out.println("\n"); + + + IOperacionesCRUDMasConsultasCursoAcademico impProyecto = new ProyectoDaoJPA(); + System.out.println("Total Proyectos: " + impProyecto.obtenerListaEntidades().size()); + System.out.println("\n"); + + System.out.println("Total Proyectos del curso " + curso.getCodigo() + ": " + impProyecto.obtenerListaEntidadesPorCursoAcademico(curso).size()); + System.out.println("\n"); +*/ + + } + +} + diff --git a/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/IOperacionesCRUDEntidad.java b/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/IOperacionesCRUDEntidad.java new file mode 100644 index 0000000..563ee6d --- /dev/null +++ b/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/IOperacionesCRUDEntidad.java @@ -0,0 +1,16 @@ +package org.palomafp.dam2.aadd.proyectos.dao; + +import java.util.List; + +import org.palomafp.dam2.aadd.proyectos.exceptions.ProyectosIESException; + +public interface IOperacionesCRUDEntidad { + + T obtenerEntidadPorID(K claveUnica) throws ProyectosIESException; + List obtenerListaEntidades() throws ProyectosIESException; + void insertarEntidad(T entidad) throws ProyectosIESException; + void eliminarEntidad(T entidad) throws ProyectosIESException; + void actualizarEntidad(T entidad) throws ProyectosIESException; + T obtenerEntidadPorNombre(String nombre) throws ProyectosIESException; + +} diff --git a/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/IOperacionesCRUDMasConsultasCursoAcademico.java b/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/IOperacionesCRUDMasConsultasCursoAcademico.java new file mode 100644 index 0000000..1a71f3e --- /dev/null +++ b/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/IOperacionesCRUDMasConsultasCursoAcademico.java @@ -0,0 +1,12 @@ +package org.palomafp.dam2.aadd.proyectos.dao; + +import java.util.List; + +import org.palomafp.dam2.aadd.proyectos.exceptions.ProyectosIESException; +import org.palomafp.dam2.aadd.proyectos.vo.CursoAcademico; + +public interface IOperacionesCRUDMasConsultasCursoAcademico extends IOperacionesCRUDEntidad{ + + List obtenerListaEntidadesPorCursoAcademico(CursoAcademico cursoAcademico) throws ProyectosIESException; + +} diff --git a/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/IOperacionesCRUDMasConsultasEntidad.java b/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/IOperacionesCRUDMasConsultasEntidad.java new file mode 100644 index 0000000..c36b02e --- /dev/null +++ b/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/IOperacionesCRUDMasConsultasEntidad.java @@ -0,0 +1,14 @@ +package org.palomafp.dam2.aadd.proyectos.dao; + +import java.util.List; + +import org.palomafp.dam2.aadd.proyectos.exceptions.ProyectosIESException; +import org.palomafp.dam2.aadd.proyectos.vo.CursoAcademico; +import org.palomafp.dam2.aadd.proyectos.vo.Proyecto; + +public interface IOperacionesCRUDMasConsultasEntidad extends IOperacionesCRUDMasConsultasCursoAcademico{ + + List obtenerListaEntidadesPorProyecto(Proyecto proyecto) throws ProyectosIESException; + List obtenerListaEntidadesPorProyectoCurso(Proyecto proyecto, CursoAcademico cursoAcademico) throws ProyectosIESException; + +} diff --git a/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/impl/ConceptoDaoJPA.java b/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/impl/ConceptoDaoJPA.java new file mode 100644 index 0000000..02229d4 --- /dev/null +++ b/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/impl/ConceptoDaoJPA.java @@ -0,0 +1,262 @@ +package org.palomafp.dam2.aadd.proyectos.dao.impl; + +import java.util.List; + +import org.palomafp.dam2.aadd.proyectos.dao.IOperacionesCRUDMasConsultasEntidad; +import org.palomafp.dam2.aadd.proyectos.exceptions.ProyectosIESException; +import org.palomafp.dam2.aadd.proyectos.util.GestorConexionJPA; +import org.palomafp.dam2.aadd.proyectos.vo.Concepto; +import org.palomafp.dam2.aadd.proyectos.vo.CursoAcademico; +import org.palomafp.dam2.aadd.proyectos.vo.Proyecto; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.TypedQuery; + +public class ConceptoDaoJPA implements IOperacionesCRUDMasConsultasEntidad{ + + + @Override + public Concepto obtenerEntidadPorNombre(String nombre) throws ProyectosIESException{ + Concepto concepto =null; + EntityManager entityManager = null; + + try { + entityManager = GestorConexionJPA.getEntityManager(); + TypedQuery query = entityManager.createQuery("SELECT c FROM Concepto c WHERE c.nombre = :nombre", Concepto.class); + query.setParameter("nombre", nombre); + concepto=query.getSingleResult(); + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Concepto.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + return concepto; + } + + @Override + public Concepto obtenerEntidadPorID(Integer identificador) throws ProyectosIESException{ + Concepto concepto =null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + // Consultar por ID + concepto= entityManager.find(Concepto.class, identificador); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Concepto.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + return concepto; + } + + @Override + public List obtenerListaEntidades() throws ProyectosIESException{ + List listaConceptos=null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + listaConceptos= entityManager.createQuery("SELECT c FROM Concepto c", Concepto.class).getResultList(); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Concepto.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + + return listaConceptos; + } + + @Override + public void insertarEntidad(Concepto entidad) throws ProyectosIESException{ + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + entityManager.getTransaction().begin(); + + // Insertar + entityManager.persist(entidad); + + entityManager.getTransaction().commit(); + + } catch (Exception e) { + if (entityManager.getTransaction() != null && entityManager.getTransaction().isActive()) { + entityManager.getTransaction().rollback(); + } + + throw new ProyectosIESException(e, ProyectosIESException.ERROR_CREACION, Concepto.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + } + + @Override + public void eliminarEntidad(Concepto entidad) throws ProyectosIESException{ + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + entityManager.getTransaction().begin(); + + // Si no existe la entidad en el ámbito del "Entity Manager" lo incluye (para así poder borrarlo) + if (!entityManager.contains(entidad)) + entidad = entityManager.merge(entidad); + + entityManager.remove(entidad); + + entityManager.getTransaction().commit(); + + } catch (Exception e) { + if (entityManager.getTransaction() != null && entityManager.getTransaction().isActive()) { + entityManager.getTransaction().rollback(); + } + throw new ProyectosIESException(e, ProyectosIESException.ERROR_ELIMINACION, Concepto.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + } + + @Override + public void actualizarEntidad(Concepto entidad) throws ProyectosIESException{ + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + entityManager.getTransaction().begin(); + + // Actualizar + // Para poder actulizarse la entidad, ésta ha de encontarse en el ámbito del entityManager + if (!entityManager.contains(entidad)) + entidad=entityManager.merge(entidad); + + entityManager.getTransaction().commit(); + + } catch (Exception e) { + if (entityManager.getTransaction() != null && entityManager.getTransaction().isActive()) { + entityManager.getTransaction().rollback(); + } + + throw new ProyectosIESException(e, ProyectosIESException.ERROR_ACTUALIZACION, Concepto.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + } + + @Override + public List obtenerListaEntidadesPorCursoAcademico(CursoAcademico cursoAcademico) throws ProyectosIESException { + List listaConceptos=null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + String sentenciaJPQL = """ + SELECT c + FROM ProyectoCurso pc + INNER JOIN Gasto g ON g.proyectoCurso = pc + INNER JOIN g.concepto c + WHERE pc.curso.codigo = :codigoCurso + """; + TypedQuery query = entityManager.createQuery(sentenciaJPQL, Concepto.class); + query.setParameter("codigoCurso", cursoAcademico.getCodigo()); + + listaConceptos=query.getResultList(); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Concepto.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + + return listaConceptos; + } + + @Override + public List obtenerListaEntidadesPorProyecto(Proyecto proyecto) throws ProyectosIESException { + List listaConceptos=null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + String sentenciaJPQL = """ + SELECT c + FROM ProyectoCurso pc + INNER JOIN Gasto g ON g.proyectoCurso = pc + INNER JOIN g.concepto c + WHERE pc.proyecto.identificador= :idProyecto + """; + TypedQuery query = entityManager.createQuery(sentenciaJPQL, Concepto.class); + query.setParameter("idProyecto", proyecto.getIdentificador()); + + listaConceptos=query.getResultList(); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Concepto.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + + return listaConceptos; + } + + @Override + public List obtenerListaEntidadesPorProyectoCurso(Proyecto proyecto, CursoAcademico cursoAcademico) + throws ProyectosIESException { + List listaConceptos=null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + String sentenciaJPQL = """ + SELECT c + FROM ProyectoCurso pc + INNER JOIN Gasto g ON g.proyectoCurso = pc + INNER JOIN g.concepto c + WHERE pc.proyecto.identificador= :idProyecto + and pc.curso.codigo = :codigoCurso + """; + TypedQuery query = entityManager.createQuery(sentenciaJPQL, Concepto.class); + query.setParameter("idProyecto", proyecto.getIdentificador()); + query.setParameter("codigoCurso", cursoAcademico.getCodigo()); + + listaConceptos=query.getResultList(); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Concepto.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + + return listaConceptos; + } + + +} diff --git a/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/impl/CursoAcademicoDaoJPA.java b/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/impl/CursoAcademicoDaoJPA.java new file mode 100644 index 0000000..65d0f1d --- /dev/null +++ b/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/impl/CursoAcademicoDaoJPA.java @@ -0,0 +1,162 @@ +package org.palomafp.dam2.aadd.proyectos.dao.impl; + +import java.util.List; + +import org.palomafp.dam2.aadd.proyectos.dao.IOperacionesCRUDEntidad; +import org.palomafp.dam2.aadd.proyectos.exceptions.ProyectosIESException; +import org.palomafp.dam2.aadd.proyectos.util.GestorConexionJPA; +import org.palomafp.dam2.aadd.proyectos.vo.CursoAcademico; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.TypedQuery; + +public class CursoAcademicoDaoJPA implements IOperacionesCRUDEntidad{ + + @Override + public CursoAcademico obtenerEntidadPorNombre(String nombre) throws ProyectosIESException{ + CursoAcademico cursoAcademico =null; + EntityManager entityManager = null; + + try { + entityManager = GestorConexionJPA.getEntityManager(); + TypedQuery query = entityManager.createQuery("SELECT c FROM CursoAcademico c WHERE c.nombre = :nombre", CursoAcademico.class); + query.setParameter("nombre", nombre); + cursoAcademico=query.getSingleResult(); + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, CursoAcademico.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + return cursoAcademico; + } + + @Override + public CursoAcademico obtenerEntidadPorID(String codigo) throws ProyectosIESException{ + CursoAcademico cursoAcademico =null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + // Consultar por ID + cursoAcademico= entityManager.find(CursoAcademico.class, codigo); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, CursoAcademico.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + return cursoAcademico; + } + + + @Override + public List obtenerListaEntidades() throws ProyectosIESException{ + List listaCursosAcademicos=null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + listaCursosAcademicos= entityManager.createQuery("SELECT c FROM CursoAcademico c", CursoAcademico.class).getResultList(); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, CursoAcademico.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + + return listaCursosAcademicos; + } + + @Override + public void insertarEntidad(CursoAcademico entidad) throws ProyectosIESException{ + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + entityManager.getTransaction().begin(); + + // Insertar + entityManager.persist(entidad); + + entityManager.getTransaction().commit(); + + } catch (Exception e) { + if (entityManager.getTransaction() != null && entityManager.getTransaction().isActive()) { + entityManager.getTransaction().rollback(); + } + + throw new ProyectosIESException(e, ProyectosIESException.ERROR_CREACION, CursoAcademico.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + } + + @Override + public void eliminarEntidad(CursoAcademico entidad) throws ProyectosIESException{ + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + entityManager.getTransaction().begin(); + + // Si no existe la entidad en el ámbito del "Entity Manager" lo incluye (para así poder borrarlo) + if (!entityManager.contains(entidad)) + entidad = entityManager.merge(entidad); + + entityManager.remove(entidad); + + entityManager.getTransaction().commit(); + + } catch (Exception e) { + if (entityManager.getTransaction() != null && entityManager.getTransaction().isActive()) { + entityManager.getTransaction().rollback(); + } + throw new ProyectosIESException(e, ProyectosIESException.ERROR_ELIMINACION, CursoAcademico.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + } + + @Override + public void actualizarEntidad(CursoAcademico entidad) throws ProyectosIESException{ + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + entityManager.getTransaction().begin(); + + // Actualizar + // Para poder actulizarse la entidad, ésta ha de encontarse en el ámbito del entityManager + if (!entityManager.contains(entidad)) + entidad=entityManager.merge(entidad); + + entityManager.getTransaction().commit(); + + } catch (Exception e) { + if (entityManager.getTransaction() != null && entityManager.getTransaction().isActive()) { + entityManager.getTransaction().rollback(); + } + + throw new ProyectosIESException(e, ProyectosIESException.ERROR_ACTUALIZACION, CursoAcademico.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + } +} diff --git a/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/impl/GastoDaoJPA.java b/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/impl/GastoDaoJPA.java new file mode 100644 index 0000000..1f34f81 --- /dev/null +++ b/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/impl/GastoDaoJPA.java @@ -0,0 +1,246 @@ +package org.palomafp.dam2.aadd.proyectos.dao.impl; + +import java.util.List; + +import org.palomafp.dam2.aadd.proyectos.dao.IOperacionesCRUDMasConsultasEntidad; +import org.palomafp.dam2.aadd.proyectos.exceptions.ProyectosIESException; +import org.palomafp.dam2.aadd.proyectos.util.GestorConexionJPA; +import org.palomafp.dam2.aadd.proyectos.vo.CursoAcademico; +import org.palomafp.dam2.aadd.proyectos.vo.Gasto; +import org.palomafp.dam2.aadd.proyectos.vo.Proyecto; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.TypedQuery; + +public class GastoDaoJPA implements IOperacionesCRUDMasConsultasEntidad{ + + @Override + public Gasto obtenerEntidadPorNombre(String nombre) throws ProyectosIESException{ + Gasto gasto =null; + + // No se "implementa" + + return gasto; + } + + @Override + public Gasto obtenerEntidadPorID(Integer identificador) throws ProyectosIESException{ + Gasto gasto =null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + // Consultar por ID + gasto= entityManager.find(Gasto.class, identificador); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Gasto.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + return gasto; + } + + @Override + public List obtenerListaEntidades() throws ProyectosIESException{ + List listaGastos=null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + listaGastos= entityManager.createQuery("SELECT g FROM Gasto g", Gasto.class).getResultList(); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Gasto.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + + return listaGastos; + } + + @Override + public void insertarEntidad(Gasto entidad) throws ProyectosIESException{ + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + entityManager.getTransaction().begin(); + + // Insertar + entityManager.persist(entidad); + + entityManager.getTransaction().commit(); + + } catch (Exception e) { + if (entityManager.getTransaction() != null && entityManager.getTransaction().isActive()) { + entityManager.getTransaction().rollback(); + } + + throw new ProyectosIESException(e, ProyectosIESException.ERROR_CREACION, Gasto.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + } + + @Override + public void eliminarEntidad(Gasto entidad) throws ProyectosIESException{ + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + entityManager.getTransaction().begin(); + + // Si no existe la entidad en el ámbito del "Entity Manager" lo incluye (para así poder borrarlo) + if (!entityManager.contains(entidad)) + entidad = entityManager.merge(entidad); + + entityManager.remove(entidad); + + entityManager.getTransaction().commit(); + + } catch (Exception e) { + if (entityManager.getTransaction() != null && entityManager.getTransaction().isActive()) { + entityManager.getTransaction().rollback(); + } + throw new ProyectosIESException(e, ProyectosIESException.ERROR_ELIMINACION, Gasto.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + } + + @Override + public void actualizarEntidad(Gasto entidad) throws ProyectosIESException{ + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + entityManager.getTransaction().begin(); + + // Actualizar + // Para poder actulizarse la entidad, ésta ha de encontarse en el ámbito del entityManager + if (!entityManager.contains(entidad)) + entidad=entityManager.merge(entidad); + + entityManager.getTransaction().commit(); + + } catch (Exception e) { + if (entityManager.getTransaction() != null && entityManager.getTransaction().isActive()) { + entityManager.getTransaction().rollback(); + } + + throw new ProyectosIESException(e, ProyectosIESException.ERROR_ACTUALIZACION, Gasto.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + } + + @Override + public List obtenerListaEntidadesPorCursoAcademico(CursoAcademico cursoAcademico) throws ProyectosIESException { + List listaGastos=null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + String sentenciaJPQL = """ + SELECT g + FROM ProyectoCurso pc + INNER JOIN Gasto g ON g.proyectoCurso = pc + WHERE pc.curso.codigo = :codigoCurso + """; + TypedQuery query = entityManager.createQuery(sentenciaJPQL, Gasto.class); + query.setParameter("codigoCurso", cursoAcademico.getCodigo()); + + listaGastos=query.getResultList(); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Gasto.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + + return listaGastos; + } + + @Override + public List obtenerListaEntidadesPorProyecto(Proyecto proyecto) throws ProyectosIESException { + List listaGastos=null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + String sentenciaJPQL = """ + SELECT g + FROM ProyectoCurso pc + INNER JOIN Gasto g ON g.proyectoCurso = pc + WHERE pc.proyecto.identificador= :idProyecto + """; + TypedQuery query = entityManager.createQuery(sentenciaJPQL, Gasto.class); + query.setParameter("idProyecto", proyecto.getIdentificador()); + + listaGastos=query.getResultList(); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Gasto.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + + return listaGastos; + } + + @Override + public List obtenerListaEntidadesPorProyectoCurso(Proyecto proyecto, CursoAcademico cursoAcademico) + throws ProyectosIESException { + List listaGastos=null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + String sentenciaJPQL = """ + SELECT g + FROM ProyectoCurso pc + INNER JOIN Gasto g ON g.proyectoCurso = pc + WHERE pc.proyecto.identificador= :idProyecto + and pc.curso.codigo = :codigoCurso + """; + TypedQuery query = entityManager.createQuery(sentenciaJPQL, Gasto.class); + query.setParameter("idProyecto", proyecto.getIdentificador()); + query.setParameter("codigoCurso", cursoAcademico.getCodigo()); + + listaGastos=query.getResultList(); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Gasto.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + + return listaGastos; + } + +} diff --git a/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/impl/IngresoDaoJPA.java b/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/impl/IngresoDaoJPA.java new file mode 100644 index 0000000..b763ce1 --- /dev/null +++ b/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/impl/IngresoDaoJPA.java @@ -0,0 +1,246 @@ +package org.palomafp.dam2.aadd.proyectos.dao.impl; + +import java.util.List; + +import org.palomafp.dam2.aadd.proyectos.dao.IOperacionesCRUDMasConsultasEntidad; +import org.palomafp.dam2.aadd.proyectos.exceptions.ProyectosIESException; +import org.palomafp.dam2.aadd.proyectos.util.GestorConexionJPA; +import org.palomafp.dam2.aadd.proyectos.vo.CursoAcademico; +import org.palomafp.dam2.aadd.proyectos.vo.Ingreso; +import org.palomafp.dam2.aadd.proyectos.vo.Proyecto; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.TypedQuery; + +public class IngresoDaoJPA implements IOperacionesCRUDMasConsultasEntidad{ + + @Override + public Ingreso obtenerEntidadPorNombre(String nombre) throws ProyectosIESException{ + Ingreso ingreso =null; + + // No se "implementa" + + return ingreso; + } + + @Override + public Ingreso obtenerEntidadPorID(Integer identificador) throws ProyectosIESException{ + Ingreso ingreso =null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + // Consultar por ID + ingreso= entityManager.find(Ingreso.class, identificador); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Ingreso.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + return ingreso; + } + + @Override + public List obtenerListaEntidades() throws ProyectosIESException{ + List listaIngresos=null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + listaIngresos= entityManager.createQuery("SELECT i FROM Ingreso i", Ingreso.class).getResultList(); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Ingreso.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + + return listaIngresos; + } + + @Override + public void insertarEntidad(Ingreso entidad) throws ProyectosIESException{ + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + entityManager.getTransaction().begin(); + + // Insertar + entityManager.persist(entidad); + + entityManager.getTransaction().commit(); + + } catch (Exception e) { + if (entityManager.getTransaction() != null && entityManager.getTransaction().isActive()) { + entityManager.getTransaction().rollback(); + } + + throw new ProyectosIESException(e, ProyectosIESException.ERROR_CREACION, Ingreso.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + } + + @Override + public void eliminarEntidad(Ingreso entidad) throws ProyectosIESException{ + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + entityManager.getTransaction().begin(); + + // Si no existe la entidad en el ámbito del "Entity Manager" lo incluye (para así poder borrarlo) + if (!entityManager.contains(entidad)) + entidad = entityManager.merge(entidad); + + entityManager.remove(entidad); + + entityManager.getTransaction().commit(); + + } catch (Exception e) { + if (entityManager.getTransaction() != null && entityManager.getTransaction().isActive()) { + entityManager.getTransaction().rollback(); + } + throw new ProyectosIESException(e, ProyectosIESException.ERROR_ELIMINACION, Ingreso.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + } + + @Override + public void actualizarEntidad(Ingreso entidad) throws ProyectosIESException{ + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + entityManager.getTransaction().begin(); + + // Actualizar + // Para poder actulizarse la entidad, ésta ha de encontarse en el ámbito del entityManager + if (!entityManager.contains(entidad)) + entidad=entityManager.merge(entidad); + + entityManager.getTransaction().commit(); + + } catch (Exception e) { + if (entityManager.getTransaction() != null && entityManager.getTransaction().isActive()) { + entityManager.getTransaction().rollback(); + } + + throw new ProyectosIESException(e, ProyectosIESException.ERROR_ACTUALIZACION, Ingreso.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + } + + @Override + public List obtenerListaEntidadesPorCursoAcademico(CursoAcademico cursoAcademico) throws ProyectosIESException { + List listaIngresos=null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + String sentenciaJPQL = """ + SELECT i + FROM ProyectoCurso pc + INNER JOIN Ingreso i ON i.proyectoCurso = pc + WHERE pc.curso.codigo = :codigoCurso + """; + TypedQuery query = entityManager.createQuery(sentenciaJPQL, Ingreso.class); + query.setParameter("codigoCurso", cursoAcademico.getCodigo()); + + listaIngresos=query.getResultList(); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Ingreso.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + + return listaIngresos; + } + + @Override + public List obtenerListaEntidadesPorProyecto(Proyecto proyecto) throws ProyectosIESException { + List listaIngresos=null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + String sentenciaJPQL = """ + SELECT i + FROM ProyectoCurso pc + INNER JOIN Ingreso i ON i.proyectoCurso = pc + WHERE pc.proyecto.identificador= :idProyecto + """; + TypedQuery query = entityManager.createQuery(sentenciaJPQL, Ingreso.class); + query.setParameter("idProyecto", proyecto.getIdentificador()); + + listaIngresos=query.getResultList(); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Ingreso.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + + return listaIngresos; + } + + @Override + public List obtenerListaEntidadesPorProyectoCurso(Proyecto proyecto, CursoAcademico cursoAcademico) + throws ProyectosIESException { + List listaIngresos=null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + String sentenciaJPQL = """ + SELECT i + FROM ProyectoCurso pc + INNER JOIN Ingreso i ON i.proyectoCurso = pc + WHERE pc.proyecto.identificador= :idProyecto + and pc.curso.codigo = :codigoCurso + """; + TypedQuery query = entityManager.createQuery(sentenciaJPQL, Ingreso.class); + query.setParameter("idProyecto", proyecto.getIdentificador()); + query.setParameter("codigoCurso", cursoAcademico.getCodigo()); + + listaIngresos=query.getResultList(); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Ingreso.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + + return listaIngresos; + } + +} diff --git a/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/impl/PatrocinadorDaoJPA.java b/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/impl/PatrocinadorDaoJPA.java new file mode 100644 index 0000000..6e7e1df --- /dev/null +++ b/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/impl/PatrocinadorDaoJPA.java @@ -0,0 +1,264 @@ +package org.palomafp.dam2.aadd.proyectos.dao.impl; + +import java.util.List; + +import org.palomafp.dam2.aadd.proyectos.dao.IOperacionesCRUDMasConsultasEntidad; +import org.palomafp.dam2.aadd.proyectos.exceptions.ProyectosIESException; +import org.palomafp.dam2.aadd.proyectos.util.GestorConexionJPA; +import org.palomafp.dam2.aadd.proyectos.vo.CursoAcademico; +import org.palomafp.dam2.aadd.proyectos.vo.Patrocinador; +import org.palomafp.dam2.aadd.proyectos.vo.Proyecto; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.TypedQuery; + +public class PatrocinadorDaoJPA implements IOperacionesCRUDMasConsultasEntidad{ + + @Override + public Patrocinador obtenerEntidadPorID(Integer identificador) throws ProyectosIESException{ + Patrocinador patrocinador =null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + // Consultar por ID + patrocinador= entityManager.find(Patrocinador.class, identificador); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Patrocinador.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + return patrocinador; + } + + @Override + public Patrocinador obtenerEntidadPorNombre(String nombre) throws ProyectosIESException{ + Patrocinador patrocinador =null; + EntityManager entityManager = null; + + try { + entityManager = GestorConexionJPA.getEntityManager(); + TypedQuery query = entityManager.createQuery("SELECT p FROM Patrocinador p WHERE p.nombre = :nombre", Patrocinador.class); + query.setParameter("nombre", nombre); + + patrocinador=query.getSingleResult(); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Patrocinador.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + return patrocinador; + } + + @Override + public List obtenerListaEntidades() throws ProyectosIESException{ + List listaPatrocinadores=null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + listaPatrocinadores= entityManager.createQuery("SELECT p FROM Patrocinador p", Patrocinador.class).getResultList(); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Patrocinador.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + + return listaPatrocinadores; + } + + + @Override + public List obtenerListaEntidadesPorCursoAcademico(CursoAcademico cursoAcademico) throws ProyectosIESException { + List listaPatrocinadores=null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + String sentenciaJPQL = """ + SELECT p + FROM ProyectoCurso pc + INNER JOIN Ingreso i ON i.proyectoCurso = pc + INNER JOIN i.patrocinador p + WHERE pc.curso.codigo = :codigoCurso + """; + TypedQuery query = entityManager.createQuery(sentenciaJPQL, Patrocinador.class); + query.setParameter("codigoCurso", cursoAcademico.getCodigo()); + + listaPatrocinadores=query.getResultList(); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Patrocinador.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + + return listaPatrocinadores; + } + + @Override + public List obtenerListaEntidadesPorProyecto(Proyecto proyecto) throws ProyectosIESException { + List listaPatrocinadores=null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + String sentenciaJPQL = """ + SELECT p + FROM ProyectoCurso pc + INNER JOIN Ingreso i ON i.proyectoCurso = pc + INNER JOIN i.patrocinador p + WHERE pc.proyecto.identificador= :idProyecto + """; + TypedQuery query = entityManager.createQuery(sentenciaJPQL, Patrocinador.class); + query.setParameter("idProyecto", proyecto.getIdentificador()); + + listaPatrocinadores=query.getResultList(); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Patrocinador.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + + return listaPatrocinadores; + } + + @Override + public List obtenerListaEntidadesPorProyectoCurso(Proyecto proyecto, CursoAcademico cursoAcademico) + throws ProyectosIESException { + List listaPatrocinadores=null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + String sentenciaJPQL = """ + SELECT p + FROM ProyectoCurso pc + INNER JOIN Ingreso i ON i.proyectoCurso = pc + INNER JOIN i.patrocinador p + WHERE pc.proyecto.identificador= :idProyecto + and pc.curso.codigo = :codigoCurso + """; + TypedQuery query = entityManager.createQuery(sentenciaJPQL, Patrocinador.class); + query.setParameter("idProyecto", proyecto.getIdentificador()); + query.setParameter("codigoCurso", cursoAcademico.getCodigo()); + + listaPatrocinadores=query.getResultList(); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Patrocinador.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + + return listaPatrocinadores; + } + + + @Override + public void insertarEntidad(Patrocinador entidad) throws ProyectosIESException{ + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + entityManager.getTransaction().begin(); + + // Insertar + entityManager.persist(entidad); + + entityManager.getTransaction().commit(); + + } catch (Exception e) { + if (entityManager.getTransaction() != null && entityManager.getTransaction().isActive()) { + entityManager.getTransaction().rollback(); + } + + throw new ProyectosIESException(e, ProyectosIESException.ERROR_CREACION, Patrocinador.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + } + + @Override + public void eliminarEntidad(Patrocinador entidad) throws ProyectosIESException{ + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + entityManager.getTransaction().begin(); + + // Si no existe la entidad en el ámbito del "Entity Manager" lo incluye (para así poder borrarlo) + if (!entityManager.contains(entidad)) + entidad = entityManager.merge(entidad); + + entityManager.remove(entidad); + + entityManager.getTransaction().commit(); + + } catch (Exception e) { + if (entityManager.getTransaction() != null && entityManager.getTransaction().isActive()) { + entityManager.getTransaction().rollback(); + } + throw new ProyectosIESException(e, ProyectosIESException.ERROR_ELIMINACION, Patrocinador.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + } + + @Override + public void actualizarEntidad(Patrocinador entidad) throws ProyectosIESException{ + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + entityManager.getTransaction().begin(); + + // Actualizar + // Para poder actulizarse la entidad, ésta ha de encontarse en el ámbito del entityManager + if (!entityManager.contains(entidad)) + entidad=entityManager.merge(entidad); + + entityManager.getTransaction().commit(); + + } catch (Exception e) { + if (entityManager.getTransaction() != null && entityManager.getTransaction().isActive()) { + entityManager.getTransaction().rollback(); + } + + throw new ProyectosIESException(e, ProyectosIESException.ERROR_ACTUALIZACION, Patrocinador.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + } + +} diff --git a/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/impl/ProyectoCursoDaoJPA.java b/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/impl/ProyectoCursoDaoJPA.java new file mode 100644 index 0000000..5e888af --- /dev/null +++ b/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/impl/ProyectoCursoDaoJPA.java @@ -0,0 +1,152 @@ +package org.palomafp.dam2.aadd.proyectos.dao.impl; + +import java.util.List; + +import org.palomafp.dam2.aadd.proyectos.dao.IOperacionesCRUDEntidad; +import org.palomafp.dam2.aadd.proyectos.exceptions.ProyectosIESException; +import org.palomafp.dam2.aadd.proyectos.util.GestorConexionJPA; +import org.palomafp.dam2.aadd.proyectos.vo.ProyectoCurso; + +import jakarta.persistence.EntityManager; + +public class ProyectoCursoDaoJPA implements IOperacionesCRUDEntidad{ + + @Override + public ProyectoCurso obtenerEntidadPorNombre(String nombre) throws ProyectosIESException{ + ProyectoCurso cursoProyecto =null; + + // No se "implementa" + + return cursoProyecto; + } + + @Override + public ProyectoCurso obtenerEntidadPorID(Integer identificador) throws ProyectosIESException{ + ProyectoCurso cursoProyecto =null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + // Consultar por ID + cursoProyecto= entityManager.find(ProyectoCurso.class, identificador); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, ProyectoCurso.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + return cursoProyecto; + } + + @Override + public List obtenerListaEntidades() throws ProyectosIESException{ + List listaConceptos=null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + listaConceptos= entityManager.createQuery("SELECT cp FROM CursoProyecto cp", ProyectoCurso.class).getResultList(); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, ProyectoCurso.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + + return listaConceptos; + } + + @Override + public void insertarEntidad(ProyectoCurso entidad) throws ProyectosIESException{ + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + entityManager.getTransaction().begin(); + + // Insertar + entityManager.persist(entidad); + + entityManager.getTransaction().commit(); + + } catch (Exception e) { + if (entityManager.getTransaction() != null && entityManager.getTransaction().isActive()) { + entityManager.getTransaction().rollback(); + } + + throw new ProyectosIESException(e, ProyectosIESException.ERROR_CREACION, ProyectoCurso.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + } + + @Override + public void eliminarEntidad(ProyectoCurso entidad) throws ProyectosIESException{ + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + entityManager.getTransaction().begin(); + + // Si no existe la entidad en el ámbito del "Entity Manager" lo incluye (para así poder borrarlo) + if (!entityManager.contains(entidad)) + entidad = entityManager.merge(entidad); + + entityManager.remove(entidad); + + entityManager.getTransaction().commit(); + + } catch (Exception e) { + if (entityManager.getTransaction() != null && entityManager.getTransaction().isActive()) { + entityManager.getTransaction().rollback(); + } + throw new ProyectosIESException(e, ProyectosIESException.ERROR_ELIMINACION, ProyectoCurso.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + } + + @Override + public void actualizarEntidad(ProyectoCurso entidad) throws ProyectosIESException{ + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + entityManager.getTransaction().begin(); + + // Actualizar + // Para poder actulizarse la entidad, ésta ha de encontarse en el ámbito del entityManager + if (!entityManager.contains(entidad)) + entidad=entityManager.merge(entidad); + + entityManager.getTransaction().commit(); + + } catch (Exception e) { + if (entityManager.getTransaction() != null && entityManager.getTransaction().isActive()) { + entityManager.getTransaction().rollback(); + } + + throw new ProyectosIESException(e, ProyectosIESException.ERROR_ACTUALIZACION, ProyectoCurso.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + } + + + +} diff --git a/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/impl/ProyectoDaoJPA.java b/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/impl/ProyectoDaoJPA.java new file mode 100644 index 0000000..a2bf56d --- /dev/null +++ b/src/main/java/org/palomafp/dam2/aadd/proyectos/dao/impl/ProyectoDaoJPA.java @@ -0,0 +1,194 @@ +package org.palomafp.dam2.aadd.proyectos.dao.impl; + +import java.util.List; + +import org.palomafp.dam2.aadd.proyectos.dao.IOperacionesCRUDMasConsultasCursoAcademico; +import org.palomafp.dam2.aadd.proyectos.exceptions.ProyectosIESException; +import org.palomafp.dam2.aadd.proyectos.util.GestorConexionJPA; +import org.palomafp.dam2.aadd.proyectos.vo.CursoAcademico; +import org.palomafp.dam2.aadd.proyectos.vo.Proyecto; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.TypedQuery; + +public class ProyectoDaoJPA implements IOperacionesCRUDMasConsultasCursoAcademico{ + + @Override + public Proyecto obtenerEntidadPorNombre(String nombre) throws ProyectosIESException{ + Proyecto proyecto =null; + EntityManager entityManager = null; + + try { + entityManager = GestorConexionJPA.getEntityManager(); + TypedQuery query = entityManager.createQuery("SELECT p FROM Proyecto p WHERE p.nombre = :nombre", Proyecto.class); + query.setParameter("nombre", nombre); + proyecto=query.getSingleResult(); + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Proyecto.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + return proyecto; + } + + @Override + public Proyecto obtenerEntidadPorID(Integer identificador) throws ProyectosIESException{ + Proyecto proyecto =null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + // Consultar por ID + proyecto= entityManager.find(Proyecto.class, identificador); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Proyecto.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + return proyecto; + } + + @Override + public List obtenerListaEntidades() throws ProyectosIESException{ + List listaProyectos=null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + listaProyectos= entityManager.createQuery("SELECT p FROM Proyecto p", Proyecto.class).getResultList(); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Proyecto.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + + return listaProyectos; + } + + @Override + public void insertarEntidad(Proyecto entidad) throws ProyectosIESException{ + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + entityManager.getTransaction().begin(); + + // Insertar + entityManager.persist(entidad); + + entityManager.getTransaction().commit(); + + } catch (Exception e) { + if (entityManager.getTransaction() != null && entityManager.getTransaction().isActive()) { + entityManager.getTransaction().rollback(); + } + + throw new ProyectosIESException(e, ProyectosIESException.ERROR_CREACION, Proyecto.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + } + + @Override + public void eliminarEntidad(Proyecto entidad) throws ProyectosIESException{ + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + entityManager.getTransaction().begin(); + + // Si no existe la entidad en el ámbito del "Entity Manager" lo incluye (para así poder borrarlo) + if (!entityManager.contains(entidad)) + entidad = entityManager.merge(entidad); + + entityManager.remove(entidad); + + entityManager.getTransaction().commit(); + + } catch (Exception e) { + if (entityManager.getTransaction() != null && entityManager.getTransaction().isActive()) { + entityManager.getTransaction().rollback(); + } + throw new ProyectosIESException(e, ProyectosIESException.ERROR_ELIMINACION, Proyecto.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + } + + @Override + public void actualizarEntidad(Proyecto entidad) throws ProyectosIESException{ + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + entityManager.getTransaction().begin(); + + // Actualizar + // Para poder actulizarse la entidad, ésta ha de encontarse en el ámbito del entityManager + if (!entityManager.contains(entidad)) + entidad=entityManager.merge(entidad); + + entityManager.getTransaction().commit(); + + } catch (Exception e) { + if (entityManager.getTransaction() != null && entityManager.getTransaction().isActive()) { + entityManager.getTransaction().rollback(); + } + + throw new ProyectosIESException(e, ProyectosIESException.ERROR_ACTUALIZACION, Proyecto.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + } + + + @Override + public List obtenerListaEntidadesPorCursoAcademico(CursoAcademico cursoAcademico) throws ProyectosIESException { + List listaProyectos=null; + EntityManager entityManager = null; + + try { + + entityManager = GestorConexionJPA.getEntityManager(); + String sentenciaJPQL = """ + SELECT p + FROM ProyectoCurso pc + INNER JOIN Proyecto p ON p = pc.proyecto + WHERE pc.curso.codigo = :codigoCurso + """; + TypedQuery query = entityManager.createQuery(sentenciaJPQL, Proyecto.class); + query.setParameter("codigoCurso", cursoAcademico.getCodigo()); + + listaProyectos=query.getResultList(); + + } catch (Exception e) { + throw new ProyectosIESException(e, ProyectosIESException.ERROR_BUSQUEDA, Proyecto.class); + } finally { + if (entityManager != null) { + entityManager.close(); + } + } + + return listaProyectos; + } + +} diff --git a/src/main/java/org/palomafp/dam2/aadd/proyectos/exceptions/ProyectosIESException.java b/src/main/java/org/palomafp/dam2/aadd/proyectos/exceptions/ProyectosIESException.java new file mode 100644 index 0000000..a66732a --- /dev/null +++ b/src/main/java/org/palomafp/dam2/aadd/proyectos/exceptions/ProyectosIESException.java @@ -0,0 +1,45 @@ +package org.palomafp.dam2.aadd.proyectos.exceptions; + +public class ProyectosIESException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + public static final long ERROR_BUSQUEDA = 0; + public static final long ERROR_CREACION = 1; + public static final long ERROR_ACTUALIZACION = 2; + public static final long ERROR_ELIMINACION = 3; + public static final long ERROR_OTRO = 4; + + private long codigoError; + private String nombreClase; + + + @SuppressWarnings("rawtypes") + public ProyectosIESException(Exception excepcion, long tipoError, Class clase) { + super(excepcion); + this.codigoError = tipoError; + this.nombreClase = clase.getName(); + } + + + public long getCodigoError() { + return codigoError; + } + + + public void setCodigoError(long codigoError) { + this.codigoError = codigoError; + } + + + public String getNombreClase() { + return nombreClase; + } + + + public void setNombreClase(String nombreClase) { + this.nombreClase = nombreClase; + } + + +} diff --git a/src/main/java/org/palomafp/dam2/aadd/proyectos/util/GestorConexionJPA.java b/src/main/java/org/palomafp/dam2/aadd/proyectos/util/GestorConexionJPA.java new file mode 100644 index 0000000..6320135 --- /dev/null +++ b/src/main/java/org/palomafp/dam2/aadd/proyectos/util/GestorConexionJPA.java @@ -0,0 +1,38 @@ +package org.palomafp.dam2.aadd.proyectos.util; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityManagerFactory; +import jakarta.persistence.Persistence; + +public class GestorConexionJPA { + private static EntityManagerFactory entityManagerFactory; + + private static final String UNIDAD_PERSISTENCIA = "UP_ProyectoIES_HBT"; + + // Inicializamos ambos EntityManager + static { + try { + // Inicializamos el entityManagerFactory + entityManagerFactory = Persistence.createEntityManagerFactory(UNIDAD_PERSISTENCIA); + } catch (Exception e) { + e.printStackTrace(); + throw new ExceptionInInitializerError("Error al inicializar JPA: " + e.getMessage()); + } + } + + //EntityManager para JPA + public static EntityManager getEntityManager() { + EntityManager gestorEntidades=null; + + gestorEntidades = entityManagerFactory.createEntityManager(); + + return gestorEntidades; + } + + // Cerrar EntityManagerFactory + public static void closeEntityManagerFactories() { + if (entityManagerFactory != null && entityManagerFactory.isOpen()) { + entityManagerFactory.close(); + } + } +} diff --git a/src/main/java/org/palomafp/dam2/aadd/proyectos/vo/Concepto.java b/src/main/java/org/palomafp/dam2/aadd/proyectos/vo/Concepto.java new file mode 100644 index 0000000..be1205a --- /dev/null +++ b/src/main/java/org/palomafp/dam2/aadd/proyectos/vo/Concepto.java @@ -0,0 +1,27 @@ +package org.palomafp.dam2.aadd.proyectos.vo; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import lombok.Data; + +@Data +@Entity +@Table(name = "T_CONCEPTO") +public class Concepto { + + // Variables + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "identificador") + private int identificador; + + @Column(name = "nombre", nullable = false, unique = true) + private String nombre; + + @Column(name = "descripcion") + private String descripcion; +} diff --git a/src/main/java/org/palomafp/dam2/aadd/proyectos/vo/CursoAcademico.java b/src/main/java/org/palomafp/dam2/aadd/proyectos/vo/CursoAcademico.java new file mode 100644 index 0000000..01914e9 --- /dev/null +++ b/src/main/java/org/palomafp/dam2/aadd/proyectos/vo/CursoAcademico.java @@ -0,0 +1,31 @@ +package org.palomafp.dam2.aadd.proyectos.vo; + +import java.time.LocalDate; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import lombok.Data; + +@Data + +@Entity +@Table (name = "T_CURSO_ACADEMICO") +public class CursoAcademico { + + // Variables + @Id + @Column(name = "codigo", length = 9) + private String codigo; + + @Column(name = "fecha_inicio", nullable = false) + private LocalDate fechaInicio; + + @Column(name = "fecha_fin", nullable = false) + private LocalDate fechaFin; + + @Column(name = "nombre", nullable = false) + private String nombre; + +} diff --git a/src/main/java/org/palomafp/dam2/aadd/proyectos/vo/Gasto.java b/src/main/java/org/palomafp/dam2/aadd/proyectos/vo/Gasto.java new file mode 100644 index 0000000..7fcbe39 --- /dev/null +++ b/src/main/java/org/palomafp/dam2/aadd/proyectos/vo/Gasto.java @@ -0,0 +1,53 @@ +package org.palomafp.dam2.aadd.proyectos.vo; + +import java.time.LocalDate; +import java.time.LocalDateTime; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +import lombok.Data; + +@Data + +@Entity +@Table(name = "T_GASTO") +public class Gasto { + + // Variables + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "identificador") + private int identificador; + + @ManyToOne + @JoinColumn(name = "id_concepto", nullable = false) + private Concepto concepto; + + @ManyToOne + @JoinColumn(name = "id_proyectocurso", nullable = false) + private ProyectoCurso proyectoCurso; + + @Column(name = "num_unidades", nullable = false) + private int numeroUnidades; + + @Column(name = "precio_unidad", nullable = false) + private double precioUnitario; + + @Column(name = "fecha") + private LocalDate fecha; + + @Column(name = "observacion") + private String observacion; + + @Column(name = "fecha_creacion") + private LocalDateTime fechaCreacion; + + @Column(name = "fecha_modificacion") + private LocalDateTime fechaModificacion; +} diff --git a/src/main/java/org/palomafp/dam2/aadd/proyectos/vo/Ingreso.java b/src/main/java/org/palomafp/dam2/aadd/proyectos/vo/Ingreso.java new file mode 100644 index 0000000..215f0f6 --- /dev/null +++ b/src/main/java/org/palomafp/dam2/aadd/proyectos/vo/Ingreso.java @@ -0,0 +1,51 @@ +package org.palomafp.dam2.aadd.proyectos.vo; + +import java.time.LocalDate; +import java.time.LocalDateTime; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +import lombok.Data; + +@Data + +@Entity +@Table(name = "T_INGRESO") +public class Ingreso { + + // Variables + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "identificador") + private int identificador; + + @ManyToOne + @JoinColumn(name = "id_patrocinador", nullable = false) + private Patrocinador patrocinador; + + @Column(name = "fecha") + private LocalDate fecha; + + @ManyToOne + @JoinColumn(name = "id_proyectocurso", nullable = false) + private ProyectoCurso proyectoCurso; + + @Column(name = "cantidad", nullable = false) + private int cantidad; + + @Column(name = "observacion") + private String observacion; + + @Column(name = "fecha_creacion") + private LocalDateTime fechaCreacion; + + @Column(name = "fecha_modificacion") + private LocalDateTime fechaModificacion; + +} diff --git a/src/main/java/org/palomafp/dam2/aadd/proyectos/vo/Patrocinador.java b/src/main/java/org/palomafp/dam2/aadd/proyectos/vo/Patrocinador.java new file mode 100644 index 0000000..78ae9c7 --- /dev/null +++ b/src/main/java/org/palomafp/dam2/aadd/proyectos/vo/Patrocinador.java @@ -0,0 +1,30 @@ +package org.palomafp.dam2.aadd.proyectos.vo; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.Lob; +import jakarta.persistence.Table; +import lombok.Data; + +@Data + +@Entity +@Table(name = "T_PATROCINADOR") +public class Patrocinador { + + // Variables + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "identificador") + private int identificador; + + @Column(name = "nombre", nullable = false, unique = true) + private String nombre; + + @Lob + @Column(name = "imagen") + private byte[] imagen; +} diff --git a/src/main/java/org/palomafp/dam2/aadd/proyectos/vo/Proyecto.java b/src/main/java/org/palomafp/dam2/aadd/proyectos/vo/Proyecto.java new file mode 100644 index 0000000..5b0cfe5 --- /dev/null +++ b/src/main/java/org/palomafp/dam2/aadd/proyectos/vo/Proyecto.java @@ -0,0 +1,32 @@ +package org.palomafp.dam2.aadd.proyectos.vo; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import lombok.Data; + +@Data + +@Entity +@Table(name = "T_PROYECTO") +public class Proyecto { + + // Variables + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "identificador") + private int identificador; + + @Column(name = "nombre", nullable = false, unique = true) + private String nombre; + + @Column(name = "descripcion") + private String descripcion; + + @Column(name = "url_logo", nullable = true) + private String urlLogo; + +} diff --git a/src/main/java/org/palomafp/dam2/aadd/proyectos/vo/ProyectoCurso.java b/src/main/java/org/palomafp/dam2/aadd/proyectos/vo/ProyectoCurso.java new file mode 100644 index 0000000..93ddb2e --- /dev/null +++ b/src/main/java/org/palomafp/dam2/aadd/proyectos/vo/ProyectoCurso.java @@ -0,0 +1,32 @@ +package org.palomafp.dam2.aadd.proyectos.vo; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +import lombok.Data; + +@Data + +@Entity +@Table(name = "T_PROYECTO_CURSO") +public class ProyectoCurso { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "identificador") + private int identificador; + + @ManyToOne + @JoinColumn(name = "cod_curso", nullable = false) + private CursoAcademico curso; + + @ManyToOne + @JoinColumn(name = "id_proyecto", nullable = false) + private Proyecto proyecto; + +} diff --git a/src/main/resources/META-INF/persistence.xml b/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000..f84f3b7 --- /dev/null +++ b/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,39 @@ + + + + + + + org.hibernate.jpa.HibernatePersistenceProvider + + + org.palomafp.dam2.aadd.proyectos.vo.Concepto + org.palomafp.dam2.aadd.proyectos.vo.Patrocinador + org.palomafp.dam2.aadd.proyectos.vo.Proyecto + org.palomafp.dam2.aadd.proyectos.vo.CursoAcademico + org.palomafp.dam2.aadd.proyectos.vo.ProyectoCurso + org.palomafp.dam2.aadd.proyectos.vo.Gasto + org.palomafp.dam2.aadd.proyectos.vo.Ingreso + + + + + + + + + + + + + + + + + + + + \ No newline at end of file