CreateSiteCommand.java 11.8 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.servlets.HtmlStatusResponseHelper
 *  com.day.cq.i18n.I18n
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageManager
 *  com.day.cq.wcm.api.commands.WCMCommandContext
 *  com.day.cq.wcm.commons.ReferenceSearch
 *  com.day.cq.wcm.msm.api.Blueprint
 *  com.day.cq.wcm.msm.api.LiveRelationship
 *  com.day.cq.wcm.msm.api.LiveRelationshipManager
 *  com.day.text.Text
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.servlet.http.HttpServletRequest
 *  org.apache.commons.lang.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.request.RequestParameter
 *  org.apache.sling.api.request.RequestParameterMap
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.servlets.HtmlResponse
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.msm.impl.commands;

import com.day.cq.commons.servlets.HtmlStatusResponseHelper;
import com.day.cq.i18n.I18n;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.commands.WCMCommandContext;
import com.day.cq.wcm.commons.ReferenceSearch;
import com.day.cq.wcm.msm.api.Blueprint;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.day.cq.wcm.msm.impl.commands.LiveCommand;
import com.day.text.Text;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.request.RequestParameterMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.HtmlResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service
public class CreateSiteCommand
extends LiveCommand {
    private final Logger log = LoggerFactory.getLogger(CreateSiteCommand.class);
    static final String PARAM_IS_LIVECOPY = "isLiveCopy";
    static final String PARAM_UPDATE_ALL_LINKS = "updateAllLinks";
    static final String PARAM_MASTER_PAGES = "msm:masterPages";
    static final String PARAM_CHAPTER_PAGES = "msm:chapterPages";

    public String getCommandName() {
        return "createSite";
    }

    public HtmlResponse performCommand(WCMCommandContext ctx, SlingHttpServletRequest request, SlingHttpServletResponse response, PageManager pageManager) {
        try {
            Blueprint blueprint;
            Page master;
            String[] chapters;
            ResourceResolver resolver = request.getResourceResolver();
            String bpPath = request.getParameter("srcPath");
            String dstPath = CreateSiteCommand.getParam(request, "destPath");
            if (StringUtils.isEmpty((String)bpPath)) {
                return this.fail(I18n.get((HttpServletRequest)request, (String)"No valid Blueprint specified."));
            }
            if (StringUtils.isEmpty((String)dstPath)) {
                return this.fail(I18n.get((HttpServletRequest)request, (String)"No destination specified."));
            }
            Resource bres = resolver.getResource(bpPath);
            Blueprint blueprint2 = blueprint = bres != null ? (Blueprint)bres.adaptTo(Blueprint.class) : null;
            if (blueprint == null || StringUtils.isEmpty((String)blueprint.getSitePath())) {
                return this.fail(I18n.get((HttpServletRequest)request, (String)"Error during operation. Selected template is not a blueprint info: {0}", (String)null, (Object[])new Object[]{bpPath}));
            }
            if (Text.isDescendantOrEqual((String)blueprint.getSitePath(), (String)dstPath)) {
                return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)I18n.get((HttpServletRequest)request, (String)"Destination path cannot contain source path."));
            }
            Resource resource = resolver.getResource(blueprint.getSitePath());
            Page page = master = resource == null ? null : (Page)resource.adaptTo(Page.class);
            if (master == null || !master.hasContent()) {
                return this.fail(I18n.get((HttpServletRequest)request, (String)"Error during operation. Blueprint is not a valid Page: {0}", (String)null, (Object[])new Object[]{blueprint.getSitePath()}));
            }
            Page siteRoot = this.createCopyTarget(master, dstPath, request.getRequestParameterMap());
            this.relationshipManager.endRelationship(siteRoot.getContentResource(), false);
            Node content = (Node)siteRoot.getContentResource().adaptTo(Node.class);
            for (Map.Entry re : request.getRequestParameterMap().entrySet()) {
                String name = (String)re.getKey();
                if (!name.startsWith("./") || name.indexOf(64) >= 0 || name.endsWith("/jcr:primaryType")) continue;
                name = name.substring(2);
                String[] elems = Text.explode((String)name, (int)47);
                Node parent = content;
                String helperPath = ".";
                for (int i = 0; i < elems.length - 1; ++i) {
                    helperPath = helperPath + "/" + elems[i];
                    if (parent.hasNode(elems[i])) {
                        parent = parent.getNode(elems[i]);
                        continue;
                    }
                    String nt = request.getParameter(helperPath + "/jcr:primaryType");
                    parent.addNode(elems[i], nt);
                }
                String propName = elems[elems.length - 1];
                RequestParameter[] params = (RequestParameter[])re.getValue();
                String[] values = new String[params.length];
                for (int i2 = 0; i2 < params.length; ++i2) {
                    values[i2] = params[i2].getString();
                }
                if (values.length == 0) continue;
                if (values.length == 1) {
                    parent.setProperty(propName, values[0]);
                    continue;
                }
                parent.setProperty(propName, values);
            }
            boolean isLiveCopy = "true".equals(request.getParameter("isLiveCopy"));
            if (isLiveCopy) {
                String[] rolloutConfigs = request.getParameterValues("cq:rolloutConfigs");
                LiveRelationship ship = this.establishRelationship(master, siteRoot, false, resolver, rolloutConfigs);
                this.rollout(master, siteRoot, ship, false, resolver);
            } else if (content.getSession().hasPendingChanges()) {
                content.getSession().save();
            }
            String[] srcPaths = request.getParameterValues("msm:masterPages");
            if (srcPaths == null) {
                srcPaths = new String[]{};
            }
            boolean rolloutAllChapters = (chapters = request.getParameterValues("msm:chapterPages")) != null && chapters.length == 1 && chapters[0].equals("all");
            for (String langSrc : srcPaths) {
                Page srcPage = pageManager.getPage(langSrc);
                String relPath = langSrc.substring(blueprint.getSitePath().length());
                String langDst = siteRoot.getPath() + relPath;
                HashMap<String, String> chapRefMap = new HashMap<String, String>();
                if (srcPage == null) {
                    this.log.warn("Source page does not exist: {}", (Object)langSrc);
                } else {
                    if (isLiveCopy) {
                        this.log.info("rollout language {} -> {}", (Object)langSrc, (Object)langDst);
                        langDst = this.createLiveCopy(srcPage, siteRoot.getPath(), resolver, rolloutAllChapters, null, new String[0]).getPath();
                    } else {
                        this.log.info("copying language {} -> {}", (Object)langSrc, (Object)langDst);
                        pageManager.copy(srcPage, langDst, null, !rolloutAllChapters, false);
                    }
                    if (chapters != null && !rolloutAllChapters) {
                        for (String chapter : chapters) {
                            relPath = Text.getName((String)chapter);
                            String chapSrc = langSrc + "/" + relPath;
                            String chapDst = langDst + "/" + relPath;
                            Page chapPage = pageManager.getPage(chapSrc);
                            if (chapPage != null) {
                                if (isLiveCopy) {
                                    this.log.info("rollout chapter {} -> {}", (Object)chapSrc, (Object)chapDst);
                                    this.createLiveCopy(chapPage, langDst, resolver, true, null, new String[0]);
                                } else {
                                    this.log.info("copying chapter {} -> {}", (Object)chapSrc, (Object)chapDst);
                                    pageManager.copy(chapPage, chapDst, null, false, false);
                                }
                                chapRefMap.put(chapSrc, chapDst);
                                continue;
                            }
                            this.log.info("chapter does not exist in this language {}", (Object)chapSrc);
                        }
                    }
                }
                boolean updateAllLinks = "true".equals(request.getParameter("updateAllLinks"));
                if (!updateAllLinks || chapRefMap.isEmpty()) continue;
                this.updateReferences((Node)siteRoot.adaptTo(Node.class), chapRefMap);
                if (!content.getSession().hasPendingChanges()) continue;
                content.getSession().save();
            }
            return HtmlStatusResponseHelper.createStatusResponse((boolean)true, (String)I18n.get((HttpServletRequest)request, (String)"Site created"), (String)siteRoot.getPath());
        }
        catch (Exception e) {
            this.log.error("Error during site creation.", (Throwable)e);
            return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)I18n.get((HttpServletRequest)request, (String)e.getMessage()));
        }
    }

    private void updateReferences(Node node, Map<String, String> refMap) throws RepositoryException {
        HashSet<String> exclude = new HashSet<String>();
        exclude.add("cq:master");
        for (Map.Entry<String, String> entry : refMap.entrySet()) {
            String srcPath = entry.getKey();
            String destPath = entry.getValue();
            this.log.info("Adjusting all references under : {}", (Object)node.getPath());
            Collection adjusted = new ReferenceSearch().adjustReferences(node, srcPath, destPath, false, exclude);
            if (!this.log.isDebugEnabled()) continue;
            for (String link : adjusted) {
                this.log.debug("Adjusted : {}", (Object)link);
            }
        }
    }

    private HtmlResponse fail(String msg) {
        this.log.error(msg);
        return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)msg);
    }

    static String getParam(SlingHttpServletRequest request, String name) {
        String value = request.getParameter(name);
        if (value == null || value.equals("")) {
            return null;
        }
        return value;
    }
}