Skip to content

Commit

Permalink
Update uscore patient accelerator to externalize source connection
Browse files Browse the repository at this point in the history
  • Loading branch information
ThishaniLucas committed May 29, 2023
1 parent ae0ec07 commit e262efc
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ modules = [
[[package]]
org = "ballerina"
name = "io"
version = "1.4.0"
version = "1.4.1"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.value"}
Expand Down Expand Up @@ -248,6 +248,9 @@ dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"}
]
modules = [
{org = "ballerina", packageName = "os", moduleName = "os"}
]

[[package]]
org = "ballerina"
Expand Down Expand Up @@ -364,6 +367,7 @@ dependencies = [
{org = "ballerina", name = "http"},
{org = "ballerina", name = "lang.value"},
{org = "ballerina", name = "log"},
{org = "ballerina", name = "os"},
{org = "ballerinai", name = "observe"},
{org = "ballerinax", name = "health.fhir.r4"},
{org = "ballerinax", name = "health.fhir.r4.uscore501"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,23 @@ isolated function patientReadImpl(string id, http:RequestContext ctx) returns r4
PatientSourceConnect sourceConnect = profileImpl.get(defaultProfile);
r4:FHIRContext fhirContext = check r4:getFHIRContext(ctx);

Patient patient = check sourceConnect.read(id, fhirContext);
log:printDebug(string `[ReadImpl] Retrieved resource: ${patient.toJsonString()}`);
Patient|r4:FHIRError resourceResult = sourceConnect.read(id, fhirContext);

return new (patient);
r4:FHIRResourceEntity entity = new (check resourceResult);
return entity;
}
}

isolated function patientCreateImpl(r4:FHIRResourceEntity resourceEntity, http:RequestContext ctx) returns string|r4:FHIRError {

lock {
PatientSourceConnect sourceConnect = profileImpl.get(defaultProfile);
r4:FHIRContext fhirContext = check r4:getFHIRContext(ctx);

value:Cloneable resourceRecord = resourceEntity.unwrap();

if resourceRecord is Patient {
log:printDebug(string `[CreateImpl] Request payload: ${resourceRecord.toString()}`);
r4:FHIRContext fhirContext = check r4:getFHIRContext(ctx);
string|r4:FHIRError createResponse = check sourceConnect.create(resourceEntity, fhirContext);
return createResponse;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,19 @@
// This file is auto-generated by Ballerina Team for implementing source system connections.
// Developers are allowed modify this file as per the requirement.

import ballerina/http;
import ballerina/os;
import ballerinax/health.fhir.r4;
import ballerinax/health.fhir.r4.uscore501;

configurable string sourceSystem = os:getEnv("SOURCE_SYSTEM");

final string READ = sourceSystem.endsWith("/") ? "read/" : "/read/";
final string SEARCH = sourceSystem.endsWith("/") ? "search" : "/search";
final string CREATE = sourceSystem.endsWith("/") ? "create" : "/create";

final http:Client sourceEp = check new (sourceSystem);

public isolated class Uscore501PatientSourceConnect {

*PatientSourceConnect;
Expand All @@ -31,63 +41,92 @@ public isolated class Uscore501PatientSourceConnect {

isolated function read(string id, r4:FHIRContext ctx) returns Patient|r4:FHIRError {

//Implement source system connection here and retreive data.
//Create FHIR resource from retreived data.
uscore501:USCorePatientProfile example = {
id: "12d39",
meta: {
versionId: "abc12s3",
profile: ["http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient"]
},
identifier: [
],
implicitRules: "https://www.hl7.org/fhir",
language: "en-US",
gender: "unknown",
name: []};
return example;
http:Response|http:ClientError res = sourceEp->get(READ + id);
if (res is http:ClientError) {
r4:FHIRError fhirError = r4:createFHIRError("Error occured when calling the source system.", r4:CODE_SEVERITY_ERROR,r4:TRANSIENT_EXCEPTION);
return fhirError;
} else {
json|error payload = res.getJsonPayload();
if (payload is error) {
r4:FHIRError fhirError = r4:createFHIRError("Unable to extract JSON payload from the source response.", r4:CODE_SEVERITY_ERROR,r4:TRANSIENT_EXCEPTION);
return fhirError;
} else {
uscore501:USCorePatientProfile|error fhirResource = payload.cloneWithType(uscore501:USCorePatientProfile);

if (fhirResource is error) {
r4:FHIRError fhirError = r4:createFHIRError("Did not get a FHIR Resource from source.", r4:CODE_SEVERITY_ERROR,r4:TRANSIENT_EXCEPTION);
return fhirError;
} else {
return fhirResource;
}
}
}
}

isolated function search(map<r4:RequestSearchParameter[]> searchParameters, r4:FHIRContext ctx) returns r4:Bundle|Patient[]|r4:FHIRError {

uscore501:USCorePatientProfile[] patients = [];

//Implement source system connection here and retreive data.
//Create FHIR resource from retreived data.
uscore501:USCorePatientProfile example = {
id: "12d39",
meta: {
versionId: "abc12s3",
profile: ["http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient"]
},
identifier: [
],
implicitRules: "https://www.hl7.org/fhir",
language: "en-US"
,gender: "unknown", name: []};
uscore501:USCorePatientProfile example1 = {
id: "12c39",
meta: {
versionId: "abc12s3",
profile: ["http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient"]
},
identifier: [
],
implicitRules: "https://www.hl7.org/fhir",
language: "en-US"
,gender: "unknown", name: []};
patients.push(example);
patients.push(example1);
return patients;
//convert search parameters to map<string|string[]>
map<string|string[]> searchParams = {};
foreach var [key, value] in searchParameters.entries() {
foreach var param in value {
searchParams[key] = param.value;
}
}
//convert search parameters to query string
string queryString = "";
foreach var [key, value] in searchParams.entries() {
// check if value is an array
if (value is string[]) {
foreach var v in value {
queryString = queryString + key + "=" + v + "&";
}
} else {
queryString = queryString + key + "=" + <string>value + "&";
}
}
//remove last & if query string is not empty
if (queryString != "") {
queryString = "?" + queryString.substring(0, queryString.length() - 1);
}

http:Response|http:ClientError res = sourceEp->get(SEARCH + queryString);
if (res is http:ClientError) {
r4:FHIRError fhirError = r4:createFHIRError("Error occured when calling the source system.", r4:CODE_SEVERITY_ERROR, r4:TRANSIENT_EXCEPTION);
return fhirError;
} else {
json|error payload = res.getJsonPayload();
if (payload is error) {
r4:FHIRError fhirError = r4:createFHIRError("Unable to extract JSON payload from the source response.", r4:CODE_SEVERITY_ERROR, r4:TRANSIENT_EXCEPTION);
return fhirError;
} else {
if (payload is json[]) {
json[] payloadArray = <json[]>payload;
Patient[] fhirResources = [];
foreach var p in payloadArray {
uscore501:USCorePatientProfile|error fhirResource = p.cloneWithType(uscore501:USCorePatientProfile);
if (fhirResource is error) {
r4:FHIRError fhirError = r4:createFHIRError("Did not get a FHIR Resource from source.", r4:CODE_SEVERITY_ERROR, r4:TRANSIENT_EXCEPTION);
return fhirError;
} else {
fhirResources.push(fhirResource);
}
}
return fhirResources;
} else {
r4:FHIRError fhirError = r4:createFHIRError("Did not get a JSON[] from the source.", r4:CODE_SEVERITY_ERROR, r4:TRANSIENT_EXCEPTION);
return fhirError;
}
}
}
}

isolated function create(r4:FHIRResourceEntity patient, r4:FHIRContext ctx) returns string|r4:FHIRError {

//Implement source system connection here and persist FHIR resource.
//Must respond with ID in order to create Location header

string resourceId = "logicalId"; //returned from the source system
return resourceId;
r4:FHIRError fhirError = r4:createFHIRError("Not implemented", r4:CODE_SEVERITY_ERROR, r4:TRANSIENT_EXCEPTION, httpStatusCode = 415);
return fhirError;
}
}

0 comments on commit e262efc

Please sign in to comment.