Skip to content

Commit

Permalink
Merge pull request #158 from vinodaravind01/main
Browse files Browse the repository at this point in the history
feat: add end point for Bundle creation and dynamic metadata (v0.22.0) #123
  • Loading branch information
raphaelvp authored May 14, 2024
2 parents cae7452 + 76d1752 commit 41880a3
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 16 deletions.
6 changes: 5 additions & 1 deletion src/service/fhir-server-prime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

<!-- <dependency>-->
<!-- <groupId>org.postgresql</groupId>-->
Expand Down Expand Up @@ -147,4 +151,4 @@
</plugins>
</build>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -1099,17 +1099,21 @@ public Bundle getResourceById(@IdParam IdType theId) {
}

public String getVersionFromProperties() {
String version = ""; // Default value if reading from properties file fails
return getProverty("application.version");
}

public String getProverty(String key) {
String propertyVal = ""; // Default value if reading from properties file fails

try (InputStream input = FHIRBundleValidator.class.getClassLoader()
.getResourceAsStream("application.properties")) {
Properties prop = new Properties();
prop.load(input);
version = prop.getProperty("application.version");
propertyVal = prop.getProperty(key);
} catch (Exception e) {
e.printStackTrace();
}
return version;
return propertyVal;
}

public String diagnosticsOperation(String sessionId) {
Expand Down Expand Up @@ -1148,4 +1152,4 @@ public static String prettyPrintJsonUsingDefaultPrettyPrinter(String uglyJsonStr
return prettyJson;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ public String validateBundle(@RequestBody String qeValue,
return fhirService.validateFhirResourceData(qeValue, qeIdentifier);
}

/**
* @param qeValue The JSON input
* @param qeIdentifier This value is from the
* TECH_BD_FHIR_SERVICE_SHINNY_DATALAKE_API_URL
* header
* This header value must be like
* https://40lafnwsw7.execute-api.us-east-1.amazonaws.com/dev?processingAgent=
* @return The JSON response
*/
@PostMapping("/Bundle/")
public String validateBundleAndSave(@RequestBody String qeValue,
@RequestParam("qe") String qeIdentifier,
@RequestHeader(name = "TECH_BD_FHIR_SERVICE_SHINNY_DATALAKE_API_URL", required = false) String apiUrl) {
return fhirService.validateBundleAndSave(qeValue, qeIdentifier, apiUrl);
}

/**
* @param qeValue The JSON input
* @param qeIdentifier This value is from the TECH_BD_FHIR_SERVICE_QE_IDENTIFIER
Expand All @@ -45,4 +61,4 @@ public String validateBundleAdmin(@RequestBody String qeValue,
return fhirService.adminValidate(qeValue, qeIdentifier);
}

}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,52 @@
package org.techbd.service.http;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import org.techbd.hrsn.assurance.FHIRBundleValidator;

import java.net.MalformedURLException;
import java.net.URL;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class FhirService {
private WebClient webClient;

@Autowired
public FhirService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl(FHIRBundleValidator.getInstance().getProverty("shinnyDataLakeApiUri"))
.build();
}

public String getMetadata() {
String version = FHIRBundleValidator.getInstance().getVersionFromProperties();

ZonedDateTime currentTime = ZonedDateTime.now(ZoneId.of("UTC"));
// Format the time in the desired format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm z");
String curTime = currentTime.format(formatter);
String fhirServerUrl = FHIRBundleValidator.getInstance().getProverty("fhirServerUrl");
String operationDefinitionBaseUrl = FHIRBundleValidator.getInstance().getProverty("operationDefinitionBaseUrl");

return "<CapabilityStatement xmlns=\"http://hl7.org/fhir\">\n" + //
" <status value=\"active\"></status>\n" + //
" <date value=\"2024-05-10T10:23:24+00:00\"></date>\n" + //
" <date value=" + curTime + "></date>\n" + //
" <publisher value=\"TechBD\"></publisher>\n" + //
" <kind value=\"instance\"></kind>\n" + //
" <software>\n" + //
" <name value=\"1115-Hub FHIR Server\"></name>\n" + //
" <version value=\"" + version + "\"></version>\n" + //
" </software>\n" + //
" <implementation>\n" + //
" <description value=\"115-Hub FHIR\"></description>\n" + //
" <url value=\"https://synthetic.fhir.api.devl.techbd.org\"></url>\n" + //
" <description value=\"1115-Hub FHIR\"></description>\n" + //
" <url value=" + fhirServerUrl + "></url>\n" + //
" </implementation>\n" + //
" <fhirVersion value=\"4.0.1\"></fhirVersion>\n" + //
" <format value=\"application/fhir+xml\"></format>\n" + //
Expand Down Expand Up @@ -66,7 +89,8 @@ public String getMetadata() {
" </resource>\n" + //
" <operation>\n" + //
" <name value=\"validate\"></name>\n" + //
" <definition value=\"http://10.0.165.83:8080/OperationDefinition/Bundle--validate\"></definition>\n"
" <definition value=" + operationDefinitionBaseUrl
+ "/OperationDefinition/Bundle--validate\"></definition>\n"
+ //
" </operation>\n" + //
" </rest>\n" + //
Expand All @@ -81,7 +105,61 @@ public String validateFhirResourceData(String jsonData, String qeIdentifier) {
return FHIRBundleValidator.getInstance().validateFhirResource(jsonData, qeIdentifier);
}

@Async
public String validateBundleAndSave(String jsonData, String qeIdentifier, String apiUrl) {
// POST
// https://40lafnwsw7.execute-api.us-east-1.amazonaws.com/dev?processingAgent=QE
// Make the POST request asynchronously

// Validate process
FHIRBundleValidator.getInstance().validateFhirResource(jsonData, qeIdentifier);

// Prepare Target API URI
String targetApiUrl = null;
if (apiUrl != null && !apiUrl.isEmpty()) {
targetApiUrl = apiUrl;
this.webClient = WebClient.builder()
.baseUrl(apiUrl) // Set the base URL
.build();
} else {
targetApiUrl = FHIRBundleValidator.getInstance().getProverty("shinnyDataLakeApiUri");
}
String host = null, path = null;
try {
URL url = new URL(targetApiUrl);
host = url.getHost();
path = url.getPath();
} catch (MalformedURLException e) {
e.printStackTrace();
}

// Invoke the API URL
webClient.post()
.uri(path + "=" + qeIdentifier)
.body(BodyInserters.fromValue(jsonData))
.retrieve()
.bodyToMono(String.class)
.subscribe(response -> {
// TODO: Process the response here, and save to db.
}, (Throwable error) -> { // Explicitly specify the type Throwable
if (error instanceof WebClientResponseException) {
// TODO: Process the response here, and save to db.
WebClientResponseException responseException = (WebClientResponseException) error;
if (responseException.getStatusCode() == HttpStatus.FORBIDDEN) {
// Handle 403 Forbidden err
} else if (responseException.getStatusCode() == HttpStatus.UNAUTHORIZED) {
// Handle 403 Forbidden error
} else {
// Handle other types of WebClientResponseException
}
} else {
// Handle other types of exceptions
}
});
return "API invoked";
}

public String adminValidate(String jsonData, String qeIdentifier) {
return FHIRBundleValidator.getInstance().adminValidate(jsonData, qeIdentifier);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ spring.h2.console.enabled=true

deviceId=device123
shinnyDataLakeApiImpGuideProfileUri=https://djq7jdt8kb490.cloudfront.net/1115/StructureDefinition-SHINNYBundleProfile.json
application.version=0.21.2
shinnyDataLakeApiUri=https://40lafnwsw7.execute-api.us-east-1.amazonaws.com/dev?processingAgent=
fhirServerUrl=https://synthetic.fhir.api.devl.techbd.org
operationDefinitionBaseUrl=http://10.0.165.83:8080
application.version=0.22.0

0 comments on commit 41880a3

Please sign in to comment.