Search in sources :

Example 76 with FINAL

use of org.hl7.fhir.r4.model.Observation.ObservationStatus.FINAL 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 77 with FINAL

use of org.hl7.fhir.r4.model.Observation.ObservationStatus.FINAL 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 78 with FINAL

use of org.hl7.fhir.r4.model.Observation.ObservationStatus.FINAL in project pathling by aehrc.

the class AggregateExecutorTest method assertResponse.

void assertResponse(@Nonnull final String expectedPath, @Nonnull final AggregateResponse response) {
    final Parameters parameters = response.toParameters();
    final String actualJson = jsonParser.encodeResourceToString(parameters);
    assertJson("responses/" + expectedPath, actualJson);
}
Also used : Parameters(org.hl7.fhir.r4.model.Parameters)

Example 79 with FINAL

use of org.hl7.fhir.r4.model.Observation.ObservationStatus.FINAL in project pathling by aehrc.

the class BaseMapping method checkResponseEntry.

/**
 * Ensures that the bundle-response entry contains a successful response or otherwise throws the
 * appropriate subclass of {@link BaseServerResponseException}  capturing the error information
 * returned from the terminology server.
 *
 * <p>
 * This is adapted from HAPI: `ca.uhn.fhir.rest.client.impl.BaseClient#invokeClient(...)`.
 *
 * @param entry the bundle entry.
 * @param fhirContext the Fhir context to use.
 * @throws BaseServerResponseException then the entry status code is not 2xx.
 */
private static void checkResponseEntry(@Nonnull final BundleEntryComponent entry, @Nonnull final FhirContext fhirContext) {
    final BundleEntryResponseComponent response = entry.getResponse();
    final int statusCode = Integer.parseInt(response.getStatus());
    if (statusCode < 200 || statusCode > 299) {
        String message = "Error in response entry : HTTP " + response.getStatus();
        final IBaseOperationOutcome oo = (IBaseOperationOutcome) entry.getResource();
        final String details = OperationOutcomeUtil.getFirstIssueDetails(fhirContext, oo);
        if (StringUtils.isNotBlank(details)) {
            message = message + " : " + details;
        }
        final BaseServerResponseException exception = BaseServerResponseException.newInstance(statusCode, message);
        exception.setOperationOutcome(oo);
        throw exception;
    }
}
Also used : IBaseOperationOutcome(org.hl7.fhir.instance.model.api.IBaseOperationOutcome) BaseServerResponseException(ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException) BundleEntryResponseComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryResponseComponent)

Example 80 with FINAL

use of org.hl7.fhir.r4.model.Observation.ObservationStatus.FINAL in project pathling by aehrc.

the class ValueSetMapping method toIntersection.

/**
 * Constructs a {@link ValueSet} representing the intersection of a set of codings and a server
 *
 * @param valueSetUri the server defined value set.
 * @param codings the set of codings to intersect.
 * @return the intersection value set.
 */
@Nonnull
public static ValueSet toIntersection(@Nonnull final String valueSetUri, @Nonnull final Collection<SimpleCoding> codings) {
    final Set<CodeSystemReference> validCodeSystems = codings.stream().filter(SimpleCoding::isDefined).map(coding -> new CodeSystemReference(Optional.ofNullable(coding.getSystem()), Optional.ofNullable(coding.getVersion()))).collect(Collectors.toUnmodifiableSet());
    // Create a ValueSet to represent the intersection of the input codings and the ValueSet
    // described by the URI in the argument.
    final ValueSet intersection = new ValueSet();
    final ValueSetComposeComponent compose = new ValueSetComposeComponent();
    final List<ConceptSetComponent> includes = new ArrayList<>();
    // Create an include section for each unique code system present within the input codings.
    for (final CodeSystemReference codeSystem : validCodeSystems) {
        final ConceptSetComponent include = new ConceptSetComponent();
        include.setValueSet(Collections.singletonList(new CanonicalType(valueSetUri)));
        // noinspection OptionalGetWithoutIsPresent
        include.setSystem(codeSystem.getSystem().get());
        codeSystem.getVersion().ifPresent(include::setVersion);
        // Add the codings that match the current code system.
        final List<ConceptReferenceComponent> concepts = codings.stream().filter(codeSystem::matchesCoding).map(SimpleCoding::getCode).filter(Objects::nonNull).distinct().map(code -> {
            final ConceptReferenceComponent concept = new ConceptReferenceComponent();
            concept.setCode(code);
            return concept;
        }).collect(Collectors.toList());
        if (!concepts.isEmpty()) {
            include.setConcept(concepts);
            includes.add(include);
        }
    }
    compose.setInclude(includes);
    intersection.setCompose(compose);
    return intersection;
}
Also used : ConceptReferenceComponent(org.hl7.fhir.r4.model.ValueSet.ConceptReferenceComponent) java.util(java.util) Slf4j(lombok.extern.slf4j.Slf4j) ConceptSetComponent(org.hl7.fhir.r4.model.ValueSet.ConceptSetComponent) SimpleCoding(au.csiro.pathling.fhirpath.encoding.SimpleCoding) CanonicalType(org.hl7.fhir.r4.model.CanonicalType) ValueSet(org.hl7.fhir.r4.model.ValueSet) Collectors(java.util.stream.Collectors) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Value(lombok.Value) ValueSetComposeComponent(org.hl7.fhir.r4.model.ValueSet.ValueSetComposeComponent) ValueSetComposeComponent(org.hl7.fhir.r4.model.ValueSet.ValueSetComposeComponent) CanonicalType(org.hl7.fhir.r4.model.CanonicalType) ConceptReferenceComponent(org.hl7.fhir.r4.model.ValueSet.ConceptReferenceComponent) ConceptSetComponent(org.hl7.fhir.r4.model.ValueSet.ConceptSetComponent) SimpleCoding(au.csiro.pathling.fhirpath.encoding.SimpleCoding) ValueSet(org.hl7.fhir.r4.model.ValueSet) Nonnull(javax.annotation.Nonnull)

Aggregations

Test (org.junit.jupiter.api.Test)229 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)85 HashMap (java.util.HashMap)83 CamelSpringBootTest (org.apache.camel.test.spring.junit5.CamelSpringBootTest)59 List (java.util.List)53 Bundle (org.hl7.fhir.dstu3.model.Bundle)50 Nonnull (javax.annotation.Nonnull)48 Patient (org.hl7.fhir.dstu3.model.Patient)46 Organization (org.hl7.fhir.dstu3.model.Organization)45 ArrayList (java.util.ArrayList)44 Bundle (org.hl7.fhir.r4.model.Bundle)41 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)39 UUID (java.util.UUID)38 Collectors (java.util.stream.Collectors)38 Coding (org.hl7.fhir.r4.model.Coding)34 FhirContext (ca.uhn.fhir.context.FhirContext)33 IGenericClient (ca.uhn.fhir.rest.client.api.IGenericClient)32 IParser (ca.uhn.fhir.parser.IParser)31 IOException (java.io.IOException)29 IdType (org.hl7.fhir.dstu3.model.IdType)28