VisitFacadeEjb implements VisitFacade { @Override public List<VisitIndexDto> getIndexList(VisitCriteria visitCriteria, Integer first, Integer max, List<SortProperty> sortProperties) { if (visitCriteria == null || visitCriteria.isEmpty()) { return new ArrayList<>(); } CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<VisitIndexDto> cq = cb.createQuery(VisitIndexDto.class); Root<Visit> visit = cq.from(Visit.class); Join<Visit, Symptoms> symptoms = visit.join(Visit.SYMPTOMS, JoinType.LEFT); cq.multiselect( visit.get(Visit.ID), visit.get(Visit.UUID), visit.get(Visit.VISIT_DATE_TIME), visit.get(Visit.VISIT_STATUS), visit.get(Visit.VISIT_REMARKS), visit.get(Visit.DISEASE), symptoms.get(Symptoms.SYMPTOMATIC), symptoms.get(Symptoms.TEMPERATURE), symptoms.get(Symptoms.TEMPERATURE_SOURCE)); cq.where(visitService.buildCriteriaFilter(visitCriteria, cb, visit)); if (sortProperties != null && sortProperties.size() > 0) { List<Order> order = new ArrayList<>(sortProperties.size()); for (SortProperty sortProperty : sortProperties) { Expression<?> expression; switch (sortProperty.propertyName) { case VisitIndexDto.VISIT_DATE_TIME: case VisitIndexDto.VISIT_STATUS: case VisitIndexDto.VISIT_REMARKS: case VisitIndexDto.DISEASE: expression = visit.get(sortProperty.propertyName); break; case VisitIndexDto.SYMPTOMATIC: case VisitIndexDto.TEMPERATURE: expression = symptoms.get(sortProperty.propertyName); break; default: throw new IllegalArgumentException(sortProperty.propertyName); } order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression)); } cq.orderBy(order); } else { cq.orderBy(cb.desc(visit.get(Visit.VISIT_DATE_TIME))); } final List<VisitIndexDto> indexList; if (first != null && max != null) { indexList = em.createQuery(cq).setFirstResult(first).setMaxResults(max).getResultList(); } else { indexList = em.createQuery(cq).getResultList(); } if (indexList.size() > 0) { List<Long> visitIds = indexList.stream().map(VisitIndexDto::getId).collect(Collectors.toList()); Map<Long, List<VisitContactJurisdictionDto>> contactJurisdictions = getVisitContactJurisdictions(visitIds); Map<Long, VisitCaseJurisdictionDto> caseJurisdictions = getVisitCaseJurisdictions(visitIds); Pseudonymizer pseudonymizer = Pseudonymizer.getDefault(userService::hasRight, I18nProperties.getCaption(Captions.inaccessibleValue)); indexList.forEach(visitIndex -> { VisitContactJurisdictionDto anyContactJurisdiction = null; VisitContactJurisdictionDto matchingContactJurisdiction = null; if (contactJurisdictions.containsKey(visitIndex.getId())) { List<VisitContactJurisdictionDto> visitContactJurisdictions = contactJurisdictions.get(visitIndex.getId()); matchingContactJurisdiction = visitContactJurisdictions.stream() .filter(c -> contactJurisdictionChecker.isInJurisdictionOrOwned(c)) .findFirst() .orElse(null); if (matchingContactJurisdiction == null) { anyContactJurisdiction = visitContactJurisdictions.get(0); } } if (matchingContactJurisdiction == null && caseJurisdictions.containsKey(visitIndex.getId())) { VisitCaseJurisdictionDto visitCaseJurisdiction = caseJurisdictions.get(visitIndex.getId()); if (caseJurisdictionChecker.isInJurisdictionOrOwned(visitCaseJurisdiction)) { matchingContactJurisdiction = new VisitContactJurisdictionDto(visitCaseJurisdiction.getVisitId(), null, null, null, null, visitCaseJurisdiction); } else if (anyContactJurisdiction == null) { anyContactJurisdiction = new VisitContactJurisdictionDto(visitCaseJurisdiction.getVisitId(), null, null, null, null, visitCaseJurisdiction); } } boolean inJurisdiction = matchingContactJurisdiction != null; visitIndex.setJurisdiction(matchingContactJurisdiction != null ? matchingContactJurisdiction : anyContactJurisdiction); pseudonymizer.pseudonymizeDto(VisitIndexDto.class, visitIndex, inJurisdiction, null); }); } return indexList; }  @Override List<String> getAllActiveUuids(); @Override List<VisitDto> getAllActiveVisitsAfter(Date date); @Override List<VisitDto> getByUuids(List<String> uuids); @Override VisitDto getLastVisitByContact(ContactReferenceDto contactRef); @Override List<VisitDto> getVisitsByContactAndPeriod(ContactReferenceDto contactRef, Date begin, Date end); @Override VisitDto getLastVisitByCase(CaseReferenceDto caseRef); @Override VisitDto getVisitByUuid(String uuid); @Override VisitDto saveVisit(VisitDto dto); @Override ExternalVisitDto saveExternalVisit(final ExternalVisitDto dto); @Override void validate(VisitDto visit); @Override void deleteVisit(String visitUuid); @Override List<VisitIndexDto> getIndexList(VisitCriteria visitCriteria, Integer first, Integer max, List<SortProperty> sortProperties); @Override long count(VisitCriteria visitCriteria); @Override @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) List<VisitExportDto> getVisitsExportList(
		VisitCriteria visitCriteria,
		VisitExportType exportType,
		int first,
		int max,
		ExportConfigurationDto exportConfiguration); Visit fromDto(@NotNull VisitDto source); VisitDto convertToDto(Visit source, Pseudonymizer pseudonymizer); static VisitReferenceDto toReferenceDto(Visit source); static VisitDto toDto(Visit source);  }
@Test public void testGetIndexList() { UserDto user = creator.createUser(creator.createRDCFEntities(), "Surv", "Sup", UserRole.SURVEILLANCE_SUPERVISOR); PersonDto person = creator.createPerson(); PersonDto person2 = creator.createPerson(); PersonDto person3 = creator.createPerson(); PersonDto person4 = creator.createPerson(); PersonDto person5 = creator.createPerson(); ContactDto contact = creator.createContact(user.toReference(), person.toReference()); creator.createContact(user.toReference(), person2.toReference()); ContactDto contact2 = creator.createContact(user.toReference(), person3.toReference()); CaseDataDto caze = creator.createCase(user.toReference(), person5.toReference(), creator.createRDCF()); creator.createCase(user.toReference(), person4.toReference(), creator.createRDCF()); CaseDataDto caze2 = creator.createCase(user.toReference(), person3.toReference(), creator.createRDCF()); creator.createVisit(person.toReference()); creator.createVisit(person.toReference()); creator.createVisit(person3.toReference()); creator.createVisit(person3.toReference()); creator.createVisit(person3.toReference()); creator.createVisit(person5.toReference()); VisitDto otherVisit = creator.createVisit(person2.toReference()); VisitDto otherVisit2 = creator.createVisit(person4.toReference()); List<VisitIndexDto> indexVisits = getVisitFacade().getIndexList(new VisitCriteria().contact(contact.toReference()), null, null, null); assertThat(indexVisits, hasSize(2)); assertThat(indexVisits.stream().map(v -> v.getUuid()).collect(Collectors.toList()), not(contains(otherVisit.getUuid()))); assertThat(indexVisits.stream().map(v -> v.getUuid()).collect(Collectors.toList()), not(contains(otherVisit2.getUuid()))); indexVisits = getVisitFacade().getIndexList(new VisitCriteria().caze(caze.toReference()), null, null, null); assertThat(indexVisits, hasSize(1)); assertThat(indexVisits.stream().map(v -> v.getUuid()).collect(Collectors.toList()), not(contains(otherVisit.getUuid()))); assertThat(indexVisits.stream().map(v -> v.getUuid()).collect(Collectors.toList()), not(contains(otherVisit2.getUuid()))); indexVisits = getVisitFacade().getIndexList(new VisitCriteria().contact(contact2.toReference()), null, null, null); assertThat(indexVisits, hasSize(3)); assertThat(indexVisits.stream().map(v -> v.getUuid()).collect(Collectors.toList()), not(contains(otherVisit.getUuid()))); assertThat(indexVisits.stream().map(v -> v.getUuid()).collect(Collectors.toList()), not(contains(otherVisit2.getUuid()))); List<VisitIndexDto> indexVisits2 = getVisitFacade().getIndexList(new VisitCriteria().caze(caze2.toReference()), null, null, null); assertEquals(indexVisits.size(), indexVisits2.size()); for (int i = 0; i < indexVisits.size(); ++i) { assertEquals(indexVisits.get(i).getUuid(), indexVisits2.get(i).getUuid()); } }