Search in sources :

Example 1 with ElementWithIndex

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

the class QuestionnaireValidator method validateQuestionannaireResponseItems.

private void validateQuestionannaireResponseItems(ValidatorHostContext hostContext, QuestionnaireWithContext qsrc, List<QuestionnaireItemComponent> qItems, List<ValidationMessage> errors, Element element, NodeStack stack, boolean inProgress, Element questionnaireResponseRoot, QStack qstack) {
    List<Element> items = new ArrayList<Element>();
    element.getNamedChildren("item", items);
    // now, sort into stacks
    Map<String, List<ElementWithIndex>> map = new HashMap<String, List<ElementWithIndex>>();
    int lastIndex = -1;
    int counter = 0;
    for (Element item : items) {
        String linkId = item.getNamedChildValue("linkId");
        if (rule(errors, IssueType.REQUIRED, item.line(), item.col(), stack.getLiteralPath(), !Utilities.noString(linkId), I18nConstants.QUESTIONNAIRE_QR_ITEM_NOLINKID)) {
            int index = getLinkIdIndex(qItems, linkId);
            if (index == -1) {
                QuestionnaireItemComponent qItem = findQuestionnaireItem(qsrc, linkId);
                if (qItem != null) {
                    rule(errors, IssueType.STRUCTURE, item.line(), item.col(), stack.getLiteralPath(), index > -1, misplacedItemError(qItem));
                    NodeStack ns = stack.push(item, counter, null, null);
                    validateQuestionnaireResponseItem(hostContext, qsrc, qItem, errors, item, ns, inProgress, questionnaireResponseRoot, qstack.push(qItem, item));
                } else
                    rule(errors, IssueType.NOTFOUND, item.line(), item.col(), stack.getLiteralPath(), index > -1, I18nConstants.QUESTIONNAIRE_QR_ITEM_NOTFOUND, linkId);
            } else {
                rule(errors, IssueType.STRUCTURE, item.line(), item.col(), stack.getLiteralPath(), index >= lastIndex, I18nConstants.QUESTIONNAIRE_QR_ITEM_ORDER);
                lastIndex = index;
                // we'll treat it as not existing for the purposes of enableWhen validation
                if (item.hasChildren("answer") || item.hasChildren("item")) {
                    List<ElementWithIndex> mapItem = map.computeIfAbsent(linkId, key -> new ArrayList<>());
                    mapItem.add(new ElementWithIndex(item, counter));
                }
            }
        }
        counter++;
    }
    // ok, now we have a list of known items, grouped by linkId. We've made an error for anything out of order
    for (QuestionnaireItemComponent qItem : qItems) {
        List<ElementWithIndex> mapItem = map.get(qItem.getLinkId());
        validateQuestionnaireResponseItem(hostContext, qsrc, errors, element, stack, inProgress, questionnaireResponseRoot, qItem, mapItem, qstack);
    }
}
Also used : QuestionnaireItemComponent(org.hl7.fhir.r5.model.Questionnaire.QuestionnaireItemComponent) HashMap(java.util.HashMap) 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) ElementWithIndex(org.hl7.fhir.validation.instance.type.QuestionnaireValidator.ElementWithIndex)

Example 2 with ElementWithIndex

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

the class QuestionnaireValidator method validateQuestionnaireResponseItem.

public void validateQuestionnaireResponseItem(ValidatorHostContext hostContext, QuestionnaireWithContext qsrc, List<ValidationMessage> errors, Element element, NodeStack stack, boolean inProgress, Element questionnaireResponseRoot, QuestionnaireItemComponent qItem, List<ElementWithIndex> mapItem, QStack qstack) {
    boolean enabled = myEnableWhenEvaluator.isQuestionEnabled(hostContext, qItem, qstack, fpe);
    if (mapItem != null) {
        if (!enabled) {
            for (ElementWithIndex e : mapItem) {
                NodeStack ns = stack.push(e.getElement(), e.getElement().getIndex(), e.getElement().getProperty().getDefinition(), e.getElement().getProperty().getDefinition());
                rule(errors, IssueType.INVALID, e.getElement().line(), e.getElement().col(), ns.getLiteralPath(), enabled, I18nConstants.QUESTIONNAIRE_QR_ITEM_NOTENABLED2, qItem.getLinkId());
            }
        }
        // Recursively validate child items
        validateQuestionnaireResponseItem(hostContext, qsrc, qItem, errors, mapItem, stack, inProgress, questionnaireResponseRoot, qstack);
    } else {
        // item is missing, is the question enabled?
        if (enabled && qItem.getRequired()) {
            String message = context.formatMessage(I18nConstants.QUESTIONNAIRE_QR_ITEM_MISSING, qItem.getLinkId());
            if (inProgress) {
                warning(errors, IssueType.REQUIRED, element.line(), element.col(), stack.getLiteralPath(), false, message);
            } else {
                rule(errors, IssueType.REQUIRED, element.line(), element.col(), stack.getLiteralPath(), false, message);
            }
        }
    }
}
Also used : NodeStack(org.hl7.fhir.validation.instance.utils.NodeStack) ElementWithIndex(org.hl7.fhir.validation.instance.type.QuestionnaireValidator.ElementWithIndex)

Aggregations

ElementWithIndex (org.hl7.fhir.validation.instance.type.QuestionnaireValidator.ElementWithIndex)2 NodeStack (org.hl7.fhir.validation.instance.utils.NodeStack)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Element (org.hl7.fhir.r5.elementmodel.Element)1 QuestionnaireItemComponent (org.hl7.fhir.r5.model.Questionnaire.QuestionnaireItemComponent)1