ConfigurationImpl.java 5.01 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.inherit.InheritanceValueMap
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.Template
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 */
package com.day.cq.wcm.webservicesupport.impl;

import com.day.cq.commons.inherit.InheritanceValueMap;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.Template;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.impl.MergedInheritanceValueMap;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Iterator;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;

public class ConfigurationImpl
implements Configuration {
    private static final String[] MERGED_PATH_SEGMENTS = new String[]{"jcr:content", "jcr:content/public"};
    private Resource resource;
    private Node node;

    public ConfigurationImpl(Resource resource) {
        this.resource = "jcr:content".equals(resource.getName()) ? resource.getParent() : resource;
        this.node = (Node)this.resource.adaptTo(Node.class);
    }

    @Override
    public String getTitle() {
        return (String)this.getProperties().get("jcr:title", String.class);
    }

    @Override
    public String getDescription() {
        return (String)this.getProperties().get("jcr:description", String.class);
    }

    @Override
    public String getName() {
        return this.resource.getName();
    }

    @Override
    public String getPath() {
        return this.resource.getPath();
    }

    @Override
    public Long getLastModified() {
        Calendar mod = (Calendar)this.getProperties().get("cq:lastModified", Calendar.class);
        if (mod != null) {
            return mod.getTimeInMillis();
        }
        mod = (Calendar)this.getProperties().get("jcr:lastModified", Calendar.class);
        return mod != null ? mod.getTimeInMillis() : 0;
    }

    @Override
    public String getIconPath() {
        try {
            if (this.node.hasNode("icon.png")) {
                return this.getPath() + "/" + "icon.png";
            }
        }
        catch (RepositoryException var1_1) {
            // empty catch block
        }
        return null;
    }

    @Override
    public String getThumbnailPath() {
        try {
            if (this.node.hasNode("thumbnail.png")) {
                return this.getPath() + "/" + "thumbnail.png";
            }
        }
        catch (RepositoryException var1_1) {
            // empty catch block
        }
        return null;
    }

    @Override
    public Template getTemplate() {
        Page page = (Page)this.resource.adaptTo(Page.class);
        return page != null ? page.getTemplate() : null;
    }

    @Override
    public Resource getParent() {
        return this.resource.getParent();
    }

    @Override
    public Resource getResource() {
        return this.resource;
    }

    @Override
    public Resource getContentResource() {
        Resource contentResource = this.resource.getResourceResolver().getResource(this.resource, "jcr:content");
        if (contentResource == null && this.isPublicOnlyConfiguration()) {
            return this.resource;
        }
        return contentResource;
    }

    @Override
    public InheritanceValueMap getProperties() {
        return this.getInheritanceValueMap(this.getPath());
    }

    @Override
    public <T> T get(String name, T defaultValue) {
        InheritanceValueMap props = this.getProperties();
        if (props == null) {
            return defaultValue;
        }
        return (T)props.get(name, defaultValue);
    }

    @Override
    public <T> T getInherited(String name, T defaultValue) {
        InheritanceValueMap props = this.getProperties();
        if (props == null) {
            return defaultValue;
        }
        return (T)props.getInherited(name, defaultValue);
    }

    @Override
    public Iterator<Configuration> listChildren() {
        HashSet<Configuration> entries = new HashSet<Configuration>();
        Iterator iter = this.resource.listChildren();
        while (iter.hasNext()) {
            Configuration cfg = (Configuration)((Resource)iter.next()).adaptTo(Configuration.class);
            if (cfg == null) continue;
            entries.add(cfg);
        }
        return entries.iterator();
    }

    private boolean isPublicOnlyConfiguration() {
        return this.resource.getName().equals("public");
    }

    private InheritanceValueMap getInheritanceValueMap(String path) {
        if (ResourceUtil.getName((String)path).equals("public")) {
            path = ResourceUtil.getParent((String)path, (int)2);
        }
        return new MergedInheritanceValueMap(this.resource.getResourceResolver(), path, MERGED_PATH_SEGMENTS, "/etc/cloudservices");
    }
}