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

Publish binary resources #194

Merged
merged 3 commits into from
Jun 3, 2024
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
5 changes: 5 additions & 0 deletions efsity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ $ fct publish -e /path/to/env.properties
```
-i or --input : Path to the project folder with the resources to be published
-bu or --fhir-base-url : The base url of the FHIR server to post resources to
-c or --composition-file : The path to the composition file
-at or --access-token : Access token to grant access to the FHIR server
-ci or --client-id : The client identifier for authentication
-cs or --client-secret :The client secret for authentication
Expand All @@ -117,6 +118,10 @@ take precedence over anything in the properties file.
You can either pass the actual accessToken as a variable or pass in the client credentials which will be used
to get an accessToken from the accessToken url provided

You must pass the path to your composition file if you want to publish any binary resources.
The binary resources listed in the composition files are the ones that will be published.
For the publishing of binary resources to work correctly, ensure that you are using the correct/recommended file/folder structure and that the file names in the composition file are in camel case.

### Validating your app configurations
The tool supports some validations for the FHIRCore app configurations. To validate you can run the command:
```console
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.TimeZone;
import java.util.UUID;
import java.util.*;
import net.jimblackler.jsonschemafriend.GenerationException;
import net.jimblackler.jsonschemafriend.ValidationException;
import org.apache.http.HttpResponse;
Expand Down Expand Up @@ -98,6 +94,12 @@ public class PublishFhirResourcesCommand implements Runnable {
required = false)
String validateResource = "true";

@CommandLine.Option(
names = {"-c", "--composition"},
description = "path of the composition configuration file",
required = false)
String compositionFilePath;

@Override
public void run() {
long start = System.currentTimeMillis();
Expand All @@ -111,7 +113,11 @@ public void run() {
}
}
try {
publishResources();
if (compositionFilePath != null) {
ArrayList<JSONObject> resourceObjects = buildBinaries(compositionFilePath, projectFolder);
buildBundle(resourceObjects);
}
buildResources();
stateManagement();
} catch (IOException | ValidationException | GenerationException e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -171,32 +177,119 @@ void setProperties(Properties properties) {
}
}

void publishResources() throws IOException, ValidationException, GenerationException {
ArrayList<String> resourceFiles = getResourceFiles(projectFolder);
/**
* This function takes in the name of a binary component/resource, converts it from camel case to
* separated by underscores. It then appends a string depending on the name, to return the actual
* file name of the binary file. For example the binaryName 'ancRegister' will be converted to
* 'anc_register_config.json'
*
* @param binaryName This is the name of a binary component/resource as it appears in the
* composition resource. Usually in camel case and matches the start of the actual file name
* @return filename This is the actual file name of the binary resource in the project folder
*/
String getFileName(String binaryName) {
String filename;
if ((binaryName.endsWith("Register")) || (binaryName.endsWith("Profile"))) {
String regex = "([a-z])([A-Z]+)";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assuming here that we are always converting from camel case

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add that to a docstring for this function?

String replacer = "$1_$2";
binaryName = binaryName.replaceAll(regex, replacer).toLowerCase();
}
if (binaryName.startsWith("strings")) {
filename = binaryName + "_config.properties";
} else {
filename = binaryName + "_config.json";
}
return filename;
}

HashMap<String, String> getDetails(JSONObject jsonObject) {
JSONObject focus = jsonObject.getJSONObject("focus");
String reference = focus.getString("reference");
JSONObject identifier = focus.getJSONObject("identifier");
String name = identifier.getString("value");

HashMap<String, String> map = new HashMap<>();
map.put("reference", reference);
map.put("name", getFileName(name));
return map;
}

/**
* This function takes in a binary file name and project folder, it then opens the filename in the
* folder ( assuming the recommended folder structure ), reads the content and returns a base64
* encoded version of the content
*
* @param fileName This is the name of the json binary file
* @param projectFolder This is the folder with all the config files
* @return base64 encoded version of the content in the binary json file
* @throws IOException
*/
String getBinaryContent(String fileName, String projectFolder) throws IOException {
String pathToFile;
if (fileName.contains("register")) {
pathToFile = projectFolder + "/registers/" + fileName;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assuming we're always using this agreed on file/folder structure

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea seems reasonable, on that note, should we to the validation checker (and later the app content from template creator) something to verify the repo follows file structure?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, created an issue here #195

} else if (fileName.contains("profile")) {
pathToFile = projectFolder + "/profiles/" + fileName;
} else if (fileName.startsWith("strings_")) {
pathToFile = projectFolder + "/translations/" + fileName;
} else {
pathToFile = projectFolder + "/" + fileName;
}

String fileContent = FctUtils.readFile(pathToFile).getContent();
return Base64.getEncoder().encodeToString(fileContent.getBytes(StandardCharsets.UTF_8));
}

ArrayList<JSONObject> buildBinaries(String compositionFilePath, String projectFolder)
throws IOException {
FctFile compositionFile = FctUtils.readFile(compositionFilePath);
JSONObject compositionResource = new JSONObject(compositionFile.getContent());
List<Map<String, String>> mapList = new ArrayList<>();
Map<String, String> detailsMap = new HashMap<>();
ArrayList<JSONObject> resourceObjects = new ArrayList<>();
boolean validateResourceBoolean = Boolean.parseBoolean(validateResource);

for (String f : resourceFiles) {
if (validateResourceBoolean) {
FctUtils.printInfo(String.format("Validating file \u001b[35m%s\u001b[0m", f));
ValidateFhirResourcesCommand.validateFhirResources(f);
} else {
FctUtils.printInfo(String.format("Publishing \u001b[35m%s\u001b[0m Without Validation", f));
if (compositionResource.has("section")) {
JSONArray compositionObjects = compositionResource.getJSONArray("section");

for (Object obj : compositionObjects) {
JSONObject jsonObject = new JSONObject(obj.toString());
if (jsonObject.has("section")) {
JSONArray section = jsonObject.getJSONArray("section");
for (Object subObj : section) {
JSONObject jo = new JSONObject(subObj.toString());
detailsMap = getDetails(jo);
}
} else {
detailsMap = getDetails(jsonObject);
}
mapList.add(detailsMap);
}

FctFile inputFile = FctUtils.readFile(f);
JSONObject resourceObject = buildResourceObject(inputFile);
resourceObjects.add(resourceObject);
}
for (Map<String, String> e : mapList) {
String filename = e.get("name");
String binaryContent = getBinaryContent(filename, projectFolder);
String contentType;

// build the bundle
JSONObject bundle = new JSONObject();
bundle.put("resourceType", "Bundle");
bundle.put("type", "transaction");
bundle.put("entry", resourceObjects);
FctUtils.printToConsole("Full Payload to POST: ");
FctUtils.printToConsole(bundle.toString());
if (filename.startsWith("strings_")) {
contentType = "text/plain";
} else {
contentType = "application/json";
}

JSONObject binaryResourceObject = new JSONObject();
binaryResourceObject.put("resourceType", "Binary");
binaryResourceObject.put("id", e.get("reference").substring(7));
binaryResourceObject.put("contentType", contentType);
binaryResourceObject.put("data", binaryContent);

JSONObject finalResourceObject = buildResourceObject(binaryResourceObject.toString());
resourceObjects.add(finalResourceObject);
}
}
return resourceObjects;
}

String getToken() {
if (accessToken == null || accessToken.isBlank()) {
if (clientId == null || clientId.isBlank()) {
throw new IllegalArgumentException(
Expand All @@ -217,7 +310,38 @@ void publishResources() throws IOException, ValidationException, GenerationExcep
accessToken =
getAccessToken(clientId, clientSecret, accessTokenUrl, grantType, username, password);
}
postRequest(bundle.toString(), accessToken);
return accessToken;
}

void buildBundle(ArrayList<JSONObject> resourceObjects) throws IOException {
JSONObject bundle = new JSONObject();
bundle.put("resourceType", "Bundle");
bundle.put("type", "transaction");
bundle.put("entry", resourceObjects);
FctUtils.printToConsole("Full Payload to POST: ");
FctUtils.printToConsole(bundle.toString());

postRequest(bundle.toString(), getToken());
}

void buildResources() throws IOException, ValidationException, GenerationException {
ArrayList<String> resourceFiles = getResourceFiles(projectFolder);
ArrayList<JSONObject> resourceObjects = new ArrayList<>();
boolean validateResourceBoolean = Boolean.parseBoolean(validateResource);

for (String f : resourceFiles) {
if (validateResourceBoolean) {
FctUtils.printInfo(String.format("Validating file \u001b[35m%s\u001b[0m", f));
ValidateFhirResourcesCommand.validateFhirResources(f);
} else {
FctUtils.printInfo(String.format("Publishing \u001b[35m%s\u001b[0m Without Validation", f));
}

FctFile inputFile = FctUtils.readFile(f);
JSONObject resourceObject = buildResourceObject(inputFile.getContent());
resourceObjects.add(resourceObject);
}
buildBundle(resourceObjects);
}

static ArrayList<String> getResourceFiles(String pathToFolder) throws IOException {
Expand Down Expand Up @@ -259,8 +383,8 @@ private static void addFhirResource(String filePath, List<String> filesArray) {
}
}

JSONObject buildResourceObject(FctFile inputFile) {
JSONObject resource = new JSONObject(inputFile.getContent());
JSONObject buildResourceObject(String fileContent) {
JSONObject resource = new JSONObject(fileContent);
String resourceType = null;
String resourceID;
if (resource.has("resourceType")) {
Expand Down
Loading
Loading