JcrRecursiveRemove.java
2.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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.nodetype.NodeDefinition
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.commons.jcr;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JcrRecursiveRemove {
private final Logger log;
public static final int DEFAULT_SAVE_EVERY_HOW_MANY = 1000;
public JcrRecursiveRemove() {
this.log = LoggerFactory.getLogger(this.getClass());
}
public int removeRecursive(Node n, int saveEveryHowManyNodes) throws RepositoryException {
if (saveEveryHowManyNodes < 1) {
saveEveryHowManyNodes = 1000;
}
String path = n.getPath();
int result = this.remove(n, n.getPath(), saveEveryHowManyNodes, 0);
this.log.info("removeRecursive({}) done: {} nodes deleted, saving...", (Object)path, (Object)result);
n.getSession().save();
this.onSave();
return result;
}
private int remove(Node n, String startPath, int saveEvery, int deletedCount) throws RepositoryException {
if (!this.canContinue()) {
this.log.info("canContinue() returns false, aborting recursive delete");
return deletedCount;
}
NodeIterator ni = n.getNodes();
while (ni.hasNext()) {
deletedCount = this.remove(ni.nextNode(), startPath, saveEvery, deletedCount);
}
NodeDefinition def = n.getDefinition();
if (!def.isProtected() && !def.isMandatory()) {
n.remove();
++deletedCount;
}
if (deletedCount % saveEvery == 0) {
this.log.info("removeRecursive({}) in progress: {} nodes deleted, saving...", (Object)startPath, (Object)deletedCount);
n.getSession().save();
this.onSave();
}
return deletedCount;
}
protected void onSave() {
}
protected boolean canContinue() {
return true;
}
}