From 29cc7261ce52517bd38054c068ef6859a4457c16 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Tue, 27 Mar 2018 18:51:02 +0200 Subject: [PATCH] [1.2.1ab] changed model + dao to do the first named query --- .../src/main/java/dst/ass1/jpa/dao/impl/CourseDAO.java | 9 ++++++++- .../main/java/dst/ass1/jpa/dao/impl/GenericDAO.java | 2 +- .../java/dst/ass1/jpa/dao/impl/ParticipantDAO.java | 10 +++++++++- .../src/main/java/dst/ass1/jpa/model/impl/Course.java | 8 ++++++++ .../main/java/dst/ass1/jpa/model/impl/Participant.java | 2 ++ 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/ass1-jpa/src/main/java/dst/ass1/jpa/dao/impl/CourseDAO.java b/ass1-jpa/src/main/java/dst/ass1/jpa/dao/impl/CourseDAO.java index d2a2e18..ea5a90d 100644 --- a/ass1-jpa/src/main/java/dst/ass1/jpa/dao/impl/CourseDAO.java +++ b/ass1-jpa/src/main/java/dst/ass1/jpa/dao/impl/CourseDAO.java @@ -3,8 +3,10 @@ package dst.ass1.jpa.dao.impl; import dst.ass1.jpa.dao.ICourseDAO; import dst.ass1.jpa.model.ICourse; import dst.ass1.jpa.model.impl.Course; +import dst.ass1.jpa.util.Constants; import javax.persistence.EntityManager; +import javax.persistence.NoResultException; import java.util.Collection; import java.util.Date; @@ -15,7 +17,12 @@ public class CourseDAO extends GenericDAO implements ICourseDAO @Override public Collection findOngoingAndFreeCoursesForPlatform(String platformName) { - return null; + try { + return em.createNamedQuery(Constants.Q_ONGOING_AND_FREE_PLATFORM_COURSES, ICourse.class) + .setParameter("platformName", platformName).getResultList(); + } catch (NoResultException e) { + return null; + } } @Override diff --git a/ass1-jpa/src/main/java/dst/ass1/jpa/dao/impl/GenericDAO.java b/ass1-jpa/src/main/java/dst/ass1/jpa/dao/impl/GenericDAO.java index 0272e27..b41954a 100644 --- a/ass1-jpa/src/main/java/dst/ass1/jpa/dao/impl/GenericDAO.java +++ b/ass1-jpa/src/main/java/dst/ass1/jpa/dao/impl/GenericDAO.java @@ -4,7 +4,7 @@ import javax.persistence.EntityManager; import java.util.List; public class GenericDAO implements dst.ass1.jpa.dao.GenericDAO { - private final EntityManager em; + protected final EntityManager em; private final Class c; protected GenericDAO(EntityManager em, Class c) { diff --git a/ass1-jpa/src/main/java/dst/ass1/jpa/dao/impl/ParticipantDAO.java b/ass1-jpa/src/main/java/dst/ass1/jpa/dao/impl/ParticipantDAO.java index 428b9a0..caa5f1c 100644 --- a/ass1-jpa/src/main/java/dst/ass1/jpa/dao/impl/ParticipantDAO.java +++ b/ass1-jpa/src/main/java/dst/ass1/jpa/dao/impl/ParticipantDAO.java @@ -3,8 +3,11 @@ package dst.ass1.jpa.dao.impl; import dst.ass1.jpa.dao.IParticipantDAO; import dst.ass1.jpa.model.IParticipant; import dst.ass1.jpa.model.impl.Participant; +import dst.ass1.jpa.util.Constants; import javax.persistence.EntityManager; +import javax.persistence.NoResultException; + public class ParticipantDAO extends GenericDAO implements IParticipantDAO { public ParticipantDAO(EntityManager em) { @@ -13,6 +16,11 @@ public class ParticipantDAO extends GenericDAO implem @Override public IParticipant findByEmail(String email) { - return null; + try { + return em.createNamedQuery(Constants.Q_PARTICIPANT_BY_EMAIL, Participant.class) + .setParameter("email", email).getSingleResult(); + } catch (NoResultException e) { + return null; + } } } \ No newline at end of file diff --git a/ass1-jpa/src/main/java/dst/ass1/jpa/model/impl/Course.java b/ass1-jpa/src/main/java/dst/ass1/jpa/model/impl/Course.java index 1af4755..0ceb118 100644 --- a/ass1-jpa/src/main/java/dst/ass1/jpa/model/impl/Course.java +++ b/ass1-jpa/src/main/java/dst/ass1/jpa/model/impl/Course.java @@ -11,6 +11,14 @@ import java.util.Date; @Entity @Table(name = Constants.T_COURSE) +@NamedQuery(name = Constants.Q_ONGOING_AND_FREE_PLATFORM_COURSES, query = "SELECT c" + + " FROM " + Constants.T_COURSE + " c" + + " JOIN c.coursePlatform cp " + + " WHERE cp." + Constants.M_COURSEPLATFORM_NAME + " = :platformName " + + " AND c." + Constants.M_COURSE_STATUS + " = dst.ass1.jpa.model.CourseStatus.ONGOING" + + " AND c." + Constants.M_COURSE_CAPACITY + " > (SELECT count(*) FROM " + Constants.T_ENROLLMENT + + " WHERE c = " + Constants.I_COURSE + " group by " + Constants.I_COURSE + ")" +) public class Course implements ICourse { @Id @GeneratedValue diff --git a/ass1-jpa/src/main/java/dst/ass1/jpa/model/impl/Participant.java b/ass1-jpa/src/main/java/dst/ass1/jpa/model/impl/Participant.java index 64786ce..aad5c5c 100644 --- a/ass1-jpa/src/main/java/dst/ass1/jpa/model/impl/Participant.java +++ b/ass1-jpa/src/main/java/dst/ass1/jpa/model/impl/Participant.java @@ -11,6 +11,8 @@ import java.util.Collection; @Entity @Table(name = Constants.T_PARTICIPANT, uniqueConstraints = {@UniqueConstraint(columnNames = {Constants.M_PARTICIPANT_ACCOUNT, Constants.M_PARTICIPANT_BANK_CODE})}) +@NamedQuery(name = Constants.Q_PARTICIPANT_BY_EMAIL, query = + "SELECT p FROM " + Constants.T_PARTICIPANT + " as p where p." + Constants.M_PERSON_EMAIL + " = :email") public class Participant extends Person implements IParticipant { @Column(nullable = false, unique = true) private String email; -- 2.43.0