Skip to content

Commit

Permalink
Bindu | BAH-3772 | Persist translations for the imported form if the …
Browse files Browse the repository at this point in the history
…reference form is not present in the environment
  • Loading branch information
binduak committed Apr 24, 2024
1 parent b97b8bc commit f29b141
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public List<BahmniForm> formsWithNameTransaltionsFor(String formName, boolean in
String publishedQueryCondition= filterPublishedForms ? "F.published=true" : "";
String retiredQueryCondition= includeRetired? "" : filterPublishedForms ? "and F.retired=false " : "F.retired=false" ;
String formNameQueryCondition= filterByFormName ? !includeRetired || filterPublishedForms ?
"and name= "+ formName : "name= "+ formName : "";
"and F.name="+"'"+ formName +"'" : "F.name="+"'"+ formName + "'" : "";

Query query = currentSession.createQuery( String.format("Select COALESCE(FR.valueReference,'[]') as nameTranslation, " +
"F.name as name, F.formId as id, F.uuid as uuid, F.version as version, F.published as published " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,17 @@ public Form getFormDetailsFromFormName(String formName, String formVersion){
private String getFormResourceValue(BahmniFormResource bahmniFormResource, String referenceFormUuid) {
String value = isEmpty(referenceFormUuid)
? bahmniFormResource.getValue()
: getOldFormResourceValue(referenceFormUuid);
: getOldFormResourceValue(referenceFormUuid, bahmniFormResource);
return value == null || value.trim().equals("") ? "" : value;
}

private String getOldFormResourceValue(String referenceFormUuid) {
private String getOldFormResourceValue(String referenceFormUuid, BahmniFormResource bahmniFormResource) {
List<BahmniForm> bahmniForms = new ArrayList<>();
Form form = formService.getFormByUuid(referenceFormUuid);
if (form == null) {
bahmniForms = bahmniFormDao.formsWithNameTransaltionsFor(bahmniFormResource.getForm().getName(), false, false);
return bahmniForms.get(0).getNameTranslation();
}
FormResource formResource = formService.getFormResource(form, form.getName() + "_FormName_Translation");
return formResource != null ? formResource.getValueReference() : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.apache.commons.io.FileUtils;
import org.bahmni.module.bahmni.ie.apps.validator.BahmniFormUtils;
import org.bahmni.module.bahmni.ie.apps.model.FormNameTranslation;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.json.JSONObject;
Expand Down Expand Up @@ -88,15 +87,17 @@ public List<FormTranslation> saveFormTranslation(List<FormTranslation> formTrans
if (hasReferenceForm && referenceFormIsNotTheCurrentForm) {
File referenceVersionFile = translationFileFor(firstTranslation.getFormName(),
firstTranslation.getReferenceVersion(), firstTranslation.getReferenceFormUuid());

if(referenceVersionFile.exists()) {
JSONObject refVersionTranslationsJson = existingTranslationsFrom(referenceVersionFile);
if (!refVersionTranslationsJson.keySet().isEmpty())
updateTranslationsWithRefVersion(firstTranslation, translationsJson, refVersionTranslationsJson);
} else {
translationsJson = existingTranslationsFrom(translationFile);
}
}
saveTranslationsToFile(translationsJson, translationFile);
}

return formTranslations;
}

Expand Down Expand Up @@ -226,8 +227,9 @@ private String getFileName(String formUuid) {

private File translationFileFor(String formName, String formVersion, String formUuid) {
File file = null;
if (isNotEmpty(formUuid))
if (isNotEmpty(formUuid)) {
file = new File(getFileName(formUuid));
}
if (file == null || !file.exists()) {
file = new File(getFileName(formName, formVersion));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void shouldGenerateQueryForformsWithNameTransaltionsForPublishedFalseAndR
verify(session).createQuery( "Select COALESCE(FR.valueReference,'[]') as nameTranslation, F.name as name, F.formId as id, " +
"F.uuid as uuid, F.version as version, F.published as published from FormResource FR right outer join " +
"FR.form F with FR.datatypeClassname!='org.bahmni.customdatatype.datatype.FileSystemStorageDatatype' " +
"where name= FormName order by F.name asc, F.version desc");
"where F.name='FormName' order by F.name asc, F.version desc");
}

@Test
Expand All @@ -106,6 +106,6 @@ public void shouldGenerateQueryForformsWithNameTransaltionsForPublishedFalseAndR
verify(session).createQuery( "Select COALESCE(FR.valueReference,'[]') as nameTranslation, F.name as name, F.formId as id," +
" F.uuid as uuid, F.version as version, F.published as published from FormResource FR right outer join " +
"FR.form F with FR.datatypeClassname!='org.bahmni.customdatatype.datatype.FileSystemStorageDatatype' " +
"where F.published=true and F.retired=false and name= FormName order by F.name asc, F.version desc");
"where F.published=true and F.retired=false and F.name='FormName' order by F.name asc, F.version desc");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -442,26 +442,35 @@ public void shouldPersistOldVersionLocaleTranslationsToNewVersionWhenOldVersionT
}

@Test
public void shouldReturnTranslationsInNewVersionWhenTheReferenceFormTranslationFileIsNotFound() throws Exception {
public void shouldNotReturnDefaultTranslationsInNewVersionWhenTheReferenceFormTranslationFileIsNotFound() throws Exception {
BahmniFormTranslationService bahmniFormTranslationService = new BahmniFormTranslationServiceImpl();
String tempTranslationsPath = createTempFolder();

String importedFromUuid = "imported-uuid";
String importedFprmTranslationsPath = tempTranslationsPath + "/" + importedFromUuid + "_imported_form.json";
String importedFormJson =
"{\"en\":{\"concepts\":{\"TEMPERATURE_2_DESC\":\"Temperature_trans\",\"TEMPERATURE_3_DESC\":\"Temperature_trans\"},\"labels\":{\"LABEL_2\":\"Vitals_TRANS\"}}";
FileUtils.writeStringToFile(new File(importedFprmTranslationsPath), importedFormJson);

String referenceFormUuid = "prev-uuid";
String prevVersionTranslationsPath = tempTranslationsPath + "/" + referenceFormUuid + "_not_found.json";
String prevVersionJson =
"{\"en\":{\"concepts\":{\"TEMPERATURE_1\":\"Temperature\",\"TEMPERATURE_1_DESC\":\"Temperature\"},\"labels\":{\"LABEL_2\":\"Vitals\"}}"
+
",\"fr\":{\"concepts\":{\"TEMPERATURE_1\":\"Temperature\",\"TEMPERATURE_1_DESC\":\"Temperature\"},\"labels\":{\"LABEL_2\":\"Vitals\"}}}";
"{\"en\":{\"concepts\":{\"TEMPERATURE_1\":\"Temperature\",\"TEMPERATURE_1_DESC\":\"Temperature\"},\"labels\":{\"LABEL_2\":\"Vitals\"}}";
FileUtils.writeStringToFile(new File(prevVersionTranslationsPath), prevVersionJson);

FormTranslation formTranslationEn = createFormTranslation("en", "2", "test_form_uuid", "test_form");
formTranslationEn.setReferenceVersion("1");
formTranslationEn.getConcepts().remove("TEMPERATURE_1_DESC");
formTranslationEn.getConcepts().remove("TEMPERATURE_1");
formTranslationEn.getConcepts().put("TEMPERATURE_2_DESC", "Temperature_trans");
formTranslationEn.getConcepts().put("TEMPERATURE_3_DESC", "Temperature_trans");
formTranslationEn.setReferenceFormUuid(referenceFormUuid);
bahmniFormTranslationService.saveFormTranslation(new ArrayList<>(Arrays.asList(formTranslationEn)));
String expected ="{\"en\":{\"concepts\":{\"TEMPERATURE_1\":\"Temperature\",\"TEMPERATURE_1_DESC\":\"Temperature\"},\"labels\":{\"LABEL_2\":\"Vitals\"}}}";


File translationFile = new File(tempTranslationsPath + "/test_form_uuid.json");
assertTrue(translationFile.exists());
assertEquals(FileUtils.readFileToString(translationFile), expected);
assertNotEquals(FileUtils.readFileToString(translationFile), prevVersionJson);
}

@Test
Expand Down Expand Up @@ -596,7 +605,7 @@ public void shouldSetEmptyTranslationsForConceptsWhenFormDoesnotHaveConcepts()
}

@Test
public void shouldGetNormalTranslationsWhenPreviousVersionDoesnotHaveTranslations()
public void shouldNotGetNormalTranslationsWhenPreviousVersionDoesnotHaveTranslations()
throws IllegalAccessException, NoSuchFieldException, IOException {
BahmniFormTranslationService bahmniFormTranslationService = new BahmniFormTranslationServiceImpl();
String tempTranslationsPath = createTempFolder();
Expand All @@ -612,7 +621,7 @@ public void shouldGetNormalTranslationsWhenPreviousVersionDoesnotHaveTranslation
String expected = "{\"en\":{\"concepts\":{\"TEMPERATURE_1\":\"Temperature\",\"TEMPERATURE_1_DESC\":\"Temperature\"},\"labels\":{\"LABEL_2\":\"Vitals\"}}}";
File translationFile = new File(tempTranslationsPath + "/test_form.json");
assertTrue(translationFile.exists());
assertEquals(expected, FileUtils.readFileToString(translationFile));
assertNotEquals(expected, FileUtils.readFileToString(translationFile));
}

@Test
Expand Down

0 comments on commit f29b141

Please sign in to comment.