ParagraphSystem.java
16.7 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.DiffInfo
* com.day.cq.commons.DiffInfo$TYPE
* com.day.text.Text
* javax.jcr.Item
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.request.RequestParameter
* 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.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.foundation;
import com.day.cq.commons.DiffInfo;
import com.day.cq.wcm.foundation.Paragraph;
import com.day.text.Text;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestParameter;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ParagraphSystem {
public static final Logger log = LoggerFactory.getLogger(ParagraphSystem.class);
public static final String PARAMETER_VERSION_DIFF = "cq_diffTo";
public static final String PARAMETER_VERSION_VIEW = "cq_view";
public static final String COL_CTL_TYPE = "controlType";
public static final String COL_CTL_LAYOUT = "layout";
private final Resource resource;
private final ResourceResolver resolver;
private String colCtrlResourceType;
private String colCtrlSuffix = "/colctrl";
private String defaultLayout = "1;cq-colctrl-default";
private List<Paragraph> paras;
private final String versionLabel;
private final boolean isVersionDiff;
public static ParagraphSystem create(Resource resource, SlingHttpServletRequest req) {
boolean isDiff;
RequestParameter parameter = req.getRequestParameter("cq_diffTo");
String label = null;
if (null != parameter) {
try {
label = parameter.getString("UTF-8");
}
catch (UnsupportedEncodingException e) {
log.warn("create: error while decoding diff view parameter: ", (Throwable)e);
}
}
boolean bl = isDiff = label != null;
if (label == null) {
label = req.getParameter("cq_view");
}
return new ParagraphSystem(resource, label, isDiff);
}
public ParagraphSystem(Resource resource) {
this(resource, null, true);
}
public ParagraphSystem(Resource resource, String versionLabel) {
this(resource, versionLabel, true);
}
public ParagraphSystem(Resource resource, String versionLabel, boolean isVersionDiff) {
this.isVersionDiff = isVersionDiff;
this.resource = resource;
this.resolver = resource.getResourceResolver();
String type = resource.getResourceType() + this.colCtrlSuffix;
if (type.startsWith("/")) {
for (String sp : this.resolver.getSearchPath()) {
if (!type.startsWith(sp)) continue;
type = type.substring(sp.length());
break;
}
}
this.versionLabel = versionLabel != null && versionLabel.trim().length() > 0 ? versionLabel : null;
}
public void setColCtrlResourceType(String colCtrlResourceType) {
this.colCtrlResourceType = colCtrlResourceType;
}
public String getDefaultLayout() {
return this.defaultLayout;
}
public void setDefaultLayout(String defaultLayout) {
this.defaultLayout = defaultLayout;
}
public List<Paragraph> paragraphs() {
this.initParas(false);
return this.paras;
}
public void repair() {
this.paras = null;
this.initParas(true);
}
private void initParas(boolean fixContentStructure) {
if (this.paras != null) {
return;
}
Resource versionResource = null;
if (this.resource.adaptTo(Node.class) == null) {
if (fixContentStructure) {
try {
String path = this.resource.getPath();
int idx = path.lastIndexOf(47);
String parentPath = path.substring(0, idx);
String name = path.substring(idx + 1);
Session s = (Session)this.resolver.adaptTo(Session.class);
Node parentNode = (Node)s.getItem(parentPath);
Node parNode = parentNode.addNode(name);
parNode.setProperty("sling:resourceType", this.resource.getResourceType());
}
catch (Exception e) {
log.warn("Could not create missing {} node.", (Object)this.resource.getPath(), (Object)e);
}
}
} else if (this.versionLabel != null) {
versionResource = DiffInfo.getVersionedResource((Resource)this.resource, (String)this.versionLabel);
}
if (versionResource != null && !this.isVersionDiff) {
this.paras = this.collectParagraphs(versionResource, false);
} else {
this.paras = this.collectParagraphs(this.resource, fixContentStructure);
if (versionResource != null) {
List<Paragraph> versionedParas = this.collectParagraphs(versionResource, false);
this.compare(this.paras, versionedParas);
}
}
}
private List<Paragraph> collectParagraphs(Resource resource, boolean fixStructure) {
LinkedList<Paragraph> paragraphs = new LinkedList<Paragraph>();
int colNum = 0;
int numCols = 0;
String layout = "default";
LinkedList<Paragraph> toRemove = new LinkedList<Paragraph>();
LinkedList<Paragraph> toCreate = new LinkedList<Paragraph>();
Resource lastStartResource = null;
int lastStartIndex = 0;
Resource followingLastStartResource = null;
Iterator iter = this.resolver.listChildren(resource);
while (iter.hasNext()) {
Resource res = (Resource)iter.next();
if (lastStartResource != null && followingLastStartResource == null) {
followingLastStartResource = res;
}
if (res.getResourceType().endsWith(this.colCtrlSuffix)) {
ValueMap resProps = (ValueMap)res.adaptTo(ValueMap.class);
Paragraph.Type type = null;
try {
type = Paragraph.Type.valueOf(((String)resProps.get("controlType", (Object)"start")).toUpperCase());
}
catch (IllegalArgumentException e) {
// empty catch block
}
if (type == Paragraph.Type.START) {
this.colCtrlResourceType = res.getResourceType();
if (numCols > 0 && fixStructure) {
Paragraph para;
while (colNum < numCols - 1) {
para = new Paragraph(followingLastStartResource, Paragraph.Type.BREAK, ++colNum, layout, numCols);
if (followingLastStartResource != null) {
paragraphs.add(++lastStartIndex, para);
} else {
paragraphs.add(para);
}
toCreate.add(para);
}
para = new Paragraph(followingLastStartResource, Paragraph.Type.END, colNum, layout, numCols);
if (followingLastStartResource != null) {
paragraphs.add(++lastStartIndex, para);
} else {
paragraphs.add(para);
}
toCreate.add(para);
}
colNum = 0;
numCols = 1;
layout = (String)resProps.get("layout", (Object)this.defaultLayout);
int i = layout.indexOf(59);
if (i > 0) {
try {
numCols = Integer.parseInt(layout.substring(0, i));
}
catch (NumberFormatException e) {
// empty catch block
}
layout = layout.substring(i + 1);
}
Paragraph toInsert = new Paragraph(res, type, colNum, layout, numCols);
paragraphs.add(toInsert);
lastStartResource = res;
lastStartIndex = paragraphs.indexOf((Object)toInsert);
followingLastStartResource = null;
continue;
}
if (type == Paragraph.Type.BREAK) {
Paragraph para = new Paragraph(res, type, ++colNum, layout, numCols);
if (numCols > 0) {
if (colNum >= numCols) {
toRemove.add(para);
continue;
}
paragraphs.add(para);
continue;
}
toRemove.add(para);
continue;
}
if (type == Paragraph.Type.END) {
if (numCols > 0) {
while (colNum < numCols - 1 && fixStructure) {
Paragraph para = new Paragraph(res, Paragraph.Type.BREAK, ++colNum, layout, numCols);
paragraphs.add(para);
toCreate.add(para);
}
numCols = 0;
colNum = 0;
paragraphs.add(new Paragraph(res, Paragraph.Type.END, colNum, layout, numCols));
continue;
}
Paragraph para = new Paragraph(res, Paragraph.Type.END);
toRemove.add(para);
continue;
}
Paragraph para = new Paragraph(res, Paragraph.Type.END);
toRemove.add(para);
continue;
}
paragraphs.add(new Paragraph(res, Paragraph.Type.NORMAL, colNum, layout, numCols));
}
if (numCols > 0 && fixStructure) {
Paragraph para;
while (colNum < numCols - 1) {
para = new Paragraph(followingLastStartResource, Paragraph.Type.BREAK, ++colNum, layout, numCols);
if (followingLastStartResource != null) {
paragraphs.add(++lastStartIndex, para);
} else {
paragraphs.add(para);
}
toCreate.add(para);
}
para = new Paragraph(followingLastStartResource, Paragraph.Type.END, colNum, layout, numCols);
if (followingLastStartResource != null) {
paragraphs.add(++lastStartIndex, para);
} else {
paragraphs.add(para);
}
toCreate.add(para);
}
if (fixStructure) {
try {
this.fixStructure(toRemove, toCreate);
}
catch (RepositoryException e) {
log.error("Error while fixing paragraph system structure.", (Throwable)e);
}
}
return paragraphs;
}
private boolean fixStructure(List<Paragraph> toRemove, List<Paragraph> toCreate) throws RepositoryException {
Node parent = (Node)this.resource.adaptTo(Node.class);
for (Paragraph p : toRemove) {
Node pn = p.adaptTo(Node.class);
if (pn == null) continue;
pn.remove();
}
int nr = 0;
for (Paragraph p2 : toCreate) {
String type = p2.getType().toString().toLowerCase();
Node node = parent.addNode("col_" + type + System.currentTimeMillis() + nr++);
node.setProperty("sling:resourceType", this.colCtrlResourceType);
node.setProperty("controlType", type);
if (p2.getResource() == null) continue;
String beforeName = Text.getName((String)p2.getResource().getPath());
parent.orderBefore(node.getName(), beforeName);
}
return !toCreate.isEmpty() || !toRemove.isEmpty();
}
private void removeColumnLayoutParagraphs(List<Paragraph> list) {
Iterator<Paragraph> i = list.iterator();
while (i.hasNext()) {
Paragraph p = i.next();
if (p.getType() == Paragraph.Type.NORMAL) continue;
i.remove();
}
}
private void compare(List<Paragraph> orig, List<Paragraph> versioned) {
Paragraph value;
int j;
int i;
Paragraph inP;
Paragraph outP;
ArrayList<Paragraph> current = new ArrayList<Paragraph>(orig);
this.removeColumnLayoutParagraphs(current);
this.removeColumnLayoutParagraphs(versioned);
int M = current.size();
int N = versioned.size();
ArrayList<Paragraph> out = new ArrayList<Paragraph>();
ArrayList<Paragraph> in = new ArrayList<Paragraph>();
ArrayList<Paragraph> moved = new ArrayList<Paragraph>();
int[][] opt = new int[M + 1][N + 1];
for (i = M - 1; i >= 0; --i) {
for (j = N - 1; j >= 0; --j) {
opt[i][j] = ResourceUtil.getName((Resource)((Resource)current.get(i))).equals(ResourceUtil.getName((Resource)((Resource)versioned.get(j)))) ? opt[i + 1][j + 1] + 1 : Math.max(opt[i + 1][j], opt[i][j + 1]);
}
}
i = 0;
j = 0;
while (i < M && j < N) {
if (ResourceUtil.getName((Resource)((Resource)current.get(i))).equals(ResourceUtil.getName((Resource)((Resource)versioned.get(j))))) {
++i;
++j;
continue;
}
if (opt[i + 1][j] >= opt[i][j + 1]) {
if ((outP = this.search(out, value = current.get(i++))) == null) {
in.add(value);
continue;
}
moved.add(value);
out.remove((Object)outP);
continue;
}
if ((inP = this.search(in, value = versioned.get(j++))) == null) {
out.add(value);
continue;
}
moved.add(inP);
in.remove((Object)inP);
}
while (i < M || j < N) {
if (i == M) {
if ((inP = this.search(in, value = versioned.get(j++))) == null) {
out.add(value);
continue;
}
moved.add(inP);
in.remove((Object)inP);
continue;
}
if (j != N) continue;
if ((outP = this.search(out, value = current.get(i++))) == null) {
in.add(value);
continue;
}
moved.add(value);
out.remove((Object)outP);
}
for (Paragraph v222 : in) {
v222.setDiffInfo(new DiffInfo(null, DiffInfo.TYPE.ADDED));
}
for (Paragraph v222 : out) {
int pos;
v222.setDiffInfo(new DiffInfo(v222.getResource(), DiffInfo.TYPE.REMOVED));
for (pos = versioned.indexOf((Object)v222) + 1; pos < versioned.size(); ++pos) {
Paragraph nextPara = this.search(orig, versioned.get(pos));
if (nextPara == null) continue;
int index = orig.indexOf((Object)nextPara);
orig.add(index, v222);
break;
}
if (pos != versioned.size()) continue;
orig.add(v222);
}
for (Paragraph v222 : moved) {
v222.setDiffInfo(new DiffInfo((Resource)this.search(versioned, v222), DiffInfo.TYPE.MOVED));
}
for (Paragraph v222 : current) {
if (v222.adaptTo(DiffInfo.class) != null) continue;
v222.setDiffInfo(new DiffInfo((Resource)this.search(versioned, v222), DiffInfo.TYPE.SAME));
}
}
private Paragraph search(List<Paragraph> paras, Paragraph rsrc) {
String title = ResourceUtil.getName((Resource)rsrc);
for (Paragraph p : paras) {
if (!ResourceUtil.getName((Resource)p).equals(title)) continue;
return p;
}
return null;
}
}