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 24, 2024
1 parent 3613058 commit f906fb6
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 @@ -109,6 +109,7 @@ $ fct publish -e /path/to/env.properties
-au or --accessToken-url : The endpoint for the authentication server
-g or --grant-type : The authorization code grant type
-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 @@ -74,6 +76,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 @@ -89,7 +98,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 @@ -147,12 +156,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 Down Expand Up @@ -183,7 +200,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 @@ -194,7 +211,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 f906fb6

Please sign in to comment.