CreateTemplateStep.java 6.83 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.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.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.Rectangle;
import java.awt.geom.Rectangle2D;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Hashtable;
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.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={200})})
public class CreateTemplateStep
extends AbstractImporterStep {
    @Override
    public boolean execute(ImporterContext ctx) {
        block17 : {
            ctx.output("Creating template", "heading");
            ctx.doIndent();
            Resource templatesRes = ctx.getResolver().getResource("/apps/" + ctx.getProjectName() + "/" + "templates");
            if (templatesRes == null) {
                ctx.error("Could not locate templates folder.");
                ctx.undoIndent();
                return false;
            }
            Node templatesNode = (Node)templatesRes.adaptTo(Node.class);
            Node template = null;
            Hashtable<String, Object> props = new Hashtable<String, Object>();
            ArrayList<String> paths = new ArrayList<String>();
            paths.add("/content/" + ctx.getProjectName() + "(/.*)?");
            props.put("allowedPaths", paths);
            props.put("jcr:title", ctx.getTitle() + " Content Page");
            props.put("jcr:description", "Generic template for most " + ctx.getTitle() + " pages");
            props.put("ranking", new Long(90));
            try {
                if (templatesNode.hasNode("contentpage") && ctx.isOverwrite()) {
                    templatesNode.getNode("contentpage").remove();
                    templatesNode.getSession().save();
                }
            }
            catch (RepositoryException e) {
                ctx.error("Repository failure: " + e.getMessage(), (Exception)e);
                ctx.undoIndent();
                return false;
            }
            try {
                template = Utils.createNode(templatesNode, "contentpage", "cq:Template", props, ctx);
            }
            catch (RepositoryException e) {
                ctx.error("Could not create template, maybe tick overwrite option", (Exception)e);
                ctx.undoIndent();
                return false;
            }
            ctx.output("Template Node created");
            props.clear();
            props.put("sling:resourceType", ctx.getProjectName() + "/components/contentpage");
            try {
                Node c = Utils.createNode(template, "jcr:content", "cq:PageContent", props, ctx);
                props.put("sling:resourceType", "foundation/components/titleImage");
                Utils.createNode(c, "titleImage", "nt:unstructured", props, ctx);
            }
            catch (RepositoryException e) {
                ctx.error("Could not create content node: " + e.getMessage(), (Exception)e);
                ctx.undoIndent();
                return false;
            }
            ctx.output("Content Node created");
            if (!"".equals(ctx.getThumbnail().getString())) {
                try {
                    RequestParameter param = ctx.getThumbnail();
                    if (!"image/png".equals(param.getContentType())) {
                        ctx.error("Could not upload image: has to be a png!");
                        ctx.undoIndent();
                        return false;
                    }
                    Utils.createFile(template, "thumbnail.png", param.getInputStream(), "image/png", ctx);
                    ctx.output("Thumbnail uploaded");
                    Resource thumbLoc = ctx.getResolver().getResource("/apps/" + ctx.getProjectName() + "/templates/contentpage/thumbnail.png");
                    if (thumbLoc == null) {
                        ctx.error("Could not read image for resizing");
                        break block17;
                    }
                    Layer thumbnail = ImageHelper.createLayer((Resource)thumbLoc);
                    int width = thumbnail.getWidth();
                    int height = thumbnail.getHeight();
                    double ratio = Math.min(128.0 / (double)width, 98.0 / (double)height);
                    thumbnail.resize((int)Math.ceil((double)width * ratio), (int)Math.ceil((double)height * ratio));
                    thumbnail.crop((Rectangle2D)new Rectangle(128, 98));
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    try {
                        thumbnail.write("images/png", 1.0, (OutputStream)out);
                        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
                        template.getNode("thumbnail.png").remove();
                        template.getSession().save();
                        Utils.createFile(template, "thumbnail.png", in, "image/png", ctx);
                    }
                    catch (Exception e) {
                        ctx.error("Couldn't store thumbnail: " + e.getMessage(), e);
                    }
                    ctx.output("Created Preview image");
                }
                catch (RepositoryException e) {
                    ctx.error("Could not upload thumbnail image: " + e.getMessage(), (Exception)e);
                }
                catch (IOException e) {
                    ctx.error("IO Error while trying to upload image: " + e.getMessage(), e);
                }
            } else {
                ctx.output("No preview image available", "skip");
            }
        }
        ctx.undoIndent();
        return true;
    }
}