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

com.adobe.aemds.guide.utils.GuideValueMapResource Maven / Gradle / Ivy

/*******************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2017 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 may be covered by U.S. and Foreign Patents,
 * patents in process, 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.aemds.guide.utils;

import com.adobe.granite.ui.components.ds.ValueMapResource;
import org.apache.commons.lang.StringUtils;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceMetadata;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;

import javax.jcr.Node;
import java.util.Collection;
import java.util.Collections;

/**
 * An extension of ValueMapResource implementation that can return parent resource of this synthetic resource if passed
 * through its constructor.
 */
public class GuideValueMapResource extends ValueMapResource {

    private Resource parentResource;

    /**
     * Creates a GuideValueMapResource based on provided parameters
     * @param resourceResolver The resource resolver
     * @param path The resource path
     * @param resourceType The resource type
     * @param vm The properties of the resource
     * @param parentResource parent resource of this synthetic resource.
     */
    public GuideValueMapResource(ResourceResolver resourceResolver, String path, String resourceType, ValueMap vm, Resource parentResource) {
        super(resourceResolver, path, resourceType, vm);
        this.parentResource = parentResource;
    }

    /**
     * Creates a GuideValueMapResource based on provided parameters, with the ability to define the children and its parent.
     * @param resourceResolver The resource resolver
     * @param path The resource path
     * @param resourceType The resource type
     * @param vm The properties of the resource
     * @param children The children of the resource
     * @param parentResource parent resource of this synthetic resource.
     */
    public GuideValueMapResource(ResourceResolver resourceResolver, String path, String resourceType, ValueMap vm, Collection children, Resource parentResource) {
        super(resourceResolver, path, resourceType, vm, children);
        this.parentResource = parentResource;
    }

    /**
     * Creates a GuideValueMapResource based on provided parameters
     * @param resourceResolver The resource resolver
     * @param rm The resource meta data
     * @param resourceType The resource type
     * @param vm The properties of the resource
     * @param parentResource parent resource of this synthetic resource.
     */

    public GuideValueMapResource(ResourceResolver resourceResolver, ResourceMetadata rm, String resourceType, ValueMap vm, Resource parentResource) {
        super(resourceResolver, rm, resourceType, vm);
        this.parentResource = parentResource;
    }

    /**
     * Creates a ValueMapResource based on provided parameters, with the ability to define the children
     * @param resourceResolver The resource resolver
     * @param rm The resource meta data
     * @param resourceType The resource type
     * @param vm The properties of the resource
     * @param children The children of the resource
     * @param parentResource parent resource of this synthetic resource.
     */
    public GuideValueMapResource(ResourceResolver resourceResolver, ResourceMetadata rm, String resourceType, ValueMap vm, Collection children, Resource parentResource) {
        super(resourceResolver, rm, resourceType, vm, children);
        this.parentResource = parentResource;
    }

    @SuppressWarnings("unchecked")
    @Override
    public  Type adaptTo(Class type) {
        if(Node.class.equals(type)){
            String master = super.getValueMap().get("path", "");
            if(StringUtils.isNotBlank(master)){
                return super.getResourceResolver().resolve(master).adaptTo(type);
            }
        }
        return super.adaptTo(type);
    }

    /**
     * @see org.apache.sling.api.resource.Resource#getChild(String)
     */
    @Override
    public Resource getChild(String relPath) {
        String[] split = StringUtils.split(relPath, "/");
        Resource retVal = this;
        for(String str : split) {
            Iterable children = Collections.EMPTY_LIST;
            if(retVal != null) {
                children = retVal.getChildren();
            }
            for (Resource child : children) {
                if (StringUtils.equals(child.getPath(), retVal.getPath() + "/" + str)) {
                    retVal = child;
                }
            }
            if(retVal != null && !StringUtils.equals(Text.getName(retVal.getPath()), str)){
                retVal = null;
            }
        }
        return retVal;
    }


    @Override
    public Resource getParent() {
        if (this.parentResource == null){
            return super.getParent();
        }
        return this.parentResource;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy