diff --git a/backend/src/main/java/uk/co/neviyn/observations/dao/ObservationDao.java b/backend/src/main/java/uk/co/neviyn/observations/dao/ObservationDao.java index 241c04f..de07173 100644 --- a/backend/src/main/java/uk/co/neviyn/observations/dao/ObservationDao.java +++ b/backend/src/main/java/uk/co/neviyn/observations/dao/ObservationDao.java @@ -32,24 +32,49 @@ public class ObservationDao extends AbstractDAO { return currentSession().createQuery("from Observation", Observation.class).list(); } + public List filteredList(Site site, Tutor tutor, DateTime startDate, DateTime endDate, String whom) { + final CriteriaBuilder builder = currentSession().getCriteriaBuilder(); + CriteriaQuery criteriaQuery = builder.createQuery(Observation.class); + Root root = criteriaQuery.from(Observation.class); + List predicates = new ArrayList<>(); + if (site != null) { + predicates.add(builder.equal(root.get("site"), site)); + } + if (startDate != null) { + predicates.add(builder.greaterThanOrEqualTo(root.get("date"), startDate)); + } + if (endDate != null) { + predicates.add(builder.lessThanOrEqualTo(root.get("date"), endDate)); + } + if (whom != null) { + predicates.add(builder.equal(root.get("whom"), whom)); + } + if (!predicates.isEmpty()) { + criteriaQuery.having(predicates.toArray(new Predicate[0])); + } + Query query = currentSession().createQuery(criteriaQuery); + System.out.println(query.getQueryString()); + return query.getResultList(); + } + /** * Generate the average observation stats with data constraints. - * @param site Restrict to a site. - * @param tutor Restrict to a tutor. + * + * @param site Restrict to a site. + * @param tutor Restrict to a tutor. * @param startDate Restrict to after a certain date. - * @param endDate Restrict to before a certain date. - * @param whom Restrict to a particular person/group who was observed. + * @param endDate Restrict to before a certain date. + * @param whom Restrict to a particular person/group who was observed. * @return Average observation scores per day. */ // TODO: Add tutor filter - public List averageStats(Site site, Tutor tutor, DateTime startDate, - DateTime endDate, String whom) { + public List averageStats(Site site, Tutor tutor, DateTime startDate, DateTime endDate, String whom) { CriteriaBuilder builder = currentSession().getCriteriaBuilder(); CriteriaQuery criteriaQuery = builder.createQuery(AverageStats.class); Root root = criteriaQuery.from(Observation.class); criteriaQuery.multiselect(builder.avg(root.get("monitoring")), builder.avg(root.get("control")), - builder.avg(root.get("conservatism")), builder.avg(root.get("teamwork")), - builder.avg(root.get("knowledge")), root.get("date")); + builder.avg(root.get("conservatism")), builder.avg(root.get("teamwork")), builder.avg(root.get("knowledge")), + root.get("date")); criteriaQuery.groupBy(root.get("date")); List predicates = new ArrayList<>(); if (site != null) { @@ -64,7 +89,9 @@ public class ObservationDao extends AbstractDAO { if (whom != null) { predicates.add(builder.equal(root.get("whom"), whom)); } - criteriaQuery.having(predicates.toArray(new Predicate[0])); + if (!predicates.isEmpty()) { + criteriaQuery.having(predicates.toArray(new Predicate[0])); + } Query query = currentSession().createQuery(criteriaQuery); System.out.println(query.getQueryString()); return query.getResultList(); diff --git a/backend/src/main/java/uk/co/neviyn/observations/resources/ObservationResource.java b/backend/src/main/java/uk/co/neviyn/observations/resources/ObservationResource.java index 8cdd347..50d19db 100644 --- a/backend/src/main/java/uk/co/neviyn/observations/resources/ObservationResource.java +++ b/backend/src/main/java/uk/co/neviyn/observations/resources/ObservationResource.java @@ -2,6 +2,7 @@ package uk.co.neviyn.observations.resources; import io.dropwizard.hibernate.UnitOfWork; import java.util.HashSet; +import java.util.List; import java.util.Set; import javax.validation.constraints.NotNull; import javax.ws.rs.Consumes; @@ -38,6 +39,7 @@ public class ObservationResource { /** * Create a new Observation. + * * @param newObservation New observation data. * @return ID of newly created observation. */ @@ -51,12 +53,11 @@ public class ObservationResource { tutors.add(tutorDao.get(l)); } final Site site = siteDao.get(newObservation.getSiteId()); - Observation observation = Observation.builder().site(site).tutors(tutors) - .observed(newObservation.getObserved()).type(TrainingType.valueOf(newObservation.getType())) - .monitoring(newObservation.getMonitoring()).control(newObservation.getControl()) - .conservatism(newObservation.getConservatism()).teamwork(newObservation.getTeamwork()) - .knowledge(newObservation.getKnowledge()).observations(newObservation.getRawData()) - .date(submissionDate).whom(newObservation.getWhom()).build(); + Observation observation = Observation.builder().site(site).tutors(tutors).observed(newObservation.getObserved()) + .type(TrainingType.valueOf(newObservation.getType())).monitoring(newObservation.getMonitoring()) + .control(newObservation.getControl()).conservatism(newObservation.getConservatism()) + .teamwork(newObservation.getTeamwork()).knowledge(newObservation.getKnowledge()) + .observations(newObservation.getRawData()).date(submissionDate).whom(newObservation.getWhom()).build(); for (Tutor t : tutors) { t.getObservations().add(observation); } @@ -65,21 +66,54 @@ public class ObservationResource { return observation.getId(); } + @GET + @UnitOfWork + public List observations(@QueryParam("site") Integer siteId, @QueryParam("tutor") Integer tutorId, + @QueryParam("startDate") String startDate, @QueryParam("endDate") String endDate, + @QueryParam("whom") String whom) { + Site site = null; + Tutor tutor = null; + DateTime start = null; + DateTime end = null; + try { + site = siteDao.get(siteId); + } catch (Exception e) { + log.warn("Couldn't get site with ID " + siteId); + } + try { + tutor = tutorDao.get(tutorId); + } catch (Exception e) { + log.warn("Couldn't get tutor with ID " + tutorId); + } + try { + start = DateTime.parse(startDate); + } catch (Exception e) { + log.warn("Couldn't get a valid date from " + startDate); + } + try { + end = DateTime.parse(endDate); + } catch (Exception e) { + log.warn("Couldn't get a valid date from " + endDate); + } + return dao.filteredList(site, tutor, start, end, whom); + } + /** * Get a Chart.js compatible llist of average observation scores grouped by day. - * @param siteId Only observations for site. - * @param tutorId Only observations for tutor. + * + * @param siteId Only observations for site. + * @param tutorId Only observations for tutor. * @param startDate Only observations after startDate. - * @param endDate Only observations before endDate. - * @param whom Only observations of whom. + * @param endDate Only observations before endDate. + * @param whom Only observations of whom. * @return Chart.js stats object. */ @Path("/average") @GET @UnitOfWork public AverageStatsChartJs averageObservationScores(@QueryParam("site") Integer siteId, - @QueryParam("tutor") Integer tutorId, @QueryParam("startDate") String startDate, - @QueryParam("endDate") String endDate, @QueryParam("whom") String whom) { + @QueryParam("tutor") Integer tutorId, @QueryParam("startDate") String startDate, + @QueryParam("endDate") String endDate, @QueryParam("whom") String whom) { Site site = null; Tutor tutor = null; DateTime start = null;