Skip to content

Commit

Permalink
Merge pull request #31 from vinodaravind01/main
Browse files Browse the repository at this point in the history
feat: add methods for fetching FHIR profile version
  • Loading branch information
ratheesh-kr authored Jun 14, 2024
2 parents f2da12d + 5098157 commit 5cab470
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 14 deletions.
2 changes: 1 addition & 1 deletion hub-prime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</parent>
<groupId>org.techbd</groupId>
<artifactId>hub-prime</artifactId>
<version>0.65.1</version>
<version>0.65.2</version>
<packaging>war</packaging>
<name>TechBD Hub (Prime)</name>
<description>TechBD Hub (Primary)</description>
Expand Down
10 changes: 0 additions & 10 deletions hub-prime/src/main/java/org/inferno/validator/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public class Validator {
private final Map<String, NpmPackage> loadedPackages;

private static String assignedUrlFrom = null;
private static String vesrionFrom = null;

private static final Logger LOGGER = LoggerFactory.getLogger(Validator.class);

Expand Down Expand Up @@ -118,14 +117,6 @@ public static void setAssignedUrlFrom(String assignedUrlFrom) {
Validator.assignedUrlFrom = assignedUrlFrom;
}

public String getVersionFrom() {
return vesrionFrom;
}

public static void setVersionFrom(String vesrionFrom) {
Validator.vesrionFrom = vesrionFrom;
}

/**
* Lists the names of resources defined for this version of the validator.
*
Expand Down Expand Up @@ -253,7 +244,6 @@ public void loadProfile(byte[] profile) throws IOException {
StructureDefinition sd = (StructureDefinition)resource;
LOGGER.info("Loaded profile from file, url: " + sd.getUrl() + " version: " + sd.getVersion());
setAssignedUrlFrom(sd.getUrl());
setVersionFrom(sd.getVersion());
} else if (resource != null) {
LOGGER.info("Loaded resource from file but it wasn't a StructureDefinition, it was a "
+ resource.fhirType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import org.apache.commons.text.StringEscapeUtils;
import org.hl7.fhir.r5.model.OperationOutcome;
//import org.techbd.orchestrate.fhir.OrchestrationEngine.OrchestrationSession;
import org.techbd.util.JsonText.JsonTextSerializer;

import com.fasterxml.jackson.core.JsonProcessingException;
Expand All @@ -44,6 +43,7 @@
import jakarta.validation.constraints.NotNull;

import org.inferno.validator.Validator;
import org.springframework.cache.annotation.Cacheable;

/**
* The {@code OrchestrationEngine} class is responsible for managing and
Expand Down Expand Up @@ -137,6 +137,36 @@ public void orchestrate(@NotNull final OrchestrationSession... sessions) {
}
}

@Cacheable("fhirProfile")
public static String fetchFhirProfileVersion(String fhirProfileUrl) {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(fhirProfileUrl))
.build();

String fhirProfileVersion = client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenApply(responseBody -> {
final var objectMapper = new ObjectMapper();

try {
// Read JSON response and parse it into a JsonNode
final var rootNode = objectMapper.readTree(responseBody);

// Get the value of the "version" key from FHIR IG profile JSON
JsonNode versionNode = rootNode.path("version");
return versionNode.asText();

} catch (IOException e) {
e.printStackTrace();
return null;
}
})
.join(); // Wait for the async operation to complete

return fhirProfileVersion;
}

public ValidationEngine getValidationEngine(@NotNull final ValidationEngineIdentifier type,
@NotNull final String fhirProfileUrl) {
ValidationEngineKey key = new ValidationEngineKey(type, fhirProfileUrl);
Expand Down Expand Up @@ -198,6 +228,8 @@ public interface ValidationResult {

String getProfileUrl();

String getFhirProfileVersion();

ValidationEngine.Observability getObservability();

boolean isValid();
Expand Down Expand Up @@ -237,9 +269,11 @@ public static class HapiValidationEngine implements OrchestrationEngine.Validati
private final FhirContext fhirContext;
private final FhirValidator validator;
private final ValidationOptions options;
private final String fhirProfileVersion;

private HapiValidationEngine(final Builder builder) {
this.fhirProfileUrl = builder.fhirProfileUrl;
fhirProfileVersion = OrchestrationEngine.fetchFhirProfileVersion(fhirProfileUrl);
this.fhirContext = FhirContext.forR4();
this.options = new ValidationOptions();
if (this.fhirProfileUrl != null) {
Expand Down Expand Up @@ -305,6 +339,11 @@ public String getProfileUrl() {
return HapiValidationEngine.this.fhirProfileUrl;
}

@Override
public String getFhirProfileVersion() {
return HapiValidationEngine.this.fhirProfileVersion;
}

@Override
public ValidationEngine.Observability getObservability() {
return observability;
Expand Down Expand Up @@ -359,6 +398,11 @@ public String getProfileUrl() {
return HapiValidationEngine.this.fhirProfileUrl;
}

@Override
public String getFhirProfileVersion() {
return HapiValidationEngine.this.fhirProfileVersion;
}

@Override
public ValidationEngine.Observability getObservability() {
return observability;
Expand Down Expand Up @@ -402,9 +446,11 @@ public static class Hl7ValidationEngineEmbedded implements ValidationEngine {
private final Instant engineInitAt = Instant.now();
private final Instant engineConstructedAt;
private final String fhirProfileUrl;
private final String fhirProfileVersion;

private Hl7ValidationEngineEmbedded(final Builder builder) {
this.fhirProfileUrl = builder.fhirProfileUrl;
fhirProfileVersion = OrchestrationEngine.fetchFhirProfileVersion(fhirProfileUrl);
engineConstructedAt = Instant.now();
observability = new Observability(Hl7ValidationEngineEmbedded.class.getName(),
"HL7 Official Embedded (TODO: version)", engineInitAt,
Expand Down Expand Up @@ -436,6 +482,11 @@ public String getProfileUrl() {
return Hl7ValidationEngineEmbedded.this.fhirProfileUrl;
}

@Override
public String getFhirProfileVersion() {
return Hl7ValidationEngineEmbedded.this.fhirProfileVersion;
}

@Override
public ValidationEngine.Observability getObservability() {
return observability;
Expand Down Expand Up @@ -481,9 +532,11 @@ public static class Hl7ValidationEngineApi implements ValidationEngine {
private final String locale;
private final String fileType;
private final String fileName;
private final String fhirProfileVersion;

private Hl7ValidationEngineApi(final Builder builder) {
this.fhirProfileUrl = builder.fhirProfileUrl;
fhirProfileVersion = OrchestrationEngine.fetchFhirProfileVersion(fhirProfileUrl);
this.fhirContext = "4.0.1";
this.locale = "en";
this.fileType = "json";
Expand Down Expand Up @@ -606,6 +659,11 @@ public String getProfileUrl() {
return Hl7ValidationEngineApi.this.fhirProfileUrl;
}

@Override
public String getFhirProfileVersion() {
return Hl7ValidationEngineApi.this.fhirProfileVersion;
}

@Override
public ValidationEngine.Observability getObservability() {
return observability;
Expand Down Expand Up @@ -650,9 +708,12 @@ public static class InfernoValidationEngine implements ValidationEngine {
private final String fhirProfileUrl;
private final Validator validator;
private List<String> fhirBundleProfile;

private final String fhirProfileVersion;

private InfernoValidationEngine(final Builder builder) {
this.fhirProfileUrl = builder.fhirProfileUrl;
fhirProfileVersion = OrchestrationEngine.fetchFhirProfileVersion(fhirProfileUrl);
//fhirProfileVersion = OrchestrationEngine.fetchFhirProfileVersion(fhirProfileUrl);
Validator tempValidator;
try {
tempValidator = new Validator("hub-prime/igs", false);
Expand Down Expand Up @@ -685,7 +746,7 @@ public ValidationResult validate(@NotNull final String payload) {

OperationOutcome oo = validator.validate(payloadContent, fhirBundleProfile);
ArrayNode issueArray = displayValidationErrors(oo, false);
ObjectMapper mapper = new ObjectMapper();
final var mapper = new ObjectMapper();
String responseBody = mapper.writeValueAsString(issueArray);

final Instant completedAt = Instant.now();
Expand Down Expand Up @@ -752,6 +813,11 @@ public String getProfileUrl() {
return InfernoValidationEngine.this.fhirProfileUrl;
}

@Override
public String getFhirProfileVersion() {
return InfernoValidationEngine.this.fhirProfileVersion;
}

@Override
public ValidationEngine.Observability getObservability() {
return observability;
Expand Down Expand Up @@ -805,6 +871,11 @@ public String getProfileUrl() {
return InfernoValidationEngine.this.fhirProfileUrl;
}

@Override
public String getFhirProfileVersion() {
return InfernoValidationEngine.this.fhirProfileVersion;
}

@Override
public ValidationEngine.Observability getObservability() {
return observability;
Expand Down

0 comments on commit 5cab470

Please sign in to comment.