Search in sources :

Example 11 with QuestionnaireWithContext

use of org.hl7.fhir.validation.instance.type.QuestionnaireValidator.QuestionnaireWithContext in project org.hl7.fhir.core by hapifhir.

the class QuestionnaireValidator method validateQuestionnaireResponseItem.

private void validateQuestionnaireResponseItem(ValidatorHostContext hostContext, QuestionnaireWithContext qsrc, QuestionnaireItemComponent qItem, List<ValidationMessage> errors, Element element, NodeStack stack, boolean inProgress, Element questionnaireResponseRoot, QStack qstack) {
    String text = element.getNamedChildValue("text");
    rule(errors, IssueType.INVALID, element.line(), element.col(), stack.getLiteralPath(), Utilities.noString(text) || text.equals(qItem.getText()), I18nConstants.QUESTIONNAIRE_QR_ITEM_TEXT, qItem.getLinkId());
    List<Element> answers = new ArrayList<Element>();
    element.getNamedChildren("answer", answers);
    if (inProgress)
        warning(errors, IssueType.REQUIRED, element.line(), element.col(), stack.getLiteralPath(), isAnswerRequirementFulfilled(qItem, answers), I18nConstants.QUESTIONNAIRE_QR_ITEM_MISSING, qItem.getLinkId());
    else if (myEnableWhenEvaluator.isQuestionEnabled(hostContext, qItem, qstack, fpe)) {
        rule(errors, IssueType.REQUIRED, element.line(), element.col(), stack.getLiteralPath(), isAnswerRequirementFulfilled(qItem, answers), I18nConstants.QUESTIONNAIRE_QR_ITEM_MISSING, qItem.getLinkId());
    } else if (!answers.isEmpty()) {
    // items without answers should be allowed, but not items with answers to questions that are disabled
    // it appears that this is always a duplicate error - it will always already have been reported, so no need to report it again?
    // GDG 2019-07-13
    // rule(errors, IssueType.INVALID, element.line(), element.col(), stack.getLiteralPath(), !isAnswerRequirementFulfilled(qItem, answers), I18nConstants.QUESTIONNAIRE_QR_ITEM_NOTENABLED, qItem.getLinkId());
    }
    if (answers.size() > 1)
        rule(errors, IssueType.INVALID, answers.get(1).line(), answers.get(1).col(), stack.getLiteralPath(), qItem.getRepeats(), I18nConstants.QUESTIONNAIRE_QR_ITEM_ONLYONEA);
    int i = 0;
    for (Element answer : answers) {
        NodeStack ns = stack.push(answer, i, null, null);
        if (qItem.getType() != null) {
            switch(qItem.getType()) {
                case GROUP:
                    rule(errors, IssueType.STRUCTURE, answer.line(), answer.col(), stack.getLiteralPath(), false, I18nConstants.QUESTIONNAIRE_QR_ITEM_GROUP);
                    break;
                case // nothing
                DISPLAY:
                    break;
                case BOOLEAN:
                    validateQuestionnaireResponseItemType(errors, answer, ns, "boolean");
                    break;
                case DECIMAL:
                    validateQuestionnaireResponseItemType(errors, answer, ns, "decimal");
                    break;
                case INTEGER:
                    validateQuestionnaireResponseItemType(errors, answer, ns, "integer");
                    break;
                case DATE:
                    validateQuestionnaireResponseItemType(errors, answer, ns, "date");
                    break;
                case DATETIME:
                    validateQuestionnaireResponseItemType(errors, answer, ns, "dateTime");
                    break;
                case TIME:
                    validateQuestionnaireResponseItemType(errors, answer, ns, "time");
                    break;
                case STRING:
                    validateQuestionnaireResponseItemType(errors, answer, ns, "string");
                    break;
                case TEXT:
                    validateQuestionnaireResponseItemType(errors, answer, ns, "text");
                    break;
                case URL:
                    validateQuestionnaireResponseItemType(errors, answer, ns, "uri");
                    break;
                case ATTACHMENT:
                    validateQuestionnaireResponseItemType(errors, answer, ns, "Attachment");
                    break;
                case REFERENCE:
                    validateQuestionnaireResponseItemType(errors, answer, ns, "Reference");
                    break;
                case QUANTITY:
                    if ("Quantity".equals(validateQuestionnaireResponseItemType(errors, answer, ns, "Quantity")))
                        if (qItem.hasExtension("???"))
                            validateQuestionnaireResponseItemQuantity(errors, answer, ns);
                    break;
                case CODING:
                    String itemType = validateQuestionnaireResponseItemType(errors, answer, ns, "Coding", "date", "time", "integer", "string");
                    if (itemType != null) {
                        if (itemType.equals("Coding"))
                            validateAnswerCode(errors, answer, ns, qsrc, qItem, false);
                        else if (itemType.equals("date"))
                            checkOption(errors, answer, ns, qsrc, qItem, "date");
                        else if (itemType.equals("time"))
                            checkOption(errors, answer, ns, qsrc, qItem, "time");
                        else if (itemType.equals("integer"))
                            checkOption(errors, answer, ns, qsrc, qItem, "integer");
                        else if (itemType.equals("string"))
                            checkOption(errors, answer, ns, qsrc, qItem, "string");
                    }
                    break;
                // case QUESTION:
                case NULL:
                    // no validation
                    break;
                case QUESTION:
                    throw new Error("Shouldn't get here?");
            }
        }
        if (qItem.getType() != QuestionnaireItemType.GROUP) {
            // if it's a group, we already have an error before getting here, so no need to hammer away on that
            validateQuestionannaireResponseItems(hostContext, qsrc, qItem.getItem(), errors, answer, stack, inProgress, questionnaireResponseRoot, qstack);
        }
        i++;
    }
    if (qItem.getType() == null) {
        fail(errors, IssueType.REQUIRED, element.line(), element.col(), stack.getLiteralPath(), false, I18nConstants.QUESTIONNAIRE_QR_ITEM_NOTYPE, qItem.getLinkId());
    } else if (qItem.getType() == QuestionnaireItemType.DISPLAY) {
        List<Element> items = new ArrayList<Element>();
        element.getNamedChildren("item", items);
        rule(errors, IssueType.STRUCTURE, element.line(), element.col(), stack.getLiteralPath(), items.isEmpty(), I18nConstants.QUESTIONNAIRE_QR_ITEM_DISPLAY, qItem.getLinkId());
    } else if (qItem.getType() != QuestionnaireItemType.GROUP) {
        List<Element> items = new ArrayList<Element>();
        element.getNamedChildren("item", items);
        rule(errors, IssueType.STRUCTURE, element.line(), element.col(), stack.getLiteralPath(), items.isEmpty(), I18nConstants.QUESTIONNAIRE_QR_ITEM_GROUP_ANSWER, qItem.getLinkId());
    } else {
        validateQuestionannaireResponseItems(hostContext, qsrc, qItem.getItem(), errors, element, stack, inProgress, questionnaireResponseRoot, qstack);
    }
}
Also used : Element(org.hl7.fhir.r5.elementmodel.Element) ArrayList(java.util.ArrayList) NodeStack(org.hl7.fhir.validation.instance.utils.NodeStack) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

Element (org.hl7.fhir.r5.elementmodel.Element)9 NodeStack (org.hl7.fhir.validation.instance.utils.NodeStack)9 ArrayList (java.util.ArrayList)7 FHIRException (org.hl7.fhir.exceptions.FHIRException)6 QuestionnaireItemAnswerOptionComponent (org.hl7.fhir.r5.model.Questionnaire.QuestionnaireItemAnswerOptionComponent)5 List (java.util.List)2 Coding (org.hl7.fhir.r5.model.Coding)2 ElementWithIndex (org.hl7.fhir.validation.instance.type.QuestionnaireValidator.ElementWithIndex)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 ValidationResult (org.hl7.fhir.r5.context.IWorkerContext.ValidationResult)1 DateType (org.hl7.fhir.r5.model.DateType)1 IntegerType (org.hl7.fhir.r5.model.IntegerType)1 Questionnaire (org.hl7.fhir.r5.model.Questionnaire)1 QuestionnaireItemComponent (org.hl7.fhir.r5.model.Questionnaire.QuestionnaireItemComponent)1 StringType (org.hl7.fhir.r5.model.StringType)1 TimeType (org.hl7.fhir.r5.model.TimeType)1 ValueSet (org.hl7.fhir.r5.model.ValueSet)1 ValidationContextCarrier (org.hl7.fhir.r5.utils.validation.ValidationContextCarrier)1 ValidationOptions (org.hl7.fhir.utilities.validation.ValidationOptions)1