diff --git a/efsity/README.md b/efsity/README.md index 4395ee06..2ec13eea 100644 --- a/efsity/README.md +++ b/efsity/README.md @@ -98,6 +98,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. diff --git a/efsity/src/main/java/org/smartregister/command/PublishFhirResourcesCommand.java b/efsity/src/main/java/org/smartregister/command/PublishFhirResourcesCommand.java index e5267a5a..f904d40a 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; @@ -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(); @@ -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); @@ -86,12 +95,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); } @@ -107,7 +124,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)){ @@ -118,7 +135,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()); } diff --git a/efsity/src/main/java/org/smartregister/command/ValidateFhirResourcesCommand.java b/efsity/src/main/java/org/smartregister/command/ValidateFhirResourcesCommand.java index 9a7b1e9c..4d6820ad 100644 --- a/efsity/src/main/java/org/smartregister/command/ValidateFhirResourcesCommand.java +++ b/efsity/src/main/java/org/smartregister/command/ValidateFhirResourcesCommand.java @@ -42,7 +42,7 @@ public class ValidateFhirResourcesCommand implements Runnable { names = {"-s", "--schema"}, description = "configs schema" ) - private String configSchema; + private static String configSchema; @Override public void run() { @@ -55,7 +55,7 @@ public void run() { } } - private void validateFhirResources(String inputFilePath) throws IOException, ValidationException, GenerationException { + public static void validateFhirResources(String inputFilePath) throws IOException, ValidationException, GenerationException { long start = System.currentTimeMillis(); @@ -119,7 +119,7 @@ private void validateFhirResources(String inputFilePath) throws IOException, Val } } - private int validateResource(FhirValidator validator, IBaseResource resource) { + private static int validateResource(FhirValidator validator, IBaseResource resource) { ValidationResult result = validator.validateWithResult(resource); @@ -146,12 +146,12 @@ private int validateResource(FhirValidator validator, IBaseResource resource) { } } - boolean isConfigFile(FctFile inputFile){ + static boolean isConfigFile(FctFile inputFile){ JSONObject resource = new JSONObject(inputFile.getContent()); return resource.has("configType"); } - int validateConfig(FctFile configFile) throws GenerationException, ValidationException { + static int validateConfig(FctFile configFile) throws GenerationException, ValidationException { SchemaStore schemaStore = new SchemaStore(); Schema schema = schemaStore.loadSchema(new File(String.valueOf(Paths.get(configSchema))));