ContactService extends AbstractCoreAdoService<Contact> { public Set<Contact> getAllRelevantContacts(Person person, Disease disease, Date referenceDate) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Contact> cq = cb.createQuery(Contact.class); Root<Contact> contactRoot = cq.from(Contact.class); Predicate filter = and(cb, createDefaultFilter(cb, contactRoot), buildRelevantContactsFilter(person, disease, referenceDate, cb, contactRoot)); cq.where(filter); return new HashSet<>(em.createQuery(cq).getResultList()); } ContactService(); List<Contact> findBy(ContactCriteria contactCriteria, User user); List<Contact> getAllActiveContactsAfter(Date date); @Override Predicate createChangeDateFilter(CriteriaBuilder cb, From<?, Contact> from, Date date); @Override Predicate createChangeDateFilter(CriteriaBuilder cb, From<?, Contact> from, Timestamp date); List<String> getAllActiveUuids(User user); int getContactCountByCase(Case caze); List<Contact> getAllByResultingCase(Case caze); List<Object[]> getSourceCaseClassifications(List<Long> caseIds); List<Contact> getFollowUpBetween(@NotNull Date fromDate, @NotNull Date toDate); List<Contact> getByPersonAndDisease(Person person, Disease disease); Set<Contact> getAllRelevantContacts(Person person, Disease disease, Date referenceDate); List<String> getDeletedUuidsSince(User user, Date since); List<MapContactDto> getContactsForMap(
		Region region,
		District district,
		Disease disease,
		Date fromDate,
		Date toDate,
		User user,
		List<String> caseUuids); List<DashboardContactDto> getContactsForDashboard(Region region, District district, Disease disease, Date from, Date to, User user); Predicate getRegionDistrictDiseasePredicate(
		Region region,
		District district,
		Disease disease,
		CriteriaBuilder cb,
		Root<Contact> contact,
		Join<Contact, Case> caze); List<DashboardQuarantineDataDto> getQuarantineDataForDashBoard(
		Region region,
		District district,
		Disease disease,
		Date from,
		Date to,
		User user); Map<ContactStatus, Long> getNewContactCountPerStatus(ContactCriteria contactCriteria, User user); Map<ContactClassification, Long> getNewContactCountPerClassification(ContactCriteria contactCriteria, User user); Map<FollowUpStatus, Long> getNewContactCountPerFollowUpStatus(ContactCriteria contactCriteria, User user); int getFollowUpUntilCount(ContactCriteria contactCriteria, User user); void udpateContactStatus(Contact contact); void updateFollowUpUntilAndStatus(Contact contact); List<Contact> getAllByVisit(Visit visit); @SuppressWarnings("rawtypes") @Override Predicate createUserFilter(CriteriaBuilder cb, CriteriaQuery cq, From<?, Contact> contactPath); @SuppressWarnings("rawtypes") Predicate createUserFilterForJoin(CriteriaBuilder cb, CriteriaQuery cq, From<?, Contact> contactPath); @SuppressWarnings("rawtypes") Predicate createUserFilterWithoutCase(CriteriaBuilder cb, CriteriaQuery cq, From<?, Contact> contactPath); Predicate buildCriteriaFilter(ContactCriteria contactCriteria, CriteriaBuilder cb, Root<Contact> from); @Override void delete(Contact contact); Predicate createActiveContactsFilter(CriteriaBuilder cb, Root<Contact> root); Predicate createDefaultFilter(CriteriaBuilder cb, Root<Contact> root); Predicate isInJurisdictionOrOwned(CriteriaBuilder cb, CriteriaQuery<Long> cq, ContactJoins joins);  }
@Test public void testGetAllRelevantContacts() { UserDto user = creator.createUser(creator.createRDCFEntities(), UserRole.SURVEILLANCE_SUPERVISOR); Date referenceDate = DateHelper.subtractDays(new Date(), FollowUpLogic.ALLOWED_DATE_OFFSET * 2); PersonDto contactPerson = creator.createPerson(); Person contactPersonEntity = getPersonService().getByUuid(contactPerson.getUuid()); ContactDto contact1 = creator.createContact(user.toReference(), contactPerson.toReference()); contact1.setReportDateTime(DateHelper.addDays(referenceDate, FollowUpLogic.ALLOWED_DATE_OFFSET + 1)); contact1 = getContactFacade().saveContact(contact1); ContactDto contact2 = creator.createContact(user.toReference(), contactPerson.toReference()); contact2.setReportDateTime(referenceDate); contact2.setLastContactDate(DateHelper.addDays(referenceDate, FollowUpLogic.ALLOWED_DATE_OFFSET + 1)); contact2 = getContactFacade().saveContact(contact2); Set<Contact> contacts = getContactService().getAllRelevantContacts(contactPersonEntity, contact1.getDisease(), referenceDate); assertThat(contacts, empty()); contact1.setReportDateTime(DateHelper.addDays(referenceDate, FollowUpLogic.ALLOWED_DATE_OFFSET)); contact2.setLastContactDate(DateHelper.addDays(referenceDate, FollowUpLogic.ALLOWED_DATE_OFFSET)); contact1 = getContactFacade().saveContact(contact1); contact2 = getContactFacade().saveContact(contact2); contacts = getContactService().getAllRelevantContacts(contactPersonEntity, contact1.getDisease(), referenceDate); assertThat(contacts, hasSize(2)); contact1.setReportDateTime(DateHelper.subtractDays(referenceDate, FollowUpLogic.ALLOWED_DATE_OFFSET + 1)); contact2.setLastContactDate(DateHelper.subtractDays(referenceDate, FollowUpLogic.ALLOWED_DATE_OFFSET + 1)); contact1 = getContactFacade().saveContact(contact1); contact2 = getContactFacade().saveContact(contact2); contacts = getContactService().getAllRelevantContacts(contactPersonEntity, contact1.getDisease(), referenceDate); assertThat(contacts, hasSize(2)); contact1.setFollowUpUntil(DateHelper.subtractDays(referenceDate, FollowUpLogic.ALLOWED_DATE_OFFSET + 1)); contact1.setOverwriteFollowUpUntil(true); contact2.setFollowUpUntil(DateHelper.subtractDays(referenceDate, FollowUpLogic.ALLOWED_DATE_OFFSET + 1)); contact2.setOverwriteFollowUpUntil(true); contact1 = getContactFacade().saveContact(contact1); contact2 = getContactFacade().saveContact(contact2); contacts = getContactService().getAllRelevantContacts(contactPersonEntity, contact1.getDisease(), referenceDate); assertThat(contacts, empty()); contact1.setFollowUpUntil(DateHelper.subtractDays(referenceDate, FollowUpLogic.ALLOWED_DATE_OFFSET)); contact2.setFollowUpUntil(DateHelper.subtractDays(referenceDate, FollowUpLogic.ALLOWED_DATE_OFFSET)); contact1 = getContactFacade().saveContact(contact1); contact2 = getContactFacade().saveContact(contact2); contacts = getContactService().getAllRelevantContacts(contactPersonEntity, contact1.getDisease(), referenceDate); assertThat(contacts, hasSize(2)); contact1.setFollowUpUntil(DateHelper.addDays(referenceDate, FollowUpLogic.ALLOWED_DATE_OFFSET + 1)); contact2.setFollowUpUntil(DateHelper.addDays(referenceDate, FollowUpLogic.ALLOWED_DATE_OFFSET + 1)); contact1 = getContactFacade().saveContact(contact1); contact2 = getContactFacade().saveContact(contact2); contacts = getContactService().getAllRelevantContacts(contactPersonEntity, contact1.getDisease(), referenceDate); assertThat(contacts, hasSize(2)); contact1.setReportDateTime(DateHelper.subtractDays(referenceDate, FollowUpLogic.ALLOWED_DATE_OFFSET * 2)); contact1.setLastContactDate(DateHelper.subtractDays(referenceDate, FollowUpLogic.ALLOWED_DATE_OFFSET * 2)); contact1.setFollowUpUntil(DateHelper.subtractDays(referenceDate, FollowUpLogic.ALLOWED_DATE_OFFSET * 2)); contact2.setReportDateTime(DateHelper.subtractDays(referenceDate, FollowUpLogic.ALLOWED_DATE_OFFSET * 2)); contact2.setLastContactDate(DateHelper.subtractDays(referenceDate, FollowUpLogic.ALLOWED_DATE_OFFSET * 2)); contact2.setFollowUpUntil(DateHelper.subtractDays(referenceDate, FollowUpLogic.ALLOWED_DATE_OFFSET * 2)); contact1 = getContactFacade().saveContact(contact1); contact2 = getContactFacade().saveContact(contact2); contacts = getContactService().getAllRelevantContacts(contactPersonEntity, contact1.getDisease(), referenceDate); assertThat(contacts, empty()); PersonDto contactPerson2 = creator.createPerson(); ContactDto contact3 = creator.createContact(user.toReference(), contactPerson2.toReference()); contact3.setReportDateTime(referenceDate); contact3 = getContactFacade().saveContact(contact3); ContactDto contact4 = creator.createContact(user.toReference(), contactPerson.toReference(), Disease.CSM); contact4.setReportDateTime(referenceDate); contact4 = getContactFacade().saveContact(contact4); contacts = getContactService().getAllRelevantContacts(contactPersonEntity, contact1.getDisease(), referenceDate); assertThat(contacts, empty()); }