VisitService extends AbstractAdoService<Visit> { public Set<Visit> getAllRelevantVisits(Person person, Disease disease, Date startDate, Date endDate) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Visit> cq = cb.createQuery(Visit.class); Root<Visit> visitRoot = cq.from(Visit.class); cq.where(buildRelevantVisitsFilter(person, disease, startDate, endDate, cb, visitRoot)); return new HashSet<>(em.createQuery(cq).getResultList()); } VisitService(); List<String> getAllActiveUuids(User user); List<Visit> getAllActiveVisitsAfter(Date date); List<Visit> getAllByContact(Contact contact); Set<Visit> getAllRelevantVisits(Person person, Disease disease, Date startDate, Date endDate); Predicate buildRelevantVisitsFilter(Person person, Disease disease, Date startDate, Date endDate, CriteriaBuilder cb, Root<Visit> from); Predicate buildCriteriaFilter(VisitCriteria criteria, CriteriaBuilder cb, Root<Visit> from); @SuppressWarnings("rawtypes") @Override Predicate createUserFilter(CriteriaBuilder cb, CriteriaQuery cq, From<?, Visit> from); @Override Predicate createChangeDateFilter(CriteriaBuilder cb, From<?, Visit> visitPath, Timestamp date);  }
@Test public void testGetAllRelevantVisits() { Date startDate = DateHelper.subtractDays(new Date(), FollowUpLogic.ALLOWED_DATE_OFFSET * 2); Date endDate = DateHelper.addDays(startDate, 28); PersonDto visitPerson = creator.createPerson(); Person visitPersonEntity = getPersonService().getByUuid(visitPerson.getUuid()); PersonDto visitPerson3 = creator.createPerson(); Date startDate3 = DateHelper.subtractDays(new Date(), FollowUpLogic.ALLOWED_DATE_OFFSET * 2); Date endDate3 = DateHelper.addDays(startDate, 28); Person visitPersonEntity3 = getPersonService().getByUuid(visitPerson3.getUuid()); VisitDto visit = creator.createVisit(Disease.EVD, visitPerson.toReference()); visit.setVisitDateTime(DateHelper.subtractDays(startDate, FollowUpLogic.ALLOWED_DATE_OFFSET + 1)); getVisitFacade().saveVisit(visit); Set<Visit> visits = getVisitService().getAllRelevantVisits(visitPersonEntity, visit.getDisease(), startDate, endDate); assertThat(visits, empty()); VisitDto visit4 = creator.createVisit(Disease.EVD, visitPerson3.toReference()); visit4.setVisitDateTime(DateHelper.subtractDays(startDate, FollowUpLogic.ALLOWED_DATE_OFFSET + 1)); getVisitFacade().saveVisit(visit4); Set<Visit> visits4 = getVisitService().getAllRelevantVisits(visitPersonEntity3, visit4.getDisease(), startDate3, endDate3); assertThat(visits4, empty()); visit.setVisitDateTime(DateHelper.subtractDays(startDate, FollowUpLogic.ALLOWED_DATE_OFFSET)); getVisitFacade().saveVisit(visit); visits = getVisitService().getAllRelevantVisits(visitPersonEntity, visit.getDisease(), startDate, endDate); assertThat(visits, hasSize(1)); visit4.setVisitDateTime(DateHelper.subtractDays(startDate3, FollowUpLogic.ALLOWED_DATE_OFFSET)); getVisitFacade().saveVisit(visit4); visits4 = getVisitService().getAllRelevantVisits(visitPersonEntity3, visit4.getDisease(), startDate3, endDate3); assertThat(visits4, hasSize(1)); visit.setVisitDateTime(DateHelper.addDays(endDate, FollowUpLogic.ALLOWED_DATE_OFFSET + 1)); getVisitFacade().saveVisit(visit); visits = getVisitService().getAllRelevantVisits(visitPersonEntity, visit.getDisease(), startDate, endDate); assertThat(visits, empty()); visit4.setVisitDateTime(DateHelper.addDays(endDate3, FollowUpLogic.ALLOWED_DATE_OFFSET + 1)); getVisitFacade().saveVisit(visit4); visits4 = getVisitService().getAllRelevantVisits(visitPersonEntity3, visit4.getDisease(), startDate3, endDate3); assertThat(visits4, empty()); visit.setVisitDateTime(DateHelper.addDays(endDate, FollowUpLogic.ALLOWED_DATE_OFFSET)); getVisitFacade().saveVisit(visit); visits = getVisitService().getAllRelevantVisits(visitPersonEntity, visit.getDisease(), startDate, endDate); assertThat(visits, hasSize(1)); visit4.setVisitDateTime(DateHelper.addDays(endDate, FollowUpLogic.ALLOWED_DATE_OFFSET)); getVisitFacade().saveVisit(visit4); visits4 = getVisitService().getAllRelevantVisits(visitPersonEntity3, visit4.getDisease(), startDate3, endDate3); assertThat(visits4, hasSize(1)); visit.setVisitDateTime(DateHelper.addDays(startDate, 14)); getVisitFacade().saveVisit(visit); visits = getVisitService().getAllRelevantVisits(visitPersonEntity, visit.getDisease(), startDate, endDate); assertThat(visits, hasSize(1)); visit4.setVisitDateTime(DateHelper.addDays(startDate3, 14)); getVisitFacade().saveVisit(visit4); visits4 = getVisitService().getAllRelevantVisits(visitPersonEntity3, visit4.getDisease(), startDate3, endDate3); assertThat(visits4, hasSize(1)); PersonDto visitPerson2 = creator.createPerson(); VisitDto visit2 = creator.createVisit(Disease.EVD, visitPerson2.toReference()); VisitDto visit3 = creator.createVisit(Disease.CSM, visitPerson.toReference()); visit2.setVisitDateTime(DateHelper.addDays(startDate, 14)); visit3.setVisitDateTime(DateHelper.addDays(startDate, 14)); getVisitFacade().saveVisit(visit2); getVisitFacade().saveVisit(visit3); visits = getVisitService().getAllRelevantVisits(visitPersonEntity, visit.getDisease(), startDate, endDate); assertThat(visits, hasSize(1)); visit2 = creator.createVisit(Disease.EVD, visitPerson2.toReference()); visit3 = creator.createVisit(Disease.CSM, visitPerson3.toReference()); visit2.setVisitDateTime(DateHelper.addDays(startDate3, 14)); visit3.setVisitDateTime(DateHelper.addDays(startDate3, 14)); getVisitFacade().saveVisit(visit2); getVisitFacade().saveVisit(visit3); visits4 = getVisitService().getAllRelevantVisits(visitPersonEntity3, visit4.getDisease(), startDate3, endDate3); assertThat(visits4, hasSize(1)); }