Skip to content

Commit

Permalink
Validate FHIR Resources before publishing.
Browse files Browse the repository at this point in the history
 fixes #87
  • Loading branch information
lincmba committed Jan 10, 2024
1 parent 6d5d9e2 commit d6ef6d4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions efsity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ $ fct publish -e /path/to/env.properties
-bu or --fhir-base-url : The base url of the FHIR server to post resources to
-at or --access-token : Access token to grant access to the FHIR server
-e or --env : A properties file that contains the neessary variables
-vr or --validate-resources : (Optional) whether to validate FHIR resources before publishing or not. Optional boolean - default is `true`
```
You can either pass your variables on the CLI or include them in the properties file. Variables passed on CLI
take precedence over anything in the properties file.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.smartregister.command;

import net.jimblackler.jsonschemafriend.GenerationException;
import net.jimblackler.jsonschemafriend.ValidationException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
Expand Down Expand Up @@ -41,6 +43,13 @@ public class PublishFhirResourcesCommand implements Runnable{
description = "path to env.properties file")
String propertiesFile;

@CommandLine.Option(
names = {"-vr", "--validate-resource"},
description =
"-vr or --validate-resource - (Optional) whether to validate FHIR resources before publishing or not. Optional boolean - default is `true`",
required = false)
private String validateResource = "true";

@Override
public void run() {
long start = System.currentTimeMillis();
Expand All @@ -56,7 +65,7 @@ public void run() {
try {
publishResources();
stateManagement();
} catch (IOException e) {
} catch (IOException | ValidationException | GenerationException e) {
throw new RuntimeException(e);
}
FctUtils.printCompletedInDuration(start);
Expand Down Expand Up @@ -86,12 +95,20 @@ void setProperties(Properties properties){
}
}

void publishResources() throws IOException {
void publishResources() 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("Publishing Without Validation");
}

FctFile inputFile = FctUtils.readFile(f);
// TODO check if file contains valid fhir resource
JSONObject resourceObject = buildResourceObject(inputFile);
resourceObjects.add(resourceObject);
}
Expand All @@ -107,7 +124,7 @@ void publishResources() throws IOException {
postRequest(bundle.toString(), accessToken);
}

ArrayList<String> getResourceFiles(String pathToFolder) throws IOException {
static ArrayList<String> getResourceFiles(String pathToFolder) throws IOException {
ArrayList<String> filesArray = new ArrayList<>();
Path projectPath = Paths.get(pathToFolder);
if (Files.isDirectory(projectPath)){
Expand All @@ -118,7 +135,7 @@ ArrayList<String> getResourceFiles(String pathToFolder) throws IOException {
return filesArray;
}

void getFiles(ArrayList<String> filesArray, File file){
static void getFiles(ArrayList<String> filesArray, File file){
if (file.isFile()) {
filesArray.add(file.getAbsolutePath());
}
Expand Down

0 comments on commit d6ef6d4

Please sign in to comment.