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 package info.magnolia.setup.for5_2;
35
36 import info.magnolia.jcr.predicate.NodeTypePredicate;
37 import info.magnolia.jcr.util.NodeUtil;
38 import info.magnolia.jcr.util.NodeVisitor;
39 import info.magnolia.module.InstallContext;
40 import info.magnolia.module.delta.AbstractRepositoryTask;
41 import info.magnolia.module.delta.TaskExecutionException;
42
43 import java.util.ArrayList;
44 import java.util.List;
45
46 import javax.jcr.Node;
47 import javax.jcr.RepositoryException;
48 import javax.jcr.Session;
49
50 import org.apache.commons.lang.StringUtils;
51 import org.apache.jackrabbit.JcrConstants;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55
56
57
58
59 public class IdentifySameNameSiblingNodesTask extends AbstractRepositoryTask {
60 private static final Logger log = LoggerFactory.getLogger(IdentifySameNameSiblingNodesTask.class);
61
62 protected final String workspace;
63 protected Session session;
64 private final String subPath;
65 private final List<String> nodeTypes;
66 private final boolean evaluateSupertypes;
67 private final String siblingMatcherPattern = ".*\\[\\d+\\]";
68
69
70
71
72
73
74
75
76
77 public IdentifySameNameSiblingNodesTask(String name, String description, String workspace, String subPath, List<String> nodeTypes, boolean evaluateSupertypes) {
78 super(name, description);
79 this.workspace = workspace;
80 this.subPath = StringUtils.isBlank(subPath) ? "/" : subPath;
81 this.nodeTypes = nodeTypes != null ? nodeTypes : new ArrayList<String>();
82 this.evaluateSupertypes = evaluateSupertypes;
83 }
84
85 @Override
86 protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException {
87 try{
88 session = installContext.getJCRSession(workspace);
89
90 if (nodeTypes.isEmpty()) {
91 nodeTypes.add(JcrConstants.NT_BASE);
92 }
93 for (String nodeType : nodeTypes) {
94 Node rootNode = session.getNode(subPath);
95 NodeTypePredicate predicate = new NodeTypePredicate(nodeType, evaluateSupertypes);
96 NodeUtil.visit(rootNode, createSiblingsVisitor(installContext), predicate);
97
98 }
99 }catch(RepositoryException re) {
100 installContext.error("Unable to perform Migration task " + getName(), re);
101 throw new TaskExecutionException(re.getMessage());
102 }
103
104 }
105
106
107
108
109 private NodeVisitor createSiblingsVisitor(final InstallContext installContext) {
110 NodeVisitor visitor = new NodeVisitor() {
111
112 @Override
113 public void visit(Node node) throws RepositoryException {
114 if (isSameNameSiblingNode(node)) {
115 handleNode(node, installContext);
116 }
117 }
118 };
119 return visitor;
120 }
121
122
123
124
125 protected boolean isSameNameSiblingNode(Node node) {
126 try {
127 String path = node.getPath();
128 return path.matches(siblingMatcherPattern);
129 } catch (RepositoryException re) {
130 log.warn("Got exception by checking if '{}' is a sibling node", NodeUtil.getName(node), re);
131 return false;
132 }
133 }
134
135
136
137
138
139 protected void handleNode(Node node, InstallContext installContext) throws RepositoryException {
140 installContext.warn("Found same name sibling of '" + node.getName() + "' at '" + node.getPath() + "'");
141 }
142
143 }