TemplateConfImpl.java 7.32 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.core.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TemplateConfImpl {
    private static final Logger log = LoggerFactory.getLogger(TemplateConfImpl.class);
    private final Resource originalResource;
    private final ResourceResolver configResolver;

    public TemplateConfImpl(Resource resource) {
        this.originalResource = resource;
        this.configResolver = resource != null ? resource.getResourceResolver() : null;
    }

    public ValueMap getItem(String itemName) {
        Resource res = this.getItemResource(itemName);
        Resource valueMapRes = this.handleJcrContent(res);
        if (res == null) {
            return ValueMap.EMPTY;
        }
        if (log.isTraceEnabled() && "jcr:content".equals(valueMapRes.getName())) {
            log.trace("- using jcr:content child for ValueMap: {}", (Object)valueMapRes.getPath());
        }
        return valueMapRes.getValueMap();
    }

    public Resource getItemResource(String itemName) {
        if (this.originalResource == null) {
            return null;
        }
        if (log.isTraceEnabled()) {
            log.trace("- searching for item '{}'", (Object)itemName);
        }
        int idx = 1;
        for (String path : this.getPaths()) {
            Resource item = this.getItem(path, itemName);
            if (item != null) {
                if (log.isTraceEnabled()) {
                    log.trace("+ resolved config item at [{}]: {}", (Object)idx, (Object)item.getPath());
                }
                return item;
            }
            if (log.isTraceEnabled()) {
                log.trace("- no item '{}' under config '{}'", (Object)itemName, (Object)path);
            }
            ++idx;
        }
        if (log.isTraceEnabled()) {
            log.trace("- could not resolve any config item for '{}' (or no permissions to read it)", (Object)itemName);
        }
        return null;
    }

    public List<ValueMap> getList(String parentItemName) {
        List<Resource> resources = this.getListResources(parentItemName);
        ArrayList<ValueMap> maps = new ArrayList<ValueMap>(resources.size());
        for (Resource res : resources) {
            Resource valueMapRes = this.handleJcrContent(res);
            if (log.isTraceEnabled() && "jcr:content".equals(valueMapRes.getName())) {
                log.trace("- using jcr:content child for ValueMap: {}", (Object)valueMapRes.getPath());
            }
            maps.add(valueMapRes.getValueMap());
        }
        return maps;
    }

    public List<Resource> getListResources(String parentItemName) {
        if (log.isTraceEnabled()) {
            log.trace("- searching for list '{}'", (Object)parentItemName);
        }
        ArrayList<Resource> result = new ArrayList<Resource>();
        TreeMap<String, Resource> resourceMap = new TreeMap<String, Resource>();
        if (this.configResolver == null || this.originalResource == null) {
            return result;
        }
        for (String path : this.getPaths()) {
            Resource parentItem = this.getItem(path, parentItemName);
            if (parentItem == null) continue;
            for (Resource item : parentItem.getChildren()) {
                String itemKey = parentItemName + "/" + item.getName();
                if (resourceMap.containsKey(itemKey)) continue;
                resourceMap.put(itemKey, item);
            }
        }
        for (Map.Entry entry : resourceMap.entrySet()) {
            Resource res = (Resource)entry.getValue();
            result.add(res);
        }
        Collections.reverse(result);
        return result;
    }

    public List<String> getPaths() {
        ArrayList<String> paths = new ArrayList<String>();
        Resource conf = this.originalResource;
        while (conf != null) {
            boolean hasSettingNode = conf.getChild("settings") != null;
            boolean pathIsPartOfSettingNode = conf.getPath().indexOf("/settings/") > 0;
            String confPath = null;
            if (hasSettingNode) {
                confPath = conf.getPath() + "/";
                if (confPath != null && !confPath.equals("/conf/global")) {
                    if (log.isTraceEnabled()) {
                        log.trace("- [{}] parent config   => {}", (Object)paths.size(), (Object)confPath);
                    }
                    paths.add(confPath + "/");
                }
                conf = conf.getParent();
                continue;
            }
            if (pathIsPartOfSettingNode) {
                confPath = conf.getPath().substring(0, conf.getPath().indexOf("/settings"));
                if (confPath != null && !confPath.equals("/conf/global")) {
                    if (log.isTraceEnabled()) {
                        log.trace("- [{}] parent config   => {}", (Object)paths.size(), (Object)confPath);
                    }
                    paths.add(confPath + "/");
                    String parentPath = confPath.substring(0, confPath.lastIndexOf("/"));
                    while (!parentPath.equals("/conf")) {
                        if (log.isTraceEnabled()) {
                            log.trace("- [{}] parent config   => {}", (Object)paths.size(), (Object)parentPath);
                        }
                        paths.add(parentPath + "/");
                        parentPath = parentPath.substring(0, parentPath.lastIndexOf("/"));
                    }
                }
                conf = null;
                continue;
            }
            conf = null;
        }
        paths.add("/conf/global/");
        if (log.isTraceEnabled()) {
            log.trace("- [{}] apps config => {}", (Object)paths.size(), (Object)"/conf/global");
        }
        paths.add("/apps/");
        if (log.isTraceEnabled()) {
            log.trace("- [{}] apps config => {}", (Object)paths.size(), (Object)"/apps");
        }
        paths.add("/libs/");
        if (log.isTraceEnabled()) {
            log.trace("- [{}] libs config => {}", (Object)paths.size(), (Object)"/libs");
        }
        return Collections.unmodifiableList(paths);
    }

    private Resource handleJcrContent(Resource resource) {
        Resource jcrContent;
        if (resource != null && (jcrContent = resource.getChild("jcr:content")) != null) {
            resource = jcrContent;
        }
        return resource;
    }

    private Resource getItem(String path, String itemRelativePath) {
        Resource confBase = this.configResolver.getResource(path);
        Resource item = null;
        item = confBase == null ? this.configResolver.getResource(path + "/" + "settings" + "/" + itemRelativePath) : confBase.getChild("settings/" + itemRelativePath);
        if (item == null && log.isTraceEnabled()) {
            log.trace("- missing config '{}' (or no permissions to read it)", (Object)path);
        }
        return item;
    }
}