Skip to content

Commit

Permalink
Merge pull request #107 from orbinson/feature/enable-mixin-types-base…
Browse files Browse the repository at this point in the history
…d-message-entries
  • Loading branch information
bdhoine authored Jan 23, 2025
2 parents 44e9821 + d29c3c0 commit 7575b1e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Make dictionary keys sortable [#62](https://github.com/orbinson/aem-dictionary-translator/issues/62)
- Support sling:key being optional [#62](https://github.com/orbinson/aem-dictionary-translator/issues/62)
- Render entries when not all languages are present [#62](https://github.com/orbinson/aem-dictionary-translator/issues/62)
- Enable "sling:Message" mixinType based message entries [#106](https://github.com/orbinson/aem-dictionary-translator/issues/106)

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,7 @@
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_MESSAGEENTRY;
import java.util.*;

@Component(
service = ResourceProvider.class,
Expand Down Expand Up @@ -53,7 +45,7 @@ private Map<String, Object> getValuesAndMessageEntryPaths(Resource dictionaryRes
Resource languageResource = dictionaryService.getLanguageResource(dictionaryResource, language);
if (languageResource != null) {
Resource messageEntryResource = dictionaryService.getMessageEntryResource(languageResource, key);
if (messageEntryResource != null && messageEntryResource.isResourceType(DictionaryConstants.SLING_MESSAGEENTRY)) {
if (messageEntryResource != null) {
properties.put(language, messageEntryResource.getValueMap().get(DictionaryConstants.SLING_MESSAGE, ""));
messageEntryPaths.add(messageEntryResource.getPath());
} else {
Expand Down Expand Up @@ -107,7 +99,7 @@ private boolean isMessageEntry(Resource dictionaryResource, String key) {
Resource languageResource = dictionaryService.getLanguageResource(dictionaryResource, language);
if (languageResource != null) {
Resource messageEntryResource = dictionaryService.getMessageEntryResource(languageResource, key);
if (messageEntryResource != null && messageEntryResource.isResourceType(SLING_MESSAGEENTRY)) {
if (messageEntryResource != null) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@
import com.day.cq.replication.Replicator;
import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.resource.*;
import org.apache.sling.jcr.resource.api.JcrResourceConstants;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -26,24 +21,13 @@

import javax.jcr.RepositoryException;
import javax.jcr.Session;

import javax.jcr.security.AccessControlManager;
import javax.jcr.security.Privilege;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

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_LANGUAGE;
import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;
import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.*;
import static org.apache.jackrabbit.JcrConstants.*;

@Component
public class DictionaryServiceImpl implements DictionaryService {
Expand Down Expand Up @@ -269,20 +253,25 @@ private static void updateMessage(String key, String message, Resource messageEn
public Resource getMessageEntryResource(Resource languageResource, String key) {
// In order to speed up the search, we go for the default check where it is the escaped key as node name
Resource messageEntryResource = languageResource.getChild(Text.escapeIllegalJcrChars(key));
if (messageEntryResource != null) {
if (isMessageEntryResource(messageEntryResource)) {
return messageEntryResource;
}

// Fall back to searching for the resource with sling:key as correct property
for (Resource resource : languageResource.getChildren()) {
if (key.equals(resource.getValueMap().get(DictionaryConstants.SLING_KEY))) {
if (key.equals(resource.getValueMap().get(DictionaryConstants.SLING_KEY)) && isMessageEntryResource(messageEntryResource)) {
return resource;
}
}

return null;
}

private static boolean isMessageEntryResource(Resource messageEntryResource) {
return messageEntryResource != null && (messageEntryResource.isResourceType(DictionaryConstants.SLING_MESSAGEENTRY) ||
Arrays.asList(messageEntryResource.getValueMap().get(JCR_MIXINTYPES, new String[0])).contains(SLING_MESSAGE_MIXIN));
}

@Override
public void deleteMessageEntry(ResourceResolver resourceResolver, Resource combiningMessageEntryResource) throws PersistenceException, ReplicationException {
ValueMap properties = combiningMessageEntryResource.getValueMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ private DictionaryConstants() {

public static final String SLING_KEY = "sling:key";
public static final String SLING_MESSAGE = "sling:message";
public static final String JCR_BASENAME = "jcr:basename";
public static final String SLING_MESSAGEENTRY = "sling:MessageEntry";
public static final String MIX_LANGUAGE = "mix:language";
public static final String SLING_MESSAGE_MIXIN = "sling:Message";

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
jcr:primaryType="sling:Folder"
sling:basename="/content/dictionaries/fruit/i18n"
sling:resourceType="sling:Folder">
<apple jcr:primaryType="sling:MessageEntry" sling:key="apple" sling:message="Appel"/>
<apple jcr:primaryType="nt:folder" jcr:mixinTypes="[sling:Message]" sling:key="apple" sling:message="Appel"/>
<banana jcr:primaryType="sling:MessageEntry" sling:key="banana" sling:message="Banaan"/>
<cherry jcr:primaryType="sling:MessageEntry" sling:key="cherry" sling:message="Kers"/>
<date jcr:primaryType="sling:MessageEntry" sling:key="date" sling:message="Dadel"/>
Expand Down

0 comments on commit 7575b1e

Please sign in to comment.