Search in sources :

Example 1 with TestReportDto

use of de.symeda.sormas.api.labmessage.TestReportDto in project SORMAS-Project by hzi-braunschweig.

the class RelatedLabMessageHandler method getRelatedEntities.

// related entities
public RelatedEntities getRelatedEntities(LabMessageDto labMessage) {
    String reportId = labMessage.getReportId();
    String labSampleId = labMessage.getLabSampleId();
    if (StringUtils.isBlank(reportId) || StringUtils.isBlank(labSampleId)) {
        return null;
    }
    List<SampleDto> relatedSamples = FacadeProvider.getSampleFacade().getByLabSampleId(labSampleId);
    if (relatedSamples.size() != 1) {
        return null;
    }
    SampleDto relatedSample = relatedSamples.get(0);
    List<LabMessageDto> relatedLabMessages = FacadeProvider.getLabMessageFacade().getForSample(relatedSample.toReference()).stream().filter(otherLabMessage -> reportId.equals(otherLabMessage.getReportId()) && LabMessageStatus.PROCESSED.equals(otherLabMessage.getStatus())).collect(Collectors.toList());
    PersonDto relatedPerson;
    if (relatedSample.getAssociatedCase() != null) {
        relatedPerson = FacadeProvider.getPersonFacade().getByContext(PersonContext.CASE, relatedSample.getAssociatedCase().getUuid());
    } else if (relatedSample.getAssociatedContact() != null) {
        relatedPerson = FacadeProvider.getPersonFacade().getByContext(PersonContext.CONTACT, relatedSample.getAssociatedContact().getUuid());
    } else {
        relatedPerson = FacadeProvider.getPersonFacade().getByContext(PersonContext.EVENT_PARTICIPANT, relatedSample.getAssociatedEventParticipant().getUuid());
    }
    List<PathogenTestDto> relatedPathogenTests = new ArrayList<>();
    List<TestReportDto> unmatchedTestReports = new ArrayList<>();
    boolean pathogenTestMisMatch = false;
    List<TestReportDto> testReports = labMessage.getTestReports();
    List<PathogenTestDto> samplePathogenTests = FacadeProvider.getPathogenTestFacade().getAllBySample(relatedSample.toReference());
    for (TestReportDto testReport : testReports) {
        List<PathogenTestDto> matchedPathogenTests = StringUtils.isBlank(testReport.getExternalId()) ? Collections.emptyList() : samplePathogenTests.stream().filter(pt -> matchPathogenTest(testReport, pt)).collect(Collectors.toList());
        if (matchedPathogenTests.isEmpty()) {
            unmatchedTestReports.add(testReport);
        } else if (matchedPathogenTests.size() == 1) {
            relatedPathogenTests.add(matchedPathogenTests.get(0));
        } else {
            unmatchedTestReports.add(testReport);
            pathogenTestMisMatch = true;
        }
    }
    return new RelatedEntities(relatedSample, relatedPerson, relatedPathogenTests, unmatchedTestReports, pathogenTestMisMatch, CollectionUtils.isNotEmpty(relatedLabMessages));
}
Also used : TestReportDto(de.symeda.sormas.api.labmessage.TestReportDto) FacadeProvider(de.symeda.sormas.api.FacadeProvider) BiFunction(java.util.function.BiFunction) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) Supplier(java.util.function.Supplier) StringUtils(org.apache.commons.lang3.StringUtils) PersonDto(de.symeda.sormas.api.person.PersonDto) ArrayList(java.util.ArrayList) EntityDto(de.symeda.sormas.api.EntityDto) SampleReferenceDto(de.symeda.sormas.api.sample.SampleReferenceDto) CollectionUtils(org.apache.commons.collections.CollectionUtils) PersonContext(de.symeda.sormas.api.person.PersonContext) MutableObject(org.apache.commons.lang3.mutable.MutableObject) PathogenTestDto(de.symeda.sormas.api.sample.PathogenTestDto) LabMessageStatus(de.symeda.sormas.api.labmessage.LabMessageStatus) DataHelper(de.symeda.sormas.api.utils.DataHelper) CancellationException(java.util.concurrent.CancellationException) LabMessageDto(de.symeda.sormas.api.labmessage.LabMessageDto) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) SampleDto(de.symeda.sormas.api.sample.SampleDto) Mutable(org.apache.commons.lang3.mutable.Mutable) Optional(java.util.Optional) Collections(java.util.Collections) TestReportDto(de.symeda.sormas.api.labmessage.TestReportDto) PersonDto(de.symeda.sormas.api.person.PersonDto) LabMessageDto(de.symeda.sormas.api.labmessage.LabMessageDto) ArrayList(java.util.ArrayList) PathogenTestDto(de.symeda.sormas.api.sample.PathogenTestDto) SampleDto(de.symeda.sormas.api.sample.SampleDto)

Example 2 with TestReportDto

use of de.symeda.sormas.api.labmessage.TestReportDto in project SORMAS-Project by hzi-braunschweig.

the class RelatedLabMessageHandler method handle.

public CompletionStage<HandlerResult> handle(LabMessageDto labMessage) {
    RelatedEntities relatedEntities = getRelatedEntities(labMessage);
    if (relatedEntities == null) {
        return CompletableFuture.completedFuture(HandlerResult.NOT_HANDLED);
    }
    LabMessageMapper mapper = LabMessageMapper.forLabMessage(labMessage);
    ChainHandler chainHandler = new ChainHandler();
    Supplier<CompletionStage<Boolean>> correctionFlowConfirmationSupplier = createCachedCorrectionFlowConfirmationSupplier();
    CompletionStage<HandlerResult> correctionFlow = CompletableFuture.completedFuture(HandlerResult.NOT_HANDLED);
    if (relatedEntities.relatedLabMessagesFound && !relatedEntities.pathogenTestMisMatch) {
        correctionFlow = correctionFlow.thenCompose(result -> handlePersonCorrection(labMessage, relatedEntities.person, mapper, correctionFlowConfirmationSupplier, result, chainHandler)).thenCompose((personCorrectionResult) -> handleSampleCorrection(labMessage, relatedEntities.sample, mapper, correctionFlowConfirmationSupplier, personCorrectionResult, chainHandler));
        for (PathogenTestDto p : relatedEntities.pathogenTests) {
            Optional<TestReportDto> testReport = labMessage.getTestReports().stream().filter(t -> matchPathogenTest(t, p)).findFirst();
            if (testReport.isPresent()) {
                correctionFlow = correctionFlow.thenCompose((testCorrectionResult) -> handlePathogenTestCorrection(labMessage, testReport.get(), p, mapper, correctionFlowConfirmationSupplier, testCorrectionResult, chainHandler));
            }
        }
        for (TestReportDto r : relatedEntities.unmatchedTestReports) {
            correctionFlow = correctionFlow.thenCompose((result) -> {
                if (result == HandlerResult.HANDLED) {
                    // do not handle pathogen test creation if there were no corrections in the lab message
                    return handlePathogenTestCreation(labMessage, r, relatedEntities.sample, correctionFlowConfirmationSupplier, chainHandler);
                }
                return CompletableFuture.completedFuture(result);
            });
        }
    }
    return correctionFlow.thenCompose((result) -> {
        if (result == HandlerResult.HANDLED) {
            // ask to continue post processing
            return continueProcessingConfirmation.apply(labMessage, relatedEntities.sample.toReference()).thenCompose((doPostProcess) -> CompletableFuture.completedFuture(CorrectionResult.of(result, doPostProcess)));
        }
        // if no corrections found, then continue
        return CompletableFuture.completedFuture(CorrectionResult.of(result, true));
    }).thenCompose((correctionResult) -> {
        if (correctionResult.shouldContinue) {
            return shortcutFlowConfirmation.apply(relatedEntities.relatedLabMessagesFound).thenCompose(confirmed -> {
                if (confirmed) {
                    return chainHandler.run((chain) -> shortcutHandler.handle(labMessage, relatedEntities.sample, chain)).thenCompose(handled -> CompletableFuture.completedFuture(HandlerResult.HANDLED));
                }
                return CompletableFuture.completedFuture(HandlerResult.CONTINUE);
            });
        }
        return CompletableFuture.completedFuture(correctionResult.result);
    }).exceptionally(e -> {
        if (e.getCause() instanceof CancellationException) {
            return chainHandler.savePerformed ? HandlerResult.CANCELED_WITH_UPDATES : HandlerResult.CANCELED;
        }
        throw (RuntimeException) e;
    });
}
Also used : TestReportDto(de.symeda.sormas.api.labmessage.TestReportDto) FacadeProvider(de.symeda.sormas.api.FacadeProvider) BiFunction(java.util.function.BiFunction) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) Supplier(java.util.function.Supplier) StringUtils(org.apache.commons.lang3.StringUtils) PersonDto(de.symeda.sormas.api.person.PersonDto) ArrayList(java.util.ArrayList) EntityDto(de.symeda.sormas.api.EntityDto) SampleReferenceDto(de.symeda.sormas.api.sample.SampleReferenceDto) CollectionUtils(org.apache.commons.collections.CollectionUtils) PersonContext(de.symeda.sormas.api.person.PersonContext) MutableObject(org.apache.commons.lang3.mutable.MutableObject) PathogenTestDto(de.symeda.sormas.api.sample.PathogenTestDto) LabMessageStatus(de.symeda.sormas.api.labmessage.LabMessageStatus) DataHelper(de.symeda.sormas.api.utils.DataHelper) CancellationException(java.util.concurrent.CancellationException) LabMessageDto(de.symeda.sormas.api.labmessage.LabMessageDto) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) SampleDto(de.symeda.sormas.api.sample.SampleDto) Mutable(org.apache.commons.lang3.mutable.Mutable) Optional(java.util.Optional) Collections(java.util.Collections) TestReportDto(de.symeda.sormas.api.labmessage.TestReportDto) PathogenTestDto(de.symeda.sormas.api.sample.PathogenTestDto) CancellationException(java.util.concurrent.CancellationException) CompletionStage(java.util.concurrent.CompletionStage)

Example 3 with TestReportDto

use of de.symeda.sormas.api.labmessage.TestReportDto in project SORMAS-Project by hzi-braunschweig.

the class LabMessageFacadeEjb method fromDto.

LabMessage fromDto(@NotNull LabMessageDto source, LabMessage target, boolean checkChangeDate) {
    target = DtoHelper.fillOrBuildEntity(source, target, LabMessage::new, checkChangeDate);
    target.setType(source.getType());
    target.setLabMessageDetails(source.getLabMessageDetails());
    target.setLabSampleId(source.getLabSampleId());
    target.setTestedDisease(source.getTestedDisease());
    target.setMessageDateTime(source.getMessageDateTime());
    target.setPersonBirthDateDD(source.getPersonBirthDateDD());
    target.setPersonBirthDateMM(source.getPersonBirthDateMM());
    target.setPersonBirthDateYYYY(source.getPersonBirthDateYYYY());
    target.setPersonCity(source.getPersonCity());
    target.setPersonFirstName(source.getPersonFirstName());
    target.setPersonHouseNumber(source.getPersonHouseNumber());
    target.setPersonLastName(source.getPersonLastName());
    target.setPersonPostalCode(source.getPersonPostalCode());
    target.setPersonSex(source.getPersonSex());
    target.setPersonStreet(source.getPersonStreet());
    target.setStatus(source.getStatus());
    target.setSampleDateTime(source.getSampleDateTime());
    target.setSampleMaterial(source.getSampleMaterial());
    target.setSampleMaterialText(source.getSampleMaterialText());
    target.setSampleReceivedDate(source.getSampleReceivedDate());
    target.setSpecimenCondition(source.getSpecimenCondition());
    target.setPersonPhone(source.getPersonPhone());
    target.setPersonEmail(source.getPersonEmail());
    target.setLabCity(source.getLabCity());
    target.setLabExternalId(source.getLabExternalId());
    target.setLabName(source.getLabName());
    target.setLabPostalCode(source.getLabPostalCode());
    if (source.getTestReports() != null) {
        List<TestReport> testReports = new ArrayList<>();
        for (TestReportDto t : source.getTestReports()) {
            TestReport testReport = testReportFacade.fromDto(t, target, false);
            testReports.add(testReport);
        }
        target.setTestReports(testReports);
    }
    target.setReportId(source.getReportId());
    target.setSampleOverallTestResult(source.getSampleOverallTestResult());
    if (source.getAssignee() != null) {
        target.setAssignee(userService.getByReferenceDto(source.getAssignee()));
    } else {
        target.setAssignee(null);
    }
    if (source.getSample() != null) {
        target.setSample(sampleService.getByReferenceDto(source.getSample()));
    }
    return target;
}
Also used : TestReportDto(de.symeda.sormas.api.labmessage.TestReportDto) ArrayList(java.util.ArrayList)

Example 4 with TestReportDto

use of de.symeda.sormas.api.labmessage.TestReportDto in project SORMAS-Project by hzi-braunschweig.

the class TestReportFacadeEjb method toDto.

public static TestReportDto toDto(TestReport source) {
    if (source == null) {
        return null;
    }
    TestReportDto target = new TestReportDto();
    DtoHelper.fillDto(target, source);
    target.setLabMessage(LabMessageFacadeEjb.toReferenceDto(source.getLabMessage()));
    target.setTestLabName(source.getTestLabName());
    target.setTestLabExternalId(source.getTestLabExternalId());
    target.setTestLabPostalCode(source.getTestLabPostalCode());
    target.setTestLabCity(source.getTestLabCity());
    target.setTestType(source.getTestType());
    target.setTestDateTime(source.getTestDateTime());
    target.setTestResult(source.getTestResult());
    target.setTestResultVerified(source.isTestResultVerified());
    target.setTestResultText(source.getTestResultText());
    target.setTypingId(source.getTypingId());
    target.setExternalId(source.getExternalId());
    target.setExternalOrderId(source.getExternalOrderId());
    target.setTestedDiseaseVariant(source.getTestedDiseaseVariant());
    target.setTestedDiseaseVariantDetails(source.getTestedDiseaseVariantDetails());
    target.setPreliminary(source.getPreliminary());
    target.setTestPcrTestSpecification(source.getTestPcrTestSpecification());
    return target;
}
Also used : TestReportDto(de.symeda.sormas.api.labmessage.TestReportDto)

Example 5 with TestReportDto

use of de.symeda.sormas.api.labmessage.TestReportDto in project SORMAS-Project by hzi-braunschweig.

the class LabMessageFacadeEjbMappingTest method testFromDto.

@Test
public void testFromDto() {
    LabMessageDto source = new LabMessageDto();
    TestReport testReport = new TestReport();
    TestReportDto testReportDto = TestReportFacadeEjb.toDto(testReport);
    Sample sample = new Sample();
    sample.setUuid("Uuid");
    SampleReferenceDto sampleRef = sample.toReference();
    User assignee = new User();
    assignee.setUuid("12345");
    when(sampleService.getByReferenceDto(sampleRef)).thenReturn(sample);
    when(testReportFacade.fromDto(eq(testReportDto), any(LabMessage.class), eq(false))).thenReturn(testReport);
    when(userservice.getByReferenceDto(assignee.toReference())).thenReturn(assignee);
    source.addTestReport(testReportDto);
    source.setCreationDate(new Date());
    source.setChangeDate(new Date());
    source.setUuid("UUID");
    source.setMessageDateTime(new Date());
    source.setSampleDateTime(new Date());
    source.setSampleReceivedDate(new Date());
    source.setLabSampleId("Lab Sample Id");
    source.setSampleMaterial(SampleMaterial.NASAL_SWAB);
    source.setSampleMaterialText("Sample material text");
    source.setLabName("Test Lab Name");
    source.setLabExternalId("Test Lab External Id");
    source.setLabPostalCode("Test Lab Postal Code");
    source.setLabCity("Test Lab City");
    source.setSpecimenCondition(SpecimenCondition.ADEQUATE);
    source.setTestedDisease(Disease.CORONAVIRUS);
    source.setPersonFirstName("Person First Name");
    source.setPersonLastName("Person Last Name");
    source.setPersonSex(Sex.OTHER);
    source.setPersonBirthDateDD(1);
    source.setPersonBirthDateDD(1);
    source.setPersonBirthDateYYYY(1970);
    source.setPersonPostalCode("Person Postal Code");
    source.setPersonCity("Person City");
    source.setPersonStreet("Person Street");
    source.setPersonHouseNumber("Person House Number");
    source.setPersonPhone("0123456789");
    source.setPersonEmail("mail@domain.com");
    source.setLabMessageDetails("Lab Message Details");
    source.setSampleOverallTestResult(PathogenTestResultType.POSITIVE);
    source.setSample(sampleRef);
    source.setAssignee(assignee.toReference());
    source.setType(ExternalMessageType.LAB_MESSAGE);
    LabMessage result = sut.fromDto(source, null, true);
    assertEquals(source.getTestReports(), result.getTestReports());
    assertNotSame(source.getCreationDate().getTime(), result.getCreationDate().getTime());
    assertNotSame(source.getChangeDate(), result.getChangeDate());
    assertEquals(source.getUuid(), result.getUuid());
    assertEquals(source.getMessageDateTime(), result.getMessageDateTime());
    assertEquals(source.getSampleDateTime(), result.getSampleDateTime());
    assertEquals(source.getSampleReceivedDate(), result.getSampleReceivedDate());
    assertEquals(source.getLabSampleId(), result.getLabSampleId());
    assertEquals(source.getSampleMaterial(), result.getSampleMaterial());
    assertEquals(source.getSampleMaterialText(), result.getSampleMaterialText());
    assertEquals(source.getLabName(), result.getLabName());
    assertEquals(source.getLabExternalId(), result.getLabExternalId());
    assertEquals(source.getLabPostalCode(), result.getLabPostalCode());
    assertEquals(source.getLabCity(), result.getLabCity());
    assertEquals(source.getSpecimenCondition(), result.getSpecimenCondition());
    assertEquals(source.getTestedDisease(), result.getTestedDisease());
    assertEquals(source.getPersonFirstName(), result.getPersonFirstName());
    assertEquals(source.getPersonLastName(), result.getPersonLastName());
    assertEquals(source.getPersonSex(), result.getPersonSex());
    assertEquals(source.getPersonBirthDateDD(), result.getPersonBirthDateDD());
    assertEquals(source.getPersonBirthDateMM(), result.getPersonBirthDateMM());
    assertEquals(source.getPersonBirthDateYYYY(), result.getPersonBirthDateYYYY());
    assertEquals(source.getPersonPostalCode(), result.getPersonPostalCode());
    assertEquals(source.getPersonCity(), result.getPersonCity());
    assertEquals(source.getPersonStreet(), result.getPersonStreet());
    assertEquals(source.getPersonHouseNumber(), result.getPersonHouseNumber());
    assertEquals(source.getLabMessageDetails(), result.getLabMessageDetails());
    assertEquals(source.getSampleOverallTestResult(), result.getSampleOverallTestResult());
    assertEquals(sample, result.getSample());
    assertEquals(assignee.getUuid(), result.getAssignee().getUuid());
    assertEquals(source.getType(), result.getType());
}
Also used : SampleReferenceDto(de.symeda.sormas.api.sample.SampleReferenceDto) TestReportDto(de.symeda.sormas.api.labmessage.TestReportDto) User(de.symeda.sormas.backend.user.User) Sample(de.symeda.sormas.backend.sample.Sample) LabMessageDto(de.symeda.sormas.api.labmessage.LabMessageDto) Date(java.util.Date) Test(org.junit.Test)

Aggregations

TestReportDto (de.symeda.sormas.api.labmessage.TestReportDto)30 LabMessageDto (de.symeda.sormas.api.labmessage.LabMessageDto)25 Test (org.junit.Test)22 SampleDto (de.symeda.sormas.api.sample.SampleDto)20 PathogenTestDto (de.symeda.sormas.api.sample.PathogenTestDto)16 AbstractBeanTest (de.symeda.sormas.ui.AbstractBeanTest)16 PersonDto (de.symeda.sormas.api.person.PersonDto)10 Date (java.util.Date)10 List (java.util.List)9 FacadeProvider (de.symeda.sormas.api.FacadeProvider)8 HandlerResult (de.symeda.sormas.ui.labmessage.processing.AbstractRelatedLabMessageHandler.HandlerResult)8 CompletionStage (java.util.concurrent.CompletionStage)8 Supplier (java.util.function.Supplier)8 LabMessageStatus (de.symeda.sormas.api.labmessage.LabMessageStatus)7 DataHelper (de.symeda.sormas.api.utils.DataHelper)7 ArrayList (java.util.ArrayList)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 Consumer (java.util.function.Consumer)7 SampleReferenceDto (de.symeda.sormas.api.sample.SampleReferenceDto)6 BiFunction (java.util.function.BiFunction)6