Search in sources :

Example 61 with ValidationResult

use of org.hl7.fhir.r4b.context.IWorkerContext.ValidationResult in project org.hl7.fhir.core by hapifhir.

the class InstanceValidator method checkPrimitiveBinding.

private void checkPrimitiveBinding(ValidatorHostContext hostContext, List<ValidationMessage> errors, String path, String type, ElementDefinition elementContext, Element element, StructureDefinition profile, NodeStack stack) {
    // We ignore bindings that aren't on string, uri or code
    if (!element.hasPrimitiveValue() || !("code".equals(type) || "string".equals(type) || "uri".equals(type) || "url".equals(type) || "canonical".equals(type))) {
        return;
    }
    if (noTerminologyChecks)
        return;
    String value = element.primitiveValue();
    // System.out.println("check "+value+" in "+path);
    // firstly, resolve the value set
    ElementDefinitionBindingComponent binding = elementContext.getBinding();
    if (binding.hasValueSet()) {
        ValueSet vs = resolveBindingReference(profile, binding.getValueSet(), profile.getUrl());
        if (vs == null) {
            CodeSystem cs = context.fetchCodeSystem(binding.getValueSet());
            if (rule(errors, IssueType.CODEINVALID, element.line(), element.col(), path, cs == null, I18nConstants.TERMINOLOGY_TX_VALUESET_NOTFOUND_CS, describeReference(binding.getValueSet()))) {
                warning(errors, IssueType.CODEINVALID, element.line(), element.col(), path, vs != null, I18nConstants.TERMINOLOGY_TX_VALUESET_NOTFOUND, describeReference(binding.getValueSet()));
            }
        } else {
            CodedContentValidationPolicy validationPolicy = getPolicyAdvisor() == null ? CodedContentValidationPolicy.VALUESET : getPolicyAdvisor().policyForCodedContent(this, hostContext, stack.getLiteralPath(), elementContext, profile, BindingKind.PRIMARY, vs, new ArrayList<>());
            if (validationPolicy != CodedContentValidationPolicy.IGNORE) {
                long t = System.nanoTime();
                ValidationResult vr = null;
                if (binding.getStrength() != BindingStrength.EXAMPLE) {
                    ValidationOptions options = baseOptions.setLanguage(stack.getWorkingLang()).guessSystem();
                    if (validationPolicy == CodedContentValidationPolicy.CODE) {
                        options = options.noCheckValueSetMembership();
                    }
                    vr = checkCodeOnServer(stack, vs, value, options);
                }
                timeTracker.tx(t, "vc " + value + "");
                if (binding.getStrength() == BindingStrength.REQUIRED) {
                    removeTrackedMessagesForLocation(errors, element, path);
                }
                if (vr != null && !vr.isOk()) {
                    if (vr.IsNoService())
                        txHint(errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_NOVALID_15, value);
                    else if (binding.getStrength() == BindingStrength.REQUIRED)
                        txRule(errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_NOVALID_16, value, describeValueSet(binding.getValueSet()), getErrorMessage(vr.getMessage()));
                    else if (binding.getStrength() == BindingStrength.EXTENSIBLE) {
                        if (binding.hasExtension("http://hl7.org/fhir/StructureDefinition/elementdefinition-maxValueSet"))
                            checkMaxValueSet(errors, path, element, profile, ToolingExtensions.readStringExtension(binding, "http://hl7.org/fhir/StructureDefinition/elementdefinition-maxValueSet"), value, stack);
                        else if (!noExtensibleWarnings && !isOkExtension(value, vs))
                            txWarningForLaterRemoval(element, errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_NOVALID_17, value, describeValueSet(binding.getValueSet()), getErrorMessage(vr.getMessage()));
                    } else if (binding.getStrength() == BindingStrength.PREFERRED) {
                        if (baseOnly) {
                            txHint(errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_NOVALID_18, value, describeValueSet(binding.getValueSet()), getErrorMessage(vr.getMessage()));
                        }
                    }
                }
            }
        }
    } else if (!noBindingMsgSuppressed)
        hint(errors, IssueType.CODEINVALID, element.line(), element.col(), path, !type.equals("code"), I18nConstants.TERMINOLOGY_TX_BINDING_NOSOURCE2);
}
Also used : ArrayList(java.util.ArrayList) ValidationResult(org.hl7.fhir.r5.context.IWorkerContext.ValidationResult) ValidationOptions(org.hl7.fhir.utilities.validation.ValidationOptions) ElementDefinitionBindingComponent(org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionBindingComponent) ValueSet(org.hl7.fhir.r5.model.ValueSet) CodeSystem(org.hl7.fhir.r5.model.CodeSystem)

Example 62 with ValidationResult

use of org.hl7.fhir.r4b.context.IWorkerContext.ValidationResult in project org.hl7.fhir.core by hapifhir.

the class BaseWorkerContext method verifyCodeInCodeSystem.

private ValidationResult verifyCodeInCodeSystem(ValueSet vs, String system, String code, String display) {
    ConceptDefinitionComponent cc = findCodeInConcept(vs.getCodeSystem().getConcept(), code);
    if (cc == null)
        return new ValidationResult(IssueSeverity.ERROR, "Unknown Code " + code + " in " + vs.getCodeSystem().getSystem());
    if (display == null)
        return new ValidationResult(cc);
    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
    if (cc.hasDisplay()) {
        b.append(cc.getDisplay());
        if (display.equalsIgnoreCase(cc.getDisplay()))
            return new ValidationResult(cc);
    }
    for (ConceptDefinitionDesignationComponent ds : cc.getDesignation()) {
        b.append(ds.getValue());
        if (display.equalsIgnoreCase(ds.getValue()))
            return new ValidationResult(cc);
    }
    return new ValidationResult(IssueSeverity.ERROR, "Display Name for " + code + " must be one of '" + b.toString() + "'");
}
Also used : ConceptDefinitionComponent(org.hl7.fhir.dstu2.model.ValueSet.ConceptDefinitionComponent) ConceptDefinitionDesignationComponent(org.hl7.fhir.dstu2.model.ValueSet.ConceptDefinitionDesignationComponent) CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder)

Example 63 with ValidationResult

use of org.hl7.fhir.r4b.context.IWorkerContext.ValidationResult in project org.hl7.fhir.core by hapifhir.

the class BaseWorkerContext method serverValidateCode.

private ValidationResult serverValidateCode(Parameters pin) {
    Parameters pout = txServer.operateType(ValueSet.class, "validate-code", pin);
    boolean ok = false;
    String message = "No Message returned";
    String display = null;
    for (ParametersParameterComponent p : pout.getParameter()) {
        if (p.getName().equals("result"))
            ok = ((BooleanType) p.getValue()).getValue().booleanValue();
        else if (p.getName().equals("message"))
            message = ((StringType) p.getValue()).getValue();
        else if (p.getName().equals("display"))
            display = ((StringType) p.getValue()).getValue();
    }
    if (!ok)
        return new ValidationResult(IssueSeverity.ERROR, message);
    else if (display != null)
        return new ValidationResult(new ConceptDefinitionComponent().setDisplay(display));
    else
        return new ValidationResult(null);
}
Also used : Parameters(org.hl7.fhir.dstu2.model.Parameters) ConceptDefinitionComponent(org.hl7.fhir.dstu2.model.ValueSet.ConceptDefinitionComponent) StringType(org.hl7.fhir.dstu2.model.StringType) ParametersParameterComponent(org.hl7.fhir.dstu2.model.Parameters.ParametersParameterComponent)

Example 64 with ValidationResult

use of org.hl7.fhir.r4b.context.IWorkerContext.ValidationResult in project org.hl7.fhir.core by hapifhir.

the class BaseWorkerContext method handleByCache.

private ValidationResult handleByCache(ValueSet vs, Coding coding, boolean tryCache) {
    String cacheId = cacheId(coding);
    Map<String, ValidationResult> cache = validationCache.get(vs.getUrl());
    if (cache == null) {
        cache = new HashMap<String, IWorkerContext.ValidationResult>();
        validationCache.put(vs.getUrl(), cache);
    }
    if (cache.containsKey(cacheId))
        return cache.get(cacheId);
    if (!tryCache)
        return null;
    if (!cacheValidation)
        return null;
    if (failed.contains(vs.getUrl()))
        return null;
    ValueSetExpansionOutcome vse = expandVS(vs, true);
    if (vse.getValueset() == null || notcomplete(vse.getValueset())) {
        failed.add(vs.getUrl());
        return null;
    }
    ValidationResult res = validateCode(coding, vse.getValueset());
    cache.put(cacheId, res);
    return res;
}
Also used : ValueSetExpansionOutcome(org.hl7.fhir.dstu2.terminologies.ValueSetExpander.ValueSetExpansionOutcome)

Example 65 with ValidationResult

use of org.hl7.fhir.r4b.context.IWorkerContext.ValidationResult in project org.hl7.fhir.core by hapifhir.

the class BaseWorkerContext method processValidationResult.

public ValidationResult processValidationResult(Parameters pOut) {
    boolean ok = false;
    String message = "No Message returned";
    String display = null;
    String system = null;
    String code = null;
    TerminologyServiceErrorClass err = TerminologyServiceErrorClass.UNKNOWN;
    for (ParametersParameterComponent p : pOut.getParameter()) {
        if (p.hasValue()) {
            if (p.getName().equals("result")) {
                ok = ((BooleanType) p.getValue()).getValue().booleanValue();
            } else if (p.getName().equals("message")) {
                message = ((StringType) p.getValue()).getValue();
            } else if (p.getName().equals("display")) {
                display = ((StringType) p.getValue()).getValue();
            } else if (p.getName().equals("system")) {
                system = ((StringType) p.getValue()).getValue();
            } else if (p.getName().equals("code")) {
                code = ((StringType) p.getValue()).getValue();
            } else if (p.getName().equals("cause")) {
                try {
                    IssueType it = IssueType.fromCode(((StringType) p.getValue()).getValue());
                    if (it == IssueType.UNKNOWN) {
                        err = TerminologyServiceErrorClass.UNKNOWN;
                    } else if (it == IssueType.NOTFOUND) {
                        err = TerminologyServiceErrorClass.CODESYSTEM_UNSUPPORTED;
                    } else if (it == IssueType.NOTSUPPORTED) {
                        err = TerminologyServiceErrorClass.VALUESET_UNSUPPORTED;
                    } else {
                        err = null;
                    }
                } catch (FHIRException e) {
                }
            }
        }
    }
    if (!ok) {
        return new ValidationResult(IssueSeverity.ERROR, message + " (from " + txClient.getAddress() + ")", err).setTxLink(txLog.getLastId());
    } else if (message != null && !message.equals("No Message returned")) {
        return new ValidationResult(IssueSeverity.WARNING, message + " (from " + txClient.getAddress() + ")", system, new ConceptDefinitionComponent().setDisplay(display).setCode(code)).setTxLink(txLog.getLastId());
    } else if (display != null) {
        return new ValidationResult(system, new ConceptDefinitionComponent().setDisplay(display).setCode(code)).setTxLink(txLog.getLastId());
    } else {
        return new ValidationResult(system, new ConceptDefinitionComponent().setCode(code)).setTxLink(txLog.getLastId());
    }
}
Also used : TerminologyServiceErrorClass(org.hl7.fhir.r4b.terminologies.ValueSetExpander.TerminologyServiceErrorClass) ConceptDefinitionComponent(org.hl7.fhir.r4b.model.CodeSystem.ConceptDefinitionComponent) StringType(org.hl7.fhir.r4b.model.StringType) IssueType(org.hl7.fhir.utilities.validation.ValidationMessage.IssueType) BooleanType(org.hl7.fhir.r4b.model.BooleanType) FHIRException(org.hl7.fhir.exceptions.FHIRException) ParametersParameterComponent(org.hl7.fhir.r4b.model.Parameters.ParametersParameterComponent)

Aggregations

FHIRException (org.hl7.fhir.exceptions.FHIRException)32 IOException (java.io.IOException)22 ValidationResult (org.hl7.fhir.r5.context.IWorkerContext.ValidationResult)20 TerminologyServiceException (org.hl7.fhir.exceptions.TerminologyServiceException)17 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)15 FileNotFoundException (java.io.FileNotFoundException)14 ValidationResult (ca.uhn.fhir.validation.ValidationResult)12 CodeSystem (org.hl7.fhir.r5.model.CodeSystem)12 ConceptDefinitionComponent (org.hl7.fhir.r5.model.CodeSystem.ConceptDefinitionComponent)11 ValueSet (org.hl7.fhir.r5.model.ValueSet)11 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)11 NoTerminologyServiceException (org.hl7.fhir.exceptions.NoTerminologyServiceException)10 ValidationResult (org.hl7.fhir.r4b.context.IWorkerContext.ValidationResult)10 ArrayList (java.util.ArrayList)9 Coding (org.hl7.fhir.r5.model.Coding)9 NotImplementedException (org.apache.commons.lang3.NotImplementedException)7 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)7 ValidationResult (org.hl7.fhir.r4.context.IWorkerContext.ValidationResult)7 ConceptDefinitionComponent (org.hl7.fhir.r4b.model.CodeSystem.ConceptDefinitionComponent)7 FHIRLexerException (org.hl7.fhir.r5.utils.FHIRLexer.FHIRLexerException)7