Skip to content

Commit

Permalink
Merge branch 'main' into update-error-msg
Browse files Browse the repository at this point in the history
  • Loading branch information
ageryck authored Aug 7, 2024
2 parents 451a734 + 1755391 commit acf757f
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 115 deletions.
3 changes: 2 additions & 1 deletion efsity/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repositories {

group = "org.smartregister"

version = "2.3.7-SNAPSHOT"
version = "2.3.8-SNAPSHOT"

description = "fhircore-tooling (efsity)"

Expand Down Expand Up @@ -82,6 +82,7 @@ dependencies {
implementation(deps.jsonschemafriend)
implementation(deps.picocli)
implementation(deps.xstream)
implementation(deps.icu4j)

testImplementation(kotlin("test"))
testImplementation("junit:junit:4.13.2")
Expand Down
2 changes: 2 additions & 0 deletions efsity/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ opencds-cql-version="2.4.0"
project-build-sourceEncoding="UTF-8"
spotless-version ="6.20.0"
xstream="1.4.20"
icu4j-version = "75.1"

[libraries]
caffeine = { module = "com.github.ben-manes.caffeine:caffeine", version.ref = "caffeine-version" }
Expand Down Expand Up @@ -44,6 +45,7 @@ jackson-core = { module = "com.fasterxml.jackson.core:jackson-core", version.ref
jackson-databind = { module = "com.fasterxml.jackson.core:jackson-databind", version.ref = "jackson-version" }
picocli = { module = "info.picocli:picocli", version.ref = "info-picocli-version" }
xstream = { module = "com.thoughtworks.xstream:xstream", version.ref = "xstream" }
icu4j = { module="com.ibm.icu:icu4j", version.ref = "icu4j-version" }

[bundles]
cqf-cql = ["cql-to-elm","elm","elm-jackson","model","model-jackson"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ public void run() {
long start = System.currentTimeMillis();
if (propertiesFile != null && !propertiesFile.isBlank()) {

Properties properties = FctUtils.readPropertiesFile(propertiesFile);
Properties properties = null;
try {
properties = FctUtils.readPropertiesFile(propertiesFile);
} catch (IOException e) {
throw new RuntimeException(e);
}
setProperties(properties);
}
try {
Expand All @@ -121,18 +126,21 @@ public void run() {
}

void setProperties(Properties properties) {
if (properties == null)
throw new IllegalStateException("Properties file is missing or could not be parsed");

if (projectFolder == null || projectFolder.isBlank()) {
if (properties.getProperty("projectFolder") != null) {
projectFolder = properties.getProperty("projectFolder");
} else {
throw new NullPointerException("The projectFolder is missing");
throw new IllegalStateException("The projectFolder is missing");
}
}
if (fhirBaseUrl == null || fhirBaseUrl.isBlank()) {
if (properties.getProperty("fhirBaseUrl") != null) {
fhirBaseUrl = properties.getProperty("fhirBaseUrl");
} else {
throw new NullPointerException("The fhirBaseUrl is missing");
throw new IllegalStateException("The fhirBaseUrl is missing");
}
}
if (accessToken == null || accessToken.isBlank()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.annotations.VisibleForTesting;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.smartregister.util.FCTConstants;
import org.smartregister.util.FctUtils;
import picocli.CommandLine;
Expand Down Expand Up @@ -69,7 +70,7 @@ public void run() {
}
if (extractionType != null && !Arrays.asList(extractionTypes).contains(extractionType)) {
throw new RuntimeException(
"extractionTypes should either be `all`, `configs`, `fhirContent`");
"extractionTypes should either be " + StringUtils.join(extractionTypes, ", "));
}

if (Objects.equals(mode, "extract")) {
Expand All @@ -80,16 +81,11 @@ public void run() {
FctUtils.printInfo(String.format("Input file \u001b[35m%s\u001b[0m", resourceFile));

try {
Path translationsDirectoryPath = inputFilePath.getParent().resolve("translations");

if (Objects.equals(extractionType, "configs")) {
tempsConfig = Files.createTempDirectory("configs");
} else tempsConfig = null;
if (!Files.exists(translationsDirectoryPath))
Files.createDirectories(translationsDirectoryPath);
if (translationFile == null) {
translationFile = translationsDirectoryPath + "/strings_default.properties";
}

// Check if the input path is a directory or a JSON file
if (Files.isDirectory(inputFilePath)) {
if (Objects.equals(extractionType, "configs") || inputFilePath.endsWith("configs")) {
Expand Down Expand Up @@ -443,7 +439,7 @@ private static void replaceTargetFieldsWithHashedValues(
JsonNode node,
Set<String> targetFields,
Map<String, String> textToHash,
Path filePath,
Path tempFilePath,
Path tempConfigsDir)
throws NoSuchAlgorithmException, IOException {

Expand All @@ -468,7 +464,7 @@ private static void replaceTargetFieldsWithHashedValues(
if (fieldValue.isObject() || fieldValue.isArray()) {
// Recursively update nested objects or arrays
replaceTargetFieldsWithHashedValues(
fieldValue, targetFields, textToHash, filePath, tempConfigsDir);
fieldValue, targetFields, textToHash, tempFilePath, tempConfigsDir);
}
}
} else if (node.isArray()) {
Expand All @@ -478,14 +474,11 @@ private static void replaceTargetFieldsWithHashedValues(
if (arrayElement.isObject() || arrayElement.isArray()) {
// Recursively update nested objects or arrays
replaceTargetFieldsWithHashedValues(
arrayElement, targetFields, textToHash, filePath, tempConfigsDir);
arrayElement, targetFields, textToHash, tempFilePath, tempConfigsDir);
}
}
}

String configFileSubDirectory = filePath.subpath(2, filePath.getNameCount()).toString();

Path tempFilePath = tempConfigsDir.resolve(configFileSubDirectory);
// Write the updated JSON to temp file
try (BufferedWriter writer = Files.newBufferedWriter(tempFilePath, StandardCharsets.UTF_8)) {
ObjectMapper objectMapper = new ObjectMapper();
Expand Down Expand Up @@ -551,62 +544,13 @@ private static void writePropertiesFile(Properties properties, String filePath)
}
}

public void copyDirectoryContent(Path sourceDir, Path destinationDir) {
try {
Files.walkFileTree(
sourceDir,
new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
throws IOException {
Path targetDir = destinationDir.resolve(sourceDir.relativize(dir));
if (!Files.exists(targetDir)) {
Files.createDirectory(targetDir);
}
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {

Files.copy(
file,
destinationDir.resolve(sourceDir.relativize(file)),
StandardCopyOption.REPLACE_EXISTING);
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
@VisibleForTesting
protected void copyDirectoryContent(Path sourceDir, Path destinationDir) {
FctUtils.copyDirectoryContent(sourceDir, destinationDir);
}

public void deleteDirectoryRecursively(Path dirPath) {

try {
// Delete the directory and its contents recursively
Files.walkFileTree(
dirPath,
new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc)
throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});

System.out.println("Directory and its contents deleted successfully: " + dirPath);
} catch (IOException e) {
throw new RuntimeException(e);
}
@VisibleForTesting
protected void deleteDirectoryRecursively(Path dirPath) {
FctUtils.deleteDirectoryRecursively(dirPath);
}
}
90 changes: 75 additions & 15 deletions efsity/src/main/java/org/smartregister/util/FctUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.parser.IParser;
import com.ibm.icu.text.CharsetDetector;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.ByteArrayInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -108,16 +106,19 @@ public static void writeJsonFile(String outputPath, String fhirResourceAsString)
}
}

public static Properties readPropertiesFile(String filePath) {
Properties properties = new Properties();
try (InputStream input = new FileInputStream(filePath)) {
public static Properties readPropertiesFile(String propertiesFilePath) throws IOException {

properties.load(input);
return properties;
CharsetDetector detector = new CharsetDetector();
byte[] fileBytes = Files.readAllBytes(Path.of(propertiesFilePath));
detector.setText(fileBytes);

} catch (IOException ex) {
ex.printStackTrace();
Properties properties = new Properties();
try (InputStreamReader reader =
new InputStreamReader(
new ByteArrayInputStream(fileBytes), Charset.forName(detector.detect().getName()))) {
properties.load(reader);
}

return properties;
}

Expand All @@ -138,7 +139,7 @@ public static Map<String, Map<String, String>> indexConfigurationFiles(
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (!Files.isDirectory(file)
&& (fileExtensions.length == 1 && fileExtensions[0] == "*"
&& (fileExtensions.length == 1 && "*".equals(fileExtensions[0])
|| Arrays.asList(fileExtensions)
.contains(FilenameUtils.getExtension(file.getFileName().toString())))) {

Expand Down Expand Up @@ -171,6 +172,65 @@ public static <T extends IBaseResource> T getFhirResource(Class<T> t, String con
return iParser.parseResource(t, contentAsString);
}

public static void copyDirectoryContent(Path sourceDir, Path destinationDir) {
try {
Files.walkFileTree(
sourceDir,
new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
throws IOException {
Path targetDir = destinationDir.resolve(sourceDir.relativize(dir));
if (!Files.exists(targetDir)) {
Files.createDirectory(targetDir);
}
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {

Files.copy(
file,
destinationDir.resolve(sourceDir.relativize(file)),
StandardCopyOption.REPLACE_EXISTING);
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public static void deleteDirectoryRecursively(Path dirPath) {

try {
// Delete the directory and its contents recursively
Files.walkFileTree(
dirPath,
new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc)
throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});

System.out.println("Directory and its contents deleted successfully: " + dirPath);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public static final class Constants {
public static final String HL7_FHIR_PACKAGE = "hl7.fhir.r4.core";
public static final String HL7_FHIR_PACKAGE_VERSION = "4.0.1";
Expand Down
Loading

0 comments on commit acf757f

Please sign in to comment.