From 870c74f5ce96886814c80221aa9eb16af38bb197 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Thu, 29 Mar 2018 00:59:06 +0200 Subject: [PATCH] [1.2.3b] Das war ja vergleichsweise einfach. --- .../java/dst/ass1/jpa/dao/impl/CourseDAO.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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 ea5a90d..7528a96 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 @@ -1,14 +1,21 @@ package dst.ass1.jpa.dao.impl; import dst.ass1.jpa.dao.ICourseDAO; +import dst.ass1.jpa.model.CourseStatus; 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 javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; +import java.util.ArrayList; import java.util.Collection; import java.util.Date; +import java.util.List; public class CourseDAO extends GenericDAO implements ICourseDAO { public CourseDAO(EntityManager em) { @@ -27,6 +34,18 @@ public class CourseDAO extends GenericDAO implements ICourseDAO @Override public Collection findNonCancelledCoursesBetweenStartAndEnd(Date start, Date end) { - return null; + CriteriaBuilder b = em.getCriteriaBuilder(); + CriteriaQuery q = b.createQuery(ICourse.class); + Root root = q.from(Course.class); + final List andPredicates = new ArrayList(); + + andPredicates.add(b.notEqual(root.get("status"), CourseStatus.CANCELLED)); + if (start != null) andPredicates.add(b.greaterThan(root.get("start"), start)); + if (end != null) andPredicates.add(b.lessThan(root.get("end"), end)); + + q.select(root).where(b.and(andPredicates.toArray(new Predicate[andPredicates.size()]))); + + List a = em.createQuery(q).getResultList(); + return a; } } -- 2.43.0