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

Merged
merged 29 commits into from
Jan 16, 2025
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
826f31a
init multifactor authentication setup
hilpitome Apr 26, 2024
a0e37aa
Revert "init multifactor authentication setup"
hilpitome Apr 26, 2024
77748e0
Merge branch 'main' of github.com:onaio/fhir-tooling into main
hilpitome Jul 3, 2024
8fbca8b
Merge branch 'main' of github.com:onaio/fhir-tooling into main
hilpitome Jul 16, 2024
aa69893
Merge branch 'main' of github.com:onaio/fhir-tooling into main
hilpitome Jul 17, 2024
c6f3a0a
fix regression bugs introduced in PR 214
hilpitome Jul 18, 2024
a444d02
Merge branch 'main' into fix-regression-bugs-in-pr-214
hilpitome Jul 18, 2024
9e2a609
Merge branch 'main' into fix-regression-bugs-in-pr-214
hilpitome Jul 19, 2024
df8e4a5
fix -all command not combining translations into one file
hilpitome Jul 22, 2024
b8294ee
Merge branch 'fix-regression-bugs-in-pr-214' of github.com:onaio/fhir…
hilpitome Jul 22, 2024
b9cc0f0
Merge branch 'main' into fix-regression-bugs-in-pr-214
hilpitome Jul 23, 2024
2d83906
fix failing tests
hilpitome Jul 23, 2024
ee000e8
Merge branch 'fix-regression-bugs-in-pr-214' of github.com:onaio/fhir…
hilpitome Jul 23, 2024
6c05a9f
Merge branch 'main' of github.com:onaio/fhir-tooling into main
hilpitome Jul 24, 2024
0cb63f8
Merge branch 'main' of github.com:onaio/fhir-tooling into main
hilpitome Aug 22, 2024
f9dcfcf
Merge branch 'main' of github.com:onaio/fhir-tooling into main
hilpitome Aug 26, 2024
b2955f4
Merge branch 'main' of github.com:onaio/fhir-tooling into main
hilpitome Nov 12, 2024
5c35e30
Merge branch 'main' of github.com:onaio/fhir-tooling into main
hilpitome Nov 27, 2024
11e8344
Merge branch 'main' of github.com:onaio/fhir-tooling into main
hilpitome Dec 5, 2024
061923e
Merge branch 'main' of github.com:onaio/fhir-tooling into main
hilpitome Jan 14, 2025
6e534e8
fix merge conflict
hilpitome Jan 15, 2025
38d6d89
fix merge conflict
hilpitome Jan 15, 2025
777f33b
Merge branch 'main' into fix-regression-bugs-in-pr-214
hilpitome Jan 15, 2025
fc3632c
add stringutils import
hilpitome Jan 15, 2025
665a44f
Merge branch 'fix-regression-bugs-in-pr-214' of github.com:onaio/fhir…
hilpitome Jan 15, 2025
4f3ec31
add validation to getting translations folder
hilpitome Jan 15, 2025
0ceacca
run spotlessApply
hilpitome Jan 15, 2025
0ad88d6
Merge branch 'main' into fix-regression-bugs-in-pr-214
hilpitome Jan 15, 2025
6badc28
Merge branch 'main' into fix-regression-bugs-in-pr-214
pld Jan 16, 2025
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 @@ -17,6 +17,7 @@
import java.util.Map;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.smartregister.util.FCTConstants;
import org.smartregister.util.FctUtils;
import picocli.CommandLine;
Expand Down Expand Up @@ -77,18 +78,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 {

if (Objects.equals(extractionType, "configs")) {
tempsConfig = Files.createTempDirectory("configs");
} else tempsConfig = null;
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";
Set<String> targetFields = FCTConstants.configTranslatables;
copyDirectoryContent(inputFilePath, tempsConfig);
extractContent(translationFile, inputFilePath, targetFields, extractionType);
Expand All @@ -108,6 +117,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 @@ -201,6 +211,42 @@ public void run() {
}
}

/*
assuming the expected folder structure is always as follows
root/
├── configs/
│ ├── translation/
│ ├── file1.json
│ └── file2.json
├── fhir_content/
│ ├── translation/
│ ├── example1.json
│ └── example2.json
├── other_folder/
│ ├── nested/
│ │ |
│ │ ├── file3.json
│ │ └── file4.txt
└── translation/
├── another_file.json

*/
@NotNull private static Path getTranslationDirectoryPath(@NotNull Path inputFilePath) {
Objects.requireNonNull(inputFilePath, "Input file path cannot be null");

if (inputFilePath.endsWith("configs") || inputFilePath.endsWith("fhir_content")) {
return inputFilePath.getParent().resolve("translation");
}
if (inputFilePath.toString().endsWith(".json")) {
Path parent = inputFilePath.getParent();
if (parent == null || parent.getParent() == null) {
throw new IllegalArgumentException("Invalid file path structure for: " + inputFilePath);
}
return parent.getParent().resolve("translation");
}
return inputFilePath.resolve("translation");
}

private static void mergeContent(
Path inputFilePath, String translationFile, String locale, Set<String> targetFields)
throws IOException, NoSuchAlgorithmException {
Expand Down Expand Up @@ -374,14 +420,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")) {
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 @@ -398,7 +448,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 @@ -416,13 +467,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