Search in sources :

Example 6 with ValidationContextCarrier

use of org.hl7.fhir.r5.utils.validation.ValidationContextCarrier 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)

Aggregations

ValidationOptions (org.hl7.fhir.utilities.validation.ValidationOptions)5 Coding (org.hl7.fhir.r5.model.Coding)4 ValueSet (org.hl7.fhir.r5.model.ValueSet)4 ValidationContextCarrier (org.hl7.fhir.r5.utils.validation.ValidationContextCarrier)4 IOException (java.io.IOException)3 FHIRException (org.hl7.fhir.exceptions.FHIRException)3 Test (org.junit.jupiter.api.Test)3 FileNotFoundException (java.io.FileNotFoundException)2 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)2 NoTerminologyServiceException (org.hl7.fhir.exceptions.NoTerminologyServiceException)2 TerminologyServiceException (org.hl7.fhir.exceptions.TerminologyServiceException)2 CacheToken (org.hl7.fhir.r4b.context.TerminologyCache.CacheToken)1 BooleanType (org.hl7.fhir.r4b.model.BooleanType)1 Parameters (org.hl7.fhir.r4b.model.Parameters)1 ValueSetCheckerSimple (org.hl7.fhir.r4b.terminologies.ValueSetCheckerSimple)1 ValidationResult (org.hl7.fhir.r5.context.IWorkerContext.ValidationResult)1 CacheToken (org.hl7.fhir.r5.context.TerminologyCache.CacheToken)1 Parameters (org.hl7.fhir.r5.model.Parameters)1 ValueSetCheckerSimple (org.hl7.fhir.r5.terminologies.ValueSetCheckerSimple)1