Search in sources :

Example 6 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, CodingValidationRequest codingValidationRequest, ValueSet valueSet) {
    Parameters pIn = new Parameters();
    pIn.addParameter().setName("coding").setValue(codingValidationRequest.getCoding());
    if (options.isGuessSystem()) {
        pIn.addParameter().setName("implySystem").setValue(new BooleanType(true));
    }
    if (valueSet != null) {
        pIn.addParameter().setName("valueSet").setResource(valueSet);
    }
    pIn.addParameter().setName("profile").setResource(expParameters);
    return pIn;
}
Also used : Parameters(org.hl7.fhir.r5.model.Parameters) BooleanType(org.hl7.fhir.r5.model.BooleanType)

Example 7 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, CodeableConcept code, ValueSet vs) {
    CacheToken cacheToken = txCache.generateValidationToken(options, code, vs);
    ValidationResult res = txCache.getValidation(cacheToken);
    if (res != null) {
        return res;
    }
    for (Coding c : code.getCoding()) {
        if (c.hasSystem()) {
            codeSystemsUsed.add(c.getSystem());
        }
    }
    if (options.isUseClient()) {
        // ok, first we try to validate locally
        try {
            ValueSetCheckerSimple vsc = constructValueSetCheckerSimple(options, vs);
            res = vsc.validateCode(code);
            txCache.cacheValidation(cacheToken, res, TerminologyCache.TRANSIENT);
            return res;
        } catch (Exception e) {
            if (e instanceof NoTerminologyServiceException) {
                return new ValidationResult(IssueSeverity.ERROR, "No Terminology Service", TerminologyServiceErrorClass.NOSERVICE);
            }
        }
    }
    if (!options.isUseServer()) {
        return new ValidationResult(IssueSeverity.WARNING, "Unable to validate code without using server", TerminologyServiceErrorClass.BLOCKED_BY_OPTIONS);
    }
    // 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);
    }
    txLog("$validate " + txCache.summary(code) + " for " + txCache.summary(vs));
    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.getLastId());
    }
    txCache.cacheValidation(cacheToken, res, TerminologyCache.PERMANENT);
    return res;
}
Also used : NoTerminologyServiceException(org.hl7.fhir.exceptions.NoTerminologyServiceException) Parameters(org.hl7.fhir.r5.model.Parameters) Coding(org.hl7.fhir.r5.model.Coding) CacheToken(org.hl7.fhir.r5.context.TerminologyCache.CacheToken) ValueSetCheckerSimple(org.hl7.fhir.r5.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 8 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, Coding coding) {
    Parameters pIn = new Parameters();
    pIn.addParameter().setName("coding").setValue(coding);
    if (options.isGuessSystem()) {
        pIn.addParameter().setName("implySystem").setValue(new BooleanType(true));
    }
    setTerminologyOptions(options, pIn);
    return pIn;
}
Also used : Parameters(org.hl7.fhir.r5.model.Parameters) BooleanType(org.hl7.fhir.r5.model.BooleanType)

Example 9 with ValidationOptions

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

the class BaseWorkerContext method validateOnServer.

protected ValidationResult validateOnServer(ValueSet vs, Parameters pin, ValidationOptions options) throws FHIRException {
    boolean cache = false;
    if (vs != null) {
        for (ConceptSetComponent inc : vs.getCompose().getInclude()) {
            codeSystemsUsed.add(inc.getSystem());
        }
        for (ConceptSetComponent inc : vs.getCompose().getExclude()) {
            codeSystemsUsed.add(inc.getSystem());
        }
    }
    if (vs != null) {
        if (isTxCaching && cacheId != null && vs.getUrl() != null && cached.contains(vs.getUrl() + "|" + vs.getVersion())) {
            pin.addParameter().setName("url").setValue(new UriType(vs.getUrl() + (vs.hasVersion() ? "|" + vs.getVersion() : "")));
        } else if (options.getVsAsUrl()) {
            pin.addParameter().setName("url").setValue(new StringType(vs.getUrl()));
        } else {
            pin.addParameter().setName("valueSet").setResource(vs);
            if (vs.getUrl() != null) {
                cached.add(vs.getUrl() + "|" + vs.getVersion());
            }
        }
        cache = true;
        addDependentResources(pin, vs);
    }
    if (cache) {
        pin.addParameter().setName("cache-id").setValue(new StringType(cacheId));
    }
    for (ParametersParameterComponent pp : pin.getParameter()) {
        if (pp.getName().equals("profile")) {
            throw new Error(formatMessage(I18nConstants.CAN_ONLY_SPECIFY_PROFILE_IN_THE_CONTEXT));
        }
    }
    if (expParameters == null) {
        throw new Error(formatMessage(I18nConstants.NO_EXPANSIONPROFILE_PROVIDED));
    }
    pin.addParameter().setName("profile").setResource(expParameters);
    if (txLog != null) {
        txLog.clearLastId();
    }
    if (txClient == null) {
        throw new FHIRException(formatMessage(I18nConstants.ATTEMPT_TO_USE_TERMINOLOGY_SERVER_WHEN_NO_TERMINOLOGY_SERVER_IS_AVAILABLE));
    }
    Parameters pOut;
    if (vs == null) {
        pOut = txClient.validateCS(pin);
    } else {
        pOut = txClient.validateVS(pin);
    }
    return processValidationResult(pOut);
}
Also used : ConceptSetComponent(org.hl7.fhir.r5.model.ValueSet.ConceptSetComponent) Parameters(org.hl7.fhir.r5.model.Parameters) StringType(org.hl7.fhir.r5.model.StringType) FHIRException(org.hl7.fhir.exceptions.FHIRException) UriType(org.hl7.fhir.r5.model.UriType) ParametersParameterComponent(org.hl7.fhir.r5.model.Parameters.ParametersParameterComponent)

Example 10 with ValidationOptions

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

the class TerminologyCache method generateValidationToken.

public CacheToken generateValidationToken(ValidationOptions options, Coding code, ValueSet vs) {
    CacheToken ct = new CacheToken();
    if (code.hasSystem()) {
        ct.name = getNameForSystem(code.getSystem());
        ct.hasVersion = code.hasVersion();
    } else
        ct.name = NAME_FOR_NO_SYSTEM;
    nameCacheToken(vs, ct);
    JsonParser json = new JsonParser();
    json.setOutputStyle(OutputStyle.PRETTY);
    if (vs != null && vs.hasUrl() && vs.hasVersion()) {
        try {
            ct.request = "{\"code\" : " + json.composeString(code, "codeableConcept") + ", \"url\": \"" + Utilities.escapeJson(vs.getUrl()) + "\", \"version\": \"" + Utilities.escapeJson(vs.getVersion()) + "\"" + (options == null ? "" : ", " + options.toJson()) + "}\r\n";
        } catch (IOException e) {
            throw new Error(e);
        }
    } else {
        ValueSet vsc = getVSEssense(vs);
        try {
            ct.request = "{\"code\" : " + json.composeString(code, "code") + ", \"valueSet\" :" + (vsc == null ? "null" : extracted(json, vsc)) + (options == null ? "" : ", " + options.toJson()) + "}";
        } catch (IOException e) {
            throw new Error(e);
        }
    }
    ct.key = String.valueOf(hashJson(ct.request));
    return ct;
}
Also used : IOException(java.io.IOException) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

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