CreateTemplateStep.java
6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* 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;
}
}