Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fhir): Add missing properties to CapabilityStatement #1219

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,45 @@ public Collection<Document> getDocuments() {
return documents;
}

public CapabilityStatement withUrl(Uri resourceUrl, Uri baseUrl) {
return new CapabilityStatement(
getId(),
getMeta(),
getImplicitRules(),
getLanguage(),
getText(),
resourceUrl,
getVersion(),
getName(),
getTitle(),
getStatus(),
getExperimental(),
getDate(),
getPublisher(),
getContacts(),
getDescription(),
getUsageContexts(),
getJurisdictions(),
getPurpose(),
getToolingId(),

getResourceType(),
getIdentifiers(),
getCopyright(),
getKind(),
getInstantiates(),
getImports(),
getSoftware(),
getImplementation().withUrl(baseUrl),
getFhirVersion(),
getFormats(),
getPatchFormats(),
getImplementationGuides(),
getRests(),
getMessagings(),
getDocuments());
}

public static Builder builder(String id) {
return new Builder(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public Reference getCustodian() {
return custodian;
}

public Implementation withUrl(final Uri url) {
return new Implementation(getDescription(), url, getCustodian());
}

public static Builder builder() {
return new Builder();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.junit.Test;

import com.b2international.snowowl.fhir.core.codesystems.CapabilityStatementKind;
import com.b2international.snowowl.fhir.core.model.capabilitystatement.CapabilityStatement;
import com.b2international.snowowl.fhir.core.model.operationdefinition.OperationDefinition;
import com.b2international.snowowl.fhir.tests.FhirRestTest;
Expand All @@ -40,6 +41,15 @@ public void capabilityStatementTest() {
.assertThat()
.statusCode(200)
.body("resourceType", equalTo("CapabilityStatement"))
.body("url", notNullValue())
.body("version", notNullValue())
.body("name", notNullValue())
.body("title", notNullValue())
.body("status", notNullValue())
.body("date", notNullValue())
.body("description", notNullValue())
.body("kind", equalTo(CapabilityStatementKind.INSTANCE.getCodeValue()))
.body("fhirVersion", equalTo("4.0.1"))
.body("rest[0]", notNullValue())
.body("rest[0].resource[0]", notNullValue());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,28 @@
import static com.b2international.snowowl.core.rest.OpenAPIExtensions.B2I_OPENAPI_X_NAME;
import static com.google.common.collect.Maps.newHashMap;

import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.eclipse.core.runtime.Platform;
import org.osgi.framework.Version;
import org.springdoc.webmvc.api.OpenApiWebMvcResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import com.b2international.commons.StringUtils;
import com.b2international.commons.exceptions.NotFoundException;
import com.b2international.snowowl.core.ApplicationContext;
import com.b2international.snowowl.core.CoreActivator;
import com.b2international.snowowl.core.config.SnowOwlConfiguration;
import com.b2international.snowowl.core.date.Dates;
import com.b2international.snowowl.core.rest.FhirApiConfig;
import com.b2international.snowowl.core.rest.SnowOwlOpenApiWebMvcResource;
import com.b2international.snowowl.fhir.core.codesystems.CapabilityStatementKind;
Expand Down Expand Up @@ -87,12 +87,12 @@
@Autowired
private FhirApiConfig config;

private static class Holder {
CapabilityStatement capabilityStatement;
Map<String, OperationDefinition> operationMap;
}
private record StatementAndOperations (
CapabilityStatement capabilityStatement,
Map<String, OperationDefinition> operationMap

Check warning on line 92 in fhir/com.b2international.snowowl.fhir.rest/src/com/b2international/snowowl/fhir/rest/FhirMetadataController.java

View check run for this annotation

Codecov / codecov/patch

fhir/com.b2international.snowowl.fhir.rest/src/com/b2international/snowowl/fhir/rest/FhirMetadataController.java#L92

Added line #L92 was not covered by tests
) { }

private final Supplier<Holder> capabilityStatementSupplier = Suppliers.memoize(this::initCache);
private final Supplier<StatementAndOperations> capabilityStatementSupplier = Suppliers.memoize(this::initCache);

@Operation(
summary="Retrieve the capability statement",
Expand All @@ -108,7 +108,11 @@
})
@GetMapping(value="/metadata")
public CapabilityStatement metadata() {
return capabilityStatementSupplier.get().capabilityStatement;
final String resourceUrl = MvcUriComponentsBuilder.fromMethodName(FhirMetadataController.class, "metadata").toUriString();
final String baseUrl = MvcUriComponentsBuilder.fromController(FhirMetadataController.class).toUriString();

final CapabilityStatement capabilityStatement = capabilityStatementSupplier.get().capabilityStatement();
return capabilityStatement.withUrl(new Uri(resourceUrl), new Uri(baseUrl));
}

/**
Expand Down Expand Up @@ -137,8 +141,7 @@
return operationDefinition;
}

private Holder initCache() {
final Holder holder = new Holder();
private StatementAndOperations initCache() {
// XXX: we know that the subclass is instantiated (in SnowOwlApiConfig)
final OpenAPI openAPI = ((SnowOwlOpenApiWebMvcResource) openApiWebMvcResource).getOpenApi();
final Collection<Resource> resources = collectResources(openAPI);
Expand All @@ -148,12 +151,12 @@
.mode(RestfulCapabilityMode.SERVER)
.resources(resources);

holder.operationMap = newHashMap();
final Map<String, OperationDefinition> operationMap = newHashMap();

for (final OperationDefinition operationDefinition : operationDefinitions) {
for (final Code code : operationDefinition.getResources()) {
final String key = code.getCodeValue() + operationDefinition.getName();
holder.operationMap.put(key, operationDefinition);
operationMap.put(key, operationDefinition);

restBuilder.addOperation(com.b2international.snowowl.fhir.core.model.capabilitystatement.Operation.builder()
.name(operationDefinition.getName())
Expand All @@ -162,34 +165,58 @@
}
}

final String serverVersion = Platform.getBundle(CoreActivator.PLUGIN_ID)
.getVersion()
.toString();
CapabilityStatement capabilityStatement = createCapabilityStatement(restBuilder);

String description = ApplicationContext.getServiceForClass(SnowOwlConfiguration.class).getDescription();
return new StatementAndOperations(capabilityStatement, operationMap);
}

private CapabilityStatement createCapabilityStatement(final Rest.Builder restBuilder) {

holder.capabilityStatement = CapabilityStatement.builder()
.name("FHIR Capability statement for Snow Owl© Terminology Server")
final Version bundleVersion = Platform.getBundle(CoreActivator.PLUGIN_ID).getVersion();
final String softwareVersion = bundleVersion.toString();
final String qualifier = bundleVersion.getQualifier();
final Date date = getDateFromQualifier(qualifier);

final String description = ApplicationContext.getServiceForClass(SnowOwlConfiguration.class)
.getDescription();

return CapabilityStatement.builder()
// .url(...) is added later
.version(softwareVersion)
.name("snow-owl-capability-statement")
.title("FHIR Capability statement for Snow Owl© Terminology Server")
.description("This statement describes FHIR resource types and operations supported by the terminology server.")
.status(PublicationStatus.ACTIVE)
.experimental(false)
.date(date)
.kind(CapabilityStatementKind.INSTANCE.getCode())
.fhirVersion("4.0.1")
.experimental(false)
.addInstantiate(new Uri("http://hl7.org/fhir/CapabilityStatement/terminology-server"))
.software(Software.builder()
.name("Snow Owl©")
.version(serverVersion)
.build())
.version(softwareVersion)
.build())
.implementation(Implementation.builder()
.url("https://b2ihealthcare.com")
.description(description)
.build())
.build())
.addFormat(new Code(AbstractFhirController.APPLICATION_FHIR_JSON))
.addRest(restBuilder.build())
.build();

return holder;
}

private Date getDateFromQualifier(final String qualifier) {
if (StringUtils.isEmpty(qualifier) || "qualifier".equals(qualifier)) {
return Dates.todayGmt();

Check warning on line 210 in fhir/com.b2international.snowowl.fhir.rest/src/com/b2international/snowowl/fhir/rest/FhirMetadataController.java

View check run for this annotation

Codecov / codecov/patch

fhir/com.b2international.snowowl.fhir.rest/src/com/b2international/snowowl/fhir/rest/FhirMetadataController.java#L210

Added line #L210 was not covered by tests
}

try {
return Dates.parse(qualifier, "yyyyMMddHHmm");
} catch (IllegalArgumentException e) {
return Dates.todayGmt();

Check warning on line 216 in fhir/com.b2international.snowowl.fhir.rest/src/com/b2international/snowowl/fhir/rest/FhirMetadataController.java

View check run for this annotation

Codecov / codecov/patch

fhir/com.b2international.snowowl.fhir.rest/src/com/b2international/snowowl/fhir/rest/FhirMetadataController.java#L215-L216

Added lines #L215 - L216 were not covered by tests
}
}

private Collection<Resource> collectResources(final OpenAPI openAPI) {
final Paths paths = openAPI.getPaths();
final List<io.swagger.v3.oas.models.tags.Tag> tags = openAPI.getTags();
Expand Down