Search in sources :

Example 41 with ResourceType

use of org.hl7.fhir.dstu3.model.ResourceType in project pathling by aehrc.

the class ManifestConverterTest method convertsManifest.

@Test
void convertsManifest() {
    database = new Database(configuration, spark, fhirEncoders, executor);
    final PassportScope passportScope = new PassportScope();
    final VisaManifest manifest = new VisaManifest();
    manifest.setPatientIds(Arrays.asList(PATIENT_ID_1, PATIENT_ID_2, PATIENT_ID_3, PATIENT_ID_4));
    final ManifestConverter manifestConverter = new ManifestConverter(configuration, fhirContext);
    manifestConverter.populateScope(passportScope, manifest);
    // Convert the scope to JSON and compare it to a test fixture.
    final Gson gson = new GsonBuilder().create();
    final String json = gson.toJson(passportScope);
    assertJson("responses/ManifestConverterTest/convertsManifest.json", json);
    // our test patients.
    for (final ResourceType resourceType : passportScope.keySet()) {
        if (AVAILABLE_RESOURCE_TYPES.contains(resourceType)) {
            boolean found = false;
            for (final String filter : passportScope.get(resourceType)) {
                final Dataset<Row> dataset = assertThatResultOf(resourceType, filter).isElementPath(BooleanPath.class).selectResult().apply(result -> result.filter(result.columns()[1])).getDataset();
                if (dataset.count() > 0) {
                    found = true;
                }
            }
            assertTrue(found, "No results found for " + resourceType.toCode());
        }
    }
}
Also used : Arrays(java.util.Arrays) Dataset(org.apache.spark.sql.Dataset) DynamicPropertySource(org.springframework.test.context.DynamicPropertySource) Autowired(org.springframework.beans.factory.annotation.Autowired) DynamicPropertyRegistry(org.springframework.test.context.DynamicPropertyRegistry) ResourceType(org.hl7.fhir.r4.model.Enumerations.ResourceType) BooleanPath(au.csiro.pathling.fhirpath.element.BooleanPath) GsonBuilder(com.google.gson.GsonBuilder) FhirContext(ca.uhn.fhir.context.FhirContext) FhirEncoders(au.csiro.pathling.encoders.FhirEncoders) Gson(com.google.gson.Gson) Assertions.assertJson(au.csiro.pathling.test.assertions.Assertions.assertJson) Nonnull(javax.annotation.Nonnull) MockBean(org.springframework.boot.test.mock.mockito.MockBean) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) Configuration(au.csiro.pathling.Configuration) TestPropertySource(org.springframework.test.context.TestPropertySource) Row(org.apache.spark.sql.Row) File(java.io.File) Test(org.junit.jupiter.api.Test) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) AbstractParserTest(au.csiro.pathling.fhirpath.parser.AbstractParserTest) Database(au.csiro.pathling.io.Database) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) GsonBuilder(com.google.gson.GsonBuilder) BooleanPath(au.csiro.pathling.fhirpath.element.BooleanPath) Gson(com.google.gson.Gson) ResourceType(org.hl7.fhir.r4.model.Enumerations.ResourceType) Database(au.csiro.pathling.io.Database) Row(org.apache.spark.sql.Row) Test(org.junit.jupiter.api.Test) AbstractParserTest(au.csiro.pathling.fhirpath.parser.AbstractParserTest)

Example 42 with ResourceType

use of org.hl7.fhir.dstu3.model.ResourceType in project pathling by aehrc.

the class AbstractParserTest method mockResource.

void mockResource(final ResourceType... resourceTypes) {
    for (final ResourceType resourceType : resourceTypes) {
        final Dataset<Row> dataset = TestHelpers.getDatasetForResourceType(spark, resourceType);
        when(database.read(resourceType)).thenReturn(dataset);
    }
}
Also used : ResourceType(org.hl7.fhir.r4.model.Enumerations.ResourceType) Row(org.apache.spark.sql.Row)

Example 43 with ResourceType

use of org.hl7.fhir.dstu3.model.ResourceType in project pathling by aehrc.

the class ImportExecutor method execute.

/**
 * Executes an import request.
 *
 * @param inParams a FHIR {@link Parameters} object describing the import request
 * @return a FHIR {@link OperationOutcome} resource describing the result
 */
@Nonnull
public OperationOutcome execute(@Nonnull @ResourceParam final Parameters inParams) {
    // Parse and validate the JSON request.
    final List<ParametersParameterComponent> sourceParams = inParams.getParameter().stream().filter(param -> "source".equals(param.getName())).collect(Collectors.toList());
    if (sourceParams.isEmpty()) {
        throw new InvalidUserInputError("Must provide at least one source parameter");
    }
    log.info("Received $import request");
    // the corresponding table in the warehouse.
    for (final ParametersParameterComponent sourceParam : sourceParams) {
        final ParametersParameterComponent resourceTypeParam = sourceParam.getPart().stream().filter(param -> "resourceType".equals(param.getName())).findFirst().orElseThrow(() -> new InvalidUserInputError("Must provide resourceType for each source"));
        final ParametersParameterComponent urlParam = sourceParam.getPart().stream().filter(param -> "url".equals(param.getName())).findFirst().orElseThrow(() -> new InvalidUserInputError("Must provide url for each source"));
        // The mode parameter defaults to 'overwrite'.
        final ImportMode importMode = sourceParam.getPart().stream().filter(param -> "mode".equals(param.getName()) && param.getValue() instanceof CodeType).findFirst().map(param -> ImportMode.fromCode(((CodeType) param.getValue()).asStringValue())).orElse(ImportMode.OVERWRITE);
        final String resourceCode = ((CodeType) resourceTypeParam.getValue()).getCode();
        final ResourceType resourceType = ResourceType.fromCode(resourceCode);
        // Get an encoder based on the declared resource type within the source parameter.
        final ExpressionEncoder<IBaseResource> fhirEncoder;
        try {
            fhirEncoder = fhirEncoders.of(resourceType.toCode());
        } catch (final UnsupportedResourceError e) {
            throw new InvalidUserInputError("Unsupported resource type: " + resourceCode);
        }
        // Read the resources from the source URL into a dataset of strings.
        final Dataset<String> jsonStrings = readStringsFromUrl(urlParam);
        // Parse each line into a HAPI FHIR object, then encode to a Spark dataset.
        final Dataset<IBaseResource> resources = jsonStrings.map(jsonToResourceConverter(), fhirEncoder);
        log.info("Importing {} resources (mode: {})", resourceType.toCode(), importMode.getCode());
        if (importMode == ImportMode.OVERWRITE) {
            database.overwrite(resourceType, resources.toDF());
        } else {
            database.merge(resourceType, resources.toDF());
        }
    }
    // We return 200, as this operation is currently synchronous.
    log.info("Import complete");
    // Construct a response.
    final OperationOutcome opOutcome = new OperationOutcome();
    final OperationOutcomeIssueComponent issue = new OperationOutcomeIssueComponent();
    issue.setSeverity(IssueSeverity.INFORMATION);
    issue.setCode(IssueType.INFORMATIONAL);
    issue.setDiagnostics("Data import completed successfully");
    opOutcome.getIssue().add(issue);
    return opOutcome;
}
Also used : UrlType(org.hl7.fhir.r4.model.UrlType) URLDecoder(java.net.URLDecoder) Getter(lombok.Getter) Dataset(org.apache.spark.sql.Dataset) ResourceType(org.hl7.fhir.r4.model.Enumerations.ResourceType) UnsupportedResourceError(au.csiro.pathling.encoders.UnsupportedResourceError) FhirEncoders(au.csiro.pathling.encoders.FhirEncoders) AccessRules(au.csiro.pathling.io.AccessRules) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) InvalidUserInputError(au.csiro.pathling.errors.InvalidUserInputError) SecurityError(au.csiro.pathling.errors.SecurityError) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) MapFunction(org.apache.spark.api.java.function.MapFunction) SparkSession(org.apache.spark.sql.SparkSession) ParametersParameterComponent(org.hl7.fhir.r4.model.Parameters.ParametersParameterComponent) IssueSeverity(org.hl7.fhir.r4.model.OperationOutcome.IssueSeverity) PersistenceScheme(au.csiro.pathling.io.PersistenceScheme) Collectors(java.util.stream.Collectors) Profile(org.springframework.context.annotation.Profile) FhirContextFactory(au.csiro.pathling.fhir.FhirContextFactory) StandardCharsets(java.nio.charset.StandardCharsets) OperationOutcomeIssueComponent(org.hl7.fhir.r4.model.OperationOutcome.OperationOutcomeIssueComponent) ExpressionEncoder(org.apache.spark.sql.catalyst.encoders.ExpressionEncoder) OperationOutcome(org.hl7.fhir.r4.model.OperationOutcome) ResourceParam(ca.uhn.fhir.rest.annotation.ResourceParam) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) Database(au.csiro.pathling.io.Database) IssueType(org.hl7.fhir.r4.model.OperationOutcome.IssueType) Optional(java.util.Optional) Parameters(org.hl7.fhir.r4.model.Parameters) CodeType(org.hl7.fhir.r4.model.CodeType) Preconditions.checkUserInput(au.csiro.pathling.utilities.Preconditions.checkUserInput) FilterFunction(org.apache.spark.api.java.function.FilterFunction) InvalidUserInputError(au.csiro.pathling.errors.InvalidUserInputError) OperationOutcomeIssueComponent(org.hl7.fhir.r4.model.OperationOutcome.OperationOutcomeIssueComponent) UnsupportedResourceError(au.csiro.pathling.encoders.UnsupportedResourceError) ResourceType(org.hl7.fhir.r4.model.Enumerations.ResourceType) OperationOutcome(org.hl7.fhir.r4.model.OperationOutcome) CodeType(org.hl7.fhir.r4.model.CodeType) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) ParametersParameterComponent(org.hl7.fhir.r4.model.Parameters.ParametersParameterComponent) Nonnull(javax.annotation.Nonnull)

Example 44 with ResourceType

use of org.hl7.fhir.dstu3.model.ResourceType in project pathling by aehrc.

the class UpdateProvider method update.

/**
 * Implements the update operation.
 *
 * @param id the id of the resource to be updated
 * @param resource the resource to be updated
 * @return a {@link MethodOutcome} describing the result of the operation
 * @see <a href="https://hl7.org/fhir/R4/http.html#update">update</a>
 */
@Update
@OperationAccess("update")
@SuppressWarnings("UnusedReturnValue")
public MethodOutcome update(@Nullable @IdParam final IdType id, @Nullable @ResourceParam final IBaseResource resource) {
    checkUserInput(id != null && !id.isEmpty(), "ID must be supplied");
    checkUserInput(resource != null, "Resource must be supplied");
    final String resourceId = id.getIdPart();
    final IBaseResource preparedResource = prepareResourceForUpdate(resource, resourceId);
    database.merge(resourceType, preparedResource);
    final MethodOutcome outcome = new MethodOutcome();
    outcome.setId(resource.getIdElement());
    outcome.setResource(preparedResource);
    return outcome;
}
Also used : IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) MethodOutcome(ca.uhn.fhir.rest.api.MethodOutcome) OperationAccess(au.csiro.pathling.security.OperationAccess) Database.prepareResourceForUpdate(au.csiro.pathling.io.Database.prepareResourceForUpdate) Update(ca.uhn.fhir.rest.annotation.Update)

Example 45 with ResourceType

use of org.hl7.fhir.dstu3.model.ResourceType in project pathling by aehrc.

the class BatchProvider method batch.

@Transaction
@OperationAccess("batch")
public Bundle batch(@Nullable @TransactionParam final Bundle bundle) {
    checkUserInput(bundle != null, "Bundle must be provided");
    final Map<ResourceType, List<IBaseResource>> resourcesForUpdate = new EnumMap<>(ResourceType.class);
    final Bundle response = new Bundle();
    response.setType(BATCHRESPONSE);
    // the responses should these operations be successful.
    for (final Bundle.BundleEntryComponent entry : bundle.getEntry()) {
        processEntry(resourcesForUpdate, response, entry);
    }
    if (!resourcesForUpdate.isEmpty()) {
        // Merge in any updated resources into their respective tables.
        update(resourcesForUpdate);
    }
    return response;
}
Also used : Bundle(org.hl7.fhir.r4.model.Bundle) BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) ResourceType(org.hl7.fhir.r4.model.Enumerations.ResourceType) ArrayList(java.util.ArrayList) List(java.util.List) EnumMap(java.util.EnumMap) OperationAccess(au.csiro.pathling.security.OperationAccess) Transaction(ca.uhn.fhir.rest.annotation.Transaction)

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