use of de.symeda.sormas.api.sample.SampleSimilarityCriteria in project SORMAS-Project by hzi-braunschweig.
the class SampleFacadeEjb method getSimilarSamples.
@Override
public List<SampleDto> getSimilarSamples(SampleSimilarityCriteria criteria) {
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<Sample> cq = cb.createQuery(Sample.class);
final Root<Sample> root = cq.from(Sample.class);
cq.distinct(true);
SampleQueryContext sampleQueryContext = new SampleQueryContext(cb, cq, root);
SampleCriteria sampleCriteria = new SampleCriteria();
sampleCriteria.caze(criteria.getCaze()).contact(criteria.getContact()).eventParticipant(criteria.getEventParticipant());
Predicate filter = sampleService.createUserFilter(sampleQueryContext, sampleCriteria);
filter = CriteriaBuilderHelper.and(cb, filter, sampleService.buildCriteriaFilter(sampleCriteria, sampleQueryContext));
Predicate similarityFilter = null;
if (criteria.getLabSampleId() != null) {
similarityFilter = cb.equal(root.get(Sample.LAB_SAMPLE_ID), criteria.getLabSampleId());
}
Date sampleDateTime = criteria.getSampleDateTime();
SampleMaterial sampleMaterial = criteria.getSampleMaterial();
if (sampleDateTime != null && sampleMaterial != null) {
Predicate dateAndMaterialFilter = cb.and(cb.between(root.get(Sample.SAMPLE_DATE_TIME), DateHelper.getStartOfDay(DateHelper.subtractDays(sampleDateTime, SIMILARITY_DATE_TIME_THRESHOLD)), DateHelper.getEndOfDay(DateHelper.addDays(sampleDateTime, SIMILARITY_DATE_TIME_THRESHOLD))), cb.equal(root.get(Sample.SAMPLE_MATERIAL), sampleMaterial));
similarityFilter = CriteriaBuilderHelper.or(cb, similarityFilter, dateAndMaterialFilter);
}
filter = CriteriaBuilderHelper.and(cb, filter, similarityFilter);
if (filter != null) {
cq.where(filter);
}
List<Sample> samples = em.createQuery(cq).getResultList();
if (samples.size() == 0 && (sampleDateTime == null || sampleMaterial == null)) {
return getByCriteria(sampleCriteria);
}
Pseudonymizer pseudonymizer = Pseudonymizer.getDefault(userService::hasRight);
return samples.stream().map(s -> convertToDto(s, pseudonymizer)).collect(Collectors.toList());
}
use of de.symeda.sormas.api.sample.SampleSimilarityCriteria in project SORMAS-Project by hzi-braunschweig.
the class AbstractLabMessageProcessingFlow method pickOrCreateSample.
private CompletionStage<ProcessingResult<PickOrCreateSampleResult>> pickOrCreateSample(Consumer<SampleSimilarityCriteria> addSampleSearchCriteria, LabMessageDto labMessage) {
SampleSimilarityCriteria sampleCriteria = createSampleCriteria(labMessage);
addSampleSearchCriteria.accept(sampleCriteria);
List<SampleDto> samples = FacadeProvider.getSampleFacade().getSimilarSamples(sampleCriteria);
PickOrCreateSampleResult result = new PickOrCreateSampleResult();
if (samples.isEmpty()) {
result.setNewSample(true);
return ProcessingResult.continueWith(result).asCompletedFuture();
}
HandlerCallback<PickOrCreateSampleResult> callback = new HandlerCallback<>();
handlePickOrCreateSample(samples, labMessage, callback);
return callback.futureResult;
}
use of de.symeda.sormas.api.sample.SampleSimilarityCriteria in project SORMAS-Project by hzi-braunschweig.
the class AbstractLabMessageProcessingFlow method createSampleCriteria.
private SampleSimilarityCriteria createSampleCriteria(LabMessageDto labMessage) {
SampleSimilarityCriteria sampleCriteria = new SampleSimilarityCriteria();
sampleCriteria.setLabSampleId(labMessage.getLabSampleId());
sampleCriteria.setSampleDateTime(labMessage.getSampleDateTime());
sampleCriteria.setSampleMaterial(labMessage.getSampleMaterial());
return sampleCriteria;
}
use of de.symeda.sormas.api.sample.SampleSimilarityCriteria in project SORMAS-Project by hzi-braunschweig.
the class SampleFacadeEjbTest method testGetSimilarSamples.
@Test
public void testGetSimilarSamples() {
TestDataCreator.RDCF rdcf = creator.createRDCF();
UserDto officer = creator.createUser(rdcf, UserRole.SURVEILLANCE_OFFICER);
CaseDataDto caze = creator.createCase(officer.toReference(), creator.createPerson().toReference(), rdcf);
Date sampleDateTime1 = DateHelper.parseDate("11.02.2021", new SimpleDateFormat("dd.MM.yyyy"));
creator.createSample(caze.toReference(), officer.toReference(), rdcf.facility, (s) -> {
s.setLabSampleID("case_sample_id");
s.setSampleDateTime(sampleDateTime1);
s.setSampleMaterial(SampleMaterial.BLOOD);
});
Date sampleDateTime2 = DateHelper.parseDate("08.02.2021", new SimpleDateFormat("dd.MM.yyyy"));
creator.createSample(caze.toReference(), officer.toReference(), rdcf.facility, (s) -> {
s.setLabSampleID("case_sample_id_2");
s.setSampleDateTime(sampleDateTime2);
s.setSampleMaterial(SampleMaterial.BLOOD);
});
ContactReferenceDto contact = creator.createContact(officer.toReference(), creator.createPerson().toReference()).toReference();
SampleDto contactSample = creator.createSample(contact, sampleDateTime1, new Date(), officer.toReference(), SampleMaterial.BLOOD, rdcf.facility);
EventParticipantDto eventParticipant = creator.createEventParticipant(creator.createEvent(officer.toReference()).toReference(), creator.createPerson(), officer.toReference());
SampleDto eventParticipantSample = creator.createSample(eventParticipant.toReference(), sampleDateTime1, new Date(), officer.toReference(), SampleMaterial.BLOOD, rdcf.facility);
SampleSimilarityCriteria criteria = new SampleSimilarityCriteria();
criteria.caze(caze.toReference());
criteria.setLabSampleId("case_sample_id");
List<SampleDto> similarSamples = getSampleFacade().getSimilarSamples(criteria);
MatcherAssert.assertThat(similarSamples, hasSize(1));
// should return all samples for unknown lab sample id and missing date and material
criteria.setLabSampleId("unknown_id");
similarSamples = getSampleFacade().getSimilarSamples(criteria);
MatcherAssert.assertThat(similarSamples, hasSize(2));
criteria.setSampleMaterial(SampleMaterial.BLOOD);
criteria.setSampleDateTime(DateHelper.addDays(sampleDateTime2, 1));
similarSamples = getSampleFacade().getSimilarSamples(criteria);
MatcherAssert.assertThat(similarSamples, hasSize(2));
criteria.setSampleDateTime(DateHelper.subtractDays(sampleDateTime2, 1));
similarSamples = getSampleFacade().getSimilarSamples(criteria);
MatcherAssert.assertThat(similarSamples, hasSize(1));
criteria.setSampleDateTime(DateHelper.addDays(sampleDateTime1, 3));
similarSamples = getSampleFacade().getSimilarSamples(criteria);
MatcherAssert.assertThat(similarSamples, hasSize(0));
criteria.setSampleDateTime(DateHelper.subtractDays(sampleDateTime2, 3));
similarSamples = getSampleFacade().getSimilarSamples(criteria);
MatcherAssert.assertThat(similarSamples, hasSize(0));
// contact samples
SampleSimilarityCriteria contactSampleCriteria = new SampleSimilarityCriteria().contact(contact);
contactSampleCriteria.setSampleDateTime(sampleDateTime1);
contactSampleCriteria.setSampleMaterial(SampleMaterial.BLOOD);
List<SampleDto> contactSimilarSamples = getSampleFacade().getSimilarSamples(contactSampleCriteria);
MatcherAssert.assertThat(contactSimilarSamples, hasSize(1));
MatcherAssert.assertThat(contactSimilarSamples.get(0).getUuid(), is(contactSample.getUuid()));
// event participant samples
SampleSimilarityCriteria eventParticipantSampleCriteria = new SampleSimilarityCriteria().eventParticipant(eventParticipant.toReference());
eventParticipantSampleCriteria.setSampleDateTime(sampleDateTime1);
eventParticipantSampleCriteria.setSampleMaterial(SampleMaterial.BLOOD);
List<SampleDto> eventParticipantSimilarSamples = getSampleFacade().getSimilarSamples(eventParticipantSampleCriteria);
MatcherAssert.assertThat(eventParticipantSimilarSamples, hasSize(1));
MatcherAssert.assertThat(eventParticipantSimilarSamples.get(0).getUuid(), is(eventParticipantSample.getUuid()));
}
use of de.symeda.sormas.api.sample.SampleSimilarityCriteria in project SORMAS-Project by hzi-braunschweig.
the class LabMessageController method createSampleCriteria.
private SampleSimilarityCriteria createSampleCriteria(LabMessageDto labMessageDto) {
SampleSimilarityCriteria sampleCriteria = new SampleSimilarityCriteria();
sampleCriteria.setLabSampleId(labMessageDto.getLabSampleId());
sampleCriteria.setSampleDateTime(labMessageDto.getSampleDateTime());
sampleCriteria.setSampleMaterial(labMessageDto.getSampleMaterial());
return sampleCriteria;
}
Aggregations