ImporterUtil.java
7.2 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* javax.jcr.Node
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
*/
package com.day.cq.wcm.designimporter.util;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.designimporter.impl.common.PathSchemeHelper;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
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;
public class ImporterUtil {
public static Resource findImporter(Resource resource) {
List<Resource> found = ImporterUtil.findImporters(resource);
if (found.size() > 0) {
return found.get(0);
}
return null;
}
public static List<Resource> findImporters(Resource resource) {
return ImporterUtil.findImporters(resource, false);
}
public static List<Resource> findImporters(Resource resource, boolean recursive) {
LinkedList<Resource> found = new LinkedList<Resource>();
if (resource != null) {
Page page = (Page)resource.adaptTo(Page.class);
if (recursive) {
ImporterUtil.findImporterComponent(resource, found);
} else {
ImporterUtil.findImporterComponent(page.getContentResource(), found);
}
}
return found;
}
public static boolean checkCanvasPrimary(Resource resource) throws IllegalArgumentException {
return !ImporterUtil.checkCanvasSecondary(resource);
}
public static boolean checkCanvasSecondary(Resource resource) throws IllegalArgumentException {
ValueMap valueMap;
if (!ImporterUtil.isCanvas(resource)) {
throw new IllegalArgumentException("The passed resource is not a valid canvas resource");
}
Resource canvas = ImporterUtil.getCanvas(resource);
Resource importer = canvas.getParent();
if (canvas != null && (valueMap = (ValueMap)importer.adaptTo(ValueMap.class)).containsKey((Object)"cq:secondaryCanvas")) {
return (Boolean)valueMap.get("cq:secondaryCanvas", Boolean.class);
}
return false;
}
public static Resource getCanvasDesign(Resource resource) {
Resource canvas = ImporterUtil.getCanvas(resource);
PageManager pm = (PageManager)resource.getResourceResolver().adaptTo(PageManager.class);
if (ImporterUtil.checkCanvasSecondary(canvas)) {
Page primaryPage = pm.getContainingPage(canvas).getParent();
Resource importer = ImporterUtil.findImporter((Resource)primaryPage.adaptTo(Resource.class));
canvas = ImporterUtil.getCanvas(importer);
}
String canvasDesignPath = PathSchemeHelper.getCanvasDesignPath(canvas);
return resource.getResourceResolver().resolve(canvasDesignPath);
}
public static Resource getCanvas(Resource resource) {
if (ResourceUtil.isA((Resource)resource, (String)"wcm/designimporter/components/importer")) {
return resource.getChild("canvas");
}
if (ResourceUtil.isA((Resource)resource.getParent(), (String)"wcm/designimporter/components/importer")) {
return resource;
}
return null;
}
private static void findImporterComponent(Resource root, List<Resource> components) {
if (ImporterUtil.isImporter(root)) {
components.add(root);
} else {
Iterator childIterator = root.listChildren();
while (childIterator.hasNext()) {
Resource child = (Resource)childIterator.next();
ImporterUtil.findImporterComponent(child, components);
}
}
}
public static boolean isCanvas(Resource resource) {
if (resource == null) {
return false;
}
if (ImporterUtil.isImporter(resource.getParent())) {
return true;
}
return false;
}
public static boolean isImporter(Resource resource) {
return ResourceUtil.isA((Resource)resource, (String)"wcm/designimporter/components/importer");
}
public static boolean isImporterPage(Page page) {
if (page == null) {
return false;
}
return ResourceUtil.isA((Resource)page.getContentResource(), (String)"wcm/designimporter/components/importerpage");
}
public static boolean isImporterPage(Resource resource) {
PageManager pageManager = (PageManager)resource.getResourceResolver().adaptTo(PageManager.class);
Page containingPage = pageManager.getContainingPage(resource);
return ImporterUtil.isImporterPage(containingPage);
}
public static void deleteCanvasArtifact(Resource resource) throws RepositoryException {
ImporterUtil.deleteCanvasArtifact(resource, true);
}
public static void deleteCanvasArtifact(Resource resource, boolean cleanupDanglingAscendants) throws RepositoryException {
if (resource != null && !ResourceUtil.isNonExistingResource((Resource)resource)) {
Resource parent = resource.getParent();
Node node = (Node)resource.adaptTo(Node.class);
node.remove();
node.getSession().save();
if (cleanupDanglingAscendants) {
ImporterUtil.cleanupDanglingAscendants(parent);
}
}
}
public static void cleanupDanglingAscendants(Resource resource) throws RepositoryException {
if (resource.getPath().contains("/canvas/")) {
Resource last = null;
while (resource != null) {
Iterator iter = resource.listChildren();
if (iter.hasNext()) {
iter.next();
}
if ("canvas".equals(resource.getName()) || iter.hasNext()) break;
last = resource;
resource = resource.getParent();
}
if (last != null) {
((Node)last.adaptTo(Node.class)).remove();
}
}
}
public static Resource getDanglingAscendantRoot(Resource resource) throws RepositoryException {
if (resource.getPath().contains("/canvas/") && !resource.getParent().listChildren().hasNext()) {
boolean isParentCanvas;
Resource last;
boolean parentHasMoreChildren;
Resource curr = resource;
do {
last = curr;
curr = curr.getParent();
isParentCanvas = "canvas".equals(curr.getName());
Iterator iter = curr.listChildren();
if (iter.hasNext()) {
iter.next();
}
parentHasMoreChildren = iter.hasNext();
} while (!isParentCanvas && !parentHasMoreChildren);
return last;
}
return resource;
}
}