Search in sources :

Example 1 with Component

use of com.day.cq.wcm.api.components.Component 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 Component

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

the class SharedComponentPropertiesBindingsValuesProvider method addBindings.

@Override
public void addBindings(Bindings bindings) {
    Resource resource = (Resource) bindings.get("resource");
    Component component = WCMUtils.getComponent(resource);
    if (component != null) {
        if (pageRootProvider != null) {
            setSharedProperties(bindings, resource, component);
        } else {
            log.debug("Page Root Provider must be configured for shared component properties to be supported");
        }
        setMergedProperties(bindings, resource);
    }
}
Also used : Resource(org.apache.sling.api.resource.Resource) Component(com.day.cq.wcm.api.components.Component)

Example 3 with Component

use of com.day.cq.wcm.api.components.Component 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 4 with Component

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

the class WCMViewsFilter method getComponentViews.

/**
 * Get the WCM Views for the component; Looks at both the content resource for the special wcmViews property
 * and looks up to the resourceType's cq:Component properties for wcmViews.
 *
 * @param request the request
 * @return the WCM Views for the component
 */
private List<String> getComponentViews(final SlingHttpServletRequest request) {
    final Set<String> views = new HashSet<String>();
    final Resource resource = request.getResource();
    if (resource == null) {
        return new ArrayList<String>(views);
    }
    final Component component = WCMUtils.getComponent(resource);
    final ValueMap properties = resource.adaptTo(ValueMap.class);
    if (component != null) {
        views.addAll(Arrays.asList(component.getProperties().get(PN_WCM_VIEWS, new String[] {})));
    }
    if (properties != null) {
        views.addAll(Arrays.asList(properties.get(PN_WCM_VIEWS, new String[] {})));
    }
    return new ArrayList<String>(views);
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) Component(com.day.cq.wcm.api.components.Component) HashSet(java.util.HashSet)

Example 5 with Component

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

the class UrlFilter method findUrlFilterDefinitionComponent.

private Component findUrlFilterDefinitionComponent(Resource resource, ComponentManager componentManager) {
    if (resource == null) {
        return null;
    }
    Resource contentResource = resource.getChild("jcr:content");
    if (contentResource != null) {
        resource = contentResource;
    }
    Component component = componentManager.getComponentOfResource(resource);
    return findUrlFilterDefinitionComponent(component);
}
Also used : Resource(org.apache.sling.api.resource.Resource) Component(com.day.cq.wcm.api.components.Component)

Aggregations

Component (com.day.cq.wcm.api.components.Component)11 Resource (org.apache.sling.api.resource.Resource)5 ComponentManager (com.day.cq.wcm.api.components.ComponentManager)4 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)3 Page (com.day.cq.wcm.api.Page)2 HashMap (java.util.HashMap)2 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)2 ValueMap (org.apache.sling.api.resource.ValueMap)2 SlingBindings (org.apache.sling.api.scripting.SlingBindings)2 MockSlingHttpServletRequest (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest)2 PageRootProvider (com.adobe.acs.commons.wcm.PageRootProvider)1 LinkHandler (com.adobe.cq.wcm.core.components.internal.link.LinkHandler)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 RepositoryException (javax.jcr.RepositoryException)1 SimpleBindings (javax.script.SimpleBindings)1 JspException (javax.servlet.jsp.JspException)1 JspWriter (javax.servlet.jsp.JspWriter)1