Search in sources :

Example 21 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.r5.terminologies.ValueSetExpander.TerminologyServiceErrorClass) ConceptDefinitionComponent(org.hl7.fhir.r5.model.CodeSystem.ConceptDefinitionComponent) StringType(org.hl7.fhir.r5.model.StringType) IssueType(org.hl7.fhir.utilities.validation.ValidationMessage.IssueType) BooleanType(org.hl7.fhir.r5.model.BooleanType) FHIRException(org.hl7.fhir.exceptions.FHIRException) ParametersParameterComponent(org.hl7.fhir.r5.model.Parameters.ParametersParameterComponent)

Example 22 with ValidationResult

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

the class ProfileUtilities method describeCoded.

private Piece describeCoded(HierarchicalTableGenerator gen, DataType fixed) {
    if (fixed instanceof Coding) {
        Coding c = (Coding) fixed;
        ValidationResult vr = context.validateCode(terminologyServiceOptions, c.getSystem(), c.getVersion(), c.getCode(), c.getDisplay());
        if (vr.getDisplay() != null)
            return gen.new Piece(null, " (" + vr.getDisplay() + ")", null).addStyle("color: darkgreen");
    } else if (fixed instanceof CodeableConcept) {
        CodeableConcept cc = (CodeableConcept) fixed;
        for (Coding c : cc.getCoding()) {
            ValidationResult vr = context.validateCode(terminologyServiceOptions, c.getSystem(), c.getVersion(), c.getCode(), c.getDisplay());
            if (vr.getDisplay() != null)
                return gen.new Piece(null, " (" + vr.getDisplay() + ")", null).addStyle("color: darkgreen");
        }
    }
    return null;
}
Also used : Coding(org.hl7.fhir.r5.model.Coding) Piece(org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Piece) ValidationResult(org.hl7.fhir.r5.context.IWorkerContext.ValidationResult) CodeableConcept(org.hl7.fhir.r5.model.CodeableConcept)

Example 23 with ValidationResult

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

the class BaseWorkerContext method validateOnServer.

protected ValidationResult validateOnServer(ValueSet vs, Parameters pin, ValidationOptions options) throws FHIRException {
    boolean cache = false;
    if (vs != null) {
        for (ConceptSetComponent inc : vs.getCompose().getInclude()) {
            codeSystemsUsed.add(inc.getSystem());
        }
        for (ConceptSetComponent inc : vs.getCompose().getExclude()) {
            codeSystemsUsed.add(inc.getSystem());
        }
    }
    if (vs != null) {
        if (isTxCaching && cacheId != null && vs.getUrl() != null && cached.contains(vs.getUrl() + "|" + vs.getVersion())) {
            pin.addParameter().setName("url").setValue(new UriType(vs.getUrl() + (vs.hasVersion() ? "|" + vs.getVersion() : "")));
        } else if (options.getVsAsUrl()) {
            pin.addParameter().setName("url").setValue(new StringType(vs.getUrl()));
        } else {
            pin.addParameter().setName("valueSet").setResource(vs);
            if (vs.getUrl() != null) {
                cached.add(vs.getUrl() + "|" + vs.getVersion());
            }
        }
        cache = true;
        addDependentResources(pin, vs);
    }
    if (cache) {
        pin.addParameter().setName("cache-id").setValue(new StringType(cacheId));
    }
    for (ParametersParameterComponent pp : pin.getParameter()) {
        if (pp.getName().equals("profile")) {
            throw new Error(formatMessage(I18nConstants.CAN_ONLY_SPECIFY_PROFILE_IN_THE_CONTEXT));
        }
    }
    if (expParameters == null) {
        throw new Error(formatMessage(I18nConstants.NO_EXPANSIONPROFILE_PROVIDED));
    }
    pin.addParameter().setName("profile").setResource(expParameters);
    if (txLog != null) {
        txLog.clearLastId();
    }
    if (txClient == null) {
        throw new FHIRException(formatMessage(I18nConstants.ATTEMPT_TO_USE_TERMINOLOGY_SERVER_WHEN_NO_TERMINOLOGY_SERVER_IS_AVAILABLE));
    }
    Parameters pOut;
    if (vs == null) {
        pOut = txClient.validateCS(pin);
    } else {
        pOut = txClient.validateVS(pin);
    }
    return processValidationResult(pOut);
}
Also used : ConceptSetComponent(org.hl7.fhir.r5.model.ValueSet.ConceptSetComponent) Parameters(org.hl7.fhir.r5.model.Parameters) StringType(org.hl7.fhir.r5.model.StringType) FHIRException(org.hl7.fhir.exceptions.FHIRException) UriType(org.hl7.fhir.r5.model.UriType) ParametersParameterComponent(org.hl7.fhir.r5.model.Parameters.ParametersParameterComponent)

Example 24 with ValidationResult

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

the class TerminologyCache method getCacheEntry.

private CacheEntry getCacheEntry(String request, String resultString) throws IOException {
    CacheEntry ce = new CacheEntry();
    ce.persistent = true;
    ce.request = request;
    boolean e = resultString.charAt(0) == 'e';
    resultString = resultString.substring(3);
    JsonObject o = (JsonObject) new com.google.gson.JsonParser().parse(resultString);
    String error = loadJS(o.get("error"));
    if (e) {
        if (o.has("valueSet"))
            ce.e = new ValueSetExpansionOutcome((ValueSet) new JsonParser().parse(o.getAsJsonObject("valueSet")), error, TerminologyServiceErrorClass.UNKNOWN);
        else
            ce.e = new ValueSetExpansionOutcome(error, TerminologyServiceErrorClass.UNKNOWN);
    } else {
        String t = loadJS(o.get("severity"));
        IssueSeverity severity = t == null ? null : IssueSeverity.fromCode(t);
        String display = loadJS(o.get("display"));
        String code = loadJS(o.get("code"));
        String system = loadJS(o.get("system"));
        String definition = loadJS(o.get("definition"));
        t = loadJS(o.get("class"));
        TerminologyServiceErrorClass errorClass = t == null ? null : TerminologyServiceErrorClass.valueOf(t);
        ce.v = new ValidationResult(severity, error, system, new ConceptDefinitionComponent().setDisplay(display).setDefinition(definition).setCode(code)).setErrorClass(errorClass);
    }
    return ce;
}
Also used : TerminologyServiceErrorClass(org.hl7.fhir.r5.terminologies.ValueSetExpander.TerminologyServiceErrorClass) ConceptDefinitionComponent(org.hl7.fhir.r5.model.CodeSystem.ConceptDefinitionComponent) IssueSeverity(org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity) JsonObject(com.google.gson.JsonObject) ValueSetExpansionOutcome(org.hl7.fhir.r5.terminologies.ValueSetExpander.ValueSetExpansionOutcome) ValidationResult(org.hl7.fhir.r5.context.IWorkerContext.ValidationResult) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

Example 25 with ValidationResult

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

the class ValueSetCheckerSimple method validateCode.

public ValidationResult validateCode(Coding code) throws FHIRException {
    String warningMessage = null;
    // first, we validate the concept itself
    ValidationResult res = null;
    boolean inExpansion = false;
    boolean inInclude = false;
    String system = code.hasSystem() ? code.getSystem() : getValueSetSystemOrNull();
    if (options.getValueSetMode() != ValueSetMode.CHECK_MEMERSHIP_ONLY) {
        if (system == null && !code.hasDisplay()) {
            // dealing with just a plain code (enum)
            system = systemForCodeInValueSet(code.getCode());
        }
        if (!code.hasSystem()) {
            if (options.isGuessSystem() && system == null && Utilities.isAbsoluteUrl(code.getCode())) {
                // this arises when using URIs bound to value sets
                system = "urn:ietf:rfc:3986";
            }
            code.setSystem(system);
        }
        inExpansion = checkExpansion(code);
        inInclude = checkInclude(code);
        CodeSystem cs = resolveCodeSystem(system);
        if (cs == null) {
            warningMessage = "Unable to resolve system " + system;
            if (!inExpansion) {
                if (valueset != null && valueset.hasExpansion()) {
                    return new ValidationResult(IssueSeverity.ERROR, context.formatMessage(I18nConstants.CODESYSTEM_CS_UNK_EXPANSION, valueset.getUrl(), code.getCode().toString(), code.getSystem()));
                } else {
                    throw new FHIRException(warningMessage);
                }
            }
        }
        if (cs != null && cs.hasSupplements()) {
            return new ValidationResult(IssueSeverity.ERROR, context.formatMessage(I18nConstants.CODESYSTEM_CS_NO_SUPPLEMENT, cs.getUrl()));
        }
        if (cs != null && cs.getContent() != CodeSystemContentMode.COMPLETE) {
            warningMessage = "Resolved system " + system + ", but the definition is not complete";
            if (!inExpansion && cs.getContent() != CodeSystemContentMode.FRAGMENT) {
                // we're going to give it a go if it's a fragment
                throw new FHIRException(warningMessage);
            }
        }
        if (cs != null) /*&& (cs.getContent() == CodeSystemContentMode.COMPLETE || cs.getContent() == CodeSystemContentMode.FRAGMENT)*/
        {
            if (!(cs.getContent() == CodeSystemContentMode.COMPLETE || cs.getContent() == CodeSystemContentMode.FRAGMENT)) {
                // we can't validate that here.
                throw new FHIRException("Unable to evaluate based on empty code system");
            }
            res = validateCode(code, cs);
        } else if (cs == null && valueset.hasExpansion() && inExpansion) {
            // we just take the value set as face value then
            res = new ValidationResult(system, new ConceptDefinitionComponent().setCode(code.getCode()).setDisplay(code.getDisplay()));
        } else {
            // disabled waiting for discussion
            throw new FHIRException("No try the server");
        }
    } else {
        inExpansion = checkExpansion(code);
        inInclude = checkInclude(code);
    }
    List<String> warnings = new ArrayList<>();
    // then, if we have a value set, we check it's in the value set
    if (valueset != null && options.getValueSetMode() != ValueSetMode.NO_MEMBERSHIP_CHECK) {
        if ((res == null || res.isOk())) {
            Boolean ok = codeInValueSet(system, code.getCode(), warnings);
            if (ok == null || !ok) {
                if (res == null) {
                    res = new ValidationResult((IssueSeverity) null, null);
                }
                if (!inExpansion && !inInclude) {
                    if (warnings != null) {
                        res.setMessage("Not in value set " + valueset.getUrl() + " (" + warnings + ")").setSeverity(IssueSeverity.ERROR);
                    } else {
                        res.setMessage("Not in value set " + valueset.getUrl()).setSeverity(IssueSeverity.ERROR);
                    }
                } else if (warningMessage != null) {
                    res = new ValidationResult(IssueSeverity.WARNING, context.formatMessage(I18nConstants.CODE_FOUND_IN_EXPANSION_HOWEVER_, warningMessage));
                } else if (inExpansion) {
                    res.setMessage("Code found in expansion, however: " + res.getMessage());
                } else if (inInclude) {
                    res.setMessage("Code found in include, however: " + res.getMessage());
                }
            }
        }
    }
    return res;
}
Also used : ConceptDefinitionComponent(org.hl7.fhir.r5.model.CodeSystem.ConceptDefinitionComponent) ArrayList(java.util.ArrayList) IssueSeverity(org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity) ValidationResult(org.hl7.fhir.r5.context.IWorkerContext.ValidationResult) CodeSystem(org.hl7.fhir.r5.model.CodeSystem) FHIRException(org.hl7.fhir.exceptions.FHIRException)

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