Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package com.exadel.aem.toolkit.core.lists.servlets;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Nonnull;
Expand All @@ -26,21 +25,18 @@
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceMetadata;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.commons.jcr.JcrConstants;
import com.adobe.granite.ui.components.ds.DataSource;
import com.adobe.granite.ui.components.ds.SimpleDataSource;
import com.adobe.granite.ui.components.ds.ValueMapResource;

import com.exadel.aem.toolkit.core.CoreConstants;
import com.exadel.aem.toolkit.core.utils.ResourceFactory;

/**
* Provides the collection of AEM resources that will represent Exadel Toolbox Lists items. This collection will be displayed
Expand Down Expand Up @@ -76,10 +72,11 @@ protected void doGet(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHtt
List<Resource> actualList = new ArrayList<>();
while (resources.hasNext()) {
Resource item = resources.next();
ValueMap valueMap = new ValueMapDecorator(new HashMap<>());
valueMap.put(CoreConstants.PN_VALUE, item.getPath());
valueMap.put(CoreConstants.PN_TEXT, item.getValueMap().get(JcrConstants.JCR_TITLE, StringUtils.EMPTY));
actualList.add(new ValueMapResource(resolver, new ResourceMetadata(), JcrConstants.NT_UNSTRUCTURED, valueMap));
actualList.add(ResourceFactory
.newResource(resolver)
.property(CoreConstants.PN_VALUE, item.getPath())
.property(CoreConstants.PN_TEXT, item.getValueMap().get(JcrConstants.JCR_TITLE, StringUtils.EMPTY))
.build());
}
DataSource dataSource = new SimpleDataSource(actualList.iterator());
request.setAttribute(DataSource.class.getName(), dataSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

import com.exadel.aem.toolkit.core.CoreConstants;
import com.exadel.aem.toolkit.core.lists.models.SimpleListItem;
import com.exadel.aem.toolkit.core.utils.ResourceFactory;

/**
* Contains methods for manipulating EToolbox Lists
Expand Down Expand Up @@ -216,7 +217,7 @@ public static <T> Page createList(
List<Resource> resources = values.stream()
.map(mapping)
.filter(Objects::nonNull)
.map(properties -> ListResourceUtil.createValueMapResource(resourceResolver, properties))
.map(properties -> ResourceFactory.newResource(resourceResolver).properties(properties).build())
.collect(Collectors.toList());

return createResourceList(resourceResolver, path, resources);
Expand Down Expand Up @@ -245,7 +246,7 @@ public static Page createList(
reportNoItems(path);
}

List<Resource> resources = ListResourceUtil.mapToValueMapResources(resourceResolver, values);
List<Resource> resources = ListResourceUtil.mapToResources(resourceResolver, values);
return createResourceList(resourceResolver, path, resources);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,18 @@

import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.StringUtils;
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.wrappers.ValueMapDecorator;
import org.apache.sling.jcr.resource.api.JcrResourceConstants;
import com.day.cq.commons.jcr.JcrConstants;
import com.adobe.granite.ui.components.ds.ValueMapResource;

import com.exadel.aem.toolkit.core.CoreConstants;
import com.exadel.aem.toolkit.core.lists.ListConstants;
import com.exadel.aem.toolkit.core.lists.models.SimpleListItem;
import com.exadel.aem.toolkit.core.utils.ObjectConversionUtil;
import com.exadel.aem.toolkit.core.utils.ResourceFactory;
import com.exadel.aem.toolkit.core.utils.ValueMapUtil;

/**
Expand Down Expand Up @@ -71,47 +69,26 @@ public static void createListItem(
}

/**
* Creates a {@link ValueMapResource} representation of a list entry using the provided {@code title} and {@code
* value}
* @param resourceResolver Sling {@link ResourceResolver} instance used to create the list
* @param title String value representing the title of the list entry
* @param value String value representing the value of the list entry
* @return {@link ValueMapResource} object
*/
public static Resource createValueMapResource(ResourceResolver resourceResolver, String title, Object value) {
Map<String, Object> properties = new HashMap<>();
properties.put(JcrConstants.JCR_TITLE, title);
properties.put(CoreConstants.PN_VALUE, value);
return createValueMapResource(resourceResolver, properties);
}

/**
* Creates a {@link ValueMapResource} representation of a list entry using the provided properties
* @param resourceResolver Sling {@link ResourceResolver} instance used to create the list
* @param properties Resource properties
* @return {@link ValueMapResource}
*/
public static Resource createValueMapResource(ResourceResolver resourceResolver, Map<String, Object> properties) {
return new ValueMapResource(resourceResolver, StringUtils.EMPTY, JcrConstants.NT_UNSTRUCTURED, new ValueMapDecorator(properties));
}

/**
* Converts a key-value map to the list of {@link ValueMapResource} objects
* @param resourceResolver Sling {@link ResourceResolver} instance used to create the list
* @param values {@code Map} instance that will be converted to the {@link ValueMapResource}
* @return List of {@link ValueMapResource} objects
* Converts a key-value map to the list of resources representing list items
* @param resourceResolver {@link ResourceResolver} instance used to create the list
* @param values {@code Map} instance that will be converted to the list of resources
* @return List of {@link Resource} objects
*/
public static List<Resource> mapToValueMapResources(ResourceResolver resourceResolver, Map<String, Object> values) {
public static List<Resource> mapToResources(ResourceResolver resourceResolver, Map<String, Object> values) {
return MapUtils.emptyIfNull(values)
.entrySet()
.stream()
.map(entry -> createValueMapResource(resourceResolver, entry.getKey(), entry.getValue()))
.map(entry -> ResourceFactory
.newResource(resourceResolver)
.property(JcrConstants.JCR_TITLE, entry.getKey())
.property(CoreConstants.PN_VALUE, entry.getValue())
.build())
.collect(Collectors.toList());
}

/**
* Returns a {@code BiFunction} representing the conversion of a Sling model instance into a {@code Map} that can
* further be used for creating a {@link ValueMapResource}
* further be used for creating a list item resource
* @param modelType Type of the Sling model
* @return {@code BiFunction}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -28,17 +28,15 @@
import org.apache.commons.lang3.tuple.Pair;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.commons.jcr.JcrConstants;
import com.adobe.granite.ui.components.ds.ValueMapResource;

import com.exadel.aem.toolkit.core.CoreConstants;
import com.exadel.aem.toolkit.core.optionprovider.OptionProviderConstants;
import com.exadel.aem.toolkit.core.optionprovider.services.impl.PathParameters;
import com.exadel.aem.toolkit.core.optionprovider.utils.PatternUtil;
import com.exadel.aem.toolkit.core.utils.ResourceFactory;

/**
* Invoked by {@link ClassOptionSourceResolver} to convert a Java class containing constants into an options data
Expand Down Expand Up @@ -67,82 +65,88 @@ class ConstantsResolverHelper {
* @return A non-null {@code Resource} object
*/
Resource resolve(SlingHttpServletRequest request) {
List<ValueMap> individualFieldValueMaps = Arrays.stream(source.getFields())
List<Map<String, Object>> individualFieldValueMaps = Arrays.stream(source.getFields())
.filter(field -> Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers()))
.map(field -> new ValueMapBuilder()
.put(OptionProviderConstants.PARAMETER_NAME, field.getName())
.put(JcrConstants.JCR_TITLE, field.getName())
.put(CoreConstants.PN_VALUE, getFieldInvocationResult(field))
.build())
.map(field -> {
Map<String, Object> map = new HashMap<>();
map.put(OptionProviderConstants.PARAMETER_NAME, field.getName());
map.put(JcrConstants.JCR_TITLE, field.getName());
map.put(CoreConstants.PN_VALUE, getFieldInvocationResult(field));
return map;
})
.collect(Collectors.toList());
List<ValueMap> pairedValueMaps = reduce(individualFieldValueMaps, pathParameters);
List<Map<String, Object>> pairedValueMaps = reduce(individualFieldValueMaps, pathParameters);

List<Resource> dataSourceOptions = pairedValueMaps
.stream()
.map(valueMap -> new ValueMapResource(
request.getResourceResolver(),
valueMap.get(OptionProviderConstants.PARAMETER_NAME, String.class),
JcrConstants.NT_UNSTRUCTURED,
valueMap))
.map(valueMap -> ResourceFactory
.newResource(request.getResourceResolver())
.path(valueMap.getOrDefault(OptionProviderConstants.PARAMETER_NAME, StringUtils.EMPTY).toString())
.properties(valueMap)
.build())
.collect(Collectors.toList());

return new ValueMapResource(
request.getResourceResolver(),
StringUtils.EMPTY,
JcrConstants.NT_UNSTRUCTURED,
new ValueMapDecorator(Collections.emptyMap()),
dataSourceOptions);
return ResourceFactory.newResource(request.getResourceResolver())
.children(dataSourceOptions)
.build();
}

/**
* Compacts the provided list of {@link Resource}s that represent separate Java class constants. We search among the
* Compacts the provided list of {@code Map}s that represent separate Java class constants. We search among the
* value maps for every pair that refer to the same logical item (like {@code COLOR_LABEL} and {@code COLOR_VALUE})
* and merge it into a single value map that contains both title and value
* @param individualMaps Collection of {@code Resource} instances representing constants
Comment thread
smiakchilo marked this conversation as resolved.
Outdated
* @param pathParameters {@link PathParameters} object that is used to modify the list of options
* @return A reduced list of value maps
*/
private static List<ValueMap> reduce(List<ValueMap> individualMaps, PathParameters pathParameters) {
private static List<Map<String, Object>> reduce(
List<Map<String, Object>> individualMaps,
PathParameters pathParameters) {

if (!PatternUtil.isPattern(pathParameters.getTextMember())
|| !PatternUtil.isPattern(pathParameters.getValueMember())
|| IterableUtils.isEmpty(individualMaps)) {
return individualMaps;
}
Map<String, Pair<String, String>> nameTextEntries = individualMaps
.stream()
.filter(valueMap -> PatternUtil.isMatch(valueMap.get(JcrConstants.JCR_TITLE, String.class), pathParameters.getTextMember()))
.filter(valueMap -> PatternUtil.isMatch(
valueMap.getOrDefault(JcrConstants.JCR_TITLE, StringUtils.EMPTY).toString(),
pathParameters.getTextMember()))
.collect(Collectors.toMap(
valueMap -> PatternUtil.strip(
valueMap.get(JcrConstants.JCR_TITLE, String.class),
valueMap.getOrDefault(JcrConstants.JCR_TITLE, StringUtils.EMPTY).toString(),
pathParameters.getTextMember()),
valueMap -> Pair.of(
valueMap.get(OptionProviderConstants.PARAMETER_NAME, String.class),
valueMap.get(CoreConstants.PN_VALUE, String.class)),
valueMap.getOrDefault(OptionProviderConstants.PARAMETER_NAME, StringUtils.EMPTY).toString(),
valueMap.getOrDefault(CoreConstants.PN_VALUE, StringUtils.EMPTY).toString()),
(first, second) -> first,
LinkedHashMap::new));

Map<String, Object> valueEntries = individualMaps
.stream()
.filter(valueMap -> PatternUtil.isMatch(valueMap.get(JcrConstants.JCR_TITLE, String.class), pathParameters.getValueMember()))
.filter(valueMap -> PatternUtil.isMatch(
valueMap.getOrDefault(JcrConstants.JCR_TITLE, StringUtils.EMPTY).toString(),
pathParameters.getValueMember()))
.collect(Collectors.toMap(
valueMap -> PatternUtil.strip(valueMap.get(JcrConstants.JCR_TITLE, String.class), pathParameters.getValueMember()),
valueMap -> valueMap.get(CoreConstants.PN_VALUE, StringUtils.EMPTY)));
valueMap -> PatternUtil.strip(
valueMap.getOrDefault(JcrConstants.JCR_TITLE, StringUtils.EMPTY).toString(),
pathParameters.getValueMember()),
valueMap -> valueMap.getOrDefault(CoreConstants.PN_VALUE, StringUtils.EMPTY).toString()));

List<ValueMap> result = new ArrayList<>();
List<Map<String, Object>> result = new ArrayList<>();
for (Map.Entry<String, Pair<String, String>> textEntry : nameTextEntries.entrySet()) {
Object value = valueEntries.get(textEntry.getKey());
if (value == null) {
continue;
}
Pair<String, String> nameAndText = textEntry.getValue();
ValueMap valueMap = new ValueMapBuilder()
.put(
Map<String, Object> map = new HashMap<>();
map.put(
OptionProviderConstants.PARAMETER_NAME,
PatternUtil.strip(nameAndText.getLeft(), pathParameters.getTextMember()))
.put(pathParameters.getTextMember(), nameAndText.getRight())
.put(pathParameters.getValueMember(), value)
.build();
result.add(valueMap);
PatternUtil.strip(nameAndText.getLeft(), pathParameters.getTextMember()));
map.put(pathParameters.getTextMember(), nameAndText.getRight());
map.put(pathParameters.getValueMember(), value);
result.add(map);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,20 @@
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.commons.jcr.JcrConstants;
import com.adobe.granite.ui.components.ds.ValueMapResource;

import com.exadel.aem.toolkit.core.CoreConstants;
import com.exadel.aem.toolkit.core.optionprovider.OptionProviderConstants;
import com.exadel.aem.toolkit.core.utils.ResourceFactory;

/**
* Invoked by {@link ClassOptionSourceResolver} to convert a Java enum into an options data source
Expand Down Expand Up @@ -64,23 +61,21 @@ class EnumResolverHelper {
Resource resolve(SlingHttpServletRequest request) {
List<Resource> children = new ArrayList<>();
for (Object enumConstant : source.getEnumConstants()) {
ValueMap valueMap = new ValueMapDecorator(buildPropertyMap(enumConstant));
children.add(new ValueMapResource(
request.getResourceResolver(),
valueMap.get(OptionProviderConstants.PARAMETER_NAME, String.class),
JcrConstants.NT_UNSTRUCTURED,
new ValueMapDecorator(buildPropertyMap(enumConstant))));
Map<String, Object> valueMap = buildPropertyMap(enumConstant);
children.add(ResourceFactory
.newResource(request.getResourceResolver())
.path(valueMap.getOrDefault(OptionProviderConstants.PARAMETER_NAME, StringUtils.EMPTY).toString())
.properties(valueMap)
.build());
}
return new ValueMapResource(
request.getResourceResolver(),
StringUtils.EMPTY,
JcrConstants.NT_UNSTRUCTURED,
new ValueMapDecorator(Collections.emptyMap()),
children);
return ResourceFactory
.newResource(request.getResourceResolver())
.children(children)
.build();
}

/**
* Creates a {@link ValueMap} instance representing a single data source option for the given enum constant
* Creates a {@code Map} instance representing a single data source option for the given enum constant
* @param enumConstant An enum object
* @return {@link Map} object
*/
Expand Down
Loading
Loading