use of org.hl7.fhir.r4b.terminologies.ValueSetExpanderSimple in project org.hl7.fhir.core by hapifhir.
the class ValueSetExpanderSimple method importValueSet.
private ValueSet importValueSet(String value, ValueSetExpansionComponent exp, Parameters expParams, boolean noInactive, ValueSet valueSet) throws ETooCostly, TerminologyServiceException, FileNotFoundException, IOException, FHIRFormatError {
if (value == null)
throw fail("unable to find value set with no identity");
ValueSet vs = context.fetchResource(ValueSet.class, value);
if (vs == null) {
if (context.fetchResource(CodeSystem.class, value) != null) {
throw fail("Cannot include value set " + value + " because it's actually a code system");
} else {
throw fail("Unable to find imported value set " + value);
}
}
if (noInactive) {
expParams = expParams.copy();
expParams.addParameter("activeOnly", true);
}
ValueSetExpansionOutcome vso = new ValueSetExpanderSimple(context, allErrors).expand(vs, expParams);
if (vso.getError() != null) {
addErrors(vso.getAllErrors());
throw fail("Unable to expand imported value set " + vs.getUrl() + ": " + vso.getError());
}
if (vs.hasVersion())
if (!existsInParams(exp.getParameter(), "version", new UriType(vs.getUrl() + "|" + vs.getVersion())))
exp.getParameter().add(new ValueSetExpansionParameterComponent().setName("version").setValue(new UriType(vs.getUrl() + "|" + vs.getVersion())));
for (Extension ex : vso.getValueset().getExpansion().getExtension()) {
if (ex.getUrl().equals(ToolingExtensions.EXT_EXP_TOOCOSTLY)) {
if (ex.getValue() instanceof BooleanType) {
exp.getExtension().add(new Extension(ToolingExtensions.EXT_EXP_TOOCOSTLY).setValue(new UriType(value)));
} else {
exp.getExtension().add(ex);
}
}
}
for (ValueSetExpansionParameterComponent p : vso.getValueset().getExpansion().getParameter()) {
if (!existsInParams(exp.getParameter(), p.getName(), p.getValue()))
exp.getParameter().add(p);
}
if (isValueSetUnionImports(valueSet)) {
copyExpansion(vso.getValueset().getExpansion().getContains());
}
// if we're importing a value set, we have to be combining, so we won't try for a heirarchy
canBeHeirarchy = false;
return vso.getValueset();
}
use of org.hl7.fhir.r4b.terminologies.ValueSetExpanderSimple in project org.hl7.fhir.core by hapifhir.
the class ValueSetExpanderSimple method importValueSet.
private ValueSet importValueSet(String value, ValueSetExpansionComponent exp, Parameters expParams) throws ETooCostly, TerminologyServiceException, FileNotFoundException, IOException, FHIRFormatError {
if (value == null)
throw fail("unable to find value set with no identity");
ValueSet vs = context.fetchResource(ValueSet.class, value);
if (vs == null) {
if (context.fetchResource(CodeSystem.class, value) != null) {
throw fail("Cannot include value set " + value + " because it's actually a code system");
} else {
throw fail("Unable to find imported value set " + value);
}
}
ValueSetExpansionOutcome vso = new ValueSetExpanderSimple(context, allErrors).expand(vs, expParams);
if (vso.getError() != null) {
addErrors(vso.getAllErrors());
throw fail("Unable to expand imported value set " + vs.getUrl() + ": " + vso.getError());
}
if (vs.hasVersion())
if (!existsInParams(exp.getParameter(), "version", new UriType(vs.getUrl() + "|" + vs.getVersion())))
exp.getParameter().add(new ValueSetExpansionParameterComponent().setName("version").setValue(new UriType(vs.getUrl() + "|" + vs.getVersion())));
for (Extension ex : vso.getValueset().getExpansion().getExtension()) {
if (ex.getUrl().equals(ToolingExtensions.EXT_EXP_TOOCOSTLY)) {
if (ex.getValue() instanceof BooleanType) {
exp.getExtension().add(new Extension(ToolingExtensions.EXT_EXP_TOOCOSTLY).setValue(new UriType(value)));
} else {
exp.getExtension().add(ex);
}
}
}
for (ValueSetExpansionParameterComponent p : vso.getValueset().getExpansion().getParameter()) {
if (!existsInParams(exp.getParameter(), p.getName(), p.getValue()))
exp.getParameter().add(p);
}
copyExpansion(vso.getValueset().getExpansion().getContains());
// if we're importing a value set, we have to be combining, so we won't try for a heirarchy
canBeHeirarchy = false;
return vso.getValueset();
}
use of org.hl7.fhir.r4b.terminologies.ValueSetExpanderSimple in project org.hl7.fhir.core by hapifhir.
the class BaseWorkerContext method expandVS.
public ValueSetExpansionOutcome expandVS(ValueSet vs, boolean cacheOk, boolean hierarchical, boolean incompleteOk, Parameters pIn) {
if (pIn == null) {
throw new Error(formatMessage(I18nConstants.NO_PARAMETERS_PROVIDED_TO_EXPANDVS));
}
Parameters p = pIn.copy();
if (vs.hasExpansion()) {
return new ValueSetExpansionOutcome(vs.copy());
}
if (!vs.hasUrl()) {
throw new Error(formatMessage(I18nConstants.NO_VALUE_SET_IN_URL));
}
for (ConceptSetComponent inc : vs.getCompose().getInclude()) {
codeSystemsUsed.add(inc.getSystem());
}
for (ConceptSetComponent inc : vs.getCompose().getExclude()) {
codeSystemsUsed.add(inc.getSystem());
}
CacheToken cacheToken = txCache.generateExpandToken(vs, hierarchical);
ValueSetExpansionOutcome res;
if (cacheOk) {
res = txCache.getExpansion(cacheToken);
if (res != null) {
return res;
}
}
p.setParameter("includeDefinition", false);
p.setParameter("excludeNested", !hierarchical);
if (incompleteOk) {
p.setParameter("incomplete-ok", true);
}
List<String> allErrors = new ArrayList<>();
// ok, first we try to expand locally
ValueSetExpanderSimple vse = constructValueSetExpanderSimple();
try {
res = vse.expand(vs, p);
allErrors.addAll(vse.getAllErrors());
if (res.getValueset() != null) {
if (!res.getValueset().hasUrl()) {
throw new Error(formatMessage(I18nConstants.NO_URL_IN_EXPAND_VALUE_SET));
}
txCache.cacheExpansion(cacheToken, res, TerminologyCache.TRANSIENT);
return res;
}
} catch (Exception e) {
allErrors.addAll(vse.getAllErrors());
e.printStackTrace();
}
// if that failed, we try to expand on the server
if (addDependentResources(p, vs)) {
p.addParameter().setName("cache-id").setValue(new StringType(cacheId));
}
if (noTerminologyServer) {
return new ValueSetExpansionOutcome(formatMessage(I18nConstants.ERROR_EXPANDING_VALUESET_RUNNING_WITHOUT_TERMINOLOGY_SERVICES), TerminologyServiceErrorClass.NOSERVICE, allErrors);
}
Map<String, String> params = new HashMap<String, String>();
params.put("_limit", Integer.toString(expandCodesLimit));
params.put("_incomplete", "true");
txLog("$expand on " + txCache.summary(vs));
try {
ValueSet result = txClient.expandValueset(vs, p, params);
if (!result.hasUrl()) {
result.setUrl(vs.getUrl());
}
if (!result.hasUrl()) {
throw new Error(formatMessage(I18nConstants.NO_URL_IN_EXPAND_VALUE_SET_2));
}
res = new ValueSetExpansionOutcome(result).setTxLink(txLog.getLastId());
} catch (Exception e) {
res = new ValueSetExpansionOutcome(e.getMessage() == null ? e.getClass().getName() : e.getMessage(), TerminologyServiceErrorClass.UNKNOWN, allErrors).setTxLink(txLog == null ? null : txLog.getLastId());
}
txCache.cacheExpansion(cacheToken, res, TerminologyCache.PERMANENT);
return res;
}
Aggregations