Search in sources :

Example 66 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(ValidationOptions options, Coding code, ValueSet vs, ValidationContextCarrier ctxt) {
    if (options == null) {
        options = ValidationOptions.defaults();
    }
    if (code.hasSystem()) {
        codeSystemsUsed.add(code.getSystem());
    }
    CacheToken cacheToken = txCache != null ? txCache.generateValidationToken(options, code, vs) : null;
    ValidationResult res = null;
    if (txCache != null) {
        res = txCache.getValidation(cacheToken);
    }
    if (res != null) {
        return res;
    }
    if (options.isUseClient()) {
        // ok, first we try to validate locally
        try {
            ValueSetCheckerSimple vsc = new ValueSetCheckerSimple(options, vs, this, ctxt);
            if (!vsc.isServerSide(code.getSystem())) {
                res = vsc.validateCode(code);
                if (txCache != null) {
                    txCache.cacheValidation(cacheToken, res, TerminologyCache.TRANSIENT);
                }
                return res;
            }
        } catch (Exception e) {
        }
    }
    String codeKey = code.hasVersion() ? code.getSystem() + "|" + code.getVersion() : code.getSystem();
    if (!options.isUseServer()) {
        return new ValidationResult(IssueSeverity.WARNING, formatMessage(I18nConstants.UNABLE_TO_VALIDATE_CODE_WITHOUT_USING_SERVER), TerminologyServiceErrorClass.BLOCKED_BY_OPTIONS);
    }
    if (unsupportedCodeSystems.contains(codeKey)) {
        return new ValidationResult(IssueSeverity.ERROR, formatMessage(I18nConstants.TERMINOLOGY_TX_SYSTEM_NOTKNOWN, code.getSystem()), TerminologyServiceErrorClass.CODESYSTEM_UNSUPPORTED);
    }
    // if that failed, we try to validate on the server
    if (noTerminologyServer) {
        return new ValidationResult(IssueSeverity.ERROR, formatMessage(I18nConstants.ERROR_VALIDATING_CODE_RUNNING_WITHOUT_TERMINOLOGY_SERVICES), TerminologyServiceErrorClass.NOSERVICE);
    }
    String csumm = txCache != null ? txCache.summary(code) : null;
    if (txCache != null) {
        tlog("$validate " + csumm + " for " + txCache.summary(vs));
    } else {
        tlog("$validate " + csumm + " before cache exists");
    }
    try {
        Parameters pIn = new Parameters();
        pIn.addParameter().setName("coding").setValue(code);
        if (options.isGuessSystem()) {
            pIn.addParameter().setName("implySystem").setValue(new BooleanType(true));
        }
        setTerminologyOptions(options, pIn);
        res = validateOnServer(vs, pIn, options);
    } catch (Exception e) {
        res = new ValidationResult(IssueSeverity.ERROR, e.getMessage() == null ? e.getClass().getName() : e.getMessage()).setTxLink(txLog == null ? null : txLog.getLastId()).setErrorClass(TerminologyServiceErrorClass.SERVER_ERROR);
    }
    if (res.getErrorClass() == TerminologyServiceErrorClass.CODESYSTEM_UNSUPPORTED && !code.hasVersion()) {
        unsupportedCodeSystems.add(codeKey);
    } else if (txCache != null) {
        // we never cache unsuppoted code systems - we always keep trying (but only once per run)
        txCache.cacheValidation(cacheToken, res, TerminologyCache.PERMANENT);
    }
    return res;
}
Also used : Parameters(org.hl7.fhir.r4b.model.Parameters) CacheToken(org.hl7.fhir.r4b.context.TerminologyCache.CacheToken) BooleanType(org.hl7.fhir.r4b.model.BooleanType) ValueSetCheckerSimple(org.hl7.fhir.r4b.terminologies.ValueSetCheckerSimple) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) FileNotFoundException(java.io.FileNotFoundException) NoTerminologyServiceException(org.hl7.fhir.exceptions.NoTerminologyServiceException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 67 with ValidationResult

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

the class InstanceValidator method checkMaxValueSet.

private void checkMaxValueSet(List<ValidationMessage> errors, String path, Element element, StructureDefinition profile, String maxVSUrl, CodeableConcept cc, NodeStack stack) {
    ValueSet valueset = resolveBindingReference(profile, maxVSUrl, profile.getUrl());
    if (valueset == null) {
        CodeSystem cs = context.fetchCodeSystem(maxVSUrl);
        if (rule(errors, IssueType.CODEINVALID, element.line(), element.col(), path, cs == null, I18nConstants.TERMINOLOGY_TX_VALUESET_NOTFOUND_CS, describeReference(maxVSUrl))) {
            warning(errors, IssueType.CODEINVALID, element.line(), element.col(), path, valueset != null, I18nConstants.TERMINOLOGY_TX_VALUESET_NOTFOUND, describeReference(maxVSUrl));
        }
    } else {
        try {
            long t = System.nanoTime();
            ValidationResult vr = checkCodeOnServer(stack, valueset, cc, false);
            timeTracker.tx(t, "vc " + cc.toString());
            if (!vr.isOk()) {
                if (vr.getErrorClass() != null && vr.getErrorClass().isInfrastructure())
                    txWarning(errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_NOVALID_7, describeValueSet(maxVSUrl), vr.getMessage());
                else
                    txRule(errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_NOVALID_8, describeValueSet(maxVSUrl), ccSummary(cc));
            }
        } catch (Exception e) {
            if (STACK_TRACE)
                e.printStackTrace();
            warning(errors, IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_ERROR_CODEABLECONCEPT_MAX, e.getMessage());
        }
    }
}
Also used : ValidationResult(org.hl7.fhir.r5.context.IWorkerContext.ValidationResult) ValueSet(org.hl7.fhir.r5.model.ValueSet) CodeSystem(org.hl7.fhir.r5.model.CodeSystem) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) NotImplementedException(org.apache.commons.lang3.NotImplementedException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) FHIRLexerException(org.hl7.fhir.r5.utils.FHIRLexer.FHIRLexerException)

Example 68 with ValidationResult

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

the class InstanceValidator method checkMaxValueSet.

private void checkMaxValueSet(List<ValidationMessage> errors, String path, Element element, StructureDefinition profile, String maxVSUrl, String value, NodeStack stack) {
    ValueSet valueset = resolveBindingReference(profile, maxVSUrl, profile.getUrl());
    if (valueset == null) {
        CodeSystem cs = context.fetchCodeSystem(maxVSUrl);
        if (rule(errors, IssueType.CODEINVALID, element.line(), element.col(), path, cs == null, I18nConstants.TERMINOLOGY_TX_VALUESET_NOTFOUND_CS, describeReference(maxVSUrl))) {
            warning(errors, IssueType.CODEINVALID, element.line(), element.col(), path, valueset != null, I18nConstants.TERMINOLOGY_TX_VALUESET_NOTFOUND, describeReference(maxVSUrl));
        }
    } else {
        try {
            long t = System.nanoTime();
            ValidationResult vr = checkCodeOnServer(stack, valueset, value, baseOptions.setLanguage(stack.getWorkingLang()));
            timeTracker.tx(t, "vc " + value);
            if (!vr.isOk()) {
                if (vr.getErrorClass() != null && vr.getErrorClass().isInfrastructure())
                    txWarning(errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_NOVALID_9, describeValueSet(maxVSUrl), vr.getMessage());
                else
                    txRule(errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_NOVALID_11, describeValueSet(maxVSUrl), vr.getMessage());
            }
        } catch (Exception e) {
            if (STACK_TRACE)
                e.printStackTrace();
            warning(errors, IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_ERROR_CODEABLECONCEPT_MAX, e.getMessage());
        }
    }
}
Also used : ValidationResult(org.hl7.fhir.r5.context.IWorkerContext.ValidationResult) ValueSet(org.hl7.fhir.r5.model.ValueSet) CodeSystem(org.hl7.fhir.r5.model.CodeSystem) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) NotImplementedException(org.apache.commons.lang3.NotImplementedException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) FHIRLexerException(org.hl7.fhir.r5.utils.FHIRLexer.FHIRLexerException)

Example 69 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, Type fixed) {
    if (fixed instanceof Coding) {
        Coding c = (Coding) fixed;
        ValidationResult vr = context.validateCode(c.getSystem(), 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(c.getSystem(), 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.dstu3.model.Coding) Piece(org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Piece) ValidationResult(org.hl7.fhir.dstu3.context.IWorkerContext.ValidationResult) CodeableConcept(org.hl7.fhir.dstu3.model.CodeableConcept)

Example 70 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, boolean doCache) throws Exception {
    if (noTerminologyServer) {
        return new ValidationResult(null, null, TerminologyServiceErrorClass.NOSERVICE);
    }
    String cacheName = doCache ? generateCacheName(pin) : null;
    ValidationResult res = loadFromCache(cacheName);
    if (res != null) {
        return res;
    }
    tlog("Terminology Server: $validate-code " + describeValidationParameters(pin));
    for (ParametersParameterComponent pp : pin.getParameter()) {
        if (pp.getName().equals("profile")) {
            throw new Error("Can only specify profile in the context");
        }
    }
    if (expProfile == null) {
        throw new Exception("No ExpansionProfile provided");
    }
    pin.addParameter().setName("profile").setResource(expProfile);
    Parameters pout = txServer.operateType(ValueSet.class, "validate-code", pin);
    boolean ok = false;
    String message = "No Message returned";
    String display = null;
    TerminologyServiceErrorClass err = TerminologyServiceErrorClass.UNKNOWN;
    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();
        } 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.NOTSUPPORTED) {
                    err = TerminologyServiceErrorClass.VALUESET_UNSUPPORTED;
                }
            } catch (FHIRException e) {
            }
        }
    }
    if (!ok) {
        res = new ValidationResult(IssueSeverity.ERROR, message, err);
    } else if (display != null) {
        res = new ValidationResult(new ConceptDefinitionComponent().setDisplay(display));
    } else {
        res = new ValidationResult(new ConceptDefinitionComponent());
    }
    saveToCache(res, cacheName);
    return res;
}
Also used : TerminologyServiceErrorClass(org.hl7.fhir.dstu3.terminologies.ValueSetExpander.TerminologyServiceErrorClass) Parameters(org.hl7.fhir.dstu3.model.Parameters) StringType(org.hl7.fhir.dstu3.model.StringType) IssueType(org.hl7.fhir.utilities.validation.ValidationMessage.IssueType) BooleanType(org.hl7.fhir.dstu3.model.BooleanType) FHIRException(org.hl7.fhir.exceptions.FHIRException) 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) ConceptDefinitionComponent(org.hl7.fhir.dstu3.model.CodeSystem.ConceptDefinitionComponent) ParametersParameterComponent(org.hl7.fhir.dstu3.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