CreateDesignStep.java 13.1 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Workspace
 *  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.wcm.siteimporter.ImporterContext;
import com.day.cq.wcm.siteimporter.ImporterStep;
import com.day.cq.wcm.siteimporter.ProgressTracker;
import com.day.cq.wcm.siteimporter.ResourcesBoard;
import com.day.cq.wcm.siteimporter.internal.resource.BinaryImporterResource;
import com.day.cq.wcm.siteimporter.internal.resource.CssImporterResource;
import com.day.cq.wcm.siteimporter.internal.resource.HtmlImporterResource;
import com.day.cq.wcm.siteimporter.internal.resource.ImporterResource;
import com.day.cq.wcm.siteimporter.internal.resource.ImporterResourceVisitor;
import com.day.cq.wcm.siteimporter.internal.resource.JsImporterResource;
import com.day.cq.wcm.siteimporter.internal.resource.ReferenceLocation;
import com.day.cq.wcm.siteimporter.internal.steps.AbstractImporterStep;
import com.day.cq.wcm.siteimporter.internal.util.Utils;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
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={300})})
public class CreateDesignStep
extends AbstractImporterStep
implements ImporterResourceVisitor {
    @Override
    public boolean execute(ImporterContext ctx) {
        ctx.output("Creating design", "heading");
        ctx.doIndent();
        if (!this.createSetup(ctx)) {
            ctx.undoIndent();
            return false;
        }
        ctx.output("Adapted Design");
        ImporterResource original = ctx.getBoard().getResource(ctx.getOriginal(), 0, ctx);
        ArrayList<ReferenceLocation> refs = original.getExternalReferences();
        Iterator<ReferenceLocation> refIt = refs.iterator();
        ctx.output("Downloading assets referenced from the main html", "heading");
        ctx.doIndent();
        while (refIt.hasNext()) {
            ReferenceLocation next = refIt.next();
            if (next.getType() == 0) continue;
            ImporterResource res = ctx.getBoard().getResource(next.getLink(), next.getType(), ctx);
            res.triggerAction(this);
        }
        ctx.undoIndent();
        ctx.output("Finished Download of all Assets");
        ctx.undoIndent();
        return true;
    }

    protected boolean createSetup(ImporterContext ctx) {
        Node designRootNode;
        block10 : {
            Resource designRoot = ctx.getResolver().getResource("/etc/designs");
            if (designRoot == null) {
                ctx.error("Couldn't find design root");
                return false;
            }
            designRootNode = (Node)designRoot.adaptTo(Node.class);
            try {
                if (!designRootNode.hasNode(ctx.getProjectName())) break block10;
                if (ctx.isOverwrite()) {
                    designRootNode.getNode(ctx.getProjectName()).remove();
                    designRootNode.getSession().save();
                    break block10;
                }
                ctx.error("Design already exists, maybe tick overwrite option");
                return false;
            }
            catch (RepositoryException e) {
                ctx.error("Repository Exception: " + e.getMessage(), (Exception)e);
                return false;
            }
        }
        try {
            designRootNode.getSession().getWorkspace().copy("/etc/designs/geometrixx", "/etc/designs/" + ctx.getProjectName());
        }
        catch (Exception e) {
            ctx.error("Couldn't copy geometrixx design: " + e.getMessage());
            return false;
        }
        ctx.output("Copied Geometrixx Design as foundation");
        try {
            Node designNode = designRootNode.getNode(ctx.getProjectName());
            ctx.addPath(designNode.getPath());
            Node designContent = designNode.getNode("jcr:content");
            designContent.setProperty("jcr:title", ctx.getTitle() + " Demo Site");
            designContent.getSession().save();
            Node designFolder = designRootNode.getNode(ctx.getProjectName());
            Utils.createNode(designFolder, "resources", "nt:folder", ctx);
        }
        catch (RepositoryException e) {
            ctx.error("Couldn't adapt Design Title: " + e.getMessage(), (Exception)e);
            return false;
        }
        try {
            Node staticNode = designRootNode.getNode(ctx.getProjectName() + "/static.css");
            staticNode.remove();
            designRootNode.getSession().save();
            designRootNode.getSession().getWorkspace().copy("/libs/wcm/siteimporter/resources/static.css", "/etc/designs/" + ctx.getProjectName() + "/static.css");
        }
        catch (RepositoryException e) {
            ctx.error("Couldn't exchange static.css: " + e.getMessage());
        }
        return true;
    }

    @Override
    public Object performBinary(BinaryImporterResource resource) {
        ImporterContext ctx = resource.getContext();
        URL location = resource.getLocation();
        String path = "/" + location.getHost() + location.getPath();
        if (resource.isDownloaded()) {
            ctx.output("Skipping redundant appearance of " + location.toExternalForm(), "skip");
            return true;
        }
        ctx.output("Downloading file " + location.toExternalForm() + " at " + path);
        Node folder = this.getStorageFolder(path, ctx);
        if (folder == null) {
            ctx.error("Repository Exception during download of " + path);
            return false;
        }
        if (path.endsWith("/")) {
            path = path.substring(0, path.length() - 1);
        }
        try {
            InputStream downloadStream = resource.getDownloadStream();
            if (downloadStream != null) {
                String downloadPath = path.substring(path.lastIndexOf("/") + 1);
                Node download = Utils.createFile(folder, downloadPath, downloadStream, resource.getMimeType(), ctx);
                resource.setDownloadLocation(download.getPath());
            } else {
                ctx.error("Skipping download of " + resource.getLocation() + ". Could not obtain download stream.");
            }
        }
        catch (RepositoryException e) {
            ctx.error("Repository Exception during storage of " + path + ": " + e.getMessage(), (Exception)e);
            return false;
        }
        catch (IOException e) {
            ctx.error("Couldn't access external resource: <a href=\"" + resource.getLocation() + "\">" + path + "</a>", e);
            return false;
        }
        return true;
    }

    @Override
    public Object performCss(CssImporterResource resource) {
        String content;
        ImporterContext ctx = resource.getContext();
        URL location = resource.getLocation();
        String path = location.getPath();
        if (resource.isDownloaded()) {
            ctx.output("Skipping redundant appearance of " + path, "skip");
            return true;
        }
        ctx.output("Downloading file " + location.toExternalForm() + " at " + path);
        Node folder = this.getStorageFolder(path, ctx);
        if (folder == null) {
            ctx.error("Repository Exception during download of " + path);
            return false;
        }
        try {
            content = resource.getContent();
        }
        catch (IOException e) {
            ctx.error("Couldn't access external resource: <a href=\"" + resource.getLocation() + "\">" + path + "</a>", e);
            return false;
        }
        ArrayList<ReferenceLocation> references = resource.getExternalReferences();
        ctx.output("Diving into " + path + ", downloading " + references.size() + " referenced assets", "heading");
        ctx.doIndent();
        for (ReferenceLocation next : references) {
            ImporterResource res = ctx.getBoard().getResource(next.getLink(), next.getType(), ctx);
            if (next.getType() == 1 && res.isDownloaded()) continue;
            res.triggerAction(this);
        }
        ctx.undoIndent();
        content = this.rewriteLinks(resource, content);
        ctx.output("Rewrote Links in " + path);
        if (path.endsWith("/")) {
            path = path.substring(0, path.length() - 1);
        }
        try {
            String title = path.substring(path.lastIndexOf("/") + 1);
            Node download = Utils.createFile(folder, title, content, "text/css", resource.getCharset(), (ProgressTracker)ctx);
            resource.setDownloadLocation(download.getPath());
        }
        catch (RepositoryException e) {
            ctx.error("Repository Exception during storage of " + path, (Exception)e);
            return false;
        }
        catch (Exception e) {
            ctx.error("Another Exception: " + e.getMessage(), e);
            return false;
        }
        return true;
    }

    @Override
    public Object performHtml(HtmlImporterResource resource) {
        return true;
    }

    @Override
    public Object performJs(JsImporterResource resource) {
        String content;
        ImporterContext ctx = resource.getContext();
        URL location = resource.getLocation();
        String path = location.getPath();
        if (resource.isDownloaded()) {
            ctx.output("Skipping redundant appearance of " + path, "skip");
            return true;
        }
        ctx.output("Downloading file " + location.toExternalForm() + " at " + path);
        Node folder = this.getStorageFolder(path, ctx);
        if (folder == null) {
            ctx.error("Repository Exception during download of " + path);
            return false;
        }
        try {
            content = resource.getContent();
        }
        catch (IOException e) {
            ctx.error("Couldn't access external resource: <a href=\"" + resource.getLocation() + "\">" + path + "</a>", e);
            return false;
        }
        ArrayList<ReferenceLocation> references = resource.getExternalReferences();
        ctx.output("Diving into " + path + ", downloading " + references.size() + " referenced assets");
        for (ReferenceLocation next : references) {
            ImporterResource res = ctx.getBoard().getResource(next.getLink(), next.getType(), ctx);
            res.triggerAction(this);
        }
        content = this.rewriteLinks(resource, content);
        ctx.output("Rewrote Links in " + path);
        if (path.endsWith("/")) {
            path = path.substring(0, path.length() - 1);
        }
        try {
            String title = path.substring(path.lastIndexOf("/") + 1);
            Node download = Utils.createFile(folder, title, content, "application/x-javascript", resource.getCharset(), (ProgressTracker)ctx);
            resource.setDownloadLocation(download.getPath());
        }
        catch (RepositoryException e) {
            ctx.error("Repository Exception during storage of " + path + ": " + e.getMessage(), (Exception)e);
            return false;
        }
        catch (IOException e) {
            ctx.error("IOException during storage of " + path + ": " + e.getMessage(), e);
            return false;
        }
        ctx.output("Downloaded file " + path);
        return true;
    }

    private Node getStorageFolder(String path, ImporterContext ctx) {
        Resource root = ctx.getResolver().getResource("/etc/designs/" + ctx.getProjectName() + "/" + "resources");
        if (root == null) {
            ctx.error("Couldn't locate project design");
            return null;
        }
        return Utils.createPath(root, path, ctx, false);
    }

    protected String rewriteLinks(ImporterResource resource, String content) {
        ImporterContext ctx = resource.getContext();
        ArrayList<ReferenceLocation> refs = resource.getExternalReferences();
        Object[] refsArr = new ReferenceLocation[refs.size()];
        refs.toArray(refsArr);
        Arrays.sort(refsArr);
        for (Object next : refsArr) {
            ImporterResource ress = ctx.getBoard().getResource(next.getLink(), ctx);
            String path = ctx.getContextPath() + "/etc/designs/" + ctx.getProjectName() + "/" + "resources" + next.getLink().getPath();
            if (ress.isDownloaded()) {
                path = ress.getDownloadLocation();
            }
            content = content.substring(0, next.getStart()) + path + content.substring(next.getEnd());
        }
        return content;
    }
}