Search in sources :

Example 31 with ValidationResult

use of org.hl7.fhir.r5.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, Coding c, 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, c, true);
            timeTracker.tx(t, "vc " + c.getSystem() + "#" + c.getCode() + " '" + c.getDisplay() + "'");
            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_10, describeValueSet(maxVSUrl), c.getSystem(), c.getCode());
            }
        } 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 32 with ValidationResult

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

the class ValueSetValidator method validateValueSetIncludeConcept.

private boolean validateValueSetIncludeConcept(List<ValidationMessage> errors, Element concept, NodeStack stack, String system, String version) {
    String code = concept.getChildValue("code");
    if (version == null) {
        ValidationResult vv = context.validateCode(ValidationOptions.defaults(), new Coding(system, code, null), null);
        if (vv.getErrorClass() == TerminologyServiceErrorClass.CODESYSTEM_UNSUPPORTED) {
            return false;
        } else {
            boolean ok = vv.isOk();
            warning(errors, IssueType.BUSINESSRULE, stack.getLiteralPath(), ok, I18nConstants.VALUESET_INCLUDE_INVALID_CONCEPT_CODE, system, code);
        }
    } else {
        ValidationResult vv = context.validateCode(ValidationOptions.defaults(), new Coding(system, code, null).setVersion(version), null);
        if (vv.getErrorClass() == TerminologyServiceErrorClass.CODESYSTEM_UNSUPPORTED) {
            return false;
        } else {
            boolean ok = vv.isOk();
            warning(errors, IssueType.BUSINESSRULE, stack.getLiteralPath(), ok, I18nConstants.VALUESET_INCLUDE_INVALID_CONCEPT_CODE_VER, system, version, code);
        }
    }
    return true;
}
Also used : Coding(org.hl7.fhir.r5.model.Coding) ValidationResult(org.hl7.fhir.r5.context.IWorkerContext.ValidationResult)

Example 33 with ValidationResult

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

the class BaseWorkerContext method validateOnServer.

private ValidationResult validateOnServer(ValueSet vs, Parameters pin) throws FHIRException {
    if (vs != null)
        pin.addParameter().setName("valueSet").setResource(vs);
    for (ParametersParameterComponent pp : pin.getParameter()) if (pp.getName().equals("profile"))
        throw new Error("Can only specify profile in the context");
    if (expParameters == null)
        throw new Error("No ExpansionProfile provided");
    pin.addParameter().setName("profile").setResource(expParameters);
    txLog.clearLastId();
    Parameters pOut;
    if (vs == null)
        pOut = txClient.validateCS(pin);
    else
        pOut = txClient.validateVS(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)
        return new ValidationResult(IssueSeverity.ERROR, message, err).setTxLink(txLog.getLastId()).setTxLink(txLog.getLastId());
    else if (message != null && !message.equals("No Message returned"))
        return new ValidationResult(IssueSeverity.WARNING, message, new ConceptDefinitionComponent().setDisplay(display)).setTxLink(txLog.getLastId()).setTxLink(txLog.getLastId());
    else if (display != null)
        return new ValidationResult(new ConceptDefinitionComponent().setDisplay(display)).setTxLink(txLog.getLastId()).setTxLink(txLog.getLastId());
    else
        return new ValidationResult(new ConceptDefinitionComponent()).setTxLink(txLog.getLastId()).setTxLink(txLog.getLastId());
}
Also used : TerminologyServiceErrorClass(org.hl7.fhir.r4.terminologies.ValueSetExpander.TerminologyServiceErrorClass) ConceptDefinitionComponent(org.hl7.fhir.r4.model.CodeSystem.ConceptDefinitionComponent) IssueType(org.hl7.fhir.utilities.validation.ValidationMessage.IssueType) FHIRException(org.hl7.fhir.exceptions.FHIRException) ParametersParameterComponent(org.hl7.fhir.r4.model.Parameters.ParametersParameterComponent)

Example 34 with ValidationResult

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

the class BaseWorkerContext method doValidateCode.

public ValidationResult doValidateCode(ValidationOptions options, Coding code, ValueSet vs, boolean implySystem) {
    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;
    // ok, first we try to validate locally
    try {
        ValueSetCheckerSimple vsc = new ValueSetCheckerSimple(options, vs, this);
        res = vsc.validateCode(code);
        if (txCache != null)
            txCache.cacheValidation(cacheToken, res, TerminologyCache.TRANSIENT);
        return res;
    } catch (Exception e) {
    }
    // if that failed, we try to validate on the server
    if (noTerminologyServer)
        return new ValidationResult(IssueSeverity.ERROR, "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 (implySystem)
            pIn.addParameter().setName("implySystem").setValue(new BooleanType(true));
        if (options != null)
            setTerminologyOptions(options, pIn);
        res = validateOnServer(vs, pIn);
    } catch (Exception e) {
        res = new ValidationResult(IssueSeverity.ERROR, e.getMessage() == null ? e.getClass().getName() : e.getMessage()).setTxLink(txLog == null ? null : txLog.getLastId());
    }
    if (txCache != null)
        txCache.cacheValidation(cacheToken, res, TerminologyCache.PERMANENT);
    return res;
}
Also used : CacheToken(org.hl7.fhir.r4.context.TerminologyCache.CacheToken) ValueSetCheckerSimple(org.hl7.fhir.r4.terminologies.ValueSetCheckerSimple) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) FileNotFoundException(java.io.FileNotFoundException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 35 with ValidationResult

use of org.hl7.fhir.r5.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 = cs.hasUserData("tx.cs.special") ? ((SpecialCodeSystem) cs.getUserData("tx.cs.special")).findConcept(code) : findCodeInConcept(cs.getConcept(), code.getCode());
    if (cc == null) {
        if (cs.getContent() == CodeSystemContentMode.FRAGMENT) {
            return new ValidationResult(IssueSeverity.WARNING, context.formatMessage(I18nConstants.UNKNOWN_CODE__IN_FRAGMENT, gen(code), cs.getUrl()));
        } else {
            return new ValidationResult(IssueSeverity.ERROR, context.formatMessage(I18nConstants.UNKNOWN_CODE__IN_, gen(code), cs.getUrl()));
        }
    }
    if (code.getDisplay() == null) {
        return new ValidationResult(code.getSystem(), cc);
    }
    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
    if (cc.hasDisplay()) {
        b.append(cc.getDisplay());
        if (code.getDisplay().equalsIgnoreCase(cc.getDisplay())) {
            return new ValidationResult(code.getSystem(), cc);
        }
    }
    for (ConceptDefinitionDesignationComponent ds : cc.getDesignation()) {
        b.append(ds.getValue());
        if (code.getDisplay().equalsIgnoreCase(ds.getValue())) {
            return new ValidationResult(code.getSystem(), 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(code.getSystem(), cc);
            }
        }
        for (ConceptReferenceDesignationComponent ds : vs.getDesignation()) {
            b.append(ds.getValue());
            if (code.getDisplay().equalsIgnoreCase(ds.getValue())) {
                return new ValidationResult(code.getSystem(), cc);
            }
        }
    }
    return new ValidationResult(IssueSeverity.WARNING, context.formatMessage(I18nConstants.DISPLAY_NAME_FOR__SHOULD_BE_ONE_OF__INSTEAD_OF_, code.getSystem(), code.getCode(), b.toString(), code.getDisplay()), code.getSystem(), cc);
}
Also used : ConceptDefinitionComponent(org.hl7.fhir.r4b.model.CodeSystem.ConceptDefinitionComponent) ConceptDefinitionDesignationComponent(org.hl7.fhir.r4b.model.CodeSystem.ConceptDefinitionDesignationComponent) CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder) ConceptReferenceDesignationComponent(org.hl7.fhir.r4b.model.ValueSet.ConceptReferenceDesignationComponent) ValidationResult(org.hl7.fhir.r4b.context.IWorkerContext.ValidationResult) ConceptReferenceComponent(org.hl7.fhir.r4b.model.ValueSet.ConceptReferenceComponent)

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