Search in sources :

Example 1 with ValueSetCheckerSimple

use of org.hl7.fhir.r4.terminologies.ValueSetCheckerSimple in project org.hl7.fhir.core by hapifhir.

the class ValueSetExpanderSimple method expand.

@Override
public ValueSetExpansionOutcome expand(ValueSet source) {
    try {
        focus = source.copy();
        focus.setExpansion(new ValueSet.ValueSetExpansionComponent());
        focus.getExpansion().setTimestampElement(DateTimeType.now());
        focus.getExpansion().setIdentifier(Factory.createUUID());
        handleDefine(source, focus.getExpansion().getParameter());
        if (source.hasCompose())
            handleCompose(source.getCompose(), focus.getExpansion().getParameter());
        for (ValueSetExpansionContainsComponent c : codes) {
            if (map.containsKey(key(c))) {
                focus.getExpansion().getContains().add(c);
            }
        }
        return new ValueSetExpansionOutcome(focus, null);
    } catch (Exception e) {
        // that might fail too, but it might not, later.
        return new ValueSetExpansionOutcome(new ValueSetCheckerSimple(source, factory, context), e.getMessage());
    }
}
Also used : ValueSetExpansionContainsComponent(org.hl7.fhir.dstu2.model.ValueSet.ValueSetExpansionContainsComponent) ValueSetExpansionComponent(org.hl7.fhir.dstu2.model.ValueSet.ValueSetExpansionComponent) ValueSet(org.hl7.fhir.dstu2.model.ValueSet) NotImplementedException(org.apache.commons.lang3.NotImplementedException) IOException(java.io.IOException) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with ValueSetCheckerSimple

use of org.hl7.fhir.r4.terminologies.ValueSetCheckerSimple in project org.hl7.fhir.core by hapifhir.

the class ValueSetExpanderSimple method expand.

@Override
public ValueSetExpansionOutcome expand(ValueSet source) {
    try {
        focus = source.copy();
        focus.setExpansion(new ValueSet.ValueSetExpansionComponent());
        focus.getExpansion().setTimestampElement(DateTimeType.now());
        focus.getExpansion().setIdentifier(Factory.createUUID());
        if (source.hasCompose())
            handleCompose(source.getCompose(), focus.getExpansion().getParameter());
        for (ValueSetExpansionContainsComponent c : codes) {
            if (map.containsKey(key(c))) {
                focus.getExpansion().getContains().add(c);
            }
        }
        return new ValueSetExpansionOutcome(focus, null);
    } catch (RuntimeException e) {
        // it swallows bugs.. what would be expected to be caught there?
        throw e;
    } catch (Exception e) {
        // that might fail too, but it might not, later.
        return new ValueSetExpansionOutcome(new ValueSetCheckerSimple(source, factory, context), e.getMessage());
    }
}
Also used : ValueSetExpansionContainsComponent(org.hl7.fhir.dstu2016may.model.ValueSet.ValueSetExpansionContainsComponent) ValueSetExpansionComponent(org.hl7.fhir.dstu2016may.model.ValueSet.ValueSetExpansionComponent) ValueSet(org.hl7.fhir.dstu2016may.model.ValueSet) NotImplementedException(org.apache.commons.lang3.NotImplementedException) IOException(java.io.IOException) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with ValueSetCheckerSimple

use of org.hl7.fhir.r4.terminologies.ValueSetCheckerSimple 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 = new ValueSetCheckerSimple(options, vs, this);
                    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 t : codes) {
        if (!t.hasResult()) {
            Parameters pIn = new Parameters();
            pIn.addParameter().setName("coding").setValue(t.getCoding());
            if (options.isGuessSystem()) {
                pIn.addParameter().setName("implySystem").setValue(new BooleanType(true));
            }
            if (vs != null) {
                pIn.addParameter().setName("valueSet").setResource(vs);
            }
            pIn.addParameter().setName("profile").setResource(expParameters);
            setTerminologyOptions(options, pIn);
            BundleEntryComponent be = batch.addEntry();
            be.setResource(pIn);
            be.getRequest().setMethod(HTTPVerb.POST);
            be.getRequest().setUrl("CodeSystem/$validate-code");
            be.setUserData("source", t);
            systems.add(t.getCoding().getSystem());
        }
    }
    if (batch.getEntry().size() > 0) {
        tlog("$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);
        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.r4b.model.Parameters) Bundle(org.hl7.fhir.r4b.model.Bundle) BooleanType(org.hl7.fhir.r4b.model.BooleanType) 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.r4b.model.Bundle.BundleEntryComponent) ValueSetCheckerSimple(org.hl7.fhir.r4b.terminologies.ValueSetCheckerSimple) HashSet(java.util.HashSet)

Example 4 with ValueSetCheckerSimple

use of org.hl7.fhir.r4.terminologies.ValueSetCheckerSimple 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 = new ValueSetCheckerSimple(options, vs, this);
            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);
    }
    tlog("$validate " + txCache.summary(code) + " for " + txCache.summary(vs));
    try {
        Parameters pIn = new Parameters();
        pIn.addParameter().setName("codeableConcept").setValue(code);
        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.getLastId());
    }
    txCache.cacheValidation(cacheToken, res, TerminologyCache.PERMANENT);
    return res;
}
Also used : NoTerminologyServiceException(org.hl7.fhir.exceptions.NoTerminologyServiceException) Parameters(org.hl7.fhir.r4b.model.Parameters) Coding(org.hl7.fhir.r4b.model.Coding) CacheToken(org.hl7.fhir.r4b.context.TerminologyCache.CacheToken) 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 5 with ValueSetCheckerSimple

use of org.hl7.fhir.r4.terminologies.ValueSetCheckerSimple 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)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)10 IOException (java.io.IOException)10 TerminologyServiceException (org.hl7.fhir.exceptions.TerminologyServiceException)10 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)8 FHIRException (org.hl7.fhir.exceptions.FHIRException)8 ValueSet (org.hl7.fhir.r5.model.ValueSet)7 NoTerminologyServiceException (org.hl7.fhir.exceptions.NoTerminologyServiceException)6 Test (org.junit.jupiter.api.Test)6 ValidationOptions (org.hl7.fhir.utilities.validation.ValidationOptions)5 Coding (org.hl7.fhir.r5.model.Coding)4 Parameters (org.hl7.fhir.r4b.model.Parameters)3 ValueSetCheckerSimple (org.hl7.fhir.r4b.terminologies.ValueSetCheckerSimple)3 CodeableConcept (org.hl7.fhir.r5.model.CodeableConcept)3 Parameters (org.hl7.fhir.r5.model.Parameters)3 ValueSetCheckerSimple (org.hl7.fhir.r5.terminologies.ValueSetCheckerSimple)3 ValidationContextCarrier (org.hl7.fhir.r5.utils.validation.ValidationContextCarrier)3 HashSet (java.util.HashSet)2 NotImplementedException (org.apache.commons.lang3.NotImplementedException)2 CacheToken (org.hl7.fhir.r4.context.TerminologyCache.CacheToken)2 ValueSetCheckerSimple (org.hl7.fhir.r4.terminologies.ValueSetCheckerSimple)2