use of org.hl7.fhir.r5.context.IWorkerContext.ValidationResult in project org.hl7.fhir.core by hapifhir.
the class BaseWorkerContext method validateCode.
@Override
public ValidationResult validateCode(ValidationOptions options, Coding code, ValueSet vs, ValidationContextCarrier ctxt) {
if (options == null) {
options = ValidationOptions.defaults();
}
if (code.hasSystem()) {
codeSystemsUsed.add(code.getSystem());
}
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;
}
if (options.isUseClient()) {
// ok, first we try to validate locally
try {
ValueSetCheckerSimple vsc = new ValueSetCheckerSimple(options, vs, this, ctxt);
if (!vsc.isServerSide(code.getSystem())) {
res = vsc.validateCode(code);
if (txCache != null) {
txCache.cacheValidation(cacheToken, res, TerminologyCache.TRANSIENT);
}
return res;
}
} catch (Exception e) {
}
}
String codeKey = code.hasVersion() ? code.getSystem() + "|" + code.getVersion() : code.getSystem();
if (!options.isUseServer()) {
return new ValidationResult(IssueSeverity.WARNING, formatMessage(I18nConstants.UNABLE_TO_VALIDATE_CODE_WITHOUT_USING_SERVER), TerminologyServiceErrorClass.BLOCKED_BY_OPTIONS);
}
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) {
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 (options.isGuessSystem()) {
pIn.addParameter().setName("implySystem").setValue(new BooleanType(true));
}
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 == null ? null : txLog.getLastId()).setErrorClass(TerminologyServiceErrorClass.SERVER_ERROR);
}
if (res.getErrorClass() == TerminologyServiceErrorClass.CODESYSTEM_UNSUPPORTED && !code.hasVersion()) {
unsupportedCodeSystems.add(codeKey);
} else if (txCache != null) {
// we never cache unsuppoted code systems - we always keep trying (but only once per run)
txCache.cacheValidation(cacheToken, res, TerminologyCache.PERMANENT);
}
return res;
}
use of org.hl7.fhir.r5.context.IWorkerContext.ValidationResult in project org.hl7.fhir.core by hapifhir.
the class InstanceValidator method checkMaxValueSet.
private void checkMaxValueSet(List<ValidationMessage> errors, String path, Element element, StructureDefinition profile, String maxVSUrl, CodeableConcept cc, NodeStack stack) {
ValueSet valueset = resolveBindingReference(profile, maxVSUrl, profile.getUrl());
if (valueset == null) {
CodeSystem cs = context.fetchCodeSystem(maxVSUrl);
if (rule(errors, IssueType.CODEINVALID, element.line(), element.col(), path, cs == null, I18nConstants.TERMINOLOGY_TX_VALUESET_NOTFOUND_CS, describeReference(maxVSUrl))) {
warning(errors, IssueType.CODEINVALID, element.line(), element.col(), path, valueset != null, I18nConstants.TERMINOLOGY_TX_VALUESET_NOTFOUND, describeReference(maxVSUrl));
}
} else {
try {
long t = System.nanoTime();
ValidationResult vr = checkCodeOnServer(stack, valueset, cc, false);
timeTracker.tx(t, "vc " + cc.toString());
if (!vr.isOk()) {
if (vr.getErrorClass() != null && vr.getErrorClass().isInfrastructure())
txWarning(errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_NOVALID_7, describeValueSet(maxVSUrl), vr.getMessage());
else
txRule(errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_NOVALID_8, describeValueSet(maxVSUrl), ccSummary(cc));
}
} catch (Exception e) {
if (STACK_TRACE)
e.printStackTrace();
warning(errors, IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_ERROR_CODEABLECONCEPT_MAX, e.getMessage());
}
}
}
use of org.hl7.fhir.r5.context.IWorkerContext.ValidationResult in project org.hl7.fhir.core by hapifhir.
the class InstanceValidator method checkMaxValueSet.
private void checkMaxValueSet(List<ValidationMessage> errors, String path, Element element, StructureDefinition profile, String maxVSUrl, String value, NodeStack stack) {
ValueSet valueset = resolveBindingReference(profile, maxVSUrl, profile.getUrl());
if (valueset == null) {
CodeSystem cs = context.fetchCodeSystem(maxVSUrl);
if (rule(errors, IssueType.CODEINVALID, element.line(), element.col(), path, cs == null, I18nConstants.TERMINOLOGY_TX_VALUESET_NOTFOUND_CS, describeReference(maxVSUrl))) {
warning(errors, IssueType.CODEINVALID, element.line(), element.col(), path, valueset != null, I18nConstants.TERMINOLOGY_TX_VALUESET_NOTFOUND, describeReference(maxVSUrl));
}
} else {
try {
long t = System.nanoTime();
ValidationResult vr = checkCodeOnServer(stack, valueset, value, baseOptions.setLanguage(stack.getWorkingLang()));
timeTracker.tx(t, "vc " + value);
if (!vr.isOk()) {
if (vr.getErrorClass() != null && vr.getErrorClass().isInfrastructure())
txWarning(errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_NOVALID_9, describeValueSet(maxVSUrl), vr.getMessage());
else
txRule(errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_NOVALID_11, describeValueSet(maxVSUrl), vr.getMessage());
}
} catch (Exception e) {
if (STACK_TRACE)
e.printStackTrace();
warning(errors, IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_ERROR_CODEABLECONCEPT_MAX, e.getMessage());
}
}
}
use of org.hl7.fhir.r5.context.IWorkerContext.ValidationResult in project org.hl7.fhir.core by hapifhir.
the class ProfileUtilities method describeCoded.
private Piece describeCoded(HierarchicalTableGenerator gen, Type fixed) {
if (fixed instanceof Coding) {
Coding c = (Coding) fixed;
ValidationResult vr = context.validateCode(c.getSystem(), c.getCode(), c.getDisplay());
if (vr.getDisplay() != null)
return gen.new Piece(null, " (" + vr.getDisplay() + ")", null).addStyle("color: darkgreen");
} else if (fixed instanceof CodeableConcept) {
CodeableConcept cc = (CodeableConcept) fixed;
for (Coding c : cc.getCoding()) {
ValidationResult vr = context.validateCode(c.getSystem(), c.getCode(), c.getDisplay());
if (vr.getDisplay() != null)
return gen.new Piece(null, " (" + vr.getDisplay() + ")", null).addStyle("color: darkgreen");
}
}
return null;
}
use of org.hl7.fhir.r5.context.IWorkerContext.ValidationResult in project org.hl7.fhir.core by hapifhir.
the class BaseWorkerContext method serverValidateCode.
private ValidationResult serverValidateCode(Parameters pin, boolean doCache) throws Exception {
if (noTerminologyServer) {
return new ValidationResult(null, null, TerminologyServiceErrorClass.NOSERVICE);
}
String cacheName = doCache ? generateCacheName(pin) : null;
ValidationResult res = loadFromCache(cacheName);
if (res != null) {
return res;
}
tlog("Terminology Server: $validate-code " + describeValidationParameters(pin));
for (ParametersParameterComponent pp : pin.getParameter()) {
if (pp.getName().equals("profile")) {
throw new Error("Can only specify profile in the context");
}
}
if (expProfile == null) {
throw new Exception("No ExpansionProfile provided");
}
pin.addParameter().setName("profile").setResource(expProfile);
Parameters pout = txServer.operateType(ValueSet.class, "validate-code", pin);
boolean ok = false;
String message = "No Message returned";
String display = null;
TerminologyServiceErrorClass err = TerminologyServiceErrorClass.UNKNOWN;
for (ParametersParameterComponent p : pout.getParameter()) {
if (p.getName().equals("result")) {
ok = ((BooleanType) p.getValue()).getValue().booleanValue();
} else if (p.getName().equals("message")) {
message = ((StringType) p.getValue()).getValue();
} else if (p.getName().equals("display")) {
display = ((StringType) p.getValue()).getValue();
} else if (p.getName().equals("cause")) {
try {
IssueType it = IssueType.fromCode(((StringType) p.getValue()).getValue());
if (it == IssueType.UNKNOWN) {
err = TerminologyServiceErrorClass.UNKNOWN;
} else if (it == IssueType.NOTSUPPORTED) {
err = TerminologyServiceErrorClass.VALUESET_UNSUPPORTED;
}
} catch (FHIRException e) {
}
}
}
if (!ok) {
res = new ValidationResult(IssueSeverity.ERROR, message, err);
} else if (display != null) {
res = new ValidationResult(new ConceptDefinitionComponent().setDisplay(display));
} else {
res = new ValidationResult(new ConceptDefinitionComponent());
}
saveToCache(res, cacheName);
return res;
}
Aggregations