ActionManagerBridge.java 14.8 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.WCMException
 *  com.day.cq.wcm.api.msm.ActionConfig
 *  com.day.cq.wcm.api.msm.ActionManager
 *  com.day.cq.wcm.api.msm.LiveAction
 *  com.day.cq.wcm.api.msm.LiveCopy
 *  com.day.cq.wcm.api.msm.LiveRelationship
 *  com.day.cq.wcm.api.msm.LiveRelationshipManager
 *  com.day.cq.wcm.api.msm.RolloutManager
 *  com.day.cq.wcm.api.msm.RolloutManager$Trigger
 *  com.day.cq.wcm.msm.api.ActionConfig
 *  com.day.cq.wcm.msm.api.ActionManager
 *  com.day.cq.wcm.msm.api.LiveAction
 *  com.day.cq.wcm.msm.api.LiveRelationship
 *  com.day.cq.wcm.msm.api.LiveStatus
 *  com.day.cq.wcm.msm.api.RolloutManager
 *  com.day.cq.wcm.msm.api.RolloutManager$Trigger
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.io.JSONWriter
 *  org.osgi.framework.BundleContext
 *  org.osgi.framework.ServiceRegistration
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.msm.impl.compat;

import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.api.msm.LiveCopy;
import com.day.cq.wcm.api.msm.LiveRelationship;
import com.day.cq.wcm.api.msm.LiveRelationshipManager;
import com.day.cq.wcm.api.msm.RolloutManager;
import com.day.cq.wcm.msm.api.ActionConfig;
import com.day.cq.wcm.msm.api.ActionManager;
import com.day.cq.wcm.msm.api.LiveAction;
import com.day.cq.wcm.msm.api.LiveStatus;
import com.day.cq.wcm.msm.api.RolloutManager;
import com.day.cq.wcm.msm.impl.compat.ActionConfigAdaptor;
import com.day.cq.wcm.msm.impl.compat.LiveRelationshipManagerBridge;
import java.util.Collection;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service
public class ActionManagerBridge
implements com.day.cq.wcm.api.msm.ActionManager {
    @Reference
    private ActionManager actionManager = null;
    @Reference(referenceInterface=LiveRelationshipManager.class, target="(service.pid=com.day.cq.wcm.msm.impl.compat.LiveRelationshipManagerBridge)")
    private LiveRelationshipManagerBridge liveRelationshipMgr;
    private Logger log = LoggerFactory.getLogger(ActionManagerBridge.class);
    private ComponentContext componentContext;
    private final Map<String, ServiceRegistration> registered = new HashMap<String, ServiceRegistration>();

    @Activate
    protected void activate(ComponentContext context) {
        this.componentContext = context;
    }

    protected void bindLiveRelationshipMgr(LiveRelationshipManager relationMgr) {
        this.liveRelationshipMgr = (LiveRelationshipManagerBridge)relationMgr;
    }

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

    public void executeActions(ResourceResolver resolver, LiveRelationship relation, boolean autoSave, boolean isResetRollout) throws WCMException {
        com.day.cq.wcm.msm.api.LiveRelationship rel = this.liveRelationshipMgr.adapt(relation, resolver);
        if (rel != null) {
            RolloutManager.Trigger tr3 = relation.getLiveCopy().getTrigger();
            RolloutManager.Trigger trigger = tr3 == null ? null : RolloutManager.Trigger.fromName((String)tr3.name());
            this.actionManager.executeActions(resolver, rel, trigger, autoSave, isResetRollout);
        } else {
            this.log.debug("Didn't find Relation for {} to {}: no action executed", (Object)relation.getSourcePath(), (Object)relation.getTargetPath());
        }
    }

    public void executeAction(ResourceResolver resolver, LiveRelationship relation, com.day.cq.wcm.api.msm.ActionConfig config, boolean autoSave) throws WCMException {
        this.executeAction(resolver, relation, config, autoSave, false);
    }

    public void executeAction(ResourceResolver resolver, LiveRelationship relation, final com.day.cq.wcm.api.msm.ActionConfig config, boolean autoSave, boolean isResetRollout) throws WCMException {
        com.day.cq.wcm.msm.api.LiveRelationship rel = this.liveRelationshipMgr.adapt(relation, resolver);
        if (rel != null) {
            ActionConfig newActionCfg = config == null ? null : new ActionConfig(){

                public String getName() {
                    return config.getName();
                }

                public String getParameterName() {
                    return config.getParameterName();
                }

                public Map<String, String> getProperties() {
                    return config.getProperties();
                }

                public String getProperty(String propertyName) {
                    return config.getProperty(propertyName);
                }

                public boolean hasProperty(String propertyName) {
                    return config.hasProperty(propertyName);
                }

                public int getRank() {
                    return config.getRank();
                }

                public int compareTo(ActionConfig o) {
                    int cmp = this.getName().compareTo(config.getName());
                    return cmp != 0 ? this.getRank() - config.getRank() : 0;
                }

                public void write(JSONWriter out) throws JSONException {
                    config.write(out);
                }
            };
            this.actionManager.executeAction(resolver, rel, newActionCfg, autoSave, isResetRollout);
        } else {
            this.log.debug("Didn't find Relation for {} to {}: no action executed", (Object)relation.getSourcePath(), (Object)relation.getTargetPath());
        }
    }

    public Set<com.day.cq.wcm.api.msm.ActionConfig> getActionsConfig(Node node, String resourceNodeType, String actionNodeName, boolean inherited) throws RepositoryException {
        this.log.warn("Parameter resourceNodeType and actionNodeName are no longer supported, defaults are used {} resp {}", (Object)"cq:LiveSync", (Object)"cq:LiveSyncAction");
        if (node == null || node.getParent() == null) {
            return null;
        }
        HashSet<com.day.cq.wcm.api.msm.ActionConfig> ret = new HashSet<com.day.cq.wcm.api.msm.ActionConfig>();
        for (ActionConfig conf : this.actionManager.getActionsConfig(node.getParent())) {
            ret.add(new ActionConfigAdaptor(conf, inherited));
        }
        return ret;
    }

    public Set<com.day.cq.wcm.api.msm.ActionConfig> getActionsConfig(Resource resource, String resourceNodeType, String actionNodeName, boolean inherited) throws RepositoryException {
        return this.getActionsConfig((Node)resource.adaptTo(Node.class), resourceNodeType, actionNodeName, inherited);
    }

    public com.day.cq.wcm.api.msm.ActionConfig getActionConfig(Node root, boolean inherited) throws RepositoryException {
        if (root == null || root.getParent() == null) {
            return null;
        }
        Set ret = this.actionManager.getActionsConfig(root.getParent());
        for (ActionConfig conf : ret) {
            if (!conf.getName().equals(root.getName())) continue;
            this.log.debug("Found action configuration for Action {} at Node {}", (Object)conf.getName(), (Object)root.getName());
            return new ActionConfigAdaptor(conf, false);
        }
        return null;
    }

    public com.day.cq.wcm.api.msm.ActionConfig getActionConfig(Resource resource, boolean inherited) throws RepositoryException {
        return this.getActionConfig((Node)resource.adaptTo(Node.class), inherited);
    }

    public boolean isActionUpdated(SlingHttpServletRequest request, LiveRelationship relation) throws WCMException {
        try {
            Set configs = relation.getActions();
            HashMap<String, String> requestParameters = new HashMap<String, String>();
            HashSet<String> requestEmptyParameters = new HashSet<String>();
            for (LiveAction action : this.actionManager.getRegistredActions().values()) {
                String[] params;
                String paramName = action.getParameterName();
                for (String param : params = action.getPropertiesNames()) {
                    String value = request.getParameter(paramName + "/" + param);
                    if (value == null) continue;
                    if (!value.equals("")) {
                        requestParameters.put(paramName + "/" + param, value);
                        continue;
                    }
                    requestEmptyParameters.add(paramName + "/" + param);
                }
            }
            for (com.day.cq.wcm.api.msm.ActionConfig config : configs) {
                String configuredParam = config.getParameterName();
                Map properties = config.getProperties();
                if (properties == null) continue;
                for (String name : properties.keySet()) {
                    String value = (String)properties.get(name);
                    if (requestParameters.containsKey(configuredParam + "/" + name)) {
                        String postedValue = (String)requestParameters.get(configuredParam + "/" + name);
                        if (!postedValue.equalsIgnoreCase(value)) {
                            return true;
                        }
                        requestParameters.remove(configuredParam + "/" + name);
                        continue;
                    }
                    if (!requestEmptyParameters.contains(configuredParam + "/" + name) || value.equals("")) continue;
                    return true;
                }
            }
            return !requestParameters.isEmpty();
        }
        catch (Exception e) {
            throw new WCMException("Cannot write config from request", (Throwable)e);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void registerAcion(com.day.cq.wcm.api.msm.LiveAction action) {
        LiveActionAdaptor adapted = new LiveActionAdaptor(action);
        Map<String, ServiceRegistration> map = this.registered;
        synchronized (map) {
            this.registered.put(action.getName(), this.componentContext.getBundleContext().registerService(LiveAction.class.getName(), (Object)adapted, (Dictionary)new Properties()));
        }
        this.log.info("Registered LiveAction for {} that implements deprecated API", (Object)action.getName());
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void unregisterAcion(com.day.cq.wcm.api.msm.LiveAction action) {
        Map<String, ServiceRegistration> map = this.registered;
        synchronized (map) {
            ServiceRegistration reg = this.registered.remove(action.getName());
            if (reg != null) {
                reg.unregister();
            }
        }
    }

    public void writeConfigFromRequest(SlingHttpServletRequest request, Node actionsNode) throws WCMException {
        throw new UnsupportedOperationException("Config cant be written in CQ 5.3 format any longer");
    }

    protected void bindActionManager(ActionManager actionManager) {
        this.actionManager = actionManager;
    }

    protected void unbindActionManager(ActionManager actionManager) {
        if (this.actionManager == actionManager) {
            this.actionManager = null;
        }
    }

    protected void unbindLiveRelationshipMgr(LiveRelationshipManager liveRelationshipManager) {
        if (this.liveRelationshipMgr == liveRelationshipManager) {
            this.liveRelationshipMgr = null;
        }
    }

    class LiveActionAdaptor
    implements LiveAction {
        private final com.day.cq.wcm.api.msm.LiveAction delegatee;

        LiveActionAdaptor(com.day.cq.wcm.api.msm.LiveAction action) {
            this.delegatee = action;
        }

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

        public void write(JSONWriter out) throws JSONException {
        }

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

        public void execute(Resource source, Resource target, com.day.cq.wcm.msm.api.LiveRelationship relation, boolean autoSave, boolean isResetRollout) throws WCMException {
            this.execute(source.getResourceResolver(), relation, null, autoSave, isResetRollout);
        }

        public int getRank() {
            return this.delegatee.getRank();
        }

        public String[] getPropertiesNames() {
            return this.delegatee.getPropertiesNames();
        }

        public String getParameterName() {
            return this.delegatee.getParameterName();
        }

        public void execute(ResourceResolver resolver, com.day.cq.wcm.msm.api.LiveRelationship relation, ActionConfig config, boolean autoSave) throws WCMException {
            this.execute(resolver, relation, config, autoSave, false);
        }

        public void execute(ResourceResolver resolver, com.day.cq.wcm.msm.api.LiveRelationship relation, ActionConfig config, boolean autoSave, boolean isResetRollout) throws WCMException {
            Iterator<LiveRelationship> rels;
            LiveRelationship rel = null;
            if (relation.getStatus().isTargetExisting()) {
                rel = ActionManagerBridge.this.liveRelationshipMgr.getLiveRelationship(resolver.getResource(relation.getTargetPath()), false);
            } else if (relation.getStatus().isSourceExisting() && (rels = ActionManagerBridge.this.liveRelationshipMgr.getLiveRelationships(resolver.resolve(relation.getSourcePath()), null, new String[]{relation.getTargetPath()}, false).iterator()).hasNext()) {
                rel = rels.next();
            }
            this.delegatee.execute(resolver, rel, (com.day.cq.wcm.api.msm.ActionConfig)new ActionConfigAdaptor(config, false), autoSave, isResetRollout);
        }
    }

}