CreateContentStep.java 4.67 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.jcr.JcrUtil
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageManager
 *  com.day.cq.wcm.api.WCMException
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 */
package com.day.cq.wcm.siteimporter.internal.steps;

import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.siteimporter.ImporterContext;
import com.day.cq.wcm.siteimporter.ImporterStep;
import com.day.cq.wcm.siteimporter.internal.steps.AbstractImporterStep;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;

@Component(metatype=0)
@Service(value={ImporterStep.class})
@Properties(value={@Property(name="importerstep.order", intValue={500})})
public class CreateContentStep
extends AbstractImporterStep {
    @Override
    public boolean execute(ImporterContext ctx) {
        ctx.output("Creating Content", "heading");
        ctx.doIndent();
        PageManager pm = (PageManager)ctx.getResolver().adaptTo(PageManager.class);
        String template = "/apps/" + ctx.getProjectName() + "/templates/contentpage";
        Resource content = ctx.getResolver().getResource("/content/" + JcrUtil.createValidName((String)ctx.getProjectName()));
        if (content != null) {
            if (ctx.isOverwrite()) {
                Node contentNode = (Node)content.adaptTo(Node.class);
                try {
                    Node parent = contentNode.getParent();
                    contentNode.remove();
                    parent.getSession().save();
                }
                catch (RepositoryException e) {
                    ctx.error("Failure during removal of content node: " + e.getMessage(), (Exception)e);
                    ctx.undoIndent();
                    return false;
                }
            } else {
                ctx.error("Content already exists, maybe tick overwrite option");
                ctx.undoIndent();
                return false;
            }
        }
        try {
            Page page = pm.create("/content", "", template, ctx.getProjectName());
            ctx.addPath(page.getPath());
            try {
                Node con = (Node)page.getContentResource().adaptTo(Node.class);
                con.setProperty("cq:designPath", "/etc/designs/" + ctx.getProjectName());
                con.getParent().getSession().save();
            }
            catch (Exception e) {
                ctx.error("Couldn't set Design Path on Root Page:" + e.getMessage(), e);
                ctx.undoIndent();
                return false;
            }
            Page langPage = pm.create(page.getPath(), "", template, ctx.getDefaultLanguage());
            ctx.output("Created content root " + langPage.getPath());
            String[] pagelines = ctx.getSitemap().split("\r\n");
            Page lastPage = langPage;
            int currentDepth = 0;
            for (int i = 0; i < pagelines.length; ++i) {
                int depth = 0;
                while (pagelines[i].startsWith(" ") || pagelines[i].startsWith("\t")) {
                    ++depth;
                    pagelines[i] = pagelines[i].substring(1);
                }
                if ("".equals(pagelines[i])) continue;
                while (depth < currentDepth) {
                    lastPage = lastPage.getParent();
                    --currentDepth;
                    ctx.undoIndent();
                }
                lastPage = pm.create(lastPage.getPath(), "", template, pagelines[i]);
                ++currentDepth;
                ctx.output("Created Page " + lastPage.getPath());
                ctx.doIndent();
            }
            while (currentDepth > 0) {
                ctx.undoIndent();
                --currentDepth;
            }
        }
        catch (WCMException e) {
            ctx.error("Failure during page creation:" + e.getMessage(), (Exception)e);
            ctx.undoIndent();
            return false;
        }
        ctx.undoIndent();
        return true;
    }
}