CreateContentStep.java
4.67 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
/*
* 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;
}
}