Search in sources :

Example 1 with CodingValidationRequest

use of org.hl7.fhir.r5.context.IWorkerContext.CodingValidationRequest 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 2 with CodingValidationRequest

use of org.hl7.fhir.r5.context.IWorkerContext.CodingValidationRequest 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 3 with CodingValidationRequest

use of org.hl7.fhir.r5.context.IWorkerContext.CodingValidationRequest in project org.hl7.fhir.core by hapifhir.

the class ValueSetRenderer method getConceptsForCodes.

private Map<String, ConceptDefinitionComponent> getConceptsForCodes(CodeSystem e, ConceptSetComponent inc) {
    if (e == null) {
        e = getContext().getWorker().fetchCodeSystem(inc.getSystem());
    }
    ValueSetExpansionComponent vse = null;
    if (!context.isNoSlowLookup() && !getContext().getWorker().hasCache()) {
        try {
            ValueSetExpansionOutcome vso = getContext().getWorker().expandVS(inc, false);
            ValueSet valueset = vso.getValueset();
            if (valueset == null)
                throw new TerminologyServiceException("Error Expanding ValueSet: " + vso.getError());
            vse = valueset.getExpansion();
        } catch (TerminologyServiceException e1) {
            return null;
        }
    }
    Map<String, ConceptDefinitionComponent> results = new HashMap<>();
    List<CodingValidationRequest> serverList = new ArrayList<>();
    // 1st pass, anything we can resolve internally
    for (ConceptReferenceComponent cc : inc.getConcept()) {
        String code = cc.getCode();
        ConceptDefinitionComponent v = null;
        if (e != null) {
            v = getConceptForCode(e.getConcept(), code);
        }
        if (v == null && vse != null) {
            v = getConceptForCodeFromExpansion(vse.getContains(), code);
        }
        if (v != null) {
            results.put(code, v);
        } else {
            serverList.add(new CodingValidationRequest(new Coding(inc.getSystem(), code, null)));
        }
    }
    if (!context.isNoSlowLookup() && !serverList.isEmpty()) {
        getContext().getWorker().validateCodeBatch(getContext().getTerminologyServiceOptions(), serverList, null);
        for (CodingValidationRequest vr : serverList) {
            ConceptDefinitionComponent v = vr.getResult().asConceptDefinition();
            if (v != null) {
                results.put(vr.getCoding().getCode(), v);
            }
        }
    }
    return results;
}
Also used : ValueSetExpansionComponent(org.hl7.fhir.r4b.model.ValueSet.ValueSetExpansionComponent) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConceptReferenceComponent(org.hl7.fhir.r4b.model.ValueSet.ConceptReferenceComponent) ConceptDefinitionComponent(org.hl7.fhir.r4b.model.CodeSystem.ConceptDefinitionComponent) Coding(org.hl7.fhir.r4b.model.Coding) ValueSetExpansionOutcome(org.hl7.fhir.r4b.terminologies.ValueSetExpander.ValueSetExpansionOutcome) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) ValueSet(org.hl7.fhir.r4b.model.ValueSet) CodingValidationRequest(org.hl7.fhir.r4b.context.IWorkerContext.CodingValidationRequest)

Example 4 with CodingValidationRequest

use of org.hl7.fhir.r5.context.IWorkerContext.CodingValidationRequest in project org.hl7.fhir.core by hapifhir.

the class ValueSetRenderer method getConceptsForCodes.

private Map<String, ConceptDefinitionComponent> getConceptsForCodes(CodeSystem e, ConceptSetComponent inc) {
    if (e == null) {
        e = getContext().getWorker().fetchCodeSystem(inc.getSystem());
    }
    ValueSetExpansionComponent vse = null;
    if (!context.isNoSlowLookup() && !getContext().getWorker().hasCache()) {
        try {
            ValueSetExpansionOutcome vso = getContext().getWorker().expandVS(inc, false, false);
            ValueSet valueset = vso.getValueset();
            if (valueset == null)
                throw new TerminologyServiceException("Error Expanding ValueSet: " + vso.getError());
            vse = valueset.getExpansion();
        } catch (TerminologyServiceException e1) {
            return null;
        }
    }
    Map<String, ConceptDefinitionComponent> results = new HashMap<>();
    List<CodingValidationRequest> serverList = new ArrayList<>();
    // 1st pass, anything we can resolve internally
    for (ConceptReferenceComponent cc : inc.getConcept()) {
        String code = cc.getCode();
        ConceptDefinitionComponent v = null;
        if (e != null) {
            v = getConceptForCode(e.getConcept(), code);
        }
        if (v == null && vse != null) {
            v = getConceptForCodeFromExpansion(vse.getContains(), code);
        }
        if (v != null) {
            results.put(code, v);
        } else {
            serverList.add(new CodingValidationRequest(new Coding(inc.getSystem(), code, null)));
        }
    }
    if (!context.isNoSlowLookup() && !serverList.isEmpty()) {
        getContext().getWorker().validateCodeBatch(getContext().getTerminologyServiceOptions(), serverList, null);
        for (CodingValidationRequest vr : serverList) {
            ConceptDefinitionComponent v = vr.getResult().asConceptDefinition();
            if (v != null) {
                results.put(vr.getCoding().getCode(), v);
            }
        }
    }
    return results;
}
Also used : ValueSetExpansionComponent(org.hl7.fhir.r5.model.ValueSet.ValueSetExpansionComponent) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConceptReferenceComponent(org.hl7.fhir.r5.model.ValueSet.ConceptReferenceComponent) ConceptDefinitionComponent(org.hl7.fhir.r5.model.CodeSystem.ConceptDefinitionComponent) Coding(org.hl7.fhir.r5.model.Coding) ValueSetExpansionOutcome(org.hl7.fhir.r5.terminologies.ValueSetExpander.ValueSetExpansionOutcome) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) ValueSet(org.hl7.fhir.r5.model.ValueSet) CodingValidationRequest(org.hl7.fhir.r5.context.IWorkerContext.CodingValidationRequest)

Example 5 with CodingValidationRequest

use of org.hl7.fhir.r5.context.IWorkerContext.CodingValidationRequest 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

TerminologyServiceException (org.hl7.fhir.exceptions.TerminologyServiceException)4 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)2 FHIRException (org.hl7.fhir.exceptions.FHIRException)2 NoTerminologyServiceException (org.hl7.fhir.exceptions.NoTerminologyServiceException)2 Parameters (org.hl7.fhir.r5.model.Parameters)2 CodingValidationRequest (org.hl7.fhir.r4b.context.IWorkerContext.CodingValidationRequest)1 BooleanType (org.hl7.fhir.r4b.model.BooleanType)1 Bundle (org.hl7.fhir.r4b.model.Bundle)1 BundleEntryComponent (org.hl7.fhir.r4b.model.Bundle.BundleEntryComponent)1 ConceptDefinitionComponent (org.hl7.fhir.r4b.model.CodeSystem.ConceptDefinitionComponent)1 Coding (org.hl7.fhir.r4b.model.Coding)1 Parameters (org.hl7.fhir.r4b.model.Parameters)1 ValueSet (org.hl7.fhir.r4b.model.ValueSet)1 ConceptReferenceComponent (org.hl7.fhir.r4b.model.ValueSet.ConceptReferenceComponent)1 ValueSetExpansionComponent (org.hl7.fhir.r4b.model.ValueSet.ValueSetExpansionComponent)1