use of au.csiro.pathling.encoders.UnsupportedResourceError 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;
}
Aggregations