Skip to content
Open
Show file tree
Hide file tree
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 @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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 {

Expand Down
5 changes: 5 additions & 0 deletions readme/ampathforms.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).