Skip to content

Commit

Permalink
Transfer LinkInspector settings from the 'OSGi console' to the 'Grani…
Browse files Browse the repository at this point in the history
…t' console. #47

-fixed git comments
  • Loading branch information
Saldatsenka, Siarhei committed Sep 16, 2024
1 parent e176441 commit ceb0d95
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -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<Resource> children) {

Resource items = createResource(resolver, path + "/items", Collections.emptyMap(), children);

Map<String, Object> 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<String, Object> properties, Collection<Resource> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,13 @@

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;
import org.apache.http.entity.ContentType;
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;

Expand Down Expand Up @@ -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<Resource> children) {

Resource items = createResource(resolver, path + "/items", Collections.emptyMap(), children);

Map<String, Object> 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<String, Object> properties, Collection<Resource> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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",
Expand All @@ -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);
}

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

Expand Down Expand Up @@ -99,7 +97,7 @@ private String getResourceTypeByType(int type) {

private Map<String, List<AttributeDefinition>> getServiceSettings() {
Map<String, List<AttributeDefinition>> 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);
Expand Down

0 comments on commit ceb0d95

Please sign in to comment.