BlueprintImpl.java 12.3 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.Filter
 *  com.day.cq.tagging.Tag
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageManager
 *  com.day.cq.wcm.api.Template
 *  com.day.cq.wcm.api.WCMException
 *  com.day.cq.wcm.msm.api.Blueprint
 *  com.day.cq.wcm.msm.api.RolloutConfig
 *  com.day.cq.wcm.msm.api.RolloutConfigManager
 *  com.day.text.Text
 *  javax.jcr.Node
 *  javax.jcr.PathNotFoundException
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  org.apache.commons.lang.ArrayUtils
 *  org.apache.commons.lang.StringUtils
 *  org.apache.jackrabbit.commons.JcrUtils
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.io.JSONWriter
 */
package com.day.cq.wcm.msm.impl;

import com.day.cq.commons.Filter;
import com.day.cq.tagging.Tag;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.Template;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.api.Blueprint;
import com.day.cq.wcm.msm.api.RolloutConfig;
import com.day.cq.wcm.msm.api.RolloutConfigManager;
import com.day.text.Text;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.jackrabbit.commons.JcrUtils;
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.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;

public class BlueprintImpl
implements Blueprint {
    private static final String STATEMENT = "select [jcr:path] from [cq:BlueprintSyncConfig] as a where isdescendantnode(a, '%s') order by [jcr:path] asc";
    private static final String[] EMPTY_STRING = new String[0];
    private final Page page;
    private final RolloutConfigManager rolloutConfigManager;
    private final String statement;

    BlueprintImpl(Page page, RolloutConfigManager rolloutConfigManager) {
        this.page = page;
        this.rolloutConfigManager = rolloutConfigManager;
        this.statement = String.format("select [jcr:path] from [cq:BlueprintSyncConfig] as a where isdescendantnode(a, '%s') order by [jcr:path] asc", this.getSitePath());
    }

    public String getSitePath() {
        return (String)this.getProperties().get("sitePath", String.class);
    }

    public List<RolloutConfig> getBlueprintRolloutConfig(String relPath, boolean exact) throws WCMException {
        BlueprintImpl.checkRelPath(relPath);
        String configPath = this.getAbsolutePath(relPath);
        ResourceResolver resolver = this.getResourceResolver();
        Resource parentResource = null;
        if (exact) {
            parentResource = resolver.getResource(configPath);
        } else {
            String path = this.getParentConfig(configPath);
            if (path != null) {
                parentResource = resolver.getResource(path);
            }
        }
        List<RolloutConfig> rolloutConfig = null;
        if (parentResource != null) {
            Resource configResource;
            if (parentResource.getChild("jcr:content") != null) {
                parentResource = parentResource.getChild("jcr:content");
            }
            if ((configResource = parentResource.getChild("cq:BlueprintSyncConfig")) != null) {
                try {
                    rolloutConfig = this.readRolloutConfig(configResource);
                }
                catch (RepositoryException e) {
                    throw new WCMException((Throwable)e);
                }
            }
        }
        return rolloutConfig;
    }

    private String getParentConfig(String absPath) throws WCMException {
        String hit = null;
        Iterator itr = this.getResourceResolver().queryResources(this.statement, "JCR-SQL2");
        while (itr.hasNext()) {
            String path = Text.getRelativeParent((String)((String)((Map)itr.next()).get("jcr:path")), (int)2);
            if (Text.isDescendantOrEqual((String)path, (String)absPath)) {
                hit = path;
            }
            if (path.length() <= absPath.length()) continue;
            break;
        }
        return hit;
    }

    public String getIconPath() {
        return null;
    }

    public String getThumbnailPath() {
        Resource thumb;
        Resource resource = this.page.getContentResource();
        if (resource != null && (thumb = resource.getChild("thumbnail")) != null) {
            return resource.getPath() + ".thumbnail.png";
        }
        return null;
    }

    public Long getRanking() {
        return (Long)this.getProperties().get("ranking", (Object)100000);
    }

    public void write(JSONWriter jsonWriter) throws JSONException {
        jsonWriter.object();
        String title = this.getTitle();
        if (title != null && title.length() > 0) {
            jsonWriter.key("title").value((Object)title);
        } else {
            jsonWriter.key("title").value((Object)Text.getName((String)this.getPath()));
        }
        jsonWriter.key("ranking").value((Object)this.getRanking());
        jsonWriter.key("hidden").value(this.getProperties().get("hidden", (Object)Boolean.FALSE));
        jsonWriter.key("sitePath").value((Object)this.getSitePath());
        String desc = this.getDescription();
        if (desc != null && desc.length() > 0) {
            jsonWriter.key("description").value((Object)desc);
        } else {
            jsonWriter.key("description").value((Object)"");
        }
        jsonWriter.key("path").value((Object)this.getPath());
        if (this.getThumbnailPath() != null) {
            jsonWriter.key("thumbnailPath").value((Object)this.getThumbnailPath());
        }
        if (this.getIconPath() != null) {
            jsonWriter.key("iconPath").value((Object)this.getIconPath());
        }
        jsonWriter.endObject();
    }

    public /* varargs */ List<RolloutConfig> setRolloutConfigs(String relPath, String ... rolloutConfigs) throws WCMException {
        BlueprintImpl.checkRelPath(relPath);
        ResourceResolver resolver = this.getResourceResolver();
        Resource toSet = resolver.getResource(this.getAbsolutePath(relPath));
        try {
            if (toSet == null || toSet.adaptTo(Node.class) == null) {
                throw new PathNotFoundException(String.format("%s/%s", this.getSitePath(), relPath));
            }
            this.writeConfig((Node)toSet.adaptTo(Node.class), rolloutConfigs);
            return this.readRolloutConfig(toSet);
        }
        catch (RepositoryException re) {
            throw new WCMException((Throwable)re);
        }
    }

    private String getAbsolutePath(String relPath) {
        return StringUtils.isEmpty((String)relPath) ? this.getSitePath() : String.format("%s/%s", this.getSitePath(), relPath);
    }

    private ResourceResolver getResourceResolver() throws WCMException {
        Resource resource = (Resource)this.page.adaptTo(Resource.class);
        if (resource == null) {
            throw new WCMException("Blueprint has no access to ResourceResolver");
        }
        return resource.getResourceResolver();
    }

    public String getPath() {
        return this.page.getPath();
    }

    public String getName() {
        return this.page.getName();
    }

    public String getTitle() {
        return this.page.getTitle();
    }

    public String getDescription() {
        return this.page.getDescription();
    }

    private static void checkRelPath(String relPath) {
        if (relPath != null && relPath.startsWith("/")) {
            throw new IllegalArgumentException("Given path is not a relative Path: " + relPath);
        }
    }

    private List<RolloutConfig> readRolloutConfig(Resource configResource) throws RepositoryException, WCMException {
        ArrayList<RolloutConfig> config = new ArrayList<RolloutConfig>();
        ValueMap map = (ValueMap)configResource.adaptTo(ValueMap.class);
        for (String path : (String[])map.get("cq:rolloutConfigs", (Object)EMPTY_STRING)) {
            RolloutConfig rc = this.rolloutConfigManager.getRolloutConfig(path);
            if (rc == null) continue;
            config.add(rc);
        }
        return config;
    }

    private /* varargs */ void writeConfig(Node sourceNode, String ... rolloutConfigs) throws RepositoryException {
        if (sourceNode.isNodeType("nt:hierarchyNode")) {
            sourceNode = JcrUtils.getOrAddNode((Node)sourceNode, (String)"jcr:content");
        }
        if (ArrayUtils.isEmpty((Object[])rolloutConfigs) && sourceNode.hasNode("cq:BlueprintSyncConfig")) {
            sourceNode.getNode("cq:BlueprintSyncConfig").remove();
        } else if (!ArrayUtils.isEmpty((Object[])rolloutConfigs)) {
            Node configNode = JcrUtils.getOrAddNode((Node)sourceNode, (String)"cq:BlueprintSyncConfig", (String)"cq:BlueprintSyncConfig");
            configNode.setProperty("cq:rolloutConfigs", rolloutConfigs);
        }
    }

    public PageManager getPageManager() {
        return this.page.getPageManager();
    }

    public Resource getContentResource() {
        return this.page.getContentResource();
    }

    public Resource getContentResource(String relPath) {
        return this.page.getContentResource(relPath);
    }

    public Iterator<Page> listChildren() {
        return this.page.listChildren();
    }

    public Iterator<Page> listChildren(Filter<Page> filter, boolean deep) {
        return this.page.listChildren(filter, deep);
    }

    public Iterator<Page> listChildren(Filter<Page> filter) {
        return this.listChildren(filter, false);
    }

    public boolean hasChild(String name) {
        return this.page.hasChild(name);
    }

    public int getDepth() {
        return this.page.getDepth();
    }

    public Page getParent() {
        return this.page.getParent();
    }

    public Page getParent(int level) {
        return this.page.getParent(level);
    }

    public Page getAbsoluteParent(int level) {
        return this.page.getAbsoluteParent(level);
    }

    public ValueMap getProperties() {
        return this.page.getProperties();
    }

    public ValueMap getProperties(String relPath) {
        return this.page.getProperties(relPath);
    }

    public String getPageTitle() {
        return this.page.getPageTitle();
    }

    public String getNavigationTitle() {
        return this.page.getNavigationTitle();
    }

    public boolean isHideInNav() {
        return this.page.isHideInNav();
    }

    public boolean hasContent() {
        return this.page.hasContent();
    }

    public boolean isValid() {
        return this.page.isValid();
    }

    public long timeUntilValid() {
        return this.page.timeUntilValid();
    }

    public Calendar getOnTime() {
        return this.page.getOnTime();
    }

    public Calendar getOffTime() {
        return this.page.getOffTime();
    }

    public String getLastModifiedBy() {
        return this.page.getLastModifiedBy();
    }

    public Calendar getLastModified() {
        return this.page.getLastModified();
    }

    public String getVanityUrl() {
        return this.page.getVanityUrl();
    }

    public Tag[] getTags() {
        return this.page.getTags();
    }

    public void lock() throws WCMException {
        this.page.lock();
    }

    public boolean isLocked() {
        return this.page.isLocked();
    }

    public String getLockOwner() {
        return this.page.getLockOwner();
    }

    public boolean canUnlock() {
        return this.page.canUnlock();
    }

    public void unlock() throws WCMException {
        this.page.unlock();
    }

    public Template getTemplate() {
        return this.page.getTemplate();
    }

    public Locale getLanguage(boolean ignoreContent) {
        return this.page.getLanguage(ignoreContent);
    }

    public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
        return (AdapterType)this.page.adaptTo(type);
    }

    public Calendar getDeleted() {
        return this.page.getDeleted();
    }

    public String getDeletedBy() {
        return this.page.getDeletedBy();
    }
}