From ceb0d956b456375e05acc514ef2b8f5a328a7467 Mon Sep 17 00:00:00 2001 From: "Saldatsenka, Siarhei" Date: Mon, 16 Sep 2024 10:01:47 +0200 Subject: [PATCH] Transfer LinkInspector settings from the 'OSGi console' to the 'Granit' console. #47 -fixed git comments --- .../core/services/util/GraniteUtil.java | 34 +++++++++++++++++++ .../core/services/util/ServletUtil.java | 24 ------------- .../servlets/LinkTypesDataSourceServlet.java | 7 ++-- .../servlets/SettingsDataSourceServlet.java | 12 +++---- 4 files changed, 41 insertions(+), 36 deletions(-) create mode 100644 core/src/main/java/com/exadel/etoolbox/linkinspector/core/services/util/GraniteUtil.java diff --git a/core/src/main/java/com/exadel/etoolbox/linkinspector/core/services/util/GraniteUtil.java b/core/src/main/java/com/exadel/etoolbox/linkinspector/core/services/util/GraniteUtil.java new file mode 100644 index 00000000..f1f9d260 --- /dev/null +++ b/core/src/main/java/com/exadel/etoolbox/linkinspector/core/services/util/GraniteUtil.java @@ -0,0 +1,34 @@ +package com.exadel.etoolbox.linkinspector.core.services.util; + +import com.adobe.granite.ui.components.ds.ValueMapResource; +import com.day.cq.commons.jcr.JcrConstants; +import org.apache.commons.lang3.StringUtils; +import org.apache.sling.api.resource.Resource; +import org.apache.sling.api.resource.ResourceResolver; +import org.apache.sling.api.resource.ValueMap; +import org.apache.sling.api.wrappers.ValueMapDecorator; +import org.apache.sling.jcr.resource.api.JcrResourceConstants; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +public class GraniteUtil { + public static Resource createTab(ResourceResolver resolver, String path, String title, Collection children) { + + Resource items = createResource(resolver, path + "/items", Collections.emptyMap(), children); + + Map properties = new HashMap<>(); + properties.put(JcrConstants.JCR_TITLE, title); + properties.put(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, "granite/ui/components/coral/foundation/container"); + return createResource(resolver, path, properties, Collections.singletonList(items)); + } + + public static Resource createResource(ResourceResolver resolver, String path, Map properties, Collection children) { + + ValueMap valueMap = new ValueMapDecorator(properties); + String resourceType = StringUtils.defaultIfEmpty(properties.getOrDefault(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, StringUtils.EMPTY).toString(), JcrConstants.NT_UNSTRUCTURED); + return new ValueMapResource(resolver, path, resourceType, valueMap, children); + } +} diff --git a/core/src/main/java/com/exadel/etoolbox/linkinspector/core/services/util/ServletUtil.java b/core/src/main/java/com/exadel/etoolbox/linkinspector/core/services/util/ServletUtil.java index 375ec9e5..d9884ae7 100644 --- a/core/src/main/java/com/exadel/etoolbox/linkinspector/core/services/util/ServletUtil.java +++ b/core/src/main/java/com/exadel/etoolbox/linkinspector/core/services/util/ServletUtil.java @@ -14,8 +14,6 @@ package com.exadel.etoolbox.linkinspector.core.services.util; -import com.adobe.granite.ui.components.ds.ValueMapResource; -import com.day.cq.commons.jcr.JcrConstants; import org.apache.commons.lang.CharEncoding; import org.apache.commons.lang.math.NumberUtils; import org.apache.commons.lang3.StringUtils; @@ -23,11 +21,6 @@ import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.request.RequestParameter; -import org.apache.sling.api.resource.Resource; -import org.apache.sling.api.resource.ResourceResolver; -import org.apache.sling.api.resource.ValueMap; -import org.apache.sling.api.wrappers.ValueMapDecorator; -import org.apache.sling.jcr.resource.api.JcrResourceConstants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -72,21 +65,4 @@ public static void writeJsonResponse(SlingHttpServletResponse response, String j LOG.error("Failed to write json to response", e); } } - - public static Resource createTab(ResourceResolver resolver, String path, String title, Collection children) { - - Resource items = createResource(resolver, path + "/items", Collections.emptyMap(), children); - - Map properties = new HashMap<>(); - properties.put(JcrConstants.JCR_TITLE, title); - properties.put(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, "granite/ui/components/coral/foundation/container"); - return createResource(resolver, path, properties, Collections.singletonList(items)); - } - - public static Resource createResource(ResourceResolver resolver, String path, Map properties, Collection children) { - - ValueMap valueMap = new ValueMapDecorator(properties); - String resourceType = StringUtils.defaultIfEmpty(properties.getOrDefault(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, StringUtils.EMPTY).toString(), JcrConstants.NT_UNSTRUCTURED); - return new ValueMapResource(resolver, path, resourceType, valueMap, children); - } } \ No newline at end of file diff --git a/core/src/main/java/com/exadel/etoolbox/linkinspector/core/servlets/LinkTypesDataSourceServlet.java b/core/src/main/java/com/exadel/etoolbox/linkinspector/core/servlets/LinkTypesDataSourceServlet.java index 77b8cf25..08c92919 100644 --- a/core/src/main/java/com/exadel/etoolbox/linkinspector/core/servlets/LinkTypesDataSourceServlet.java +++ b/core/src/main/java/com/exadel/etoolbox/linkinspector/core/servlets/LinkTypesDataSourceServlet.java @@ -3,7 +3,7 @@ import com.adobe.granite.ui.components.ds.DataSource; import com.adobe.granite.ui.components.ds.SimpleDataSource; import com.exadel.etoolbox.linkinspector.api.LinkResolver; -import com.exadel.etoolbox.linkinspector.core.services.util.ServletUtil; +import com.exadel.etoolbox.linkinspector.core.services.util.GraniteUtil; import org.apache.commons.lang3.StringUtils; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; @@ -18,9 +18,6 @@ import javax.servlet.Servlet; import java.util.*; -/** - * @author Sergey Soldatenko - */ @Component(service = {Servlet.class}) @SlingServletResourceTypes( resourceTypes = "/bin/etoolbox/link-inspector/settings/linktypes", @@ -45,7 +42,7 @@ protected void doGet(final SlingHttpServletRequest request, final SlingHttpServl fieldProperties.put("uncheckedValue", Boolean.FALSE); fieldProperties.put("checked", Boolean.FALSE); fieldProperties.put("value", Boolean.TRUE); - Resource checkBox = ServletUtil.createResource(request.getResourceResolver(), serviceName, fieldProperties, Collections.emptyList()); + Resource checkBox = GraniteUtil.createResource(request.getResourceResolver(), serviceName, fieldProperties, Collections.emptyList()); links.add(checkBox); } diff --git a/core/src/main/java/com/exadel/etoolbox/linkinspector/core/servlets/SettingsDataSourceServlet.java b/core/src/main/java/com/exadel/etoolbox/linkinspector/core/servlets/SettingsDataSourceServlet.java index 011e5e2f..b9fd3ced 100644 --- a/core/src/main/java/com/exadel/etoolbox/linkinspector/core/servlets/SettingsDataSourceServlet.java +++ b/core/src/main/java/com/exadel/etoolbox/linkinspector/core/servlets/SettingsDataSourceServlet.java @@ -1,13 +1,11 @@ package com.exadel.etoolbox.linkinspector.core.servlets; -/** - * @author Sergey Soldatenko - */ import com.adobe.granite.ui.components.ds.DataSource; import com.adobe.granite.ui.components.ds.SimpleDataSource; import com.exadel.etoolbox.linkinspector.api.LinkResolver; -import com.exadel.etoolbox.linkinspector.core.services.util.ServletUtil; +import com.exadel.etoolbox.linkinspector.core.services.util.GraniteUtil; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; @@ -62,10 +60,10 @@ protected void doGet(final SlingHttpServletRequest request, final SlingHttpServl fieldProperties.put("name", "./" + serviceName + "." + attributeDefinition.getID()); fieldProperties.put("fieldLabel", attributeDefinition.getName()); fieldProperties.put("fieldDescription", attributeDefinition.getDescription()); - Resource textField = ServletUtil.createResource(request.getResourceResolver(), serviceName + "." + attributeDefinition.getID(), fieldProperties, Collections.emptyList()); + Resource textField = GraniteUtil.createResource(request.getResourceResolver(), serviceName + "." + attributeDefinition.getID(), fieldProperties, Collections.emptyList()); innerFields.add(textField); } - Resource tab = ServletUtil.createTab(request.getResourceResolver(), serviceName, splitCamelCase(StringUtils.substringAfterLast(serviceName, ".")), innerFields); + Resource tab = GraniteUtil.createTab(request.getResourceResolver(), serviceName, splitCamelCase(StringUtils.substringAfterLast(serviceName, ".")), innerFields); tabs.add(tab); } @@ -99,7 +97,7 @@ private String getResourceTypeByType(int type) { private Map> getServiceSettings() { Map> result = new HashMap<>(); - for (LinkResolver linkResolver : linkResolvers) { + for (LinkResolver linkResolver : CollectionUtils.emptyIfNull(linkResolvers)) { Bundle bundle = FrameworkUtil.getBundle(linkResolver.getClass()); MetaTypeInformation metaTypeInformation = metaTypeService.getMetaTypeInformation(bundle); ObjectClassDefinition objectClassDefinition = metaTypeInformation.getObjectClassDefinition(linkResolver.getClass().getName(), null);