SampleFacadeEjb implements SampleFacade { @Override public List<SampleIndexDto> getIndexList(SampleCriteria sampleCriteria, Integer first, Integer max, List<SortProperty> sortProperties) { final CriteriaBuilder cb = em.getCriteriaBuilder(); final CriteriaQuery<SampleIndexDto> cq = cb.createQuery(SampleIndexDto.class); final Root<Sample> sample = cq.from(Sample.class); SampleJoins joins = new SampleJoins(sample); final Join<Sample, Case> caze = joins.getCaze(); final Join<Case, District> caseDistrict = joins.getCaseDistrict(); final Join<Sample, Contact> contact = joins.getContact(); final Join<Contact, District> contactDistrict = joins.getContactDistrict(); final Join<Case, District> contactCaseDistrict = joins.getContactCaseDistrict(); final Join<EventParticipant, Event> event = joins.getEvent(); final Join<Location, District> eventDistrict = joins.getEventDistrict(); Expression<Object> diseaseSelect = cb.selectCase() .when(cb.isNotNull(caze), caze.get(Case.DISEASE)) .otherwise(cb.selectCase().when(cb.isNotNull(contact), contact.get(Contact.DISEASE)).otherwise(event.get(Event.DISEASE))); Expression<Object> diseaseDetailsSelect = cb.selectCase() .when(cb.isNotNull(caze), caze.get(Case.DISEASE_DETAILS)) .otherwise(cb.selectCase().when(cb.isNotNull(contact), contact.get(Contact.DISEASE_DETAILS)).otherwise(event.get(Event.DISEASE_DETAILS))); Expression<Object> districtSelect = cb.selectCase() .when(cb.isNotNull(caseDistrict), caseDistrict.get(District.NAME)) .otherwise( cb.selectCase() .when(cb.isNotNull(contactDistrict), contactDistrict.get(District.NAME)) .otherwise( cb.selectCase() .when(cb.isNotNull(contactCaseDistrict), contactCaseDistrict.get(District.NAME)) .otherwise(eventDistrict.get(District.NAME)))); cq.distinct(true); List<Selection<?>> selections = new ArrayList<>( Arrays.asList( sample.get(Sample.UUID), caze.get(Case.EPID_NUMBER), sample.get(Sample.LAB_SAMPLE_ID), sample.get(Sample.SAMPLE_DATE_TIME), sample.get(Sample.SHIPPED), sample.get(Sample.SHIPMENT_DATE), sample.get(Sample.RECEIVED), sample.get(Sample.RECEIVED_DATE), sample.get(Sample.SAMPLE_MATERIAL), sample.get(Sample.SAMPLE_PURPOSE), sample.get(Sample.SPECIMEN_CONDITION), joins.getLab().get(Facility.NAME), joins.getReferredSample().get(Sample.UUID), caze.get(Case.UUID), joins.getCasePerson().get(Person.FIRST_NAME), joins.getCasePerson().get(Person.LAST_NAME), joins.getContact().get(Contact.UUID), joins.getContactPerson().get(Person.FIRST_NAME), joins.getContactPerson().get(Person.LAST_NAME), joins.getEventParticipant().get(EventParticipant.UUID), joins.getEventParticipantPerson().get(Person.FIRST_NAME), joins.getEventParticipantPerson().get(Person.LAST_NAME), diseaseSelect, diseaseDetailsSelect, sample.get(Sample.PATHOGEN_TEST_RESULT), sample.get(Sample.ADDITIONAL_TESTING_REQUESTED), cb.isNotEmpty(sample.get(Sample.ADDITIONAL_TESTS)), districtSelect, joins.getReportingUser().get(User.UUID), joins.getLab().get(Facility.UUID))); selections.addAll(getCaseJurisdictionSelections(joins)); selections.addAll(getContactJurisdictionSelections(joins)); selections.addAll(getEventJurisdictionSelections(joins)); cq.multiselect(selections); Predicate filter = sampleService.createUserFilter(cq, cb, joins); if (sampleCriteria != null) { Predicate criteriaFilter = sampleService.buildCriteriaFilter(sampleCriteria, cb, joins); filter = AbstractAdoService.and(cb, filter, criteriaFilter); } if (filter != null) { cq.where(filter); } if (sortProperties != null && sortProperties.size() > 0) { List<Order> order = new ArrayList<>(sortProperties.size()); for (SortProperty sortProperty : sortProperties) { Expression<?> expression; switch (sortProperty.propertyName) { case SampleIndexDto.UUID: case SampleIndexDto.LAB_SAMPLE_ID: case SampleIndexDto.SHIPPED: case SampleIndexDto.RECEIVED: case SampleIndexDto.REFERRED: case SampleIndexDto.SAMPLE_DATE_TIME: case SampleIndexDto.SHIPMENT_DATE: case SampleIndexDto.RECEIVED_DATE: case SampleIndexDto.SAMPLE_MATERIAL: case SampleIndexDto.SAMPLE_PURPOSE: case SampleIndexDto.PATHOGEN_TEST_RESULT: case SampleIndexDto.ADDITIONAL_TESTING_STATUS: expression = sample.get(sortProperty.propertyName); break; case SampleIndexDto.DISEASE: expression = diseaseSelect; break; case SampleIndexDto.EPID_NUMBER: expression = caze.get(Case.EPID_NUMBER); break; case SampleIndexDto.ASSOCIATED_CASE: expression = joins.getCasePerson().get(Person.LAST_NAME); order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression)); expression = joins.getCasePerson().get(Person.FIRST_NAME); break; case SampleIndexDto.ASSOCIATED_CONTACT: expression = joins.getContactPerson().get(Person.LAST_NAME); order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression)); expression = joins.getContactPerson().get(Person.FIRST_NAME); break; case SampleIndexDto.ASSOCIATED_EVENT_PARTICIPANT: expression = joins.getEventParticipantPerson().get(Person.LAST_NAME); order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression)); expression = joins.getEventParticipantPerson().get(Person.FIRST_NAME); break; case SampleIndexDto.DISTRICT: expression = districtSelect; break; case SampleIndexDto.LAB: expression = joins.getLab().get(Facility.NAME); 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(sample.get(Sample.SAMPLE_DATE_TIME))); } List<SampleIndexDto> samples; if (first != null && max != null) { samples = em.createQuery(cq).setFirstResult(first).setMaxResults(max).getResultList(); } else { samples = em.createQuery(cq).getResultList(); } Pseudonymizer pseudonymizer = Pseudonymizer.getDefault(userService::hasRight, I18nProperties.getCaption(Captions.inaccessibleValue)); pseudonymizer.pseudonymizeDtoCollection( SampleIndexDto.class, samples, s -> sampleJurisdictionChecker.isInJurisdictionOrOwned(s.getJurisdiction()), null); return samples; }  @Override List<String> getAllActiveUuids(); @Override List<SampleDto> getAllActiveSamplesAfter(Date date); @Override List<SampleDto> getByUuids(List<String> uuids); @Override List<SampleDto> getByCaseUuids(List<String> caseUuids); @Override boolean exists(String uuid); @Override List<String> getDeletedUuidsSince(Date since); @Override SampleDto getSampleByUuid(String uuid); @Override SampleDto saveSample(SampleDto dto); SampleDto saveSample(SampleDto dto, boolean handleChanges); @Override SampleReferenceDto getReferenceByUuid(String uuid); @Override List<SampleIndexDto> getIndexList(SampleCriteria sampleCriteria, Integer first, Integer max, List<SortProperty> sortProperties); @Override void validate(SampleDto sample); @Override List<SampleExportDto> getExportList(SampleCriteria criteria, int first, int max); @Override List<SampleExportDto> getExportList(CaseCriteria criteria, int first, int max); @Override long count(SampleCriteria sampleCriteria); @Override SampleReferenceDto getReferredFrom(String sampleUuid); @Override void deleteSample(SampleReferenceDto sampleRef); @Override Map<PathogenTestResultType, Long> getNewTestResultCountByResultType(List<Long> caseIds); Sample fromDto(@NotNull SampleDto source); static SampleDto toDto(Sample source); static SampleReferenceDto toReferenceDto(Sample entity); @Override boolean isDeleted(String sampleUuid); Boolean isSampleEditAllowed(String sampleUuid); static final String CONTACT_CASE_REGION; static final String CONTACT_CASE_DISTRICT; static final String DISEASE; static final String DISEASE_DETAILS; static final String REGION; static final String DISTRICT; static final String DISTRICT_NAME; }
@Test public void testGetIndexList() { RDCFEntities rdcf = creator.createRDCFEntities("Region", "District", "Community", "Facility"); UserDto user = creator .createUser(rdcf.region.getUuid(), rdcf.district.getUuid(), rdcf.facility.getUuid(), "Surv", "Sup", UserRole.SURVEILLANCE_SUPERVISOR); PersonDto cazePerson = creator.createPerson("Case", "Person"); CaseDataDto caze = creator.createCase( user.toReference(), cazePerson.toReference(), Disease.EVD, CaseClassification.PROBABLE, InvestigationStatus.PENDING, new Date(), rdcf); SampleDto sample = creator.createSample(caze.toReference(), user.toReference(), rdcf.facility); SampleDto referredSample = creator.createSample(caze.toReference(), user.toReference(), rdcf.facility); sample.setReferredTo(referredSample.toReference()); creator.createAdditionalTest(sample.toReference()); creator.createAdditionalTest(sample.toReference()); List<SampleIndexDto> sampleIndexDtos = getSampleFacade().getIndexList(new SampleCriteria(), 0, 100, null); assertEquals(2, sampleIndexDtos.size()); assertEquals(AdditionalTestingStatus.PERFORMED, sampleIndexDtos.get(1).getAdditionalTestingStatus()); }