Search in sources :

Example 96 with ResourceType

use of org.hl7.fhir.dstu3.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);
    }
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) Database(au.csiro.pathling.io.Database) ReferencePath(au.csiro.pathling.fhirpath.element.ReferencePath) ResourceType(org.hl7.fhir.r4.model.Enumerations.ResourceType) Nonnull(javax.annotation.Nonnull)

Example 97 with ResourceType

use of org.hl7.fhir.dstu3.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);
}
Also used : Column(org.apache.spark.sql.Column) Dataset(org.apache.spark.sql.Dataset) ArrayList(java.util.ArrayList) ReferencePath(au.csiro.pathling.fhirpath.element.ReferencePath) ResourceType(org.hl7.fhir.r4.model.Enumerations.ResourceType) Row(org.apache.spark.sql.Row) Nonnull(javax.annotation.Nonnull)

Example 98 with ResourceType

use of org.hl7.fhir.dstu3.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;
}
Also used : Parameters(org.hl7.fhir.r4.model.Parameters) CodeType(org.hl7.fhir.r4.model.CodeType) ParametersParameterComponent(org.hl7.fhir.r4.model.Parameters.ParametersParameterComponent) Nonnull(javax.annotation.Nonnull)

Example 99 with ResourceType

use of org.hl7.fhir.dstu3.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());
    }
}
Also used : InvalidUserInputError(au.csiro.pathling.errors.InvalidUserInputError) ResourceType(org.hl7.fhir.r4.model.Enumerations.ResourceType) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Example 100 with ResourceType

use of org.hl7.fhir.dstu3.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;
}
Also used : Parameters(org.hl7.fhir.r4.model.Parameters) CodeType(org.hl7.fhir.r4.model.CodeType) UrlType(org.hl7.fhir.r4.model.UrlType) ParametersParameterComponent(org.hl7.fhir.r4.model.Parameters.ParametersParameterComponent) Nonnull(javax.annotation.Nonnull)

Aggregations

JsonElement (com.google.gson.JsonElement)33 HashSet (java.util.HashSet)26 Bundle (org.hl7.fhir.r4.model.Bundle)24 ResourceType (org.hl7.fhir.r4.model.Enumerations.ResourceType)21 Test (org.junit.Test)20 Nonnull (javax.annotation.Nonnull)18 ArrayList (java.util.ArrayList)15 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)10 FhirContext (ca.uhn.fhir.context.FhirContext)9 File (java.io.File)9 Row (org.apache.spark.sql.Row)9 IdType (org.hl7.fhir.dstu3.model.IdType)9 BundleEntryComponent (org.hl7.fhir.r4.model.Bundle.BundleEntryComponent)9 JsonObject (com.google.gson.JsonObject)8 Test (org.junit.jupiter.api.Test)8 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)8 IOException (java.io.IOException)7 List (java.util.List)7 Bundle (org.hl7.fhir.dstu3.model.Bundle)7 Resource (org.hl7.fhir.r4.model.Resource)7