CanvasReferenceProvider.java
5.85 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.designer.Designer
* com.day.cq.wcm.api.reference.Reference
* com.day.cq.wcm.api.reference.ReferenceProvider
* javax.jcr.RepositoryException
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.designimporter;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.designer.Designer;
import com.day.cq.wcm.api.reference.Reference;
import com.day.cq.wcm.api.reference.ReferenceProvider;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.jcr.RepositoryException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service
public class CanvasReferenceProvider
implements ReferenceProvider {
private static final Logger logger = LoggerFactory.getLogger(CanvasReferenceProvider.class);
private static final String IMPORTER_RESOURCE_TYPE = "wcm/designimporter/components/importer";
private static final String CQ_CLIENT_LIBRARY_FOLDER = "cq:ClientLibraryFolder";
private static final String NN_CANVAS = "canvas";
private static final String PN_REIMPORT = "reimport";
public List<Reference> findReferences(Resource resource) {
ArrayList<Reference> references = new ArrayList<Reference>();
LinkedList<Resource> foundComponents = new LinkedList<Resource>();
this.findImporterComponent(resource, foundComponents);
for (Resource importerComponent : foundComponents) {
references.addAll(this.getReferences(importerComponent));
}
Collections.reverse(references);
return references;
}
private List<Reference> getReferences(Resource importerComponent) {
boolean hasCanvas = importerComponent.getChild("canvas") != null;
ValueMap properties = (ValueMap)importerComponent.adaptTo(ValueMap.class);
boolean isReimport = Boolean.TRUE.equals(properties.get((Object)"reimport"));
LinkedList<Reference> references = new LinkedList<Reference>();
if (hasCanvas && !isReimport) {
try {
references.addAll(this.getDesignReferences(importerComponent));
references.addAll(this.getCanvasReferences(importerComponent));
}
catch (RepositoryException e) {
logger.error("Error obtaining canvas references for the resource " + importerComponent.getPath(), (Throwable)e);
}
}
return references;
}
private List<Reference> getDesignReferences(Resource importerComponent) throws RepositoryException {
LinkedList<Reference> references = new LinkedList<Reference>();
ResourceResolver resourceResolver = importerComponent.getResourceResolver();
PageManager pageManager = (PageManager)resourceResolver.adaptTo(PageManager.class);
Page page = pageManager.getContainingPage(importerComponent);
Designer designer = (Designer)resourceResolver.adaptTo(Designer.class);
String designPath = designer.getDesignPath(page);
String canvasDesignPath = designPath + "/" + "canvas" + importerComponent.getPath();
Resource canvasDesign = resourceResolver.resolve(canvasDesignPath);
this.addReferencesRecursive(canvasDesign, references);
return references;
}
private List<Reference> getCanvasReferences(Resource importerComponent) throws RepositoryException {
LinkedList<Reference> references = new LinkedList<Reference>();
Resource canvas = importerComponent.getChild("canvas");
String componentPath = "/apps/" + canvas.getResourceType();
Resource component = canvas.getResourceResolver().resolve(componentPath);
this.addReferencesRecursive(component, references);
return references;
}
private void addReferencesRecursive(Resource root, List<Reference> references) throws RepositoryException {
if (!this.isContentNode(root)) {
if (this.isClientLibFolder(root)) {
references.add(new Reference("artifact", root.getName(), root, -1));
} else {
references.add(0, new Reference("artifact", root.getName(), root, -1));
}
}
Iterator iter = root.listChildren();
while (iter.hasNext()) {
this.addReferencesRecursive((Resource)iter.next(), references);
}
}
private boolean isContentNode(Resource r) throws RepositoryException {
return "jcr:content".equals(r.getName());
}
private boolean isClientLibFolder(Resource r) throws RepositoryException {
ValueMap properties = (ValueMap)r.adaptTo(ValueMap.class);
String nodeType = (String)properties.get("jcr:primaryType", String.class);
return "cq:ClientLibraryFolder".equals(nodeType);
}
private void findImporterComponent(Resource root, List<Resource> components) {
if ("wcm/designimporter/components/importer".equals(root.getResourceType())) {
components.add(root);
} else {
Iterator childIterator = root.listChildren();
while (childIterator.hasNext()) {
Resource child = (Resource)childIterator.next();
this.findImporterComponent(child, components);
}
}
}
}