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;
}
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;
}
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);
}
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;
}
}
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;
}
Aggregations