1 package dst.ass1.jpa.dao.impl;
3 import dst.ass1.jpa.dao.ICourseDAO;
4 import dst.ass1.jpa.model.CourseStatus;
5 import dst.ass1.jpa.model.ICourse;
6 import dst.ass1.jpa.model.impl.Course;
7 import dst.ass1.jpa.util.Constants;
9 import javax.persistence.EntityManager;
10 import javax.persistence.NoResultException;
11 import javax.persistence.criteria.CriteriaBuilder;
12 import javax.persistence.criteria.CriteriaQuery;
13 import javax.persistence.criteria.Predicate;
14 import javax.persistence.criteria.Root;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Date;
18 import java.util.List;
20 public class CourseDAO extends GenericDAO<Course, ICourse> implements ICourseDAO {
21 public CourseDAO(EntityManager em) {
22 super(em, Course.class);
26 public Collection<ICourse> findOngoingAndFreeCoursesForPlatform(String platformName) {
28 return em.createNamedQuery(Constants.Q_ONGOING_AND_FREE_PLATFORM_COURSES, ICourse.class)
29 .setParameter("platformName", platformName).getResultList();
30 } catch (NoResultException e) {
36 public Collection<ICourse> findNonCancelledCoursesBetweenStartAndEnd(Date start, Date end) {
37 CriteriaBuilder b = em.getCriteriaBuilder();
38 CriteriaQuery<ICourse> q = b.createQuery(ICourse.class);
39 Root<Course> root = q.from(Course.class);
40 final List<Predicate> andPredicates = new ArrayList<Predicate>();
42 andPredicates.add(b.notEqual(root.get("status"), CourseStatus.CANCELLED));
43 if (start != null) andPredicates.add(b.greaterThan(root.get("start"), start));
44 if (end != null) andPredicates.add(b.lessThan(root.get("end"), end));
46 q.select(root).where(b.and(andPredicates.toArray(new Predicate[andPredicates.size()])));
48 List<ICourse> a = em.createQuery(q).getResultList();