Search in sources :

Example 86 with ValidationResult

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

the class StructureMapUtilities method buildCoding.

private Coding buildCoding(String uri, String code) throws FHIRException {
    // if we can get this as a valueSet, we will
    String system = null;
    String display = null;
    ValueSet vs = Utilities.noString(uri) ? null : worker.fetchResourceWithException(ValueSet.class, uri);
    if (vs != null) {
        ValueSetExpansionOutcome vse = worker.expandVS(vs, true, false);
        if (vse.getError() != null)
            throw new FHIRException(vse.getError());
        CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
        for (ValueSetExpansionContainsComponent t : vse.getValueset().getExpansion().getContains()) {
            if (t.hasCode())
                b.append(t.getCode());
            if (code.equals(t.getCode()) && t.hasSystem()) {
                system = t.getSystem();
                display = t.getDisplay();
                break;
            }
            if (code.equalsIgnoreCase(t.getDisplay()) && t.hasSystem()) {
                system = t.getSystem();
                display = t.getDisplay();
                break;
            }
        }
        if (system == null)
            throw new FHIRException("The code '" + code + "' is not in the value set '" + uri + "' (valid codes: " + b.toString() + "; also checked displays)");
    } else
        system = uri;
    ValidationResult vr = worker.validateCode(terminologyServiceOptions, system, code, null);
    if (vr != null && vr.getDisplay() != null)
        display = vr.getDisplay();
    return new Coding().setSystem(system).setCode(code).setDisplay(display);
}
Also used : ValueSetExpansionContainsComponent(org.hl7.fhir.r4.model.ValueSet.ValueSetExpansionContainsComponent) Coding(org.hl7.fhir.r4.model.Coding) ValueSetExpansionOutcome(org.hl7.fhir.r4.terminologies.ValueSetExpander.ValueSetExpansionOutcome) CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder) ValidationResult(org.hl7.fhir.r4.context.IWorkerContext.ValidationResult) ValueSet(org.hl7.fhir.r4.model.ValueSet) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 87 with ValidationResult

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

the class NarrativeGenerator method genInclude.

private boolean genInclude(ResourceContext rcontext, XhtmlNode ul, ConceptSetComponent inc, String type, List<String> langs, List<UsedConceptMap> maps) throws FHIRException, IOException {
    boolean hasExtensions = false;
    XhtmlNode li;
    li = ul.li();
    CodeSystem e = context.fetchCodeSystem(inc.getSystem());
    if (inc.hasSystem()) {
        if (inc.getConcept().size() == 0 && inc.getFilter().size() == 0) {
            li.addText(type + " all codes defined in ");
            addCsRef(inc, li, e);
        } else {
            if (inc.getConcept().size() > 0) {
                li.addText(type + " these codes as defined in ");
                addCsRef(inc, li, e);
                XhtmlNode t = li.table("none");
                boolean hasComments = false;
                boolean hasDefinition = false;
                for (ConceptReferenceComponent c : inc.getConcept()) {
                    hasComments = hasComments || ExtensionHelper.hasExtension(c, ToolingExtensions.EXT_VS_COMMENT);
                    hasDefinition = hasDefinition || ExtensionHelper.hasExtension(c, ToolingExtensions.EXT_DEFINITION);
                }
                if (hasComments || hasDefinition)
                    hasExtensions = true;
                addMapHeaders(addTableHeaderRowStandard(t, false, true, hasDefinition, hasComments, false, false, null), maps);
                for (ConceptReferenceComponent c : inc.getConcept()) {
                    XhtmlNode tr = t.tr();
                    XhtmlNode td = tr.td();
                    ConceptDefinitionComponent cc = getConceptForCode(e, c.getCode(), inc);
                    addCodeToTable(false, inc.getSystem(), c.getCode(), c.hasDisplay() ? c.getDisplay() : cc != null ? cc.getDisplay() : "", td);
                    td = tr.td();
                    if (!Utilities.noString(c.getDisplay()))
                        td.addText(c.getDisplay());
                    else if (cc != null && !Utilities.noString(cc.getDisplay()))
                        td.addText(cc.getDisplay());
                    td = tr.td();
                    if (ExtensionHelper.hasExtension(c, ToolingExtensions.EXT_DEFINITION))
                        smartAddText(td, ToolingExtensions.readStringExtension(c, ToolingExtensions.EXT_DEFINITION));
                    else if (cc != null && !Utilities.noString(cc.getDefinition()))
                        smartAddText(td, cc.getDefinition());
                    if (ExtensionHelper.hasExtension(c, ToolingExtensions.EXT_VS_COMMENT)) {
                        smartAddText(tr.td(), "Note: " + ToolingExtensions.readStringExtension(c, ToolingExtensions.EXT_VS_COMMENT));
                    }
                    for (ConceptReferenceDesignationComponent cd : c.getDesignation()) {
                        if (cd.hasLanguage() && !langs.contains(cd.getLanguage()))
                            langs.add(cd.getLanguage());
                    }
                }
            }
            boolean first = true;
            for (ConceptSetFilterComponent f : inc.getFilter()) {
                if (first) {
                    li.addText(type + " codes from ");
                    first = false;
                } else
                    li.tx(" and ");
                addCsRef(inc, li, e);
                li.tx(" where " + f.getProperty() + " " + describe(f.getOp()) + " ");
                if (e != null && codeExistsInValueSet(e, f.getValue())) {
                    String href = prefix + getCsRef(e);
                    if (href.contains("#"))
                        href = href + "-" + Utilities.nmtokenize(f.getValue());
                    else
                        href = href + "#" + e.getId() + "-" + Utilities.nmtokenize(f.getValue());
                    li.ah(href).addText(f.getValue());
                } else if ("concept".equals(f.getProperty()) && inc.hasSystem()) {
                    li.addText(f.getValue());
                    ValidationResult vr = context.validateCode(terminologyServiceOptions, inc.getSystem(), f.getValue(), null);
                    if (vr.isOk()) {
                        li.tx(" (" + vr.getDisplay() + ")");
                    }
                } else
                    li.addText(f.getValue());
                String disp = ToolingExtensions.getDisplayHint(f);
                if (disp != null)
                    li.tx(" (" + disp + ")");
            }
        }
        if (inc.hasValueSet()) {
            li.tx(", where the codes are contained in ");
            boolean first = true;
            for (UriType vs : inc.getValueSet()) {
                if (first)
                    first = false;
                else
                    li.tx(", ");
                AddVsRef(rcontext, vs.asStringValue(), li);
            }
        }
    } else {
        li.tx("Import all the codes that are contained in ");
        boolean first = true;
        for (UriType vs : inc.getValueSet()) {
            if (first)
                first = false;
            else
                li.tx(", ");
            AddVsRef(rcontext, vs.asStringValue(), li);
        }
    }
    return hasExtensions;
}
Also used : ValidationResult(org.hl7.fhir.r4.context.IWorkerContext.ValidationResult) CodeSystem(org.hl7.fhir.r4.model.CodeSystem) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 88 with ValidationResult

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

the class BaseWorkerContext method validateCode.

@Override
public ValidationResult validateCode(final ValidationOptions optionsArg, final Coding code, final ValueSet vs, final ValidationContextCarrier ctxt) {
    ValidationOptions options = optionsArg != null ? optionsArg : ValidationOptions.defaults();
    if (code.hasSystem()) {
        codeSystemsUsed.add(code.getSystem());
    }
    final CacheToken cacheToken = txCache != null ? txCache.generateValidationToken(options, code, vs) : null;
    ValidationResult res = null;
    if (txCache != null) {
        res = txCache.getValidation(cacheToken);
    }
    if (res != null) {
        updateUnsupportedCodeSystems(res, code, getCodeKey(code));
        return res;
    }
    if (options.isUseClient()) {
        // ok, first we try to validate locally
        try {
            ValueSetCheckerSimple vsc = constructValueSetCheckerSimple(options, vs, ctxt);
            if (!vsc.isServerSide(code.getSystem())) {
                res = vsc.validateCode(code);
                if (txCache != null) {
                    txCache.cacheValidation(cacheToken, res, TerminologyCache.TRANSIENT);
                }
                return res;
            }
        } catch (Exception e) {
        }
    }
    if (!options.isUseServer()) {
        return new ValidationResult(IssueSeverity.WARNING, formatMessage(I18nConstants.UNABLE_TO_VALIDATE_CODE_WITHOUT_USING_SERVER), TerminologyServiceErrorClass.BLOCKED_BY_OPTIONS);
    }
    String codeKey = getCodeKey(code);
    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) {
        txLog("$validate " + csumm + " for " + txCache.summary(vs));
    } else {
        txLog("$validate " + csumm + " before cache exists");
    }
    try {
        Parameters pIn = constructParameters(options, code);
        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);
    }
    updateUnsupportedCodeSystems(res, code, codeKey);
    if (txCache != null) {
        // we never cache unsupported 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.r5.model.Parameters) CacheToken(org.hl7.fhir.r5.context.TerminologyCache.CacheToken) ValueSetCheckerSimple(org.hl7.fhir.r5.terminologies.ValueSetCheckerSimple) ValidationOptions(org.hl7.fhir.utilities.validation.ValidationOptions) 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 89 with ValidationResult

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

the class BaseWorkerContext method validateCodeBatch.

@Override
public void validateCodeBatch(ValidationOptions options, List<? extends CodingValidationRequest> codes, ValueSet vs) {
    if (options == null) {
        options = ValidationOptions.defaults();
    }
    // 3rd pass: hit the server
    for (CodingValidationRequest t : codes) {
        t.setCacheToken(txCache != null ? txCache.generateValidationToken(options, t.getCoding(), vs) : null);
        if (t.getCoding().hasSystem()) {
            codeSystemsUsed.add(t.getCoding().getSystem());
        }
        if (txCache != null) {
            t.setResult(txCache.getValidation(t.getCacheToken()));
        }
    }
    if (options.isUseClient()) {
        for (CodingValidationRequest t : codes) {
            if (!t.hasResult()) {
                try {
                    ValueSetCheckerSimple vsc = constructValueSetCheckerSimple(options, vs);
                    ValidationResult res = vsc.validateCode(t.getCoding());
                    if (txCache != null) {
                        txCache.cacheValidation(t.getCacheToken(), res, TerminologyCache.TRANSIENT);
                    }
                    t.setResult(res);
                } catch (Exception e) {
                }
            }
        }
    }
    for (CodingValidationRequest t : codes) {
        if (!t.hasResult()) {
            String codeKey = t.getCoding().hasVersion() ? t.getCoding().getSystem() + "|" + t.getCoding().getVersion() : t.getCoding().getSystem();
            if (!options.isUseServer()) {
                t.setResult(new ValidationResult(IssueSeverity.WARNING, formatMessage(I18nConstants.UNABLE_TO_VALIDATE_CODE_WITHOUT_USING_SERVER), TerminologyServiceErrorClass.BLOCKED_BY_OPTIONS));
            } else if (unsupportedCodeSystems.contains(codeKey)) {
                t.setResult(new ValidationResult(IssueSeverity.ERROR, formatMessage(I18nConstants.TERMINOLOGY_TX_SYSTEM_NOTKNOWN, t.getCoding().getSystem()), TerminologyServiceErrorClass.CODESYSTEM_UNSUPPORTED));
            } else if (noTerminologyServer) {
                t.setResult(new ValidationResult(IssueSeverity.ERROR, formatMessage(I18nConstants.ERROR_VALIDATING_CODE_RUNNING_WITHOUT_TERMINOLOGY_SERVICES), TerminologyServiceErrorClass.NOSERVICE));
            }
        }
    }
    if (expParameters == null)
        throw new Error(formatMessage(I18nConstants.NO_EXPANSIONPROFILE_PROVIDED));
    // for those that that failed, we try to validate on the server
    Bundle batch = new Bundle();
    batch.setType(BundleType.BATCH);
    Set<String> systems = new HashSet<>();
    for (CodingValidationRequest codingValidationRequest : codes) {
        if (!codingValidationRequest.hasResult()) {
            Parameters pIn = constructParameters(options, codingValidationRequest, vs);
            setTerminologyOptions(options, pIn);
            BundleEntryComponent be = batch.addEntry();
            be.setResource(pIn);
            be.getRequest().setMethod(HTTPVerb.POST);
            be.getRequest().setUrl("CodeSystem/$validate-code");
            be.setUserData("source", codingValidationRequest);
            systems.add(codingValidationRequest.getCoding().getSystem());
        }
    }
    if (batch.getEntry().size() > 0) {
        txLog("$batch validate for " + batch.getEntry().size() + " codes on systems " + systems.toString());
        if (txClient == null) {
            throw new FHIRException(formatMessage(I18nConstants.ATTEMPT_TO_USE_TERMINOLOGY_SERVER_WHEN_NO_TERMINOLOGY_SERVER_IS_AVAILABLE));
        }
        if (txLog != null) {
            txLog.clearLastId();
        }
        Bundle resp = txClient.validateBatch(batch);
        if (resp == null) {
            throw new FHIRException(formatMessage(I18nConstants.TX_SERVER_NO_BATCH_RESPONSE));
        }
        for (int i = 0; i < batch.getEntry().size(); i++) {
            CodingValidationRequest t = (CodingValidationRequest) batch.getEntry().get(i).getUserData("source");
            BundleEntryComponent r = resp.getEntry().get(i);
            if (r.getResource() instanceof Parameters) {
                t.setResult(processValidationResult((Parameters) r.getResource()));
                if (txCache != null) {
                    txCache.cacheValidation(t.getCacheToken(), t.getResult(), TerminologyCache.PERMANENT);
                }
            } else {
                t.setResult(new ValidationResult(IssueSeverity.ERROR, getResponseText(r.getResource())).setTxLink(txLog == null ? null : txLog.getLastId()));
            }
        }
    }
}
Also used : Parameters(org.hl7.fhir.r5.model.Parameters) Bundle(org.hl7.fhir.r5.model.Bundle) FHIRException(org.hl7.fhir.exceptions.FHIRException) 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) BundleEntryComponent(org.hl7.fhir.r5.model.Bundle.BundleEntryComponent) ValueSetCheckerSimple(org.hl7.fhir.r5.terminologies.ValueSetCheckerSimple) HashSet(java.util.HashSet)

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