From 04ddc3169d0de16e94d3c92f94b2bca283dcdcda Mon Sep 17 00:00:00 2001 From: "wietse.vandeput@amplexor.com" Date: Thu, 1 Feb 2024 14:01:39 +0100 Subject: [PATCH 1/3] Issue #9: empty translations no longer leave empty sling:message, the sling:message property gets deleted. --- .../servlets/action/CreateLabelServlet.java | 13 +++++---- .../servlets/action/UpdateLabelServlet.java | 10 ++++--- .../action/CreateLabelServletTest.java | 27 +++++++++++++++++++ 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/be/orbinson/aem/dictionarytranslator/servlets/action/CreateLabelServlet.java b/core/src/main/java/be/orbinson/aem/dictionarytranslator/servlets/action/CreateLabelServlet.java index eda744de..566f83d3 100644 --- a/core/src/main/java/be/orbinson/aem/dictionarytranslator/servlets/action/CreateLabelServlet.java +++ b/core/src/main/java/be/orbinson/aem/dictionarytranslator/servlets/action/CreateLabelServlet.java @@ -19,6 +19,7 @@ import javax.servlet.Servlet; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.util.HashMap; import java.util.Map; @Component(service = Servlet.class) @@ -70,11 +71,13 @@ private void addMessage(ResourceResolver resourceResolver, Resource dictionary, if (resource != null) { String path = resource.getPath(); - resourceResolver.create(resource, JcrUtil.createValidName(key), Map.of( - "jcr:primaryType", "sling:MessageEntry", - "sling:key", key, - "sling:message", message - )); + Map properties = new HashMap<>(); + properties.put("jcr:primaryType", "sling:MessageEntry"); + properties.put("sling:key", key); + if (!message.isBlank()){ + properties.put("sling:message", message); + } + resourceResolver.create(resource, JcrUtil.createValidName(key), properties); LOG.trace("Create label with key '{}' and message '{}' on path '{}'", key, message, path); resourceResolver.commit(); } diff --git a/core/src/main/java/be/orbinson/aem/dictionarytranslator/servlets/action/UpdateLabelServlet.java b/core/src/main/java/be/orbinson/aem/dictionarytranslator/servlets/action/UpdateLabelServlet.java index d614d91f..47fd8d68 100644 --- a/core/src/main/java/be/orbinson/aem/dictionarytranslator/servlets/action/UpdateLabelServlet.java +++ b/core/src/main/java/be/orbinson/aem/dictionarytranslator/servlets/action/UpdateLabelServlet.java @@ -88,8 +88,12 @@ private void addMessage(ResourceResolver resourceResolver, Resource dictionary, if (labelResource != null) { ValueMap valueMap = labelResource.adaptTo(ModifiableValueMap.class); if (valueMap != null) { - valueMap.put("sling:message", message); - LOG.trace("Updated label with name '{}' and message '{}' on path '{}'", name, message, labelResource.getPath()); + if (message.isBlank()) { + valueMap.remove("sling:message"); + } else { + valueMap.put("sling:message", message); + LOG.trace("Updated label with name '{}' and message '{}' on path '{}'", name, message, labelResource.getPath()); + } } } resourceResolver.commit(); @@ -99,7 +103,7 @@ private void addMessage(ResourceResolver resourceResolver, Resource dictionary, } } - private Resource getLabelResource(ResourceResolver resourceResolver, Resource languageResource, String name) throws RepositoryException { + public Resource getLabelResource(ResourceResolver resourceResolver, Resource languageResource, String name) throws RepositoryException { if (languageResource.getChild(name) == null) { Session session = resourceResolver.adaptTo(Session.class); JcrUtil.createPath(languageResource.getPath() + "/" + name, "sling:MessageEntry", session); diff --git a/core/src/test/java/be/orbinson/aem/dictionarytranslator/servlets/action/CreateLabelServletTest.java b/core/src/test/java/be/orbinson/aem/dictionarytranslator/servlets/action/CreateLabelServletTest.java index 4e8fbff8..1c4dc9f2 100644 --- a/core/src/test/java/be/orbinson/aem/dictionarytranslator/servlets/action/CreateLabelServletTest.java +++ b/core/src/test/java/be/orbinson/aem/dictionarytranslator/servlets/action/CreateLabelServletTest.java @@ -18,6 +18,7 @@ import java.io.IOException; import java.util.Map; +import static junit.framework.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -88,4 +89,30 @@ void doPostWithValidParams() throws ServletException, IOException { assertEquals("greeting", properties.get("sling:key")); assertEquals("Hello", properties.get("sling:message")); } + + @Test + void doPostWithEmptyMessage() throws ServletException, IOException { + context.create().resource("/content/dictionaries/i18n/en", Map.of("jcr:language", "en")); + context.create().resource("/content/dictionaries/i18n/fr", Map.of("jcr:language", "fr")); + + context.request().setMethod("POST"); + context.request().setParameterMap(Map.of( + "dictionary", "/content/dictionaries/i18n", + "key", "greeting", + "en", "Hello", + "fr", "" + )); + + servlet.service(context.request(), context.response()); + + assertEquals(HttpServletResponse.SC_OK, context.response().getStatus()); + + Resource resource = context.resourceResolver().getResource("/content/dictionaries/i18n/fr/greeting"); + ValueMap properties = resource.getValueMap(); + assertNotNull(resource); + assertEquals("sling:MessageEntry", properties.get("jcr:primaryType")); + assertEquals("greeting", properties.get("sling:key")); +// assertEquals("Hello", properties.get("sling:message")); + assertNull(properties.get("sling:message")); + } } From d1376e9b677828923aa8d304a9a199875fb0806d Mon Sep 17 00:00:00 2001 From: "wietse.vandeput@amplexor.com" Date: Thu, 1 Feb 2024 16:26:10 +0100 Subject: [PATCH 2/3] pr fixes: - removed commented lines - used constants from previous pr --- .../servlets/action/CreateLabelServlet.java | 11 ++++++++--- .../servlets/action/UpdateLabelServlet.java | 9 ++++++--- .../servlets/action/CreateLabelServletTest.java | 17 ++++++++++------- .../servlets/action/UpdateLabelServletTest.java | 14 +++++++++----- 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/be/orbinson/aem/dictionarytranslator/servlets/action/CreateLabelServlet.java b/core/src/main/java/be/orbinson/aem/dictionarytranslator/servlets/action/CreateLabelServlet.java index 566f83d3..25084136 100644 --- a/core/src/main/java/be/orbinson/aem/dictionarytranslator/servlets/action/CreateLabelServlet.java +++ b/core/src/main/java/be/orbinson/aem/dictionarytranslator/servlets/action/CreateLabelServlet.java @@ -22,6 +22,11 @@ import java.util.HashMap; import java.util.Map; +import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_KEY; +import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_MESSAGE; +import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_MESSAGEENTRY; +import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE; + @Component(service = Servlet.class) @SlingServletResourceTypes( resourceSuperType = "granite/ui/components/coral/foundation/form", @@ -72,10 +77,10 @@ private void addMessage(ResourceResolver resourceResolver, Resource dictionary, if (resource != null) { String path = resource.getPath(); Map properties = new HashMap<>(); - properties.put("jcr:primaryType", "sling:MessageEntry"); - properties.put("sling:key", key); + properties.put(JCR_PRIMARYTYPE, SLING_MESSAGEENTRY); + properties.put(SLING_KEY, key); if (!message.isBlank()){ - properties.put("sling:message", message); + properties.put(SLING_MESSAGE, message); } resourceResolver.create(resource, JcrUtil.createValidName(key), properties); LOG.trace("Create label with key '{}' and message '{}' on path '{}'", key, message, path); diff --git a/core/src/main/java/be/orbinson/aem/dictionarytranslator/servlets/action/UpdateLabelServlet.java b/core/src/main/java/be/orbinson/aem/dictionarytranslator/servlets/action/UpdateLabelServlet.java index 47fd8d68..194148eb 100644 --- a/core/src/main/java/be/orbinson/aem/dictionarytranslator/servlets/action/UpdateLabelServlet.java +++ b/core/src/main/java/be/orbinson/aem/dictionarytranslator/servlets/action/UpdateLabelServlet.java @@ -25,6 +25,9 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_MESSAGE; +import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_MESSAGEENTRY; + @Component(service = Servlet.class) @SlingServletResourceTypes( resourceSuperType = "granite/ui/components/coral/foundation/form", @@ -89,9 +92,9 @@ private void addMessage(ResourceResolver resourceResolver, Resource dictionary, ValueMap valueMap = labelResource.adaptTo(ModifiableValueMap.class); if (valueMap != null) { if (message.isBlank()) { - valueMap.remove("sling:message"); + valueMap.remove(SLING_MESSAGE); } else { - valueMap.put("sling:message", message); + valueMap.put(SLING_MESSAGE, message); LOG.trace("Updated label with name '{}' and message '{}' on path '{}'", name, message, labelResource.getPath()); } } @@ -106,7 +109,7 @@ private void addMessage(ResourceResolver resourceResolver, Resource dictionary, public Resource getLabelResource(ResourceResolver resourceResolver, Resource languageResource, String name) throws RepositoryException { if (languageResource.getChild(name) == null) { Session session = resourceResolver.adaptTo(Session.class); - JcrUtil.createPath(languageResource.getPath() + "/" + name, "sling:MessageEntry", session); + JcrUtil.createPath(languageResource.getPath() + "/" + name, SLING_MESSAGEENTRY, session); session.save(); } return languageResource.getChild(name); diff --git a/core/src/test/java/be/orbinson/aem/dictionarytranslator/servlets/action/CreateLabelServletTest.java b/core/src/test/java/be/orbinson/aem/dictionarytranslator/servlets/action/CreateLabelServletTest.java index 1c4dc9f2..1696927c 100644 --- a/core/src/test/java/be/orbinson/aem/dictionarytranslator/servlets/action/CreateLabelServletTest.java +++ b/core/src/test/java/be/orbinson/aem/dictionarytranslator/servlets/action/CreateLabelServletTest.java @@ -18,7 +18,11 @@ import java.io.IOException; import java.util.Map; +import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_KEY; +import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_MESSAGE; +import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_MESSAGEENTRY; import static junit.framework.Assert.assertNull; +import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -85,9 +89,9 @@ void doPostWithValidParams() throws ServletException, IOException { Resource resource = context.resourceResolver().getResource("/content/dictionaries/i18n/en/greeting"); ValueMap properties = resource.getValueMap(); assertNotNull(resource); - assertEquals("sling:MessageEntry", properties.get("jcr:primaryType")); - assertEquals("greeting", properties.get("sling:key")); - assertEquals("Hello", properties.get("sling:message")); + assertEquals(SLING_MESSAGEENTRY, properties.get(JCR_PRIMARYTYPE)); + assertEquals("greeting", properties.get(SLING_KEY)); + assertEquals("Hello", properties.get(SLING_MESSAGE)); } @Test @@ -110,9 +114,8 @@ void doPostWithEmptyMessage() throws ServletException, IOException { Resource resource = context.resourceResolver().getResource("/content/dictionaries/i18n/fr/greeting"); ValueMap properties = resource.getValueMap(); assertNotNull(resource); - assertEquals("sling:MessageEntry", properties.get("jcr:primaryType")); - assertEquals("greeting", properties.get("sling:key")); -// assertEquals("Hello", properties.get("sling:message")); - assertNull(properties.get("sling:message")); + assertEquals(SLING_MESSAGEENTRY, properties.get(JCR_PRIMARYTYPE)); + assertEquals("greeting", properties.get(SLING_KEY)); + assertNull(properties.get(SLING_MESSAGE)); } } diff --git a/core/src/test/java/be/orbinson/aem/dictionarytranslator/servlets/action/UpdateLabelServletTest.java b/core/src/test/java/be/orbinson/aem/dictionarytranslator/servlets/action/UpdateLabelServletTest.java index 383e0923..c549b1ff 100644 --- a/core/src/test/java/be/orbinson/aem/dictionarytranslator/servlets/action/UpdateLabelServletTest.java +++ b/core/src/test/java/be/orbinson/aem/dictionarytranslator/servlets/action/UpdateLabelServletTest.java @@ -19,6 +19,10 @@ import java.io.IOException; import java.util.Map; +import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_KEY; +import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_MESSAGE; +import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_MESSAGEENTRY; +import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -72,12 +76,12 @@ void doPostWithValidParams() throws ServletException, IOException { context.create().resource("/content/dictionaries/i18n/en", Map.of("jcr:language", "en")); context.create().resource("/content/dictionaries/i18n/en/appel", Map.of( "dictionary", "/content/dictionaries/i18n", "key", "appel", - "en", "apple", "sling:MessageEntry", "jcr:primaryType") + "en", "apple", SLING_MESSAGEENTRY, JCR_PRIMARYTYPE) ); context.create().resource("/content/dictionaries/i18n/fr", Map.of("jcr:language", "fr")); context.create().resource("/content/dictionaries/i18n/en/appel", Map.of( "dictionary", "/content/dictionaries/i18n", "key", "appel", - "fr", "pomme", "sling:MessageEntry", "jcr:primaryType") + "fr", "pomme", SLING_MESSAGEENTRY, JCR_PRIMARYTYPE) ); context.request().setMethod("POST"); @@ -95,8 +99,8 @@ void doPostWithValidParams() throws ServletException, IOException { Resource resource = context.resourceResolver().getResource("/content/dictionaries/i18n/en/appel"); ValueMap properties = resource.getValueMap(); assertNotNull(resource); - assertEquals("sling:MessageEntry", properties.get("jcr:primaryType")); - assertEquals("appel", properties.get("sling:key")); - assertEquals("Hello", properties.get("sling:message")); + assertEquals(SLING_MESSAGEENTRY, properties.get(JCR_PRIMARYTYPE)); + assertEquals("appel", properties.get(SLING_KEY)); + assertEquals("Hello", properties.get(SLING_MESSAGE)); } } From 69e82922bfc609f68115e75e5337ff4e00afb45a Mon Sep 17 00:00:00 2001 From: "wietse.vandeput@amplexor.com" Date: Thu, 1 Feb 2024 16:41:37 +0100 Subject: [PATCH 3/3] changelog update --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c32a8e64..192d9d41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Dictionaries with .json files are no longer showed in dictionary list: [#8](https://github.com/orbinson/aem-dictionary-translator/pull/5) +- Labels with no translation for a specific language no longer have a +sling:message so the value won't be empty but will have a fallback +from another language or the key itself: [#12](https://github.com/orbinson/aem-dictionary-translator/pull/12) ### Added