Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
bdhoine committed Jun 19, 2024
1 parent 01f56ac commit 3428d83
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 28 deletions.
30 changes: 24 additions & 6 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,28 @@ Import-Package: javax.annotation;version=0.0.0,*
</build>

<dependencies>
<!-- <dependency>-->
<!-- <groupId>com.adobe.aem</groupId>-->
<!-- <artifactId>aem-sdk-api</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.adobe.aem</groupId>
<artifactId>aem-sdk-api</artifactId>
<artifactId>uber-jar</artifactId>
</dependency>

<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.component.annotations</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.annotation.versioning</artifactId>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.servlets.annotations</artifactId>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
Expand All @@ -66,11 +84,11 @@ Import-Package: javax.annotation;version=0.0.0,*
</dependency>

<!-- Testing -->
<dependency>
<groupId>com.adobe.cq</groupId>
<artifactId>core.wcm.components.core</artifactId>
<scope>test</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.adobe.cq</groupId>-->
<!-- <artifactId>core.wcm.components.core</artifactId>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@

import be.orbinson.aem.dictionarytranslator.services.DictionaryService;
import com.adobe.granite.translation.api.TranslationConfig;
import com.adobe.granite.translation.api.TranslationException;
import com.day.cq.commons.jcr.JcrConstants;
import com.day.cq.commons.jcr.JcrUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.*;
import org.apache.sling.api.resource.LoginException;
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.ResourceResolverFactory;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.jcr.resource.api.JcrResourceConstants;
import org.jetbrains.annotations.NotNull;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicReference;

import static org.apache.jackrabbit.JcrConstants.JCR_LANGUAGE;
Expand Down Expand Up @@ -58,8 +69,8 @@ private ResourceResolver getServiceResourceResolver() throws LoginException {
Resource resource = resourceResolver.getResource(path);

if (resource != null && translationConfig != null) {
try (ResourceResolver serviceResourceResolver = getServiceResourceResolver()) {
Map<String, String> languages = translationConfig.getLanguages(serviceResourceResolver);
try {
Map<String, String> languages = translationConfig.getLanguages();

resource.getChildren().forEach(child -> {
if (child.getValueMap().containsKey(JcrConstants.JCR_LANGUAGE)) {
Expand All @@ -71,8 +82,8 @@ private ResourceResolver getServiceResourceResolver() throws LoginException {
}
}
});
} catch (LoginException e) {
LOG.error("Unable to get service resource resolver to get languages", e);
} catch (TranslationException e) {
LOG.error("Unable to get languages", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package be.orbinson.aem.dictionarytranslator.servlets.datasource;

import com.adobe.granite.translation.api.TranslationConfig;
import com.adobe.granite.translation.api.TranslationException;
import com.adobe.granite.ui.components.ds.DataSource;
import com.adobe.granite.ui.components.ds.SimpleDataSource;
import com.adobe.granite.ui.components.ds.ValueMapResource;
Expand All @@ -19,7 +20,12 @@
import org.slf4j.LoggerFactory;

import javax.servlet.Servlet;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Component(service = Servlet.class)
@SlingServletResourceTypes(
Expand Down Expand Up @@ -62,21 +68,27 @@ protected void doGet(SlingHttpServletRequest request, @NotNull SlingHttpServletR
ResourceResolver resourceResolver = request.getResourceResolver();
String languageParameter = request.getRequestPathInfo().getSuffix();

Map<String, String> languageMap = new HashMap<>(translationConfig.getLanguages(resourceResolver));
filterExistingLanguages(languageMap, resourceResolver, languageParameter);
Map<String, String> sortedLanguageMap = sortByValue(languageMap);
Map<String, String> languageMap = null;
try {
languageMap = new HashMap<>(translationConfig.getLanguages());

List<Resource> resourceList = new ArrayList<>();
sortedLanguageMap.forEach((key, value) -> {
String text = String.format("%s (%s)", value, key);
ValueMap valueMap = new ValueMapDecorator(Map.of("value", key, "text", text));
filterExistingLanguages(languageMap, resourceResolver, languageParameter);
Map<String, String> sortedLanguageMap = sortByValue(languageMap);

LOG.debug("Add language '{}' to datasource with key '{}'", text, key);
resourceList.add(new ValueMapResource(resourceResolver, "", "", valueMap));
});
List<Resource> resourceList = new ArrayList<>();
sortedLanguageMap.forEach((key, value) -> {
String text = String.format("%s (%s)", value, key);
ValueMap valueMap = new ValueMapDecorator(Map.of("value", key, "text", text));

DataSource dataSource = new SimpleDataSource(resourceList.iterator());
request.setAttribute(DataSource.class.getName(), dataSource);
LOG.debug("Add language '{}' to datasource with key '{}'", text, key);
resourceList.add(new ValueMapResource(resourceResolver, "", "", valueMap));
});

DataSource dataSource = new SimpleDataSource(resourceList.iterator());
request.setAttribute(DataSource.class.getName(), dataSource);
} catch (TranslationException e) {
LOG.error("Unable to get languages", e);
}
}

}
13 changes: 11 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<componentName>Dictionary Translator</componentName>
<componentGroupName>Orbinson Tools</componentGroupName>

<aem.version>6.5.21</aem.version>
<aem.sdk.api.version>2023.6.12255.20230608T053118Z-230400</aem.sdk.api.version>
<aemanalyser.version>1.5.8</aemanalyser.version>

Expand Down Expand Up @@ -559,10 +560,18 @@ Bundle-DocURL:
<!-- ====================================================================== -->
<dependencyManagement>
<dependencies>
<!-- <dependency>-->
<!-- <groupId>io.wcm.maven</groupId>-->
<!-- <artifactId>io.wcm.maven.aem-cloud-dependencies</artifactId>-->
<!-- <version>${aem.sdk.api.version}.0000</version>-->
<!-- <type>pom</type>-->
<!-- <scope>import</scope>-->
<!-- </dependency>-->

<dependency>
<groupId>io.wcm.maven</groupId>
<artifactId>io.wcm.maven.aem-cloud-dependencies</artifactId>
<version>${aem.sdk.api.version}.0000</version>
<artifactId>io.wcm.maven.aem-dependencies</artifactId>
<version>${aem.version}.0000</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
6 changes: 5 additions & 1 deletion ui.apps/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,13 @@
<type>zip</type>
</dependency>

<!-- <dependency>-->
<!-- <groupId>com.adobe.aem</groupId>-->
<!-- <artifactId>aem-sdk-api</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.adobe.aem</groupId>
<artifactId>aem-sdk-api</artifactId>
<artifactId>uber-jar</artifactId>
</dependency>

<!-- HTL dependencies needed for the HTL Maven Plugin source code generation -->
Expand Down

0 comments on commit 3428d83

Please sign in to comment.