All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.cq.editor.model.inplaceediting.config.ConfigWrapper Maven / Gradle / Ivy

/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2016 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 *
 **************************************************************************/
package com.adobe.cq.editor.model.inplaceediting.config;

import java.util.Iterator;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
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.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.RequestAttribute;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;

/**
 * Wrapper suitable for providing its children with the resource that actually contains a given IPE configuration
 *
 * The look-up of IPE configuration search for the requested resource in the following order:
 *
 * - design/policy
 * - component local
 * - component relocated
 */
@Model(adaptables = SlingHttpServletRequest.class)
public class ConfigWrapper {

    private static final String PN_POLICY_CONTENT_RESOURCE_TYPE = "policyContentResourceType";

    private static final String COMPONENT_RELATIVE_IN_PLACE_EDITING = "cq:editConfig/cq:inplaceEditing";
    private static final String COMPONENT_RELATIVE_IPE_CONFIG = COMPONENT_RELATIVE_IN_PLACE_EDITING + "/config";
    private static final String GRANITE_CONTENT_PATH_ATTRIBUTE = "granite.ui.form.contentpath";
    private static final String POLICY_RESOURCE_TYPE = "wcm/core/components/policy/policy";
    private static final String CONFIG_PATH = "configPath";

    @Self
    private SlingHttpServletRequest slingRequest;

    @Inject
    private ResourceResolver resourceResolver;

    /** Contextual resource */
    @Inject
    private Resource resource;

    private Resource designResource;

    @ValueMapValue(optional = true,
            name = CONFIG_PATH)
    private String configPath;

    @RequestAttribute(name = GRANITE_CONTENT_PATH_ATTRIBUTE)
    private String initialContentPath;

    @PostConstruct
    protected void initModel() {
        designResource = slingRequest.getRequestPathInfo().getSuffixResource();

        if (StringUtils.isEmpty(configPath)) {
            return;
        }

        setContentPath();
    }

    /**
     * Search for an IPE configuration set on the design or on the component
     *
     * If the design has no configuration and the component has one, sets the location of the configuration to the "granite.ui.form.contentpath" slingRequest attribute
     */
    private void setContentPath() {
        Resource ipeConfiguration;

        if (designResource != null) {
            ipeConfiguration = designResource.getChild(configPath);

            // There is a configuration at the design level
            // No need to change the content path
            if (ipeConfiguration != null) {
                return;
            }
        }

        Resource componentResource = getComponentResource(slingRequest);

        if (componentResource == null) {
            return;
        }

        setInheritedContentPath(componentResource);
    }

    private void setInheritedContentPath(Resource componentResource) {
        if (componentResource == null) {
            return;
        }

        // Look for the configuration on the component
        Resource ipeConfiguration = componentResource.getChild(COMPONENT_RELATIVE_IPE_CONFIG + "/" + configPath);

        if (ipeConfiguration != null) {
            // There is a configuration locally on the component
            slingRequest.setAttribute(GRANITE_CONTENT_PATH_ATTRIBUTE, componentResource.getPath() + "/" + COMPONENT_RELATIVE_IPE_CONFIG);
        } else {
            // Look for a relocated configuration
            Resource ipeResource = componentResource.getChild(COMPONENT_RELATIVE_IN_PLACE_EDITING);

            if (ipeResource != null) {
                ValueMap ipeProperties = ipeResource.getValueMap();

                String ipeConfigPath = ipeProperties.get(CONFIG_PATH, String.class);

                if (StringUtils.isNotEmpty(ipeConfigPath)) {
                    ipeConfiguration = resourceResolver.getResource(componentResource.getPath() + "/" + COMPONENT_RELATIVE_IN_PLACE_EDITING + "/" + ipeConfigPath + "/" + configPath);

                    if (ipeConfiguration != null) {
                        // There is a relocated configuration set on the component IPE node
                        slingRequest.setAttribute(GRANITE_CONTENT_PATH_ATTRIBUTE, componentResource.getPath() + "/" + COMPONENT_RELATIVE_IN_PLACE_EDITING + "/" + ipeConfigPath);
                    }
                }
            }
        }

        // Search of an IPE configuration on the super type component
        String componentResourceSuperType = componentResource.getResourceSuperType();

        if (StringUtils.isEmpty(componentResourceSuperType)) {
            return;
        }

        Resource parentComponent = resourceResolver.getResource(componentResourceSuperType);

        if (parentComponent == null) {
            return;
        }

        setInheritedContentPath(parentComponent);
    }

    /**
     * Returns the resource concerned by the given slingRequest
     *
     * @param request
     * @return
     */
    private Resource getComponentResource(SlingHttpServletRequest request) {
        RequestParameter policyContentResourceType = request.getRequestParameter(PN_POLICY_CONTENT_RESOURCE_TYPE);
        if (policyContentResourceType != null) {
            return resourceResolver.getResource(policyContentResourceType.getString());
        }

        RequestParameter[] resourceTypesRP = request.getRequestParameters("resourceType");
        if (resourceTypesRP == null) {
            return null;
        }

        for (RequestParameter resourceTypeRP : resourceTypesRP) {
            if (resourceTypeRP != null && !POLICY_RESOURCE_TYPE.equals(resourceTypeRP.getString())) {
                String targetComponentResourceType = resourceTypeRP.getString();
                return resourceResolver.getResource(targetComponentResourceType);
            }
        }

        return null;
    }

    public Iterator getChildren() {
        return resource.listChildren();
    }

    /**
     * Sets the "granite.ui.form.contentpath" slingRequest attribute back to its initial value
     */
    public void resetAttribute() {
        slingRequest.setAttribute(GRANITE_CONTENT_PATH_ATTRIBUTE, initialContentPath);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy