Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regression bugs in pr 214 #228

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import org.jetbrains.annotations.NotNull;
import org.smartregister.util.FCTConstants;
import org.smartregister.util.FctUtils;
import picocli.CommandLine;
Expand Down Expand Up @@ -76,23 +77,26 @@ public void run() {
long start = System.currentTimeMillis();

Path inputFilePath = Paths.get(resourceFile);
FctUtils.printInfo("Starting extraction");
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";
if (Objects.equals(translationFile, null)) {
Path translationsDirectoryPath = getTranslationDirectoryPath(inputFilePath);
String defaultTranslationFile = translationsDirectoryPath + "/strings_default.properties";
if (!Files.exists(translationsDirectoryPath)) {
Files.createDirectories(translationsDirectoryPath);
Files.createFile(Paths.get(defaultTranslationFile));
}
translationFile = defaultTranslationFile;
}
tempsConfig = Files.createTempDirectory("configs");

// Check if the input path is a directory or a JSON file
if (Files.isDirectory(inputFilePath)) {
if (Objects.equals(extractionType, "configs") || inputFilePath.endsWith("configs")) {
if ("configs".equals(extractionType) || inputFilePath.endsWith("configs")) {
// handle case where extractionType has not been given and inputFilePath ends with
// configs
if (Objects.equals(extractionType, null)) extractionType = "configs";
hilpitome marked this conversation as resolved.
Show resolved Hide resolved
Set<String> targetFields = FCTConstants.configTranslatables;
copyDirectoryContent(inputFilePath, tempsConfig);
extractContent(translationFile, inputFilePath, targetFields, extractionType);
Expand All @@ -112,6 +116,7 @@ public void run() {

if (Files.exists(configsPath) && Files.isDirectory(configsPath)) {
extractionType = "configs";
copyDirectoryContent(configsPath, tempsConfig);
Set<String> targetFields = FCTConstants.configTranslatables;
extractContent(translationFile, configsPath, targetFields, extractionType);
} else {
Expand Down Expand Up @@ -205,6 +210,18 @@ public void run() {
}
}

@NotNull private static Path getTranslationDirectoryPath(Path inputFilePath) {
Path translationsDirectoryPath;
if (inputFilePath.endsWith("configs")
|| inputFilePath.endsWith("fhir_content")
|| inputFilePath.toString().endsWith(".json")) {
if (inputFilePath.toString().endsWith(".json")) {
translationsDirectoryPath = inputFilePath.getParent().getParent().resolve("translation");
} else translationsDirectoryPath = inputFilePath.getParent().resolve("translation");
} else translationsDirectoryPath = inputFilePath.resolve("translation");
return translationsDirectoryPath;
}

private static void mergeContent(
Path inputFilePath, String translationFile, String locale, Set<String> targetFields)
throws IOException, NoSuchAlgorithmException {
Expand Down Expand Up @@ -378,14 +395,18 @@ private void extractContent(
} else if (Files.isDirectory(inputFilePath)) {
// Handle the case where inputFilePath is a directory (folders may contain multiple JSON
// files)
Path inputDir;
if (extractionType.equals("configs")) {
hilpitome marked this conversation as resolved.
Show resolved Hide resolved
inputDir = tempsConfig;
} else inputDir = inputFilePath;

Files.walk(tempsConfig)
Files.walk(inputDir)
.filter(Files::isRegularFile)
.filter(file -> file.toString().endsWith(".json"))
.forEach(
file -> {
try {
if (Objects.equals(extractionType, "configs")) {
if ("configs".equals(extractionType)) {
// Extract and replace target fields with hashed values
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode =
Expand All @@ -402,7 +423,8 @@ private void extractContent(
processJsonFile(file, textToHash, targetFields);
}
} catch (IOException | NoSuchAlgorithmException e) {
deleteDirectoryRecursively(tempsConfig);
if (tempsConfig != null && Files.exists(tempsConfig))
deleteDirectoryRecursively(tempsConfig);
throw new RuntimeException(
"Error while reading file " + file.getFileName() + " " + e);
}
Expand All @@ -420,13 +442,14 @@ private void extractContent(
}
}

if (!Files.exists(propertiesFilePath)) Files.createFile(propertiesFilePath);
Properties existingProperties = FctUtils.readPropertiesFile(propertiesFilePath.toString());

// Merge existing properties with new properties
existingProperties.putAll(textToHash);
writePropertiesFile(existingProperties, translationFile);
FctUtils.printInfo(String.format("Translation file\u001b[36m %s \u001b[0m", translationFile));
if (tempsConfig != null) deleteDirectoryRecursively(tempsConfig);
if (tempsConfig != null && Files.exists(tempsConfig)) deleteDirectoryRecursively(tempsConfig);
}

private static void processJsonFile(
Expand Down
Loading