diff --git a/hapi-fhir-validation/src/main/java/org/hl7/fhir/dstu3/validation/QuestionnaireResponseValidator.java b/hapi-fhir-validation/src/main/java/org/hl7/fhir/dstu3/validation/QuestionnaireResponseValidator.java index 152fab86d4e..c92e55c0375 100644 --- a/hapi-fhir-validation/src/main/java/org/hl7/fhir/dstu3/validation/QuestionnaireResponseValidator.java +++ b/hapi-fhir-validation/src/main/java/org/hl7/fhir/dstu3/validation/QuestionnaireResponseValidator.java @@ -36,6 +36,8 @@ import org.hl7.fhir.dstu3.model.Type; import org.hl7.fhir.dstu3.model.UriType; import org.hl7.fhir.dstu3.model.ValueSet; import org.hl7.fhir.dstu3.model.ValueSet.ValueSetExpansionContainsComponent; +import org.hl7.fhir.instance.validation.DefaultEnableWhenEvaluator; +import org.hl7.fhir.instance.validation.IEnableWhenEvaluator; import org.hl7.fhir.utilities.validation.ValidationMessage; import org.hl7.fhir.utilities.validation.ValidationMessage.IssueType; diff --git a/hapi-fhir-validation/src/main/java/org/hl7/fhir/dstu3/validation/DefaultEnableWhenEvaluator.java b/hapi-fhir-validation/src/main/java/org/hl7/fhir/instance/validation/DefaultEnableWhenEvaluator.java similarity index 56% rename from hapi-fhir-validation/src/main/java/org/hl7/fhir/dstu3/validation/DefaultEnableWhenEvaluator.java rename to hapi-fhir-validation/src/main/java/org/hl7/fhir/instance/validation/DefaultEnableWhenEvaluator.java index bd20aa7d56e..5e63f463c01 100644 --- a/hapi-fhir-validation/src/main/java/org/hl7/fhir/dstu3/validation/DefaultEnableWhenEvaluator.java +++ b/hapi-fhir-validation/src/main/java/org/hl7/fhir/instance/validation/DefaultEnableWhenEvaluator.java @@ -1,12 +1,14 @@ -package org.hl7.fhir.dstu3.validation; +package org.hl7.fhir.instance.validation; import org.hl7.fhir.dstu3.model.Questionnaire.QuestionnaireItemComponent; import org.hl7.fhir.dstu3.model.Questionnaire.QuestionnaireItemEnableWhenComponent; +import java.util.ArrayList; import java.util.List; import org.hl7.fhir.dstu3.model.QuestionnaireResponse; import org.hl7.fhir.dstu3.model.QuestionnaireResponse.QuestionnaireResponseItemComponent; +import org.hl7.fhir.r4.elementmodel.Element; public class DefaultEnableWhenEvaluator implements IEnableWhenEvaluator { @@ -54,6 +56,54 @@ public class DefaultEnableWhenEvaluator implements IEnableWhenEvaluator { } + return enabled; + } + + @Override + public boolean isQuestionEnabled(org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemComponent item, + Element element) { + boolean enabled = true; + + if(item.hasEnableWhen()) { + + enabled = false; + + for( org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemEnableWhenComponent enable : item.getEnableWhen()) { + + if(enable.hasAnswer()) { + // check if referenced question has answer + + String itemId = enable.getQuestion(); + + List items = new ArrayList(); + element.getNamedChildren("item", items); + + for(Element respItem : items) { + + // TODO toteuta uudelleen + + + } + + } else { + // and if not + + String itemId = enable.getQuestion(); + + List items = new ArrayList(); + element.getNamedChildren("item", items); + + for(Element respItem : items) { + + + } + + } + } + + } + + return enabled; } diff --git a/hapi-fhir-validation/src/main/java/org/hl7/fhir/dstu3/validation/IEnableWhenEvaluator.java b/hapi-fhir-validation/src/main/java/org/hl7/fhir/instance/validation/IEnableWhenEvaluator.java similarity index 65% rename from hapi-fhir-validation/src/main/java/org/hl7/fhir/dstu3/validation/IEnableWhenEvaluator.java rename to hapi-fhir-validation/src/main/java/org/hl7/fhir/instance/validation/IEnableWhenEvaluator.java index 202922727f4..10501484723 100644 --- a/hapi-fhir-validation/src/main/java/org/hl7/fhir/dstu3/validation/IEnableWhenEvaluator.java +++ b/hapi-fhir-validation/src/main/java/org/hl7/fhir/instance/validation/IEnableWhenEvaluator.java @@ -1,13 +1,18 @@ -package org.hl7.fhir.dstu3.validation; +package org.hl7.fhir.instance.validation; import java.util.List; import org.hl7.fhir.dstu3.model.Questionnaire.QuestionnaireItemComponent; import org.hl7.fhir.dstu3.model.QuestionnaireResponse; import org.hl7.fhir.dstu3.model.QuestionnaireResponse.QuestionnaireResponseItemComponent; +import org.hl7.fhir.r4.elementmodel.Element; public interface IEnableWhenEvaluator { public boolean isQuestionEnabled(QuestionnaireItemComponent item, List theResponseItems); + + public boolean isQuestionEnabled(org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemComponent qItem, + Element element); + } diff --git a/hapi-fhir-validation/src/main/java/org/hl7/fhir/r4/validation/InstanceValidator.java b/hapi-fhir-validation/src/main/java/org/hl7/fhir/r4/validation/InstanceValidator.java index a57decbc5cb..f6a60c1b5e0 100644 --- a/hapi-fhir-validation/src/main/java/org/hl7/fhir/r4/validation/InstanceValidator.java +++ b/hapi-fhir-validation/src/main/java/org/hl7/fhir/r4/validation/InstanceValidator.java @@ -10,6 +10,8 @@ import org.hl7.fhir.exceptions.DefinitionException; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.exceptions.PathEngineException; import org.hl7.fhir.exceptions.TerminologyServiceException; +import org.hl7.fhir.instance.validation.DefaultEnableWhenEvaluator; +import org.hl7.fhir.instance.validation.IEnableWhenEvaluator; import org.hl7.fhir.r4.conformance.ProfileUtilities; import org.hl7.fhir.r4.context.IWorkerContext; import org.hl7.fhir.r4.context.IWorkerContext.ValidationResult; @@ -198,6 +200,9 @@ public class InstanceValidator extends BaseValidator implements IResourceValidat private IEvaluationContext externalHostServices; private boolean noExtensibleWarnings; private String serverBase; + + private IEnableWhenEvaluator myEnableWhenEvaluator = new DefaultEnableWhenEvaluator(); + /* * Keeps track of whether a particular profile has been checked or not yet @@ -2777,9 +2782,14 @@ public class InstanceValidator extends BaseValidator implements IResourceValidat List mapItem = map.get(qItem.getLinkId()); if (mapItem != null) validateQuestionannaireResponseItem(qsrc, qItem, errors, mapItem, stack, inProgress); - else - rule(errors, IssueType.REQUIRED, element.line(), element.col(), stack.getLiteralPath(), !qItem.getRequired(), "No response found for required item "+qItem.getLinkId()); - } + else { + //item is missing, is the question enabled? + if(! myEnableWhenEvaluator.isQuestionEnabled(qItem, element)) { + + rule(errors, IssueType.REQUIRED, element.line(), element.col(), stack.getLiteralPath(), !qItem.getRequired(), "No response found for required item "+qItem.getLinkId()); + } + } + } } private void validateQuestionnaireResponseItemQuantity( List errors, Element answer, NodeStack stack) { diff --git a/hapi-fhir-validation/src/test/java/org/hl7/fhir/dstu3/hapi/validation/QuestionnaireResponseValidatorDstu3Test.java b/hapi-fhir-validation/src/test/java/org/hl7/fhir/dstu3/hapi/validation/QuestionnaireResponseValidatorDstu3Test.java index 6503df0d6d0..e5b67b9ce76 100644 --- a/hapi-fhir-validation/src/test/java/org/hl7/fhir/dstu3/hapi/validation/QuestionnaireResponseValidatorDstu3Test.java +++ b/hapi-fhir-validation/src/test/java/org/hl7/fhir/dstu3/hapi/validation/QuestionnaireResponseValidatorDstu3Test.java @@ -348,10 +348,6 @@ public class QuestionnaireResponseValidatorDstu3Test { enable.setQuestion("link0"); enable.setHasAnswer(true); - - //q.getItemFirstRep().addEnableWhen(). - //q.addItem().setLinkId("link1").setRequired(true).setType(QuestionnaireItemType.STRING); - QuestionnaireResponse qa = new QuestionnaireResponse(); qa.setStatus(QuestionnaireResponseStatus.COMPLETED); qa.getQuestionnaire().setReference("http://example.com/Questionnaire/q1");