From 33de360b3daf08cbb3d5fe82d95ea24feff302cf Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Tue, 29 Oct 2024 13:00:55 -0600 Subject: [PATCH] Handle multiple answers --- .../extract/ProcessDefinitionItem.java | 26 ++++++++-------- .../extract/ProcessDefinitionItemTests.java | 30 +++++++++++++++++++ 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaireresponse/extract/ProcessDefinitionItem.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaireresponse/extract/ProcessDefinitionItem.java index bab9243e4..3e9012dac 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaireresponse/extract/ProcessDefinitionItem.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaireresponse/extract/ProcessDefinitionItem.java @@ -230,21 +230,23 @@ private void processChild( IBaseBackboneElement childItem, String path) { var answers = request.resolvePathList(childItem, "answer", IBaseBackboneElement.class); - var answerValue = answers.isEmpty() ? null : request.resolvePath(answers.get(0), "value"); - if (answerValue != null) { - // Check if answer type matches path types available and transform if necessary - var pathDefinition = (BaseRuntimeDeclaredChildDefinition) resourceDefinition.getChildByName(path); - if (pathDefinition instanceof RuntimeChildChoiceDefinition) { - var choices = ((RuntimeChildChoiceDefinition) pathDefinition).getChoices(); - if (!choices.contains(answerValue.getClass())) { + var pathDefinition = (BaseRuntimeDeclaredChildDefinition) resourceDefinition.getChildByName(path); + List> choices = pathDefinition instanceof RuntimeChildChoiceDefinition + ? ((RuntimeChildChoiceDefinition) pathDefinition).getChoices() + : new ArrayList<>(); + answers.stream().forEach(answer -> { + var answerValue = request.resolvePath(answer, "value"); + if (answerValue != null) { + // Check if answer type matches path types available and transform if necessary + if ((pathDefinition instanceof RuntimeChildChoiceDefinition + && !choices.contains(answerValue.getClass())) + || (pathDefinition instanceof RuntimeChildCompositeDatatypeDefinition + && !pathDefinition.getField().getType().equals(answerValue.getClass()))) { answerValue = transformValueToResource(request.getFhirVersion(), answerValue); } - } else if (pathDefinition instanceof RuntimeChildCompositeDatatypeDefinition - && !pathDefinition.getField().getType().equals(answerValue.getClass())) { - answerValue = transformValueToResource(request.getFhirVersion(), answerValue); + request.getModelResolver().setValue(resource, path, answerValue); } - request.getModelResolver().setValue(resource, path, answerValue); - } + }); } private String getDefinition( diff --git a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaireresponse/extract/ProcessDefinitionItemTests.java b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaireresponse/extract/ProcessDefinitionItemTests.java index 637a1465d..cf8e13a45 100644 --- a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaireresponse/extract/ProcessDefinitionItemTests.java +++ b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaireresponse/extract/ProcessDefinitionItemTests.java @@ -1,6 +1,7 @@ package org.opencds.cqf.fhir.cr.questionnaireresponse.extract; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; @@ -21,11 +22,14 @@ import org.hl7.fhir.r4.model.Condition; import org.hl7.fhir.r4.model.Expression; import org.hl7.fhir.r4.model.Extension; +import org.hl7.fhir.r4.model.Patient; import org.hl7.fhir.r4.model.Questionnaire; import org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemComponent; import org.hl7.fhir.r4.model.QuestionnaireResponse; +import org.hl7.fhir.r4.model.QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent; import org.hl7.fhir.r4.model.QuestionnaireResponse.QuestionnaireResponseItemComponent; import org.hl7.fhir.r4.model.Reference; +import org.hl7.fhir.r4.model.StringType; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -175,4 +179,30 @@ void testItemWithContextExtensionWithMultipleResources() { assertEquals(expectedCondition1, resources.get(0)); assertEquals(expectedCondition2, resources.get(1)); } + + @Test + void testItemWithContextExtensionWithMultipleAnswers() { + var fhirVersion = FhirVersionEnum.R4; + var patientId = "patient1"; + var item = new QuestionnaireItemComponent().setLinkId("1"); + item.setDefinition("http://hl7.org/fhir/Patient#Patient.name.given"); + var responseItem = new QuestionnaireResponseItemComponent().setLinkId("1"); + responseItem.addAnswer(new QuestionnaireResponseItemAnswerComponent().setValue(new StringType("test1"))); + responseItem.addAnswer(new QuestionnaireResponseItemAnswerComponent().setValue(new StringType("test2"))); + var questionnaire = new Questionnaire().setItem(Arrays.asList(item)); + var extension = + new Extension(Constants.SDC_QUESTIONNAIRE_ITEM_EXTRACTION_CONTEXT).setValue(new CodeType("Patient")); + questionnaire.addExtension(extension); + var response = new QuestionnaireResponse().setItem(Arrays.asList(responseItem)); + var request = newExtractRequestForVersion(fhirVersion, libraryEngine, response, questionnaire); + var resources = new ArrayList(); + var subjectId = Ids.newId(fhirVersion, "Patient", patientId); + var itemPair = new ItemPair(null, null); + fixture.processDefinitionItem(request, itemPair, resources, new Reference(subjectId)); + assertEquals(1, resources.size()); + assertInstanceOf(Patient.class, resources.get(0)); + var patient = (Patient) resources.get(0); + assertEquals("test1", patient.getNameFirstRep().getGiven().get(0).asStringValue()); + assertEquals("test2", patient.getNameFirstRep().getGiven().get(1).asStringValue()); + } }