Search in sources :

Example 71 with ValidationResult

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

the class BaseWorkerContext method validateCode.

@Override
public ValidationResult validateCode(String system, String code, String display, ConceptSetComponent vsi) {
    try {
        ValueSet vs = new ValueSet();
        vs.setUrl(Utilities.makeUuidUrn());
        vs.getCompose().addInclude(vsi);
        return verifyCodeExternal(vs, new Coding().setSystem(system).setCode(code).setDisplay(display), true);
    } catch (Exception e) {
        return new ValidationResult(IssueSeverity.FATAL, "Error validating code \"" + code + "\" in system \"" + system + "\": " + e.getMessage());
    }
}
Also used : Coding(org.hl7.fhir.dstu3.model.Coding) ValueSet(org.hl7.fhir.dstu3.model.ValueSet) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) FileNotFoundException(java.io.FileNotFoundException) NoTerminologyServiceException(org.hl7.fhir.exceptions.NoTerminologyServiceException) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 72 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(CodeSystem cs, String system, String code, String display) throws Exception {
    ConceptDefinitionComponent cc = findCodeInConcept(cs.getConcept(), code);
    if (cc == null) {
        if (cs.getContent().equals(CodeSystem.CodeSystemContentMode.COMPLETE)) {
            return new ValidationResult(IssueSeverity.ERROR, "Unknown Code " + code + " in " + cs.getUrl());
        } else if (!cs.getContent().equals(CodeSystem.CodeSystemContentMode.NOTPRESENT)) {
            return new ValidationResult(IssueSeverity.WARNING, "Unknown Code " + code + " in partial code list of " + cs.getUrl());
        } else {
            return verifyCodeExternal(null, new Coding().setSystem(system).setCode(code).setDisplay(display), false);
        }
    }
    // return new ValidationResult(IssueSeverity.ERROR, "Unknown Code "+code+" in "+cs.getUrl());
    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.WARNING, "Display Name for " + code + " must be one of '" + b.toString() + "'", cc);
}
Also used : ConceptDefinitionComponent(org.hl7.fhir.dstu3.model.CodeSystem.ConceptDefinitionComponent) Coding(org.hl7.fhir.dstu3.model.Coding) ConceptDefinitionDesignationComponent(org.hl7.fhir.dstu3.model.CodeSystem.ConceptDefinitionDesignationComponent) CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder)

Example 73 with ValidationResult

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

the class ValueSetCheckerSimple method validateCode.

private ValidationResult validateCode(Coding code, CodeSystem cs) {
    ConceptDefinitionComponent cc = findCodeInConcept(cs.getConcept(), code.getCode());
    if (cc == null)
        return new ValidationResult(IssueSeverity.ERROR, "Unknown Code " + gen(code) + " in " + cs.getUrl());
    if (code.getDisplay() == null)
        return new ValidationResult(cc);
    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
    if (cc.hasDisplay()) {
        b.append(cc.getDisplay());
        if (code.getDisplay().equalsIgnoreCase(cc.getDisplay()))
            return new ValidationResult(cc);
    }
    for (ConceptDefinitionDesignationComponent ds : cc.getDesignation()) {
        b.append(ds.getValue());
        if (code.getDisplay().equalsIgnoreCase(ds.getValue()))
            return new ValidationResult(cc);
    }
    // also check to see if the value set has another display
    ConceptReferenceComponent vs = findValueSetRef(code.getSystem(), code.getCode());
    if (vs != null && (vs.hasDisplay() || vs.hasDesignation())) {
        if (vs.hasDisplay()) {
            b.append(vs.getDisplay());
            if (code.getDisplay().equalsIgnoreCase(vs.getDisplay()))
                return new ValidationResult(cc);
        }
        for (ConceptReferenceDesignationComponent ds : vs.getDesignation()) {
            b.append(ds.getValue());
            if (code.getDisplay().equalsIgnoreCase(ds.getValue()))
                return new ValidationResult(cc);
        }
    }
    return new ValidationResult(IssueSeverity.WARNING, "Display Name for " + code.getSystem() + "#" + code.getCode() + " should be one of '" + b.toString() + "' instead of " + code.getDisplay(), cc);
}
Also used : ConceptDefinitionComponent(org.hl7.fhir.r4.model.CodeSystem.ConceptDefinitionComponent) ConceptDefinitionDesignationComponent(org.hl7.fhir.r4.model.CodeSystem.ConceptDefinitionDesignationComponent) CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder) ValidationResult(org.hl7.fhir.r4.context.IWorkerContext.ValidationResult)

Example 74 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
    String system = code.hasSystem() ? code.getSystem() : getValueSetSystem();
    if (system == null && !code.hasDisplay()) {
        // dealing with just a plain code (enum)
        system = systemForCodeInValueSet(code.getCode());
    }
    if (!code.hasSystem())
        code.setSystem(system);
    boolean inExpansion = checkExpansion(code);
    CodeSystem cs = context.fetchCodeSystem(system);
    if (cs == null) {
        warningMessage = "Unable to resolve system " + system + " - system is not specified or implicit";
        if (!inExpansion)
            throw new FHIRException(warningMessage);
    }
    if (cs != null && cs.getContent() != CodeSystemContentMode.COMPLETE) {
        warningMessage = "Unable to resolve system " + system + " - system is not complete";
        if (!inExpansion)
            throw new FHIRException(warningMessage);
    }
    ValidationResult res = null;
    if (cs != null)
        res = validateCode(code, cs);
    // then, if we have a value set, we check it's in the value set
    if ((res == null || res.isOk()) && valueset != null && !codeInValueSet(system, code.getCode())) {
        if (!inExpansion)
            res.setMessage("Not in value set " + valueset.getUrl()).setSeverity(IssueSeverity.ERROR);
        else if (warningMessage != null)
            res = new ValidationResult(IssueSeverity.WARNING, "Code found in expansion, however: " + warningMessage);
        else
            res.setMessage("Code found in expansion, however: " + res.getMessage());
    }
    return res;
}
Also used : ValidationResult(org.hl7.fhir.r4.context.IWorkerContext.ValidationResult) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 75 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(CodeableConcept code) throws FHIRException {
    // first, we validate the codings themselves
    List<String> errors = new ArrayList<String>();
    List<String> warnings = new ArrayList<String>();
    for (Coding c : code.getCoding()) {
        if (!c.hasSystem())
            warnings.add("Coding has no system");
        CodeSystem cs = context.fetchCodeSystem(c.getSystem());
        if (cs == null)
            warnings.add("Unsupported system " + c.getSystem() + " - system is not specified or implicit");
        else if (cs.getContent() != CodeSystemContentMode.COMPLETE)
            warnings.add("Unable to resolve system " + c.getSystem() + " - system is not complete");
        else {
            ValidationResult res = validateCode(c, cs);
            if (!res.isOk())
                errors.add(res.getMessage());
            else if (res.getMessage() != null)
                warnings.add(res.getMessage());
        }
    }
    if (valueset != null) {
        boolean ok = false;
        for (Coding c : code.getCoding()) {
            ok = ok || codeInValueSet(c.getSystem(), c.getCode());
        }
        if (!ok)
            errors.add(0, "None of the provided codes are in the value set " + valueset.getUrl());
    }
    if (errors.size() > 0)
        return new ValidationResult(IssueSeverity.ERROR, errors.toString());
    else if (warnings.size() > 0)
        return new ValidationResult(IssueSeverity.WARNING, warnings.toString());
    else
        return new ValidationResult(IssueSeverity.INFORMATION, null);
}
Also used : ArrayList(java.util.ArrayList) ValidationResult(org.hl7.fhir.r4.context.IWorkerContext.ValidationResult)

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