Search in sources :

Example 21 with ParametersParameterComponent

use of org.hl7.fhir.dstu2016may.model.Parameters.ParametersParameterComponent 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 22 with ParametersParameterComponent

use of org.hl7.fhir.dstu2016may.model.Parameters.ParametersParameterComponent in project pathling by aehrc.

the class ExtractResponse method toParameters.

/**
 * Converts this to a {@link Parameters} resource, based on the definition of the result of the
 * "extract" operation within the OperationDefinition.
 *
 * @return a new {@link Parameters} object
 */
public Parameters toParameters() {
    final Parameters parameters = new Parameters();
    final ParametersParameterComponent urlParameter = new ParametersParameterComponent();
    urlParameter.setName("url");
    urlParameter.setValue(new UrlType(url));
    parameters.getParameter().add(urlParameter);
    return parameters;
}
Also used : Parameters(org.hl7.fhir.r4.model.Parameters) UrlType(org.hl7.fhir.r4.model.UrlType) ParametersParameterComponent(org.hl7.fhir.r4.model.Parameters.ParametersParameterComponent)

Example 23 with ParametersParameterComponent

use of org.hl7.fhir.dstu2016may.model.Parameters.ParametersParameterComponent in project pathling by aehrc.

the class AggregateResponse method toParameters.

/**
 * Converts this to a {@link Parameters} resource, based on the definition of the result of the
 * "aggregate" operation within the OperationDefinition.
 *
 * @return a new {@link Parameters} object
 */
public Parameters toParameters() {
    final Parameters parameters = new Parameters();
    groupings.forEach(grouping -> {
        final ParametersParameterComponent groupingParameter = new ParametersParameterComponent();
        groupingParameter.setName("grouping");
        grouping.getLabels().forEach(label -> {
            final ParametersParameterComponent labelPart = new ParametersParameterComponent();
            labelPart.setName("label");
            // A "null" value is represented by the absence of a value within FHIR.
            label.ifPresent(labelPart::setValue);
            groupingParameter.getPart().add(labelPart);
        });
        grouping.getResults().forEach(result -> {
            final ParametersParameterComponent resultPart = new ParametersParameterComponent();
            resultPart.setName("result");
            // A "null" value is represented by the absence of a value within FHIR.
            result.ifPresent(resultPart::setValue);
            groupingParameter.getPart().add(resultPart);
        });
        if (grouping.getDrillDown().isPresent()) {
            final String drillDown = grouping.getDrillDown().get();
            final ParametersParameterComponent drillDownPart = new ParametersParameterComponent();
            drillDownPart.setName("drillDown");
            drillDownPart.setValue(new StringType(drillDown));
            groupingParameter.getPart().add(drillDownPart);
        }
        parameters.getParameter().add(groupingParameter);
    });
    return parameters;
}
Also used : Parameters(org.hl7.fhir.r4.model.Parameters) StringType(org.hl7.fhir.r4.model.StringType) ParametersParameterComponent(org.hl7.fhir.r4.model.Parameters.ParametersParameterComponent)

Example 24 with ParametersParameterComponent

use of org.hl7.fhir.dstu2016may.model.Parameters.ParametersParameterComponent in project pathling by aehrc.

the class TestDataImporter method run.

@Override
public void run(final String... args) {
    final String sourcePath = args[0];
    final File srcNdJsonDir = new File(sourcePath);
    final FileFilter fileFilter = new WildcardFileFilter("*.ndjson");
    final File[] srcNdJsonFiles = srcNdJsonDir.listFiles(fileFilter);
    final List<ParametersParameterComponent> sources = Stream.of(Objects.requireNonNull(srcNdJsonFiles)).map(file -> {
        final String resourceName = FilenameUtils.getBaseName(file.getName());
        final ResourceType subjectResource = ResourceType.valueOf(resourceName.toUpperCase());
        final ParametersParameterComponent source = new ParametersParameterComponent();
        source.setName("source");
        final ParametersParameterComponent resourceType = new ParametersParameterComponent();
        resourceType.setName("resourceType");
        resourceType.setValue(new CodeType(subjectResource.toCode()));
        source.addPart(resourceType);
        final ParametersParameterComponent url = new ParametersParameterComponent();
        url.setName("url");
        url.setValue(new UrlType("file://" + file.toPath()));
        source.addPart(url);
        return source;
    }).collect(Collectors.toList());
    final Parameters parameters = new Parameters();
    parameters.setParameter(sources);
    importExecutor.execute(parameters);
}
Also used : ParametersParameterComponent(org.hl7.fhir.r4.model.Parameters.ParametersParameterComponent) UrlType(org.hl7.fhir.r4.model.UrlType) SpringBootApplication(org.springframework.boot.autoconfigure.SpringBootApplication) Autowired(org.springframework.beans.factory.annotation.Autowired) ResourceType(org.hl7.fhir.r4.model.Enumerations.ResourceType) Collectors(java.util.stream.Collectors) Profile(org.springframework.context.annotation.Profile) File(java.io.File) ComponentScan(org.springframework.context.annotation.ComponentScan) ImportExecutor(au.csiro.pathling.update.ImportExecutor) Objects(java.util.Objects) SpringApplication(org.springframework.boot.SpringApplication) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) FileFilter(java.io.FileFilter) Stream(java.util.stream.Stream) WildcardFileFilter(jodd.io.filter.WildcardFileFilter) Parameters(org.hl7.fhir.r4.model.Parameters) CodeType(org.hl7.fhir.r4.model.CodeType) CommandLineRunner(org.springframework.boot.CommandLineRunner) Nonnull(javax.annotation.Nonnull) FilenameUtils(org.apache.commons.io.FilenameUtils) SparkSession(org.apache.spark.sql.SparkSession) Parameters(org.hl7.fhir.r4.model.Parameters) CodeType(org.hl7.fhir.r4.model.CodeType) ResourceType(org.hl7.fhir.r4.model.Enumerations.ResourceType) FileFilter(java.io.FileFilter) WildcardFileFilter(jodd.io.filter.WildcardFileFilter) File(java.io.File) WildcardFileFilter(jodd.io.filter.WildcardFileFilter) UrlType(org.hl7.fhir.r4.model.UrlType) ParametersParameterComponent(org.hl7.fhir.r4.model.Parameters.ParametersParameterComponent)

Example 25 with ParametersParameterComponent

use of org.hl7.fhir.dstu2016may.model.Parameters.ParametersParameterComponent in project MtbImporter by nr23730.

the class FhirResolver method resolveOncoTree.

/**
 * Converts an ICD-O-3 classification into an OncoTree coding.
 * @param topography ICD-O-3 topography code (first part)
 * @param morphology ICD-O-3 morpholoy code (second part)
 * @return FHIR coding for the corresponding OncoTree-Code.
 */
public static Coding resolveOncoTree(String topography, String morphology) {
    Parameters params = new Parameters();
    params.addParameter("system", new UriType(Settings.getFhir().getTerminology().getIcdO3Url()));
    params.addParameter("target", new UriType(Settings.getFhir().getTerminology().getOncoTreeUrl()));
    params.addParameter("conceptMap", new UriType(Settings.getFhir().getTerminology().getIcdO3ToOncoTreeConceptMapUrl()));
    params.addParameter("code", topography.split(" ")[0] + " " + morphology.split(" ")[0]);
    try {
        Parameters result = terminologyClient.operation().onInstance("ConceptMap/" + Settings.getFhir().getTerminology().getIcdO3ToOncoTreeConceptMapId()).named("translate").withParameters(params).execute();
        for (ParametersParameterComponent p : result.getParameter()) {
            if (!p.getName().equals("match")) {
                continue;
            }
            Coding coding = null;
            String str = null;
            for (ParametersParameterComponent c : p.getPart()) {
                if (c.getValue() instanceof Coding) {
                    coding = (Coding) c.getValue();
                }
                if (c.getValue() instanceof StringType && c.getName().equals("source")) {
                    str = ((StringType) c.getValue()).getValue();
                }
            }
            if (str != null && str.equals(Settings.getFhir().getTerminology().getIcdO3ToOncoTreeConceptMapUrl())) {
                return coding;
            }
        }
    } catch (FhirClientConnectionException e) {
        Logger.error("Could not connect to FHIR Terminology Server", e);
    }
    return new Coding();
}
Also used : Parameters(org.hl7.fhir.r4.model.Parameters) Coding(org.hl7.fhir.r4.model.Coding) StringType(org.hl7.fhir.r4.model.StringType) FhirClientConnectionException(ca.uhn.fhir.rest.client.exceptions.FhirClientConnectionException) UriType(org.hl7.fhir.r4.model.UriType) ParametersParameterComponent(org.hl7.fhir.r4.model.Parameters.ParametersParameterComponent)

Aggregations

ParametersParameterComponent (org.hl7.fhir.r4.model.Parameters.ParametersParameterComponent)26 Parameters (org.hl7.fhir.r4.model.Parameters)21 HashMap (java.util.HashMap)18 JsonObject (javax.json.JsonObject)15 GET (javax.ws.rs.GET)15 Path (javax.ws.rs.Path)15 Produces (javax.ws.rs.Produces)15 ExtraParameters (org.apache.camel.component.fhir.api.ExtraParameters)15 IBaseBundle (org.hl7.fhir.instance.model.api.IBaseBundle)15 FHIRException (org.hl7.fhir.exceptions.FHIRException)12 IOException (java.io.IOException)7 CodeType (org.hl7.fhir.r4.model.CodeType)7 URISyntaxException (java.net.URISyntaxException)6 Nonnull (javax.annotation.Nonnull)6 Parameters (org.hl7.fhir.dstu3.model.Parameters)6 ParametersParameterComponent (org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent)6 Bundle (org.hl7.fhir.r4.model.Bundle)6 Slf4j (lombok.extern.slf4j.Slf4j)5 Parameters (org.hl7.fhir.dstu2.model.Parameters)5 Parameters (org.hl7.fhir.dstu2016may.model.Parameters)5