From f906fb6ddbc74cb32c2c33e76ff6be19d0d17e5d Mon Sep 17 00:00:00 2001 From: lincmba Date: Thu, 21 Dec 2023 14:08:41 +0300 Subject: [PATCH] Validate FHIR Resources before publishing. fixes https://github.com/onaio/fhir-tooling/issues/87 --- efsity/README.md | 1 + .../command/PublishFhirResourcesCommand.java | 27 +++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/efsity/README.md b/efsity/README.md index 0f437a7e..4a77118a 100644 --- a/efsity/README.md +++ b/efsity/README.md @@ -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. diff --git a/efsity/src/main/java/org/smartregister/command/PublishFhirResourcesCommand.java b/efsity/src/main/java/org/smartregister/command/PublishFhirResourcesCommand.java index af53359f..6b8d81a0 100644 --- a/efsity/src/main/java/org/smartregister/command/PublishFhirResourcesCommand.java +++ b/efsity/src/main/java/org/smartregister/command/PublishFhirResourcesCommand.java @@ -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; @@ -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(); @@ -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); @@ -147,12 +156,20 @@ void setProperties(Properties properties){ } } - void publishResources() throws IOException { + void publishResources() throws IOException, ValidationException, GenerationException { ArrayList resourceFiles = getResourceFiles(projectFolder); ArrayList 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); } @@ -183,7 +200,7 @@ void publishResources() throws IOException { postRequest(bundle.toString(), accessToken); } - ArrayList getResourceFiles(String pathToFolder) throws IOException { + static ArrayList getResourceFiles(String pathToFolder) throws IOException { ArrayList filesArray = new ArrayList<>(); Path projectPath = Paths.get(pathToFolder); if (Files.isDirectory(projectPath)){ @@ -194,7 +211,7 @@ ArrayList getResourceFiles(String pathToFolder) throws IOException { return filesArray; } - void getFiles(ArrayList filesArray, File file){ + static void getFiles(ArrayList filesArray, File file){ if (file.isFile()) { filesArray.add(file.getAbsolutePath()); }