Search in sources :

Example 1 with ComponentManager

use of com.day.cq.wcm.api.components.ComponentManager in project acs-aem-commons by Adobe-Consulting-Services.

the class SharedComponentPropertiesBindingsValuesProviderTest method setUp.

@Before
public void setUp() throws Exception {
    resource = mock(Resource.class);
    pageRootProvider = mock(PageRootProvider.class);
    page = mock(Page.class);
    bindings = new SimpleBindings();
    component = mock(Component.class);
    sharedPropsResource = mock(Resource.class);
    globalPropsResource = mock(Resource.class);
    resourceResolver = mock(ResourceResolver.class);
    componentManager = mock(ComponentManager.class);
    String globalPropsPath = SITE_ROOT + "/jcr:content/" + SharedComponentProperties.NN_GLOBAL_COMPONENT_PROPERTIES;
    String sharedPropsPath = SITE_ROOT + "/jcr:content/" + SharedComponentProperties.NN_SHARED_COMPONENT_PROPERTIES + "/" + RESOURCE_TYPE;
    bindings.put("resource", resource);
    when(resource.getResourceResolver()).thenReturn(resourceResolver);
    when(resourceResolver.getResource(sharedPropsPath)).thenReturn(sharedPropsResource);
    when(resourceResolver.getResource(globalPropsPath)).thenReturn(globalPropsResource);
    when(resourceResolver.adaptTo(ComponentManager.class)).thenReturn(componentManager);
    when(componentManager.getComponentOfResource(resource)).thenReturn(component);
    when(page.getPath()).thenReturn(SITE_ROOT);
    when(pageRootProvider.getRootPage(resource)).thenReturn(page);
    when(component.getResourceType()).thenReturn(RESOURCE_TYPE);
    when(sharedPropsResource.getName()).thenReturn("Shared Properties Resource");
    when(globalPropsResource.getName()).thenReturn("Global Properties Resource");
    sharedProps = new ValueMapDecorator(new HashMap<String, Object>());
    globalProps = new ValueMapDecorator(new HashMap<String, Object>());
    sharedProps.put("shared", "value");
    globalProps.put("global", "value");
    when(globalPropsResource.getValueMap()).thenReturn(globalProps);
    when(sharedPropsResource.getValueMap()).thenReturn(sharedProps);
}
Also used : HashMap(java.util.HashMap) SimpleBindings(javax.script.SimpleBindings) Resource(org.apache.sling.api.resource.Resource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ComponentManager(com.day.cq.wcm.api.components.ComponentManager) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) Page(com.day.cq.wcm.api.Page) Component(com.day.cq.wcm.api.components.Component) PageRootProvider(com.adobe.acs.commons.wcm.PageRootProvider) Before(org.junit.Before)

Example 2 with ComponentManager

use of com.day.cq.wcm.api.components.ComponentManager in project acs-aem-commons by Adobe-Consulting-Services.

the class SharedComponentPropertiesPageInfoProvider method updateSharedComponentsMap.

/**
 * Traverse the entire set of components in the /apps directory and create a map of all component types
 * that have shared/global config dialogs.
 *
 * This is used by the JS libs in the authoring interface to determine if a component should show the
 * options for editing shared/global configs.
 */
private void updateSharedComponentsMap() {
    ResourceResolver resourceResolver = null;
    try {
        log.debug("Calculating map of components with shared properties dialogs");
        Map<String, Object> authInfo = Collections.singletonMap(ResourceResolverFactory.SUBSERVICE, (Object) SERVICE_NAME);
        resourceResolver = resourceResolverFactory.getServiceResourceResolver(authInfo);
        resourceResolver.refresh();
        ComponentManager componentManager = resourceResolver.adaptTo(ComponentManager.class);
        Map<String, List<Boolean>> localComponentsWithSharedProperties = new HashMap<>();
        for (Component component : componentManager.getComponents()) {
            if (component.getPath().startsWith("/apps")) {
                boolean hasSharedDialogForTouch = componentHasTouchDialog(component, "dialogshared");
                boolean hasGlobalDialogForTouch = componentHasTouchDialog(component, "dialogglobal");
                boolean hasSharedDialogForClassic = componentHasClassicDialog(component, "dialog_shared");
                boolean hasGlobalDialogForClassic = componentHasClassicDialog(component, "dialog_global");
                if (hasSharedDialogForTouch || hasGlobalDialogForTouch || hasSharedDialogForClassic || hasGlobalDialogForClassic) {
                    localComponentsWithSharedProperties.put(component.getResourceType(), Arrays.asList(hasSharedDialogForTouch, hasGlobalDialogForTouch, hasSharedDialogForClassic, hasGlobalDialogForClassic));
                }
            }
        }
        componentsWithSharedProperties = Collections.unmodifiableMap(localComponentsWithSharedProperties);
        log.debug("Calculated map of components with shared properties dialogs: {}", componentsWithSharedProperties);
    } catch (org.apache.sling.api.resource.LoginException e) {
        log.error("Unable to log into service user to determine list of components with shared properties dialogs", e);
    } catch (RepositoryException e) {
        log.error("Unexpected error attempting to determine list of components with shared properties dialogs", e);
    } finally {
        if (resourceResolver != null) {
            resourceResolver.close();
        }
    }
}
Also used : HashMap(java.util.HashMap) RepositoryException(javax.jcr.RepositoryException) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ComponentManager(com.day.cq.wcm.api.components.ComponentManager) JSONObject(org.apache.sling.commons.json.JSONObject) List(java.util.List) Component(com.day.cq.wcm.api.components.Component)

Example 3 with ComponentManager

use of com.day.cq.wcm.api.components.ComponentManager in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class AdaptiveImageServlet method getContentPolicy.

/**
 * Returns the content policy bound to the given component.
 *
 * @param imageResource the resource identifying the accessed image component
 * @return the content policy. May be {@code nulll} in case no content policy can be found.
 */
private ContentPolicy getContentPolicy(@NotNull Resource imageResource) {
    ResourceResolver resourceResolver = imageResource.getResourceResolver();
    ContentPolicyManager policyManager = resourceResolver.adaptTo(ContentPolicyManager.class);
    if (policyManager != null) {
        ComponentManager componentManager = resourceResolver.adaptTo(ComponentManager.class);
        if (componentManager != null) {
            com.day.cq.wcm.api.components.Component component = componentManager.getComponentOfResource(imageResource);
            if (component != null && component.getProperties() != null) {
                String delegatingResourceType = component.getProperties().get(AbstractImageDelegatingModel.IMAGE_DELEGATE, String.class);
                if (StringUtils.isNotEmpty(delegatingResourceType)) {
                    imageResource = new CoreResourceWrapper(imageResource, delegatingResourceType);
                }
            }
        }
        return policyManager.getPolicy(imageResource);
    } else {
        LOGGER.warn("Could not get policy manager from resource resolver!");
    }
    return null;
}
Also used : ContentPolicyManager(com.day.cq.wcm.api.policies.ContentPolicyManager) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ComponentManager(com.day.cq.wcm.api.components.ComponentManager) CoreResourceWrapper(com.adobe.cq.wcm.core.components.internal.resource.CoreResourceWrapper)

Example 4 with ComponentManager

use of com.day.cq.wcm.api.components.ComponentManager in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class AbstractContainerImpl method getChildren.

/**
 * Return (and cache) the list of children resources that are components
 *
 * @return List of all children resources that are components.
 */
@NotNull
protected List<Resource> getChildren() {
    if (childComponents == null) {
        Resource effectiveResource = this.getEffectiveResource();
        childComponents = Optional.ofNullable(request.getResourceResolver().adaptTo(ComponentManager.class)).map(componentManager -> StreamSupport.stream(effectiveResource.getChildren().spliterator(), false).filter(res -> Objects.nonNull(componentManager.getComponentOfResource(res)))).orElseGet(Stream::empty).collect(Collectors.toList());
    }
    return childComponents;
}
Also used : ComponentManager(com.day.cq.wcm.api.components.ComponentManager) Arrays(java.util.Arrays) ModelFactory(org.apache.sling.models.factory.ModelFactory) LinkHandler(com.adobe.cq.wcm.core.components.internal.link.LinkHandler) ArrayUtils(org.apache.commons.lang3.ArrayUtils) StringUtils(org.apache.commons.lang3.StringUtils) Container(com.adobe.cq.wcm.core.components.models.Container) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) LinkedHashMap(java.util.LinkedHashMap) Style(com.day.cq.wcm.api.designer.Style) ScriptVariable(org.apache.sling.models.annotations.injectorspecific.ScriptVariable) Map(java.util.Map) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) ContainerData(com.adobe.cq.wcm.core.components.models.datalayer.ContainerData) StreamSupport(java.util.stream.StreamSupport) ComponentExporter(com.adobe.cq.export.json.ComponentExporter) Link(com.adobe.cq.wcm.core.components.commons.link.Link) LinkedList(java.util.LinkedList) TemplatedResource(com.day.cq.wcm.api.TemplatedResource) ListItem(com.adobe.cq.wcm.core.components.models.ListItem) Resource(org.apache.sling.api.resource.Resource) Self(org.apache.sling.models.annotations.injectorspecific.Self) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Stream(java.util.stream.Stream) InjectionStrategy(org.apache.sling.models.annotations.injectorspecific.InjectionStrategy) DataLayerBuilder(com.adobe.cq.wcm.core.components.models.datalayer.builder.DataLayerBuilder) Optional(java.util.Optional) NotNull(org.jetbrains.annotations.NotNull) SlingModelFilter(com.adobe.cq.export.json.SlingModelFilter) AbstractComponentImpl(com.adobe.cq.wcm.core.components.util.AbstractComponentImpl) OSGiService(org.apache.sling.models.annotations.injectorspecific.OSGiService) TemplatedResource(com.day.cq.wcm.api.TemplatedResource) Resource(org.apache.sling.api.resource.Resource) ComponentManager(com.day.cq.wcm.api.components.ComponentManager) Stream(java.util.stream.Stream) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with ComponentManager

use of com.day.cq.wcm.api.components.ComponentManager in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class Editor method readChildren.

private void readChildren() {
    items = new ArrayList<>();
    String containerPath = request.getRequestPathInfo().getSuffix();
    if (StringUtils.isNotEmpty(containerPath)) {
        ResourceResolver resolver = request.getResourceResolver();
        container = resolver.getResource(containerPath);
        if (container != null) {
            ComponentManager componentManager = request.getResourceResolver().adaptTo(ComponentManager.class);
            if (componentManager != null) {
                for (Resource resource : container.getChildren()) {
                    if (resource != null) {
                        Component component = componentManager.getComponentOfResource(resource);
                        if (component != null) {
                            items.add(new Item(request, resource));
                        }
                    }
                }
            }
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ComponentManager(com.day.cq.wcm.api.components.ComponentManager) Resource(org.apache.sling.api.resource.Resource) Component(com.day.cq.wcm.api.components.Component)

Aggregations

ComponentManager (com.day.cq.wcm.api.components.ComponentManager)6 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)5 Resource (org.apache.sling.api.resource.Resource)4 Component (com.day.cq.wcm.api.components.Component)3 HashMap (java.util.HashMap)2 List (java.util.List)2 PageRootProvider (com.adobe.acs.commons.wcm.PageRootProvider)1 ComponentExporter (com.adobe.cq.export.json.ComponentExporter)1 SlingModelFilter (com.adobe.cq.export.json.SlingModelFilter)1 Link (com.adobe.cq.wcm.core.components.commons.link.Link)1 LinkHandler (com.adobe.cq.wcm.core.components.internal.link.LinkHandler)1 CoreResourceWrapper (com.adobe.cq.wcm.core.components.internal.resource.CoreResourceWrapper)1 Container (com.adobe.cq.wcm.core.components.models.Container)1 ListItem (com.adobe.cq.wcm.core.components.models.ListItem)1 ContainerData (com.adobe.cq.wcm.core.components.models.datalayer.ContainerData)1 DataLayerBuilder (com.adobe.cq.wcm.core.components.models.datalayer.builder.DataLayerBuilder)1 AbstractComponentImpl (com.adobe.cq.wcm.core.components.util.AbstractComponentImpl)1 ExpressionCustomizer (com.adobe.granite.ui.components.ExpressionCustomizer)1 RenderCondition (com.adobe.granite.ui.components.rendercondition.RenderCondition)1 SimpleRenderCondition (com.adobe.granite.ui.components.rendercondition.SimpleRenderCondition)1