Search in sources :

Example 26 with ValidationOptions

use of org.hl7.fhir.utilities.validation.ValidationOptions 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 27 with ValidationOptions

use of org.hl7.fhir.utilities.validation.ValidationOptions in project org.hl7.fhir.core by hapifhir.

the class CodeSystemValidator method validateCodeSystem.

public void validateCodeSystem(List<ValidationMessage> errors, Element cs, NodeStack stack, ValidationOptions options) {
    String url = cs.getNamedChildValue("url");
    String content = cs.getNamedChildValue("content");
    String caseSensitive = cs.getNamedChildValue("caseSensitive");
    String hierarchyMeaning = cs.getNamedChildValue("hierarchyMeaning");
    String supp = cs.getNamedChildValue("supplements");
    metaChecks(errors, cs, stack, url, content, caseSensitive, hierarchyMeaning, !Utilities.noString(supp));
    String vsu = cs.getNamedChildValue("valueSet");
    if (!Utilities.noString(vsu)) {
        hint(errors, IssueType.BUSINESSRULE, stack.getLiteralPath(), "complete".equals(content), I18nConstants.CODESYSTEM_CS_NO_VS_NOTCOMPLETE);
        ValueSet vs;
        try {
            vs = context.fetchResourceWithException(ValueSet.class, vsu);
        } catch (FHIRException e) {
            vs = null;
        }
        if (vs != null) {
            if (rule(errors, IssueType.BUSINESSRULE, stack.getLiteralPath(), vs.hasCompose(), I18nConstants.CODESYSTEM_CS_VS_INVALID, url, vsu)) {
                if (rule(errors, IssueType.BUSINESSRULE, stack.getLiteralPath(), vs.getCompose().getInclude().size() == 1, I18nConstants.CODESYSTEM_CS_VS_INVALID, url, vsu)) {
                    if (rule(errors, IssueType.BUSINESSRULE, stack.getLiteralPath(), vs.getCompose().getInclude().get(0).getSystem().equals(url), I18nConstants.CODESYSTEM_CS_VS_WRONGSYSTEM, url, vsu, vs.getCompose().getInclude().get(0).getSystem())) {
                        rule(errors, IssueType.BUSINESSRULE, stack.getLiteralPath(), !vs.getCompose().getInclude().get(0).hasValueSet() && !vs.getCompose().getInclude().get(0).hasConcept() && !vs.getCompose().getInclude().get(0).hasFilter(), I18nConstants.CODESYSTEM_CS_VS_INCLUDEDETAILS, url, vsu);
                        if (vs.hasExpansion()) {
                            int count = countConcepts(cs);
                            rule(errors, IssueType.BUSINESSRULE, stack.getLiteralPath(), vs.getExpansion().getContains().size() == count, I18nConstants.CODESYSTEM_CS_VS_EXP_MISMATCH, url, vsu, count, vs.getExpansion().getContains().size());
                        }
                    }
                }
            }
        }
    }
    if (supp != null) {
        if (context.supportsSystem(supp)) {
            List<Element> concepts = cs.getChildrenByName("concept");
            int ce = 0;
            for (Element concept : concepts) {
                validateSupplementConcept(errors, concept, stack.push(concept, ce, null, null), supp, options);
                ce++;
            }
        } else {
            if (cs.hasChildren("concept")) {
                warning(errors, IssueType.BUSINESSRULE, stack.getLiteralPath(), false, I18nConstants.CODESYSTEM_CS_SUPP_CANT_CHECK, supp);
            }
        }
    }
}
Also used : Element(org.hl7.fhir.r5.elementmodel.Element) ValueSet(org.hl7.fhir.r5.model.ValueSet) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 28 with ValidationOptions

use of org.hl7.fhir.utilities.validation.ValidationOptions in project org.hl7.fhir.core by hapifhir.

the class BaseWorkerContext method constructParameters.

protected Parameters constructParameters(ValidationOptions options, CodeableConcept codeableConcept) {
    Parameters pIn = new Parameters();
    pIn.addParameter().setName("codeableConcept").setValue(codeableConcept);
    setTerminologyOptions(options, pIn);
    return pIn;
}
Also used : Parameters(org.hl7.fhir.r5.model.Parameters)

Example 29 with ValidationOptions

use of org.hl7.fhir.utilities.validation.ValidationOptions 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 30 with ValidationOptions

use of org.hl7.fhir.utilities.validation.ValidationOptions 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

IOException (java.io.IOException)15 FHIRException (org.hl7.fhir.exceptions.FHIRException)12 FileNotFoundException (java.io.FileNotFoundException)8 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)8 TerminologyServiceException (org.hl7.fhir.exceptions.TerminologyServiceException)8 Parameters (org.hl7.fhir.r5.model.Parameters)7 ValueSet (org.hl7.fhir.r5.model.ValueSet)7 ValidationOptions (org.hl7.fhir.utilities.validation.ValidationOptions)7 NoTerminologyServiceException (org.hl7.fhir.exceptions.NoTerminologyServiceException)6 Test (org.junit.jupiter.api.Test)6 Coding (org.hl7.fhir.r5.model.Coding)5 ValidationOptions (ca.uhn.fhir.validation.ValidationOptions)4 Parameters (org.hl7.fhir.r4b.model.Parameters)4 ValidationContextCarrier (org.hl7.fhir.r5.utils.validation.ValidationContextCarrier)4 ValidationResult (ca.uhn.fhir.validation.ValidationResult)3 ValueSetCheckerSimple (org.hl7.fhir.r4b.terminologies.ValueSetCheckerSimple)3 OrganizationFactory.generateFakeOrganization (gov.cms.dpc.testing.factories.OrganizationFactory.generateFakeOrganization)2 HashSet (java.util.HashSet)2 Organization (org.hl7.fhir.dstu3.model.Organization)2 CacheToken (org.hl7.fhir.r4.context.TerminologyCache.CacheToken)2