Search in sources :

Example 1 with SampleSimilarityCriteria

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());
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) DtoHelper(de.symeda.sormas.backend.util.DtoHelper) RolesAllowed(javax.annotation.security.RolesAllowed) StringUtils(org.apache.commons.lang3.StringUtils) Valid(javax.validation.Valid) Page(de.symeda.sormas.api.common.Page) Predicate(javax.persistence.criteria.Predicate) UserFacadeEjb(de.symeda.sormas.backend.user.UserFacadeEjb) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) SormasToSormasOriginInfoFacadeEjb(de.symeda.sormas.backend.sormastosormas.origin.SormasToSormasOriginInfoFacadeEjb) EventParticipantReferenceDto(de.symeda.sormas.api.event.EventParticipantReferenceDto) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) FacilityService(de.symeda.sormas.backend.infrastructure.facility.FacilityService) DiseaseHelper(de.symeda.sormas.api.DiseaseHelper) FacilityHelper(de.symeda.sormas.api.infrastructure.facility.FacilityHelper) NotNull(javax.validation.constraints.NotNull) UserService(de.symeda.sormas.backend.user.UserService) User(de.symeda.sormas.backend.user.User) PathogenTestResultType(de.symeda.sormas.api.sample.PathogenTestResultType) CaseFacadeEjbLocal(de.symeda.sormas.backend.caze.CaseFacadeEjb.CaseFacadeEjbLocal) EventFacadeEjb(de.symeda.sormas.backend.event.EventFacadeEjb) SampleCriteria(de.symeda.sormas.api.sample.SampleCriteria) EventParticipant(de.symeda.sormas.backend.event.EventParticipant) Community(de.symeda.sormas.backend.infrastructure.community.Community) QueryHelper(de.symeda.sormas.backend.util.QueryHelper) Selection(javax.persistence.criteria.Selection) FacilityFacadeEjb(de.symeda.sormas.backend.infrastructure.facility.FacilityFacadeEjb) SampleFacade(de.symeda.sormas.api.sample.SampleFacade) EventParticipantFacadeEjbLocal(de.symeda.sormas.backend.event.EventParticipantFacadeEjb.EventParticipantFacadeEjbLocal) ArrayList(java.util.ArrayList) CaseReferenceDto(de.symeda.sormas.api.caze.CaseReferenceDto) Case(de.symeda.sormas.backend.caze.Case) SampleReferenceDto(de.symeda.sormas.api.sample.SampleReferenceDto) SampleListEntryDto(de.symeda.sormas.api.sample.SampleListEntryDto) LocalBean(javax.ejb.LocalBean) SampleIndexDto(de.symeda.sormas.api.sample.SampleIndexDto) SampleJurisdictionFlagsDto(de.symeda.sormas.api.sample.SampleJurisdictionFlagsDto) EJB(javax.ejb.EJB) Root(javax.persistence.criteria.Root) DataHelper(de.symeda.sormas.api.utils.DataHelper) EntityManager(javax.persistence.EntityManager) PersistenceContext(javax.persistence.PersistenceContext) SortProperty(de.symeda.sormas.api.utils.SortProperty) Captions(de.symeda.sormas.api.i18n.Captions) EventParticipantFacadeEjb(de.symeda.sormas.backend.event.EventParticipantFacadeEjb) ContactService(de.symeda.sormas.backend.contact.ContactService) SampleDto(de.symeda.sormas.api.sample.SampleDto) ContactFacadeEjb(de.symeda.sormas.backend.contact.ContactFacadeEjb) AbstractDomainObject(de.symeda.sormas.backend.common.AbstractDomainObject) Date(java.util.Date) I18nProperties(de.symeda.sormas.api.i18n.I18nProperties) LoggerFactory(org.slf4j.LoggerFactory) AccessDeniedException(de.symeda.sormas.api.utils.AccessDeniedException) Facility(de.symeda.sormas.backend.infrastructure.facility.Facility) NotificationService(de.symeda.sormas.backend.common.NotificationService) Contact(de.symeda.sormas.backend.contact.Contact) Stateless(javax.ejb.Stateless) CaseService(de.symeda.sormas.backend.caze.CaseService) NotificationDeliveryFailedException(de.symeda.sormas.backend.common.messaging.NotificationDeliveryFailedException) Person(de.symeda.sormas.backend.person.Person) CaseCriteria(de.symeda.sormas.api.caze.CaseCriteria) Collection(java.util.Collection) Region(de.symeda.sormas.backend.infrastructure.region.Region) District(de.symeda.sormas.backend.infrastructure.district.District) Pseudonymizer(de.symeda.sormas.backend.util.Pseudonymizer) Collectors(java.util.stream.Collectors) List(java.util.List) MessageContents(de.symeda.sormas.backend.common.messaging.MessageContents) CoreAdo(de.symeda.sormas.backend.common.CoreAdo) Location(de.symeda.sormas.backend.location.Location) ValidationRuntimeException(de.symeda.sormas.api.utils.ValidationRuntimeException) FacilityDto(de.symeda.sormas.api.infrastructure.facility.FacilityDto) ContactFacadeEjbLocal(de.symeda.sormas.backend.contact.ContactFacadeEjb.ContactFacadeEjbLocal) DateHelper(de.symeda.sormas.api.utils.DateHelper) AdditionalTestDto(de.symeda.sormas.api.sample.AdditionalTestDto) CaseFacadeEjb(de.symeda.sormas.backend.caze.CaseFacadeEjb) Function(java.util.function.Function) PathogenTestFacadeEjbLocal(de.symeda.sormas.backend.sample.PathogenTestFacadeEjb.PathogenTestFacadeEjbLocal) EventParticipantService(de.symeda.sormas.backend.event.EventParticipantService) AdditionalTestFacadeEjbLocal(de.symeda.sormas.backend.sample.AdditionalTestFacadeEjb.AdditionalTestFacadeEjbLocal) DeletionDetails(de.symeda.sormas.api.common.DeletionDetails) IterableHelper(de.symeda.sormas.backend.util.IterableHelper) CaseQueryContext(de.symeda.sormas.backend.caze.CaseQueryContext) CriteriaBuilderHelper(de.symeda.sormas.backend.common.CriteriaBuilderHelper) PathogenTestDto(de.symeda.sormas.api.sample.PathogenTestDto) SormasToSormasOriginInfoFacadeEjbLocal(de.symeda.sormas.backend.sormastosormas.origin.SormasToSormasOriginInfoFacadeEjb.SormasToSormasOriginInfoFacadeEjbLocal) ModelConstants(de.symeda.sormas.backend.util.ModelConstants) Logger(org.slf4j.Logger) Validations(de.symeda.sormas.api.i18n.Validations) MessageSubject(de.symeda.sormas.backend.common.messaging.MessageSubject) SampleMaterial(de.symeda.sormas.api.sample.SampleMaterial) Event(de.symeda.sormas.backend.event.Event) UserRight(de.symeda.sormas.api.user.UserRight) SampleExportDto(de.symeda.sormas.api.sample.SampleExportDto) NotificationType(de.symeda.sormas.api.user.NotificationType) ContactReferenceDto(de.symeda.sormas.api.contact.ContactReferenceDto) SampleSimilarityCriteria(de.symeda.sormas.api.sample.SampleSimilarityCriteria) ShareInfoHelper(de.symeda.sormas.backend.sormastosormas.share.shareinfo.ShareInfoHelper) Strings(de.symeda.sormas.api.i18n.Strings) Collections(java.util.Collections) Pseudonymizer(de.symeda.sormas.backend.util.Pseudonymizer) SampleCriteria(de.symeda.sormas.api.sample.SampleCriteria) Date(java.util.Date) Predicate(javax.persistence.criteria.Predicate) SampleMaterial(de.symeda.sormas.api.sample.SampleMaterial)

Example 2 with SampleSimilarityCriteria

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;
}
Also used : SampleSimilarityCriteria(de.symeda.sormas.api.sample.SampleSimilarityCriteria) SampleDto(de.symeda.sormas.api.sample.SampleDto)

Example 3 with SampleSimilarityCriteria

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;
}
Also used : SampleSimilarityCriteria(de.symeda.sormas.api.sample.SampleSimilarityCriteria)

Example 4 with SampleSimilarityCriteria

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()));
}
Also used : SampleSimilarityCriteria(de.symeda.sormas.api.sample.SampleSimilarityCriteria) CaseDataDto(de.symeda.sormas.api.caze.CaseDataDto) UserDto(de.symeda.sormas.api.user.UserDto) ContactReferenceDto(de.symeda.sormas.api.contact.ContactReferenceDto) TestDataCreator(de.symeda.sormas.backend.TestDataCreator) EventParticipantDto(de.symeda.sormas.api.event.EventParticipantDto) SimpleDateFormat(java.text.SimpleDateFormat) SampleDto(de.symeda.sormas.api.sample.SampleDto) Date(java.util.Date) AbstractBeanTest(de.symeda.sormas.backend.AbstractBeanTest) Test(org.junit.Test)

Example 5 with SampleSimilarityCriteria

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;
}
Also used : SampleSimilarityCriteria(de.symeda.sormas.api.sample.SampleSimilarityCriteria)

Aggregations

SampleSimilarityCriteria (de.symeda.sormas.api.sample.SampleSimilarityCriteria)5 SampleDto (de.symeda.sormas.api.sample.SampleDto)3 ContactReferenceDto (de.symeda.sormas.api.contact.ContactReferenceDto)2 DiseaseHelper (de.symeda.sormas.api.DiseaseHelper)1 CaseCriteria (de.symeda.sormas.api.caze.CaseCriteria)1 CaseDataDto (de.symeda.sormas.api.caze.CaseDataDto)1 CaseReferenceDto (de.symeda.sormas.api.caze.CaseReferenceDto)1 DeletionDetails (de.symeda.sormas.api.common.DeletionDetails)1 Page (de.symeda.sormas.api.common.Page)1 EventParticipantDto (de.symeda.sormas.api.event.EventParticipantDto)1 EventParticipantReferenceDto (de.symeda.sormas.api.event.EventParticipantReferenceDto)1 Captions (de.symeda.sormas.api.i18n.Captions)1 I18nProperties (de.symeda.sormas.api.i18n.I18nProperties)1 Strings (de.symeda.sormas.api.i18n.Strings)1 Validations (de.symeda.sormas.api.i18n.Validations)1 FacilityDto (de.symeda.sormas.api.infrastructure.facility.FacilityDto)1 FacilityHelper (de.symeda.sormas.api.infrastructure.facility.FacilityHelper)1 AdditionalTestDto (de.symeda.sormas.api.sample.AdditionalTestDto)1 PathogenTestDto (de.symeda.sormas.api.sample.PathogenTestDto)1 PathogenTestResultType (de.symeda.sormas.api.sample.PathogenTestResultType)1