primer commit

This commit is contained in:
Isidoro Nevares Martín 2025-11-10 09:54:38 +01:00
commit 7563c2f8e7
28 changed files with 2198 additions and 0 deletions

58
.classpath Normal file
View File

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="test" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target/
*.class

23
.project Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>proyectos_lapaloma</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,2 @@
eclipse.preferences.version=1
org.eclipse.jdt.apt.aptEnabled=false

View File

@ -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

View File

@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

58
pom.xml Normal file
View File

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ies.lapaloma.proyectos</groupId>
<artifactId>ies_lapaloma_proyectos</artifactId>
<version>1.0.1</version>
<name>Acceso_Datos_Proyecto_IES_La_Paloma</name>
<dependencies>
<!--Conector MySQL -->
<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>9.2.0</version>
</dependency>
<!-- Hibernate
https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-core -->
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.6.3.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.36</version>
<scope>provided</scope>
</dependency>
</dependencies>
<!-- Configuración del plugin de compilador de Maven -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.36</version>
</path>
</annotationProcessorPaths>
<source>23</source> <!-- Establece la versión de Java para el código fuente -->
<target>23</target> <!-- Establece la versión de Java para el bytecode -->
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,91 @@
package org.palomafp.dam2.aadd.cgf;
public class ClasePrincipal {
public static void main(String[] args) {
/*
IOperacionesCRUDEntidad<CursoAcademico, String> implCursoAcademico = new CursoAcademicoDaoJPA();
CursoAcademico curso= implCursoAcademico.obtenerEntidadPorID("2022-2023");
System.out.println(curso);
System.out.println("\n");
IOperacionesCRUDEntidad<Proyecto, Integer> implProyecto = new ProyectoDaoJPA();
Proyecto proyecto = implProyecto.obtenerEntidadPorNombre("Speedster");
System.out.println(proyecto);
System.out.println("\n");
IOperacionesCRUDMasConsultasEntidad<Patrocinador, Integer> 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<Concepto, Integer> 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<Gasto, Integer> 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<Ingreso, Integer> 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<Proyecto, Integer> 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");
*/
}
}

View File

@ -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,K> {
T obtenerEntidadPorID(K claveUnica) throws ProyectosIESException;
List<T> 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;
}

View File

@ -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<T, K> extends IOperacionesCRUDEntidad<T, K>{
List<T> obtenerListaEntidadesPorCursoAcademico(CursoAcademico cursoAcademico) throws ProyectosIESException;
}

View File

@ -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<T, K> extends IOperacionesCRUDMasConsultasCursoAcademico<T, K>{
List<T> obtenerListaEntidadesPorProyecto(Proyecto proyecto) throws ProyectosIESException;
List<T> obtenerListaEntidadesPorProyectoCurso(Proyecto proyecto, CursoAcademico cursoAcademico) throws ProyectosIESException;
}

View File

@ -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<Concepto, Integer>{
@Override
public Concepto obtenerEntidadPorNombre(String nombre) throws ProyectosIESException{
Concepto concepto =null;
EntityManager entityManager = null;
try {
entityManager = GestorConexionJPA.getEntityManager();
TypedQuery<Concepto> 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<Concepto> obtenerListaEntidades() throws ProyectosIESException{
List<Concepto> 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<Concepto> obtenerListaEntidadesPorCursoAcademico(CursoAcademico cursoAcademico) throws ProyectosIESException {
List<Concepto> 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<Concepto> 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<Concepto> obtenerListaEntidadesPorProyecto(Proyecto proyecto) throws ProyectosIESException {
List<Concepto> 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<Concepto> 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<Concepto> obtenerListaEntidadesPorProyectoCurso(Proyecto proyecto, CursoAcademico cursoAcademico)
throws ProyectosIESException {
List<Concepto> 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<Concepto> 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;
}
}

View File

@ -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<CursoAcademico, String>{
@Override
public CursoAcademico obtenerEntidadPorNombre(String nombre) throws ProyectosIESException{
CursoAcademico cursoAcademico =null;
EntityManager entityManager = null;
try {
entityManager = GestorConexionJPA.getEntityManager();
TypedQuery<CursoAcademico> 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<CursoAcademico> obtenerListaEntidades() throws ProyectosIESException{
List<CursoAcademico> 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();
}
}
}
}

View File

@ -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<Gasto, Integer>{
@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<Gasto> obtenerListaEntidades() throws ProyectosIESException{
List<Gasto> 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<Gasto> obtenerListaEntidadesPorCursoAcademico(CursoAcademico cursoAcademico) throws ProyectosIESException {
List<Gasto> 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<Gasto> 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<Gasto> obtenerListaEntidadesPorProyecto(Proyecto proyecto) throws ProyectosIESException {
List<Gasto> 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<Gasto> 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<Gasto> obtenerListaEntidadesPorProyectoCurso(Proyecto proyecto, CursoAcademico cursoAcademico)
throws ProyectosIESException {
List<Gasto> 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<Gasto> 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;
}
}

View File

@ -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<Ingreso, Integer>{
@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<Ingreso> obtenerListaEntidades() throws ProyectosIESException{
List<Ingreso> 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<Ingreso> obtenerListaEntidadesPorCursoAcademico(CursoAcademico cursoAcademico) throws ProyectosIESException {
List<Ingreso> 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<Ingreso> 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<Ingreso> obtenerListaEntidadesPorProyecto(Proyecto proyecto) throws ProyectosIESException {
List<Ingreso> 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<Ingreso> 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<Ingreso> obtenerListaEntidadesPorProyectoCurso(Proyecto proyecto, CursoAcademico cursoAcademico)
throws ProyectosIESException {
List<Ingreso> 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<Ingreso> 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;
}
}

View File

@ -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<Patrocinador, Integer>{
@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<Patrocinador> 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<Patrocinador> obtenerListaEntidades() throws ProyectosIESException{
List<Patrocinador> 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<Patrocinador> obtenerListaEntidadesPorCursoAcademico(CursoAcademico cursoAcademico) throws ProyectosIESException {
List<Patrocinador> 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<Patrocinador> 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<Patrocinador> obtenerListaEntidadesPorProyecto(Proyecto proyecto) throws ProyectosIESException {
List<Patrocinador> 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<Patrocinador> 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<Patrocinador> obtenerListaEntidadesPorProyectoCurso(Proyecto proyecto, CursoAcademico cursoAcademico)
throws ProyectosIESException {
List<Patrocinador> 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<Patrocinador> 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();
}
}
}
}

View File

@ -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<ProyectoCurso, Integer>{
@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<ProyectoCurso> obtenerListaEntidades() throws ProyectosIESException{
List<ProyectoCurso> 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();
}
}
}
}

View File

@ -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<Proyecto, Integer>{
@Override
public Proyecto obtenerEntidadPorNombre(String nombre) throws ProyectosIESException{
Proyecto proyecto =null;
EntityManager entityManager = null;
try {
entityManager = GestorConexionJPA.getEntityManager();
TypedQuery<Proyecto> 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<Proyecto> obtenerListaEntidades() throws ProyectosIESException{
List<Proyecto> 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<Proyecto> obtenerListaEntidadesPorCursoAcademico(CursoAcademico cursoAcademico) throws ProyectosIESException {
List<Proyecto> 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<Proyecto> 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;
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -0,0 +1,39 @@
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<!-- Unidad Persistencia Hibernate -->
<persistence-unit name="UP_ProyectoIES_HBT" transaction-type="RESOURCE_LOCAL">
<!-- Proveedor de Persistencia -->
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<!-- Clases a mapear -->
<class>org.palomafp.dam2.aadd.proyectos.vo.Concepto</class>
<class>org.palomafp.dam2.aadd.proyectos.vo.Patrocinador</class>
<class>org.palomafp.dam2.aadd.proyectos.vo.Proyecto</class>
<class>org.palomafp.dam2.aadd.proyectos.vo.CursoAcademico</class>
<class>org.palomafp.dam2.aadd.proyectos.vo.ProyectoCurso</class>
<class>org.palomafp.dam2.aadd.proyectos.vo.Gasto</class>
<class>org.palomafp.dam2.aadd.proyectos.vo.Ingreso</class>
<!-- Configuración para la conexión a la Base de Datos -->
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="jakarta.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
<property name="jakarta.persistence.jdbc.url" value="jdbc:mysql://5.189.134.76:3306/proyectos_ies" />
<property name="jakarta.persistence.jdbc.user" value="root" />
<property name="jakarta.persistence.jdbc.password" value="proyectos_ies+123" />
<!-- Configuración de Hibernate -->
<property name="hibernate.hbm2ddl.auto" value="none"/> <!-- 'update' para mantener el esquema en sincronía con las entidades -->
<property name="hibernate.show_sql" value="false"/> <!-- Muestra las consultas SQL -->
<property name="hibernate.format_sql" value="false"/> <!-- Da formato a las consultas SQL -->
<property name="hibernate.use_sql_comments" value="false"/> <!-- Añade comentarios en las consultas SQL -->
</properties>
</persistence-unit>
</persistence>