Skip to content

Commit f9fdc57

Browse files
(fix): Move form name translation to preload (#302)
1 parent 01dcc06 commit f9fdc57

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ See the [documentation on Initializer's logging properties](readme/rtprops.md#lo
218218
#### Version 2.10.0
219219
* Support enhanced methods for loading htmlforms when running htmlformentry 5.5.0+
220220
* Support loading drug ingredients within the drug domain, for compatible OpenMRS versions
221+
* Moved the form name translation loader to the preload function to support loading form names on instance restarts
221222

222223
#### Version 2.9.0
223224
* Fix for InitializerSerializer to ensure compatibility with OpenMRS version 2.7.0+

api/src/main/java/org/openmrs/module/initializer/api/loaders/AmpathFormsTranslationsLoader.java

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,37 @@ protected String getFileExtension() {
4141
return "json";
4242
}
4343

44+
@Override
45+
protected void preload(File file) throws Exception {
46+
String jsonTranslationsString = FileUtils.readFileToString(file, StandardCharsets.UTF_8.toString());
47+
Map<String, Object> jsonTranslationsDefinition = new ObjectMapper().readValue(jsonTranslationsString, Map.class);
48+
49+
Form form = null;
50+
51+
String formName = (String) jsonTranslationsDefinition.get("form");
52+
if (formName == null) {
53+
throw new IllegalArgumentException("'form' property is required for AMPATH forms translations loader.");
54+
}
55+
form = formService.getForm(formName);
56+
if (form == null) {
57+
throw new IllegalArgumentException(
58+
"Could not find a form named '" + formName + "'. Please ensure an existing form is configured.");
59+
}
60+
61+
String language = (String) jsonTranslationsDefinition.get("language");
62+
if (StringUtils.isBlank(language)) {
63+
throw new IllegalArgumentException("'language' property is required for AMPATH forms translations loader.");
64+
}
65+
66+
String formNameTranslation = (String) jsonTranslationsDefinition.get("form_name_translation");
67+
if (!StringUtils.isBlank(formNameTranslation)) {
68+
msgSource.addPresentation(new PresentationMessage("ui.i18n.Form.name." + form.getUuid(),
69+
LocaleUtils.toLocale(language), formNameTranslation, null));
70+
msgSource.addPresentation(new PresentationMessage("org.openmrs.Form." + form.getUuid(),
71+
LocaleUtils.toLocale(language), formNameTranslation, null));
72+
}
73+
}
74+
4475
@Override
4576
protected void load(File file) throws Exception {
4677
String jsonTranslationsString = FileUtils.readFileToString(file, StandardCharsets.UTF_8.toString());
@@ -84,13 +115,5 @@ protected void load(File file) throws Exception {
84115

85116
formResource.setValue(jsonTranslationsString);
86117
formService.saveFormResource(formResource);
87-
88-
String formNameTranslation = (String) jsonTranslationsDefinition.get("form_name_translation");
89-
if (!StringUtils.isBlank(formNameTranslation)) {
90-
msgSource.addPresentation(new PresentationMessage("ui.i18n.Form.name." + form.getUuid(),
91-
LocaleUtils.toLocale(language), formNameTranslation, null));
92-
msgSource.addPresentation(new PresentationMessage("org.openmrs.Form." + form.getUuid(),
93-
LocaleUtils.toLocale(language), formNameTranslation, null));
94-
}
95118
}
96119
}

api/src/test/java/org/openmrs/module/initializer/api/form/AmpathFormsTranslationsLoaderIntegrationTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ public void load_shouldThrowGivenMissingLanguageFieldInFormTranslationsDef() thr
169169
FileUtils.copyFile(srcFile, dstFile);
170170

171171
// Replay
172+
ampathFormsLoader.load();
172173
ampathFormsTranslationsLoader.loadUnsafe(Collections.emptyList(), true);
173174

174175
}

0 commit comments

Comments
 (0)