LiveCommand.java 10.6 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap
 *  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.api.commands.WCMCommand
 *  com.day.cq.wcm.msm.api.LiveRelationship
 *  com.day.cq.wcm.msm.api.LiveRelationshipManager
 *  com.day.cq.wcm.msm.api.RolloutConfig
 *  com.day.cq.wcm.msm.api.RolloutConfigManager
 *  com.day.cq.wcm.msm.api.RolloutManager
 *  com.day.text.Text
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.commons.lang.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.request.RequestParameter
 *  org.apache.sling.api.request.RequestParameterMap
 *  org.apache.sling.api.resource.NonExistingResource
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.msm.impl.commands;

import com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap;
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.api.commands.WCMCommand;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.day.cq.wcm.msm.api.RolloutConfig;
import com.day.cq.wcm.msm.api.RolloutConfigManager;
import com.day.cq.wcm.msm.api.RolloutManager;
import com.day.cq.wcm.msm.impl.RolloutConfigManagerFactory;
import com.day.text.Text;
import java.util.ArrayList;
import java.util.Iterator;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
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.request.RequestParameter;
import org.apache.sling.api.request.RequestParameterMap;
import org.apache.sling.api.resource.NonExistingResource;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(componentAbstract=1)
@Service
public abstract class LiveCommand
implements WCMCommand {
    private static final String[] EMPTY_ROLLOUT_CONFIG = new String[0];
    @Reference
    protected RolloutManager rolloutManager = null;
    @Reference
    protected LiveRelationshipManager relationshipManager = null;
    @Reference
    protected RolloutConfigManagerFactory rolloutConfigMgrFactory = null;
    public static final String BP_PATH_PARAM = "bpPath";
    private static final Logger log = LoggerFactory.getLogger(LiveCommand.class);
    private static final String CHILD_PATTERN = "%s/%s";

    protected Page createCopyTarget(Page master, String dstPath, RequestParameterMap map) throws WCMException, RepositoryException {
        String pageLabel;
        Page lcPage;
        Node contentNode;
        PageManager pageManager;
        String pageName = pageLabel = LiveCommand.getParameter(map, "label");
        if (StringUtils.isEmpty((String)pageLabel)) {
            pageName = master.getName();
        }
        String template = null;
        if (master.getTemplate() != null) {
            template = master.getTemplate().getPath();
        }
        if (!(contentNode = (Node)(lcPage = (pageManager = master.getPageManager()).create(dstPath, pageName, template, null, false)).getContentResource().adaptTo(Node.class)).canAddMixin("cq:LiveRelationship")) {
            contentNode.getSession().refresh(false);
            throw new WCMException(String.format("Can't establish LiveRelationship from %s to %s: Node does not allow to set mixin of %s", master.getPath(), lcPage.getPath(), "cq:LiveRelationship"));
        }
        Resource pageResource = (Resource)lcPage.adaptTo(Resource.class);
        ResourceResolver resolver = pageResource.getResourceResolver();
        LiveRelationship ship = this.establishRelationship(master, lcPage, false, resolver, LiveCommand.getRolloutConfigParam(map));
        contentNode.getParent().remove();
        this.rollout(master, lcPage, ship, false, resolver);
        String pageTitle = LiveCommand.getParameter(map, "title");
        if (resolver.getResource(lcPage.getPath()) == null) {
            if (!StringUtils.isEmpty((String)pageTitle)) {
                pageTitle = master.getTitle();
            }
            lcPage = pageManager.create(dstPath, pageName, template, pageTitle, false);
        } else if (!StringUtils.isEmpty((String)pageTitle)) {
            ((Node)lcPage.getContentResource().adaptTo(Node.class)).setProperty("jcr:title", pageTitle);
        }
        String before = LiveCommand.getParameter(map, "before");
        if (!StringUtils.isEmpty((String)before)) {
            pageManager.move(lcPage, lcPage.getPath(), Text.getName((String)before), false, false, null);
        }
        log.debug("Created Live Copy Resource at {}", (Object)lcPage.getPath());
        return lcPage;
    }

    protected /* varargs */ LiveRelationship establishRelationship(Page master, Page liveCopy, boolean deep, ResourceResolver resolver, String ... rolloutConfigs) throws WCMException {
        RolloutConfig[] configs;
        if (this.isEmpty(rolloutConfigs)) {
            rolloutConfigs = (String[])new HierarchyNodeInheritanceValueMap(liveCopy.getContentResource()).getInherited("cq:LiveSyncConfig/cq:rolloutConfigs", String[].class);
        }
        if (rolloutConfigs != null) {
            RolloutConfigManager rolloutConfigManager = this.rolloutConfigMgrFactory.create(resolver);
            ArrayList<RolloutConfig> tmp = new ArrayList<RolloutConfig>();
            for (String path : rolloutConfigs) {
                RolloutConfig rc = rolloutConfigManager.getRolloutConfig(path);
                if (rc == null) continue;
                tmp.add(rc);
            }
            configs = tmp.toArray((T[])new RolloutConfig[tmp.size()]);
        } else {
            configs = null;
        }
        return this.relationshipManager.establishRelationship(master, liveCopy, deep, false, configs);
    }

    protected void rollout(Page master, Page liveCopy, LiveRelationship ship, boolean deep, ResourceResolver resolver) throws WCMException {
        this.rolloutManager.rollout(resolver, ship, true, false);
        if (deep) {
            this.rolloutChildren(master.listChildren(), resolver, liveCopy.getPath());
        } else {
            log.debug("Live Copy at {} set to be shallow", (Object)liveCopy.getPath());
        }
    }

    protected /* varargs */ Page createLiveCopy(Page master, String dstPath, ResourceResolver resolver, boolean deep, RequestParameterMap parameterMap, String ... rolloutConfigs) throws RepositoryException, WCMException {
        Session session = (Session)resolver.adaptTo(Session.class);
        Page lcPage = this.createCopyTarget(master, dstPath, parameterMap);
        this.establishRelationship(master, lcPage, deep, resolver, rolloutConfigs);
        if (deep) {
            this.rolloutChildren(master.listChildren(), resolver, lcPage.getPath());
        }
        session.save();
        this.relationshipManager.getLiveRelationship(lcPage.getContentResource(), false);
        return lcPage;
    }

    protected boolean isEmpty(String[] values) {
        if (values != null) {
            for (String value : values) {
                if (StringUtils.isEmpty((String)value)) continue;
                return false;
            }
        }
        return true;
    }

    protected static String getParameter(RequestParameterMap map, String name) {
        return map != null && map.containsKey((Object)name) ? map.getValue(name).getString() : null;
    }

    protected static String appendChild(String parentPath, String childName) {
        return String.format("%s/%s", parentPath, childName);
    }

    private void rolloutChildren(Iterator<Page> children, ResourceResolver resolver, String target) throws WCMException {
        while (children.hasNext()) {
            Page child = children.next();
            String targetChild = LiveCommand.appendChild(target, child.getName());
            if (child.hasContent()) {
                String contentPath = LiveCommand.appendChild(targetChild, child.getContentResource().getName());
                LiveRelationship childShip = this.relationshipManager.getLiveRelationship((Resource)new NonExistingResource(resolver, contentPath), true);
                if (childShip != null) {
                    this.rolloutManager.rollout(resolver, childShip, true, false);
                } else {
                    throw new WCMException("Failed to access relation for child at " + child.getPath());
                }
            }
            this.rolloutChildren(child.listChildren(), resolver, targetChild);
        }
    }

    private static String[] getRolloutConfigParam(RequestParameterMap map) {
        String[] configs;
        if (map != null && map.containsKey((Object)"cq:rolloutConfigs")) {
            RequestParameter[] paths = map.getValues("cq:rolloutConfigs");
            configs = new String[paths.length];
            for (int i = 0; i < paths.length; ++i) {
                configs[i] = paths[i].getString();
            }
        } else {
            configs = EMPTY_ROLLOUT_CONFIG;
        }
        return configs;
    }

    protected void bindRolloutManager(RolloutManager rolloutManager) {
        this.rolloutManager = rolloutManager;
    }

    protected void unbindRolloutManager(RolloutManager rolloutManager) {
        if (this.rolloutManager == rolloutManager) {
            this.rolloutManager = null;
        }
    }

    protected void bindRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
        this.relationshipManager = liveRelationshipManager;
    }

    protected void unbindRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
        if (this.relationshipManager == liveRelationshipManager) {
            this.relationshipManager = null;
        }
    }

    protected void bindRolloutConfigMgrFactory(RolloutConfigManagerFactory rolloutConfigManagerFactory) {
        this.rolloutConfigMgrFactory = rolloutConfigManagerFactory;
    }

    protected void unbindRolloutConfigMgrFactory(RolloutConfigManagerFactory rolloutConfigManagerFactory) {
        if (this.rolloutConfigMgrFactory == rolloutConfigManagerFactory) {
            this.rolloutConfigMgrFactory = null;
        }
    }
}