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.module.delta;
35
36 import info.magnolia.jcr.predicate.NodeTypePredicate;
37 import info.magnolia.jcr.util.NodeUtil;
38 import info.magnolia.module.InstallContext;
39 import info.magnolia.repository.RepositoryConstants;
40
41 import java.util.Iterator;
42
43 import javax.jcr.Node;
44 import javax.jcr.RepositoryException;
45 import javax.jcr.Session;
46
47
48
49
50 public class RenameNodesTask extends AbstractRepositoryTask {
51 private final String workspace;
52 private final String path;
53 private final String oldName;
54 private final String newName;
55 private final String nodeType;
56
57 public RenameNodesTask(String name, String workspace, String path, String oldName, String newName, String nodeType) {
58 this(name, String.format("Rename all nodes of type '%s' under path '%s:%s' from '%s' to '%s'.", nodeType, workspace, path, oldName, newName),
59 RepositoryConstants.CONFIG, path, oldName, newName, nodeType);
60 }
61
62 public RenameNodesTask(String name, String description, String workspace, String path, String oldName,
63 String newName, String nodeType) {
64 super(name, description);
65 this.workspace = workspace;
66 this.path = path;
67 this.oldName = oldName;
68 this.newName = newName;
69 this.nodeType = nodeType;
70 }
71
72 @Override
73 protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException {
74 final Session session = installContext.getJCRSession(workspace);
75 final Node rootOfOperation = session.getNode(path);
76 Iterator<Node> iterator = NodeUtil.collectAllChildren(rootOfOperation, new NodeTypePredicate(nodeType)).iterator();
77 while (iterator.hasNext()) {
78 Node childNode = iterator.next();
79 if (oldName.equals(childNode.getName())) {
80 NodeUtil.renameNode(childNode, newName);
81 }
82 }
83 }
84 }