use of org.hl7.fhir.r4.model.ResourceType in project pathling by aehrc.
the class ResolveFunction method invoke.
@Nonnull
@Override
public FhirPath invoke(@Nonnull final NamedFunctionInput input) {
checkUserInput(input.getInput() instanceof ReferencePath, "Input to " + NAME + " function must be a Reference: " + input.getInput().getExpression());
checkNoArguments(NAME, input);
final ReferencePath inputPath = (ReferencePath) input.getInput();
final Database database = input.getContext().getDatabase();
// Get the allowed types for the input reference. This gives us the set of possible resource
// types that this reference could resolve to.
Set<ResourceType> referenceTypes = inputPath.getResourceTypes();
// If the type is Resource, all resource types need to be looked at.
if (referenceTypes.contains(ResourceType.RESOURCE)) {
referenceTypes = FhirServer.supportedResourceTypes();
}
check(referenceTypes.size() > 0);
final boolean isPolymorphic = referenceTypes.size() > 1;
final String expression = expressionFromInput(input, NAME);
if (isPolymorphic) {
return resolvePolymorphicReference(input, database, referenceTypes, expression);
} else {
final FhirContext fhirContext = input.getContext().getFhirContext();
return resolveMonomorphicReference(input, database, fhirContext, referenceTypes, expression);
}
}
use of org.hl7.fhir.r4.model.ResourceType in project pathling by aehrc.
the class ResolveFunction method resolvePolymorphicReference.
@Nonnull
private static FhirPath resolvePolymorphicReference(@Nonnull final NamedFunctionInput input, @Nonnull final Database database, @Nonnull final Iterable<ResourceType> referenceTypes, final String expression) {
final ReferencePath referencePath = (ReferencePath) input.getInput();
// If this is a polymorphic reference, create a dataset for each reference type, and union
// them together to produce the target dataset. The dataset will not contain the resources
// themselves, only a type and identifier for later resolution.
final Collection<Dataset<Row>> typeDatasets = new ArrayList<>();
for (final ResourceType referenceType : referenceTypes) {
if (FhirServer.supportedResourceTypes().contains(referenceType)) {
// We can't include the full content of the resource, as you can't union two datasets with
// different schema. The content of the resource is added later, when ofType is invoked.
final Dataset<Row> typeDatasetWithColumns = database.read(referenceType);
final Column idColumn = typeDatasetWithColumns.col("id");
Dataset<Row> typeDataset = typeDatasetWithColumns.withColumn("type", lit(referenceType.toCode()));
typeDataset = typeDataset.select(idColumn, typeDataset.col("type"));
typeDatasets.add(typeDataset);
}
}
checkUserInput(!typeDatasets.isEmpty(), "No types within reference are available, cannot resolve: " + referencePath.getExpression());
final String idColumnName = randomAlias();
final String targetColumnName = randomAlias();
Dataset<Row> targetDataset = union(typeDatasets);
Column targetId = targetDataset.col(targetDataset.columns()[0]);
Column targetType = targetDataset.col(targetDataset.columns()[1]);
targetDataset = targetDataset.withColumn(idColumnName, targetId).withColumn(targetColumnName, targetType);
targetId = targetDataset.col(idColumnName);
targetType = targetDataset.col(targetColumnName);
targetDataset = targetDataset.select(targetId, targetType);
checkNotNull(targetId);
final Column joinCondition = referencePath.getResourceEquality(targetId, targetType);
final Dataset<Row> dataset = join(referencePath.getDataset(), targetDataset, joinCondition, JoinType.LEFT_OUTER);
final Column inputId = referencePath.getIdColumn();
final Optional<Column> inputEid = referencePath.getEidColumn();
return UntypedResourcePath.build(referencePath, expression, dataset, inputId, inputEid, targetType);
}
use of org.hl7.fhir.r4.model.ResourceType in project pathling by aehrc.
the class ImportTest method buildImportParameters.
@SuppressWarnings("SameParameterValue")
@Nonnull
Parameters buildImportParameters(@Nonnull final URL jsonURL, @Nonnull final ResourceType resourceType, @Nonnull final ImportMode mode) {
final Parameters parameters = buildImportParameters(jsonURL, resourceType);
final ParametersParameterComponent sourceParam = parameters.getParameter().stream().filter(p -> p.getName().equals("source")).findFirst().orElseThrow();
sourceParam.addPart().setName("mode").setValue(new CodeType(mode.getCode()));
return parameters;
}
use of org.hl7.fhir.r4.model.ResourceType in project pathling by aehrc.
the class ImportTest method throwsOnUnsupportedResourceType.
@Test
void throwsOnUnsupportedResourceType() {
final List<ResourceType> resourceTypes = Arrays.asList(ResourceType.PARAMETERS, ResourceType.TASK, ResourceType.STRUCTUREDEFINITION, ResourceType.STRUCTUREMAP, ResourceType.BUNDLE);
for (final ResourceType resourceType : resourceTypes) {
final InvalidUserInputError error = assertThrows(InvalidUserInputError.class, () -> importExecutor.execute(buildImportParameters(new URL("file://some/url"), resourceType)), "Unsupported resource type: " + resourceType.toCode());
assertEquals("Unsupported resource type: " + resourceType.toCode(), error.getMessage());
}
}
use of org.hl7.fhir.r4.model.ResourceType in project pathling by aehrc.
the class ImportTest method buildImportParameters.
@SuppressWarnings("SameParameterValue")
@Nonnull
Parameters buildImportParameters(@Nonnull final URL jsonURL, @Nonnull final ResourceType resourceType) {
final Parameters parameters = new Parameters();
final ParametersParameterComponent sourceParam = parameters.addParameter().setName("source");
sourceParam.addPart().setName("resourceType").setValue(new CodeType(resourceType.toCode()));
sourceParam.addPart().setName("url").setValue(new UrlType(jsonURL.toExternalForm()));
return parameters;
}
Aggregations