CreateBlueprintStep.java 6.64 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.ImageHelper
 *  com.day.image.Layer
 *  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.request.RequestParameter
 *  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.ImageHelper;
import com.day.cq.wcm.siteimporter.ImporterContext;
import com.day.cq.wcm.siteimporter.ImporterStep;
import com.day.cq.wcm.siteimporter.internal.steps.AbstractImporterStep;
import com.day.cq.wcm.siteimporter.internal.util.Utils;
import com.day.image.Layer;
import java.awt.Color;
import java.awt.Paint;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
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.request.RequestParameter;
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={600})})
public class CreateBlueprintStep
extends AbstractImporterStep {
    private static final String SHADOW_RESOURCE = "/libs/wcm/siteimporter/resources/shadow.png";
    private static final String BLUEPRINT_RESOURCE = "/libs/wcm/siteimporter/resources/blueprintlogo.png";

    @Override
    public boolean execute(ImporterContext ctx) {
        ctx.output("Creating Blueprint", "heading");
        ctx.doIndent();
        try {
            Node rootNode = (Node)ctx.getResolver().getResource("/etc/blueprints").adaptTo(Node.class);
            if (rootNode.hasNode(ctx.getProjectName())) {
                if (ctx.isOverwrite()) {
                    rootNode.getNode(ctx.getProjectName()).remove();
                    rootNode.getSession().save();
                } else {
                    ctx.error("Could not create template, maybe tick overwrite option");
                    ctx.undoIndent();
                    return false;
                }
            }
            rootNode.getSession().getWorkspace().copy("/etc/blueprints/geometrixx", "/etc/blueprints/" + ctx.getProjectName());
        }
        catch (Exception e) {
            ctx.error("Couldn't copy geometrixx blueprint: " + e.getMessage(), e);
            ctx.undoIndent();
            return false;
        }
        ctx.output("Copied Geometrixx Blueprint as foundation");
        Resource blueprintRes = ctx.getResolver().getResource("/etc/blueprints/" + ctx.getProjectName());
        ctx.addPath(blueprintRes.getPath());
        Resource contentRes = ctx.getResolver().getResource("/etc/blueprints/" + ctx.getProjectName() + "/" + "jcr:content");
        if (contentRes == null) {
            ctx.error("Couldn't access Blueprint Content");
            ctx.undoIndent();
            return false;
        }
        Node content = (Node)contentRes.adaptTo(Node.class);
        try {
            content.setProperty("jcr:description", "Blueprint that uses the " + ctx.getTitle() + " Site as original.");
            content.setProperty("jcr:title", ctx.getTitle() + " Site");
            content.setProperty("sitePath", "/content/" + ctx.getProjectName());
            content.getNode("dialog").setProperty("title", ctx.getTitle() + " Blueprint");
            content.getNode("thumbnail").remove();
            content.getParent().getSession().save();
        }
        catch (Exception e) {
            ctx.error("Couldn't adapt properties of new Blueprint: " + e.getMessage(), e);
            ctx.undoIndent();
            return false;
        }
        ctx.output("Adapted Blueprint Properties");
        if (!"".equals(ctx.getThumbnail().getString())) {
            Layer bg = new Layer(200, 167, (Paint)new Color(33554431, true));
            int y = 46;
            for (int x = 5; x < 60; x += 23) {
                Layer shadow = ImageHelper.createLayer((Resource)ctx.getResolver().getResource("/libs/wcm/siteimporter/resources/shadow.png"));
                if (shadow == null) {
                    ctx.error("Couldn't read shadow file; skipped thumbnail creation");
                    ctx.undoIndent();
                    return true;
                }
                shadow.setX(x);
                shadow.setY(y);
                bg.merge(shadow);
                Resource thumbnail = ctx.getResolver().getResource("/apps/" + ctx.getProjectName() + "/templates/contentpage/thumbnail.png");
                Layer preview = ImageHelper.createLayer((Resource)thumbnail);
                if (preview == null) {
                    ctx.error("Couldn't read preview file; skipped thumbnail creation");
                    ctx.undoIndent();
                    return true;
                }
                preview.setX(x + 5);
                preview.setY(y + 5);
                bg.merge(preview);
                y -= 18;
            }
            Layer logo = ImageHelper.createLayer((Resource)ctx.getResolver().getResource("/libs/wcm/siteimporter/resources/blueprintlogo.png"));
            if (logo == null) {
                ctx.error("Couldn't read logo file; skipped thumbnail creation");
                ctx.undoIndent();
                return true;
            }
            logo.setX(138);
            logo.setY(104);
            bg.merge(logo);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            try {
                bg.write("images/png", 1.0, (OutputStream)out);
                byte[] bytes = out.toByteArray();
                ByteArrayInputStream in = new ByteArrayInputStream(bytes);
                Utils.createFile(content, "thumbnail", in, "image/png", ctx);
            }
            catch (RepositoryException e) {
                ctx.error("Repository Exception during storage of Thumbnail: " + e.getMessage(), (Exception)e);
            }
            catch (Exception e) {
                ctx.error("Couldn't store Thumbnail: " + e.getMessage(), e);
            }
            ctx.output("Created Preview image");
        } else {
            ctx.output("No preview image available", "skip");
        }
        ctx.undoIndent();
        return true;
    }
}