use of de.symeda.sormas.api.sample.SampleIndexDto in project SORMAS-Project by hzi-braunschweig.
the class SampleFacadeEjbTest method testGetIndexList.
@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());
creator.createPathogenTest(sample.toReference(), caze);
PathogenTestDto test = creator.createPathogenTest(sample.toReference(), PathogenTestType.CQ_VALUE_DETECTION, caze.getDisease(), new Date(), rdcf.facility, caze.getReportingUser(), PathogenTestResultType.PENDING, "", false);
test.setCqValue(1.5F);
getPathogenTestFacade().savePathogenTest(test);
List<SampleIndexDto> sampleIndexDtos = getSampleFacade().getIndexList(new SampleCriteria(), 0, 100, null);
assertEquals(2, sampleIndexDtos.size());
// First sample should have an additional test
assertEquals(AdditionalTestingStatus.PERFORMED, sampleIndexDtos.get(1).getAdditionalTestingStatus());
assertEquals(PathogenTestType.CQ_VALUE_DETECTION, sampleIndexDtos.get(1).getTypeOfLastTest());
assertTrue(sampleIndexDtos.get(1).getLastTestCqValue().equals(1.5F));
}
use of de.symeda.sormas.api.sample.SampleIndexDto in project SORMAS-Project by hzi-braunschweig.
the class SampleFacadeEjbTest method testGetIndexListForCaseConvertedFromContact.
@Test
public void testGetIndexListForCaseConvertedFromContact() {
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", "Person1");
CaseDataDto caze = creator.createCase(user.toReference(), cazePerson.toReference(), Disease.EVD, CaseClassification.PROBABLE, InvestigationStatus.PENDING, new Date(), rdcf);
PersonDto contactPerson = creator.createPerson("Contact", "Person2");
ContactDto contact = creator.createContact(user.toReference(), contactPerson.toReference(), caze);
SampleDto cazeSample = creator.createSample(caze.toReference(), user.toReference(), rdcf.facility);
cazeSample.setSampleDateTime(DateHelper.subtractDays(new Date(), 5));
getSampleFacade().saveSample(cazeSample);
SampleDto sample = creator.createSample(contact.toReference(), DateHelper.subtractDays(new Date(), 4), new Date(), user.toReference(), SampleMaterial.BLOOD, rdcf.facility);
SampleDto referredSample = creator.createSample(contact.toReference(), DateHelper.subtractDays(new Date(), 3), new Date(), user.toReference(), SampleMaterial.BLOOD, rdcf.facility);
sample.setReferredTo(referredSample.toReference());
creator.createAdditionalTest(sample.toReference());
CaseDataDto caseDataDto = CaseDataDto.buildFromContact(contact);
caseDataDto.setResponsibleRegion(new RegionReferenceDto(rdcf.region.getUuid(), null, null));
caseDataDto.setResponsibleDistrict(new DistrictReferenceDto(rdcf.district.getUuid(), null, null));
caseDataDto.setFacilityType(rdcf.facility.getType());
caseDataDto.setHealthFacility(new FacilityReferenceDto(rdcf.facility.getUuid(), null, null));
caseDataDto.setReportingUser(user.toReference());
CaseDataDto caseConvertedFromContact = getCaseFacade().save(caseDataDto);
getCaseFacade().setSampleAssociations(contact.toReference(), caseConvertedFromContact.toReference());
final SampleCriteria samplesConnectedToConvertedCaseCriteria = new SampleCriteria().caze(caseConvertedFromContact.toReference());
assertEquals(2, getSampleFacade().count(samplesConnectedToConvertedCaseCriteria));
final ArrayList<SortProperty> sortProperties = new ArrayList<>();
sortProperties.add(new SortProperty(SampleIndexDto.SAMPLE_DATE_TIME));
sortProperties.add(new SortProperty(SampleIndexDto.ASSOCIATED_CONTACT));
final List<SampleIndexDto> samplesOfConvertedCase = getSampleFacade().getIndexList(samplesConnectedToConvertedCaseCriteria, 0, 100, sortProperties);
assertEquals(2, samplesOfConvertedCase.size());
final SampleIndexDto sample11 = samplesOfConvertedCase.get(0);
Assert.assertEquals(sample.getUuid(), sample11.getUuid());
Assert.assertEquals(caseConvertedFromContact.getUuid(), sample11.getAssociatedCase().getUuid());
Assert.assertEquals(contact.getUuid(), sample11.getAssociatedContact().getUuid());
final SampleIndexDto sample12 = samplesOfConvertedCase.get(1);
Assert.assertEquals(referredSample.getUuid(), sample12.getUuid());
Assert.assertEquals(contact.getUuid(), sample12.getAssociatedContact().getUuid());
Assert.assertEquals(caseConvertedFromContact.getUuid(), sample11.getAssociatedCase().getUuid());
}
use of de.symeda.sormas.api.sample.SampleIndexDto in project SORMAS-Project by hzi-braunschweig.
the class SampleService method getIndexList.
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);
SampleQueryContext sampleQueryContext = new SampleQueryContext(cb, cq, sample);
SampleJoins joins = sampleQueryContext.getJoins();
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), sample.get(Sample.SAMPLING_REASON), sample.get(Sample.SAMPLING_REASON_DETAILS), 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.getLab().get(Facility.UUID)));
// Tests count subquery
Subquery<Long> testCountSq = cq.subquery(Long.class);
Root<PathogenTest> testCountRoot = testCountSq.from(PathogenTest.class);
testCountSq.where(cb.equal(testCountRoot.get(PathogenTest.SAMPLE), sample), cb.isFalse(testCountRoot.get(PathogenTest.DELETED)));
testCountSq.select(cb.countDistinct(testCountRoot.get(PathogenTest.ID)));
selections.add(testCountSq.getSelection());
selections.addAll(getJurisdictionSelections(sampleQueryContext));
cq.multiselect(selections);
Predicate filter = createUserFilter(sampleQueryContext, sampleCriteria);
if (sampleCriteria != null) {
Predicate criteriaFilter = buildCriteriaFilter(sampleCriteria, sampleQueryContext);
filter = CriteriaBuilderHelper.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 = QueryHelper.getResultList(em, cq, first, max);
if (!samples.isEmpty()) {
CriteriaQuery<Object[]> testCq = cb.createQuery(Object[].class);
Root<PathogenTest> testRoot = testCq.from(PathogenTest.class);
Expression<String> sampleIdExpr = testRoot.get(PathogenTest.SAMPLE).get(Sample.UUID);
Path<Long> testType = testRoot.get(PathogenTest.TEST_TYPE);
Path<Date> cqValue = testRoot.get(PathogenTest.CQ_VALUE);
testCq.select(cb.array(testType, cqValue, sampleIdExpr));
testCq.where(cb.isFalse(testRoot.get(PathogenTest.DELETED)), sampleIdExpr.in(samples.stream().map(SampleIndexDto::getUuid).collect(Collectors.toList())));
testCq.orderBy(cb.desc(testRoot.get(PathogenTest.CHANGE_DATE)));
List<Object[]> testList = em.createQuery(testCq).getResultList();
Map<String, Object[]> tests = testList.stream().filter(distinctByKey(pathogenTest -> pathogenTest[2])).collect(Collectors.toMap(pathogenTest -> pathogenTest[2].toString(), Function.identity()));
for (SampleIndexDto indexDto : samples) {
Optional.ofNullable(tests.get(indexDto.getUuid())).ifPresent(test -> {
indexDto.setTypeOfLastTest((PathogenTestType) test[0]);
indexDto.setLastTestCqValue((Float) test[1]);
});
}
}
Pseudonymizer pseudonymizer = Pseudonymizer.getDefault(userService::hasRight, I18nProperties.getCaption(Captions.inaccessibleValue));
Pseudonymizer emptyValuePseudonymizer = Pseudonymizer.getDefault(userService::hasRight);
pseudonymizer.pseudonymizeDtoCollection(SampleIndexDto.class, samples, s -> s.getSampleJurisdictionFlagsDto().getInJurisdiction(), (s, ignored) -> {
final SampleJurisdictionFlagsDto sampleJurisdictionFlagsDto = s.getSampleJurisdictionFlagsDto();
if (s.getAssociatedCase() != null) {
emptyValuePseudonymizer.pseudonymizeDto(CaseReferenceDto.class, s.getAssociatedCase(), sampleJurisdictionFlagsDto.getCaseInJurisdiction(), null);
}
ContactReferenceDto associatedContact = s.getAssociatedContact();
if (associatedContact != null) {
emptyValuePseudonymizer.pseudonymizeDto(ContactReferenceDto.PersonName.class, associatedContact.getContactName(), sampleJurisdictionFlagsDto.getContactInJurisdiction(), null);
if (associatedContact.getCaseName() != null) {
pseudonymizer.pseudonymizeDto(ContactReferenceDto.PersonName.class, associatedContact.getCaseName(), sampleJurisdictionFlagsDto.getContactCaseInJurisdiction(), null);
}
}
if (s.getAssociatedEventParticipant() != null) {
emptyValuePseudonymizer.pseudonymizeDto(EventParticipantReferenceDto.class, s.getAssociatedEventParticipant(), sampleJurisdictionFlagsDto.getEvenParticipantInJurisdiction(), null);
}
}, true);
return samples;
}
use of de.symeda.sormas.api.sample.SampleIndexDto in project SORMAS-Project by hzi-braunschweig.
the class SampleGrid method setLazyDataProvider.
public void setLazyDataProvider() {
DataProvider<SampleIndexDto, SampleCriteria> dataProvider = DataProvider.fromFilteringCallbacks(query -> FacadeProvider.getSampleFacade().getIndexList(query.getFilter().orElse(null), query.getOffset(), query.getLimit(), query.getSortOrders().stream().map(sortOrder -> new SortProperty(sortOrder.getSorted(), sortOrder.getDirection() == SortDirection.ASCENDING)).collect(Collectors.toList())).stream(), query -> (int) FacadeProvider.getSampleFacade().count(query.getFilter().orElse(null)));
setDataProvider(dataProvider);
setSelectionMode(SelectionMode.NONE);
}
use of de.symeda.sormas.api.sample.SampleIndexDto in project SORMAS-Project by hzi-braunschweig.
the class SampleFacadeEjbPseudonymizationTest method testPseudonymizeIndexList.
@Test
public void testPseudonymizeIndexList() {
CaseDataDto caze1 = creator.createCase(user2.toReference(), creator.createPerson("John", "Smith").toReference(), rdcf2);
SampleDto sample1 = createCaseSample(caze1, user2);
CaseDataDto caze2 = creator.createCase(user1.toReference(), creator.createPerson("John", "Smith").toReference(), rdcf1);
ContactDto contact1 = creator.createContact(user2.toReference(), null, creator.createPerson("John", "Smith").toReference(), caze2, new Date(), new Date(), Disease.CORONAVIRUS, rdcf2);
SampleDto sample2 = createCaseSample(caze2, user1);
SampleDto sample3 = createContactSample(contact1);
ContactDto contact2 = creator.createContact(user1.toReference(), null, creator.createPerson("John", "Smith").toReference(), caze2, new Date(), new Date(), Disease.CORONAVIRUS, rdcf1);
SampleDto sample4 = createContactSample(contact2);
List<SampleIndexDto> indexList = getSampleFacade().getIndexList(new SampleCriteria(), null, null, Collections.emptyList());
SampleIndexDto index1 = indexList.stream().filter(t -> t.getUuid().equals(sample1.getUuid())).findFirst().get();
assertThat(index1.getAssociatedCase().getFirstName(), is("John"));
assertThat(index1.getAssociatedCase().getLastName(), is("Smith"));
SampleIndexDto index2 = indexList.stream().filter(t -> t.getUuid().equals(sample2.getUuid())).findFirst().get();
assertThat(index2.getAssociatedCase().getFirstName(), isEmptyString());
assertThat(index2.getAssociatedCase().getLastName(), isEmptyString());
SampleIndexDto index3 = indexList.stream().filter(t -> t.getUuid().equals(sample3.getUuid())).findFirst().get();
assertThat(index3.getAssociatedContact().getCaption(), containsString("John SMITH"));
SampleIndexDto index4 = indexList.stream().filter(t -> t.getUuid().equals(sample4.getUuid())).findFirst().get();
assertThat(index4.getAssociatedContact().getCaption(), is(DataHelper.getShortUuid(index4.getAssociatedContact().getUuid())));
}
Aggregations