LaunchLiveActionBase.java 8.72 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.WCMException
 *  com.day.cq.wcm.msm.api.ActionConfig
 *  com.day.cq.wcm.msm.api.LiveAction
 *  com.day.cq.wcm.msm.api.LiveRelationship
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.Property
 *  javax.jcr.PropertyIterator
 *  javax.jcr.RepositoryException
 *  javax.jcr.nodetype.NodeDefinition
 *  javax.jcr.nodetype.NodeType
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.api.wrappers.ValueMapDecorator
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.io.JSONWriter
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.wcm.launches.impl.msm.utils;

import com.adobe.cq.wcm.launches.impl.msm.utils.LaunchExclusionConfig;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.api.ActionConfig;
import com.day.cq.wcm.msm.api.LiveAction;
import com.day.cq.wcm.msm.api.LiveRelationship;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.nodetype.NodeDefinition;
import javax.jcr.nodetype.NodeType;
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.api.wrappers.ValueMapDecorator;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class LaunchLiveActionBase
implements LiveAction {
    public static final String PROP_TITLE = "cq.wcm.msm.action.title";
    public static final String PROP_RANK = "cq.wcm.msm.action.rank";
    public static final String PROP_PROPS = "cq.wcm.msm.action.properties";
    public static final String PROP_PARAMETER = "cq.wcm.msm.action.parameter";
    public static final String ACTION_TYPE = "actiontype";
    private final ValueMap config;
    private final LaunchExclusionConfig exclusionConfig;
    protected static final Logger log = LoggerFactory.getLogger(LaunchLiveActionBase.class);

    protected LaunchLiveActionBase(ValueMap config, LaunchExclusionConfig exclusionConfig) {
        this.config = config;
        this.exclusionConfig = exclusionConfig != null ? exclusionConfig : LaunchExclusionConfig.EMPTY_EXCLUSION_CONFIG;
    }

    protected ValueMap getConfig() {
        return this.config;
    }

    protected LaunchExclusionConfig getExclusionConfig() {
        return this.exclusionConfig;
    }

    protected boolean isPageLevel(Node node) throws RepositoryException {
        return node.isNodeType("cq:Page") || node.getParent().isNodeType("cq:Page");
    }

    protected boolean isExcludedNodeType(NodeType nodeType) {
        return this.exclusionConfig != null && this.exclusionConfig.matchExcludedNodeType(nodeType.getName());
    }

    protected boolean isExcludedProperty(Property property) throws RepositoryException {
        return this.exclusionConfig != null && (this.isPageLevel(property.getParent()) ? this.exclusionConfig.matchExcludedPageProperty(property.getName()) : this.exclusionConfig.matchExcludedParagraphItem(property.getName()));
    }

    protected boolean isExcludedNode(Node node) throws RepositoryException {
        if (node == null || node.getDefinition().isProtected()) {
            return true;
        }
        if (this.exclusionConfig.matchExcludedNodeType(node.getPrimaryNodeType().getName())) {
            return true;
        }
        for (NodeType mixin : node.getMixinNodeTypes()) {
            if (!this.exclusionConfig.matchExcludedNodeType(mixin.getName())) continue;
            return true;
        }
        return false;
    }

    protected void applyFilters(Node node, boolean deep) throws RepositoryException {
        String name = node.getName();
        String primaryNodeType = node.getPrimaryNodeType().getName();
        boolean pageNameIsExcluded = this.exclusionConfig.matchExcludedParagraphItem(name);
        boolean pageTypeIsExcluded = this.exclusionConfig.matchExcludedNodeType(primaryNodeType);
        if (pageNameIsExcluded || pageTypeIsExcluded) {
            node.remove();
            return;
        }
        for (NodeType mixin : node.getMixinNodeTypes()) {
            if (!this.exclusionConfig.matchExcludedNodeType(mixin.getName())) continue;
            node.removeMixin(mixin.getName());
        }
        boolean isPage = node.isNodeType("cq:Page");
        PropertyIterator properties = node.getProperties();
        while (properties.hasNext()) {
            Property property = properties.nextProperty();
            String propertyName = property.getName();
            boolean bl = isPage ? this.exclusionConfig.matchExcludedParagraphItem(propertyName) || this.exclusionConfig.matchExcludedPageProperty(propertyName) : this.exclusionConfig.matchExcludedParagraphItem(propertyName);
            boolean propertyIsExcluded = bl;
            if (!propertyIsExcluded) continue;
            property.remove();
        }
        if (node.hasNode("jcr:content")) {
            Node content = node.getNode("jcr:content");
            this.applyFilters(content, true);
        }
        if (deep) {
            NodeIterator children = node.getNodes();
            while (children.hasNext()) {
                Node child = children.nextNode();
                if ("jcr:content".equals(child.getName())) continue;
                this.applyFilters(child, deep);
            }
        }
    }

    protected abstract LiveAction newInstance(ValueMap var1);

    public String getName() {
        return this.getClass().getSimpleName();
    }

    protected abstract boolean handles(Resource var1, Resource var2, LiveRelationship var3, boolean var4) throws RepositoryException, WCMException;

    protected abstract void doExecute(Resource var1, Resource var2, LiveRelationship var3, boolean var4, boolean var5) throws RepositoryException, WCMException;

    public void execute(Resource source, Resource target, LiveRelationship relation, boolean autoSave, boolean isResetRollout) throws WCMException {
        try {
            if (this.handles(source, target, relation, isResetRollout)) {
                this.doExecute(source, target, relation, autoSave, isResetRollout);
            } else {
                log.debug("{} did not act on request to roll-out {} to {}: but precondition was not met", (Object[])new String[]{this.getName(), relation.getSourcePath(), relation.getTargetPath()});
            }
        }
        catch (RepositoryException e) {
            throw new WCMException((Throwable)e);
        }
    }

    public void execute(ResourceResolver resolver, LiveRelationship relation, ActionConfig config, boolean autoSave) throws WCMException {
        this.execute(resolver, relation, config, autoSave, false);
    }

    public void execute(ResourceResolver resolver, LiveRelationship relation, ActionConfig config, boolean autoSave, boolean isResetRollout) throws WCMException {
        this.newInstance((ValueMap)new ValueMapDecorator(new HashMap<K, V>(config.getProperties()))).execute(resolver.getResource(relation.getSourcePath()), resolver.getResource(relation.getTargetPath()), relation, autoSave, isResetRollout);
    }

    public int getRank() {
        return (Integer)this.getConfig().get("cq.wcm.msm.action.rank", (Object)Integer.MAX_VALUE);
    }

    public String[] getPropertiesNames() {
        HashSet<String> ret = new HashSet<String>();
        for (String key : this.getConfig().keySet()) {
            if (key == null || key.matches("^(cq|sling|jcr):.*")) continue;
            ret.add(key);
        }
        return ret.toArray(new String[ret.size()]);
    }

    public String getParameterName() {
        return (String)this.getConfig().get("cq.wcm.msm.action.parameter", (Object)"");
    }

    public String getTitle() {
        ValueMap config = this.getConfig();
        return (String)config.get("jcr:title", config.get("cq.wcm.msm.action.title", (Object)this.getName()));
    }

    public void write(JSONWriter jsonWriter) throws JSONException {
        jsonWriter.object();
        jsonWriter.key("name").value((Object)this.getName());
        jsonWriter.key("parameter").value((Object)this.getParameterName());
        jsonWriter.key("title").value((Object)this.getTitle());
        jsonWriter.key("rank").value((long)this.getRank());
        jsonWriter.key("properites");
        jsonWriter.array();
        for (String prop : this.getPropertiesNames()) {
            jsonWriter.value((Object)prop);
        }
        jsonWriter.endArray();
        jsonWriter.endObject();
    }
}