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 Dec 21, 2023
1 parent 38478f6 commit 530e31f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
1 change: 1 addition & 0 deletions efsity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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();

Expand Down Expand Up @@ -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);

Expand All @@ -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))));

Expand Down

0 comments on commit 530e31f

Please sign in to comment.