Search in sources :

Example 11 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, CodeableConcept code, ValueSet vs) {
    CacheToken ct = new CacheToken();
    for (Coding c : code.getCoding()) {
        if (c.hasSystem()) {
            ct.setName(getNameForSystem(c.getSystem()));
            ct.hasVersion = c.hasVersion();
        }
    }
    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, "codeableConcept") + ", \"valueSet\" :" + 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)

Example 12 with ValidationOptions

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

the class QuestionnaireValidator method validateAnswerCode.

private void validateAnswerCode(List<ValidationMessage> errors, Element value, NodeStack stack, QuestionnaireWithContext qSrc, String ref, boolean theOpenChoice) {
    ValueSet vs = null;
    if (ref.startsWith("#") && qSrc.container != null) {
        vs = (ValueSet) loadContainedResource(errors, qSrc.containerPath, qSrc.container, ref.substring(1), ValueSet.class);
    } else {
        vs = resolveBindingReference(qSrc.q(), ref, qSrc.q().getUrl());
    }
    if (warning(errors, IssueType.CODEINVALID, value.line(), value.col(), stack.getLiteralPath(), vs != null, I18nConstants.TERMINOLOGY_TX_VALUESET_NOTFOUND, describeReference(ref))) {
        try {
            Coding c = ObjectConverter.readAsCoding(value);
            if (isBlank(c.getCode()) && isBlank(c.getSystem()) && isNotBlank(c.getDisplay())) {
                if (theOpenChoice) {
                    return;
                }
            }
            long t = System.nanoTime();
            ValidationContextCarrier vc = makeValidationContext(errors, qSrc);
            ValidationResult res = context.validateCode(new ValidationOptions(stack.getWorkingLang()), c, vs, vc);
            timeTracker.tx(t, "vc " + c.getSystem() + "#" + c.getCode() + " '" + c.getDisplay() + "'");
            if (!res.isOk()) {
                txRule(errors, res.getTxLink(), IssueType.CODEINVALID, value.line(), value.col(), stack.getLiteralPath(), false, I18nConstants.QUESTIONNAIRE_QR_ITEM_BADOPTION, c.getSystem(), c.getCode());
            } else if (res.getSeverity() != null) {
                super.addValidationMessage(errors, IssueType.CODEINVALID, value.line(), value.col(), stack.getLiteralPath(), res.getMessage(), res.getSeverity(), Source.TerminologyEngine, null);
            }
        } catch (Exception e) {
            warning(errors, IssueType.CODEINVALID, value.line(), value.col(), stack.getLiteralPath(), false, I18nConstants.QUESTIONNAIRE_QR_ITEM_CODING, e.getMessage());
        }
    }
}
Also used : Coding(org.hl7.fhir.r5.model.Coding) ValidationContextCarrier(org.hl7.fhir.r5.utils.validation.ValidationContextCarrier) ValidationResult(org.hl7.fhir.r5.context.IWorkerContext.ValidationResult) ValidationOptions(org.hl7.fhir.utilities.validation.ValidationOptions) ValueSet(org.hl7.fhir.r5.model.ValueSet) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 13 with ValidationOptions

use of org.hl7.fhir.utilities.validation.ValidationOptions 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 14 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, CodeableConcept code, ValueSet vs) {
    CacheToken ct = new CacheToken();
    for (Coding c : code.getCoding()) {
        if (c.hasSystem())
            ct.setName(getNameForSystem(c.getSystem()));
    }
    JsonParser json = new JsonParser();
    json.setOutputStyle(OutputStyle.PRETTY);
    ValueSet vsc = getVSEssense(vs);
    try {
        ct.request = "{\"code\" : " + json.composeString(code, "codeableConcept") + ", \"valueSet\" :" + json.composeString(vsc) + (options == null ? "" : ", " + options.toJson()) + "}";
    } catch (IOException e) {
        throw new Error(e);
    }
    ct.key = String.valueOf(hashNWS(ct.request));
    return ct;
}
Also used : Coding(org.hl7.fhir.r4.model.Coding) IOException(java.io.IOException) ValueSet(org.hl7.fhir.r4.model.ValueSet) JsonParser(org.hl7.fhir.r4.formats.JsonParser)

Example 15 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());
    else
        ct.name = NAME_FOR_NO_SYSTEM;
    JsonParser json = new JsonParser();
    json.setOutputStyle(OutputStyle.PRETTY);
    ValueSet vsc = getVSEssense(vs);
    try {
        ct.request = "{\"code\" : " + json.composeString(code, "code") + ", \"valueSet\" :" + (vsc == null ? "null" : json.composeString(vsc)) + (options == null ? "" : ", " + options.toJson()) + "}";
    } catch (IOException e) {
        throw new Error(e);
    }
    ct.key = String.valueOf(hashNWS(ct.request));
    return ct;
}
Also used : IOException(java.io.IOException) ValueSet(org.hl7.fhir.r4.model.ValueSet) JsonParser(org.hl7.fhir.r4.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