Skip to content

Commit

Permalink
pr fixes:
Browse files Browse the repository at this point in the history
 - removed commented lines
 - used constants from previous pr
  • Loading branch information
[email protected] committed Feb 1, 2024
1 parent 212f8ec commit d1376e9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -72,10 +77,10 @@ private void addMessage(ResourceResolver resourceResolver, Resource dictionary,
if (resource != null) {
String path = resource.getPath();
Map<String, Object> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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());
}
}
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

Expand Down Expand Up @@ -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");
Expand All @@ -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));
}
}

0 comments on commit d1376e9

Please sign in to comment.