diff --git a/api/src/main/java/org/bahmni/module/bahmni/ie/apps/dao/impl/BahmniFormDaoImpl.java b/api/src/main/java/org/bahmni/module/bahmni/ie/apps/dao/impl/BahmniFormDaoImpl.java index fdc99de..4428cad 100644 --- a/api/src/main/java/org/bahmni/module/bahmni/ie/apps/dao/impl/BahmniFormDaoImpl.java +++ b/api/src/main/java/org/bahmni/module/bahmni/ie/apps/dao/impl/BahmniFormDaoImpl.java @@ -74,7 +74,7 @@ public List 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 " + diff --git a/api/src/main/java/org/bahmni/module/bahmni/ie/apps/service/impl/BahmniFormServiceImpl.java b/api/src/main/java/org/bahmni/module/bahmni/ie/apps/service/impl/BahmniFormServiceImpl.java index ddd9911..4712b01 100644 --- a/api/src/main/java/org/bahmni/module/bahmni/ie/apps/service/impl/BahmniFormServiceImpl.java +++ b/api/src/main/java/org/bahmni/module/bahmni/ie/apps/service/impl/BahmniFormServiceImpl.java @@ -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 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; } diff --git a/api/src/main/java/org/bahmni/module/bahmni/ie/apps/service/impl/BahmniFormTranslationServiceImpl.java b/api/src/main/java/org/bahmni/module/bahmni/ie/apps/service/impl/BahmniFormTranslationServiceImpl.java index 960fd2f..3427b6e 100644 --- a/api/src/main/java/org/bahmni/module/bahmni/ie/apps/service/impl/BahmniFormTranslationServiceImpl.java +++ b/api/src/main/java/org/bahmni/module/bahmni/ie/apps/service/impl/BahmniFormTranslationServiceImpl.java @@ -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; @@ -88,15 +87,17 @@ public List saveFormTranslation(List 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; } @@ -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)); } diff --git a/api/src/test/java/org/bahmni/module/bahmni/ie/apps/dao/impl/BahmniFormDaoImplTest.java b/api/src/test/java/org/bahmni/module/bahmni/ie/apps/dao/impl/BahmniFormDaoImplTest.java index 1ae674e..4e09c45 100644 --- a/api/src/test/java/org/bahmni/module/bahmni/ie/apps/dao/impl/BahmniFormDaoImplTest.java +++ b/api/src/test/java/org/bahmni/module/bahmni/ie/apps/dao/impl/BahmniFormDaoImplTest.java @@ -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 @@ -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"); } } diff --git a/api/src/test/java/org/bahmni/module/bahmni/ie/apps/service/impl/BahmniFormTranslationServiceImplTest.java b/api/src/test/java/org/bahmni/module/bahmni/ie/apps/service/impl/BahmniFormTranslationServiceImplTest.java index 382787f..10894fa 100644 --- a/api/src/test/java/org/bahmni/module/bahmni/ie/apps/service/impl/BahmniFormTranslationServiceImplTest.java +++ b/api/src/test/java/org/bahmni/module/bahmni/ie/apps/service/impl/BahmniFormTranslationServiceImplTest.java @@ -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 @@ -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(); @@ -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