From 806aec0a11fbb6b582629ecb2c89992e2e46107e Mon Sep 17 00:00:00 2001 From: Duvet05 Date: Wed, 23 Sep 2026 16:09:34 -0500 Subject: [PATCH] fix: supply retirement reason when creating AMPATH forms --- .../api/loaders/AmpathFormsLoader.java | 7 ++- .../AmpathFormsLoaderIntegrationTest.java | 44 +++++++++++++++++++ readme/ampathforms.md | 5 +++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/org/openmrs/module/initializer/api/loaders/AmpathFormsLoader.java b/api/src/main/java/org/openmrs/module/initializer/api/loaders/AmpathFormsLoader.java index 87e737c9d..223ef6daa 100644 --- a/api/src/main/java/org/openmrs/module/initializer/api/loaders/AmpathFormsLoader.java +++ b/api/src/main/java/org/openmrs/module/initializer/api/loaders/AmpathFormsLoader.java @@ -28,6 +28,8 @@ public class AmpathFormsLoader extends BaseFileLoader { public static final String AMPATH_FORMS_UUID = "794c4598-ab82-47ca-8d18-483a8abe6f4f"; public static final String JSON_EXTENSION = "json"; + + private static final String DEFAULT_RETIRE_REASON = "Retired by Initializer"; @Autowired private DatatypeService datatypeService; @@ -135,7 +137,7 @@ protected void load(File file) throws Exception { if (!OpenmrsUtil.nullSafeEquals(form.getRetired(), formRetired)) { form.setRetired(formRetired); if (formRetired && StringUtils.isBlank(form.getRetireReason())) { - form.setRetireReason("Retired by Initializer"); + form.setRetireReason(DEFAULT_RETIRE_REASON); } needToSaveForm = true; } @@ -170,6 +172,9 @@ private void createNewForm(String uuid, String formName, String formDescription, newForm.setUuid(uuid); newForm.setDescription(formDescription); newForm.setRetired(formRetired); + if (formRetired) { + newForm.setRetireReason(DEFAULT_RETIRE_REASON); + } newForm.setPublished(formPublished); newForm.setEncounterType(encounterType); diff --git a/api/src/test/java/org/openmrs/module/initializer/api/form/AmpathFormsLoaderIntegrationTest.java b/api/src/test/java/org/openmrs/module/initializer/api/form/AmpathFormsLoaderIntegrationTest.java index 04b730401..415b20fbb 100644 --- a/api/src/test/java/org/openmrs/module/initializer/api/form/AmpathFormsLoaderIntegrationTest.java +++ b/api/src/test/java/org/openmrs/module/initializer/api/form/AmpathFormsLoaderIntegrationTest.java @@ -9,6 +9,7 @@ import org.apache.commons.io.FileUtils; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.node.ObjectNode; import org.junit.After; import org.junit.Assert; import org.junit.Test; @@ -19,6 +20,7 @@ import org.openmrs.api.db.ClobDatatypeStorage; import org.openmrs.module.initializer.DomainBaseModuleContextSensitiveTest; import org.openmrs.module.initializer.api.loaders.AmpathFormsLoader; +import org.openmrs.module.initializer.api.utils.Utils; import org.springframework.beans.factory.annotation.Autowired; public class AmpathFormsLoaderIntegrationTest extends DomainBaseModuleContextSensitiveTest { @@ -42,8 +44,50 @@ public void clean() throws IOException { FileUtils .deleteQuietly(new File(ampathFormsLoader.getDirUtil().getDomainDirPath() + "/test_form_clob_changed.json")); FileUtils.deleteQuietly(new File(ampathFormsLoader.getDirUtil().getDomainDirPath() + "/test_form_new_version.json")); + FileUtils.deleteQuietly(new File(ampathFormsLoader.getDirUtil().getDomainDirPath() + "/test_form_retired.json")); } + @Test + public void load_shouldCreateRetiredFormWithRetirementReason() throws Exception { + writeRetiredForm("Retired Test Form", "1"); + ampathFormsLoader.load(); + assertRetiredForm("Retired Test Form", "1"); + } + + @Test + public void load_shouldCreateRetiredVersionAndPreservePreviousSchema() throws Exception { + ampathFormsLoader.load(); + Form original = formService.getForm("Test Form 1"); + String reference = formService.getFormResource(original, "JSON schema").getValueReference(); + String schema = datatypeService.getClobDatatypeStorageByUuid(reference).getValue(); + writeRetiredForm("Test Form 1", "2"); + ampathFormsLoader.load(); + assertRetiredForm("Test Form 1", "2"); + Assert.assertTrue(formService.getFormByUuid(original.getUuid()).getRetired()); + Assert.assertEquals(schema, datatypeService.getClobDatatypeStorageByUuid(reference).getValue()); + } + + private void writeRetiredForm(String name, String version) throws IOException { + ObjectMapper mapper = new ObjectMapper(); + ObjectNode schema = (ObjectNode) mapper.readTree(new File( + "src/test/resources/testdata/testAmpathforms/test_form_new_version.json")); + schema.put("name", name); + schema.put("version", version); + schema.put("retired", true); + schema.put("published", false); + mapper.writeValue(new File(ampathFormsLoader.getDirUtil().getDomainDirPath(), "test_form_retired.json"), schema); + } + + private void assertRetiredForm(String name, String version) { + Form form = formService.getFormByUuid(Utils.generateUuidFromObjects(AmpathFormsLoader.AMPATH_FORMS_UUID, name, + version)); + Assert.assertNotNull(form); + Assert.assertTrue(form.getRetired()); + Assert.assertFalse(form.getPublished()); + Assert.assertEquals("Retired by Initializer", form.getRetireReason()); + Assert.assertNotNull(formService.getFormResource(form, "JSON schema")); + } + @Test public void load_shouldLoadFormWithAllAttributesSpecified() throws Exception { diff --git a/readme/ampathforms.md b/readme/ampathforms.md index 4ef0462f1..4816d3f2b 100644 --- a/readme/ampathforms.md +++ b/readme/ampathforms.md @@ -69,5 +69,10 @@ The JSON source for these forms can be obtained from [AMPATH Form Builder](https **NOTE:** When an Ampath forms file is loaded, a new resource with the existing Ampath forms translations is created +Forms declared with `"retired": true` receive the default retirement reason +`Retired by Initializer`, including when a retired form is first created or a new +retired version replaces an earlier form. OpenMRS requires this reason when saving +retired forms. The earlier version keeps its existing JSON schema resource. + #### Further examples: Please look at the test configuration folder for sample import files for all domains, see [here](../api/src/test/resources/testAppDataDir/configuration).