TemplateManagerImpl.java
18 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* 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.Template
* com.day.cq.wcm.api.TemplateManager
* com.day.cq.wcm.api.TemplatedResource
* com.day.cq.wcm.api.WCMException
* com.day.cq.wcm.api.components.ComponentContext
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Workspace
* javax.jcr.query.Query
* javax.jcr.query.QueryManager
* javax.jcr.query.QueryResult
* org.apache.commons.collections.CollectionUtils
* org.apache.commons.collections.Predicate
* org.apache.commons.lang3.StringUtils
* org.apache.sling.api.resource.ModifiableValueMap
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.api.wrappers.ValueMapDecorator
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.core.impl;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.Template;
import com.day.cq.wcm.api.TemplateManager;
import com.day.cq.wcm.api.TemplatedResource;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.api.components.ComponentContext;
import com.day.cq.wcm.core.impl.TemplateConfImpl;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TemplateManagerImpl
implements TemplateManager {
private final Logger log;
private static final String NT_PAGE_CONTENT = "cq:PageContent";
private static final String PN_POLICY = "cq:policy";
private static final String PN_SLING_RESOURCE_TYPE = "sling:resourceType";
private ResourceResolver resourceResolver;
private final String[] illegalCharacterMapping;
public TemplateManagerImpl(ResourceResolver resourceResolver) {
this.log = LoggerFactory.getLogger(this.getClass());
this.illegalCharacterMapping = JcrUtil.HYPHEN_LABEL_CHAR_MAPPING;
if (resourceResolver == null) {
throw new IllegalArgumentException("resource resolver is null");
}
this.resourceResolver = resourceResolver;
}
public Template createTemplate(String parentPath, String templateType, String templateName, ValueMap templateProperties) throws WCMException {
String templateTitle;
Template template;
if (StringUtils.isBlank((CharSequence)parentPath)) {
throw new IllegalArgumentException("parentPath is blank");
}
if (StringUtils.isBlank((CharSequence)templateType)) {
throw new IllegalArgumentException("templateType is blank");
}
if (templateProperties == null) {
templateProperties = new ValueMapDecorator(new HashMap());
}
if (StringUtils.isBlank((CharSequence)(templateTitle = (String)templateProperties.get("jcr:title", (Object)""))) && StringUtils.isBlank((CharSequence)templateName)) {
throw new IllegalArgumentException("template name and title cannot both be blank, provide either of them");
}
if (StringUtils.isBlank((CharSequence)templateName)) {
templateName = (String)templateProperties.get("jcr:title", (Object)"");
}
if (!JcrUtil.isValidName((String)(templateName = JcrUtil.createValidName((String)templateName, (String[])this.illegalCharacterMapping, (String)"_")))) {
throw new IllegalArgumentException("Illegal template name " + templateName);
}
try {
Resource templateResource = this.createTemplateResource(parentPath, templateType, templateName, templateProperties);
template = (Template)templateResource.adaptTo(Template.class);
this.copyTemplateTypePolicies(template);
Resource templateContent = templateResource.getChild("jcr:content");
ModifiableValueMap modifiableValueMap = (ModifiableValueMap)templateContent.adaptTo(ModifiableValueMap.class);
modifiableValueMap.put((Object)"cq:templateType", (Object)templateType);
templateContent.getResourceResolver().commit();
if (template == null) {
throw new WCMException("Not able to create template");
}
}
catch (RepositoryException e) {
throw new WCMException(e.getMessage(), (Throwable)e);
}
catch (PersistenceException e) {
throw new WCMException(e.getMessage(), (Throwable)e);
}
return template;
}
public void deleteTemplate(Template template) throws WCMException {
if (template != null) {
if (this.getContainingPages(template).size() > 0) {
throw new WCMException("Not able to delete template " + template.getPath() + ". It's still in use.");
}
try {
String templatePath = template.getPath();
this.resourceResolver.delete((Resource)template.adaptTo(Resource.class));
this.resourceResolver.commit();
this.log.debug("Deleted template " + templatePath);
}
catch (PersistenceException e) {
this.log.error(e.getMessage(), (Throwable)e);
}
}
}
public List<Page> getContainingPages(Template template) {
List<Page> pages = Collections.emptyList();
if (template != null) {
String templatePath = template.getPath();
String queryString = "SELECT * FROM [cq:PageContent] AS page WHERE ISDESCENDANTNODE(page, '/content') AND [cq:template]='" + templatePath + "'";
try {
Session session = (Session)this.resourceResolver.adaptTo(Session.class);
QueryManager qm = session.getWorkspace().getQueryManager();
Query query = qm.createQuery(queryString, "JCR-SQL2");
this.log.debug(query.getStatement());
QueryResult res = query.execute();
pages = new LinkedList<Page>();
NodeIterator iter = res.getNodes();
while (iter.hasNext()) {
Resource pageResource = this.resourceResolver.getResource(iter.nextNode().getPath());
Page page = (pageResource = pageResource.getParent()) == null ? null : (Page)pageResource.adaptTo(Page.class);
if (page == null) continue;
pages.add(page);
}
}
catch (RepositoryException e) {
this.log.error(e.getMessage(), (Throwable)e);
}
}
return pages;
}
public List<Resource> getStructureResources(ComponentContext componentContext) {
ArrayList<Resource> paragraphs = new ArrayList<Resource>();
Resource contentResource = componentContext.getResource();
Resource res = (Resource)contentResource.adaptTo(TemplatedResource.class);
if (res == null) {
res = contentResource;
}
Iterator it = res.listChildren();
while (it.hasNext()) {
Resource child = (Resource)it.next();
if ("cq:responsive".equals(child.getName())) continue;
paragraphs.add(child);
}
return paragraphs;
}
public Template getContainingTemplate(Resource resource) {
if (resource == null) {
return null;
}
Template template = (Template)resource.adaptTo(Template.class);
if (template == null) {
template = this.getContainingTemplate(resource.getParent());
}
return template;
}
public List<Template> getAllTemplates() {
ArrayList<Template> templates = new ArrayList<Template>();
Iterator templateResources = this.resourceResolver.findResources("SELECT * FROM cq:Template", "sql");
while (templateResources.hasNext()) {
Resource templateResource = (Resource)templateResources.next();
Template template = (Template)templateResource.adaptTo(Template.class);
if (template == null) continue;
templates.add(template);
}
return templates;
}
public List<Template> getTemplates(Predicate filter) {
List<Template> templates = this.getAllTemplates();
CollectionUtils.filter(templates, (Predicate)filter);
return templates;
}
public Template getTemplate(String templatePath) {
Resource templateResource = this.resourceResolver.getResource(templatePath);
return templateResource == null ? null : (Template)templateResource.adaptTo(Template.class);
}
public List<Template> getTemplateTypes(String parentPath) {
TemplateConfImpl templateTypesConf;
LinkedList<Template> templateTypes = new LinkedList<Template>();
Resource resource = this.resourceResolver.getResource(parentPath);
if (resource != null && (templateTypesConf = new TemplateConfImpl(resource)) != null) {
List<Resource> list = templateTypesConf.getListResources("wcm/template-types");
for (Resource r : list) {
Template t = (Template)r.adaptTo(Template.class);
if (t == null) continue;
templateTypes.add(t);
}
}
return templateTypes;
}
private void copyTemplateTypePolicies(Template template) {
Session session = (Session)this.resourceResolver.adaptTo(Session.class);
Resource templateResource = (Resource)template.adaptTo(Resource.class);
TemplateConfImpl templateConf = new TemplateConfImpl(templateResource);
Resource configurationRoot = templateConf.getItemResource("wcm/policies");
if (configurationRoot == null) {
this.log.warn("Configuration not found for template while duplicating policies from template-type", (Object)templateResource);
return;
}
this.copyTemplateTypePoliciesRecursive(templateResource.getChild("policies"), configurationRoot.getPath());
try {
session.save();
}
catch (RepositoryException e) {
this.log.warn("Unable to import default policies", (Object[])e.getStackTrace());
}
}
private void copyTemplateTypePoliciesRecursive(Resource policyMappingResource, String configurationRootPath) {
if (policyMappingResource == null) {
return;
}
this.copyTemplateTypePolicy(policyMappingResource, configurationRootPath);
if (!policyMappingResource.hasChildren()) {
return;
}
Iterator it = policyMappingResource.listChildren();
while (it.hasNext()) {
this.copyTemplateTypePoliciesRecursive((Resource)it.next(), configurationRootPath);
}
}
private void copyTemplateTypePolicy(Resource policyMappingResource, String configurationRootPath) {
if (policyMappingResource == null || StringUtils.isEmpty((CharSequence)configurationRootPath)) {
return;
}
ValueMap policyMappingValueMap = policyMappingResource.getValueMap();
if (policyMappingValueMap.containsKey((Object)"cq:policy")) {
String originalPolicyPath = (String)policyMappingValueMap.get("cq:policy", String.class);
TemplateConfImpl templateConf = new TemplateConfImpl(policyMappingResource);
Resource sourcePolicy = templateConf.getItemResource("wcm/policies/" + originalPolicyPath);
if (sourcePolicy != null && !sourcePolicy.getPath().startsWith(configurationRootPath)) {
try {
Node newPolicy = JcrUtil.createPath((String)(configurationRootPath + "/" + originalPolicyPath), (String)"nt:unstructured", (Session)((Session)this.resourceResolver.adaptTo(Session.class)));
Resource newPolicyResource = this.resourceResolver.getResource(newPolicy.getPath());
ModifiableValueMap modifiablePolicyVM = (ModifiableValueMap)newPolicyResource.adaptTo(ModifiableValueMap.class);
if (!modifiablePolicyVM.containsKey((Object)"sling:resourceType")) {
modifiablePolicyVM.putAll((Map)sourcePolicy.getValueMap());
this.copyTemplateTypePolicyChildrenRecursive(sourcePolicy, newPolicy.getPath());
}
ModifiableValueMap modifiableMappingVM = (ModifiableValueMap)policyMappingResource.adaptTo(ModifiableValueMap.class);
modifiableMappingVM.put((Object)"cq:policy", (Object)originalPolicyPath);
this.resourceResolver.commit();
}
catch (PersistenceException e) {
this.log.warn("Policy update of mapping reference failed", (Object)policyMappingResource, (Object)e.getStackTrace());
}
catch (RepositoryException e) {
this.log.warn("Policy update of mapping reference failed", (Object)policyMappingResource, (Object)e.getStackTrace());
}
}
}
}
private void copyTemplateTypePolicyChildrenRecursive(Resource originalPolicy, String newParentPolicyPath) throws PersistenceException {
if (originalPolicy == null || !originalPolicy.hasChildren()) {
return;
}
Iterator it = originalPolicy.listChildren();
while (it.hasNext()) {
Resource child = (Resource)it.next();
String newChildPath = child.getPath().replace(originalPolicy.getPath(), newParentPolicyPath);
Resource newChild = ResourceUtil.getOrCreateResource((ResourceResolver)this.resourceResolver, (String)newChildPath, (Map)child.getValueMap(), (String)"nt:unstructured", (boolean)true);
this.copyTemplateTypePolicyChildrenRecursive(child, newChild.getPath());
}
}
private Resource createTemplateResource(String parentPath, String templateType, String templateName, ValueMap templateProperties) throws RepositoryException {
Node templateNode;
Resource templateResource = null;
Session session = (Session)this.resourceResolver.adaptTo(Session.class);
Node parentNode = session.getNode(parentPath);
if (parentNode != null && (templateNode = JcrUtil.createUniqueNode((Node)parentNode, (String)templateName, (String)"cq:Template", (Session)session)) != null) {
Template seed;
Node templateContent = templateNode.addNode("jcr:content", "cq:PageContent");
if (templateContent != null) {
this.setTemplateProperties(templateContent, templateProperties);
}
Template template = seed = templateType != null ? this.getTemplate(templateType) : null;
if (seed != null) {
Resource seedResource = (Resource)seed.adaptTo(Resource.class);
Iterator it = seedResource.listChildren();
while (it.hasNext()) {
Resource child = (Resource)it.next();
String name = child.getName();
if ("jcr:content".equals(child.getName())) continue;
Node childNode = JcrUtil.copy((Node)((Node)child.adaptTo(Node.class)), (Node)templateNode, (String)child.getName());
Node contentNode = childNode.getNode("jcr:content");
if (!"initial".equals(name) && !"structure".equals(name) || contentNode == null) continue;
contentNode.setProperty("cq:template", templateNode.getPath());
}
session.save();
templateResource = this.resourceResolver.getResource(templateNode.getPath());
}
}
return templateResource;
}
private void setTemplateProperties(Node templateContentNode, ValueMap templateProperties) throws RepositoryException {
Long ranking;
String status;
String[] allowedPaths;
String description;
String title = (String)templateProperties.get("jcr:title", (Object)"");
if (StringUtils.isNotBlank((CharSequence)title)) {
templateContentNode.setProperty("jcr:title", title);
}
if (StringUtils.isNotBlank((CharSequence)(description = (String)templateProperties.get("jcr:description", (Object)"")))) {
templateContentNode.setProperty("jcr:description", description);
}
if ((ranking = (Long)templateProperties.get("ranking", Long.class)) != null) {
templateContentNode.setProperty("ranking", ranking.longValue());
}
if ((allowedPaths = (String[])templateProperties.get("allowedPaths", String[].class)) != null && allowedPaths.length > 0) {
templateContentNode.setProperty("allowedPaths", allowedPaths);
}
if (StringUtils.isNotBlank((CharSequence)(status = (String)templateProperties.get("status", String.class)))) {
templateContentNode.setProperty("status", status);
}
templateContentNode.setProperty("cq:lastModified", Calendar.getInstance());
templateContentNode.setProperty("cq:lastModifiedBy", templateContentNode.getSession().getUserID());
}
}