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.util.NodeUtil;
37 import info.magnolia.module.InstallContext;
38 import info.magnolia.repository.RepositoryConstants;
39
40 import javax.jcr.Node;
41 import javax.jcr.RepositoryException;
42 import javax.jcr.Session;
43
44
45
46
47 public class CopyNodeTask extends AbstractRepositoryTask {
48 private final String workspaceName;
49 private final String src;
50 private final String dest;
51 private final boolean overwrite;
52
53 public CopyNodeTask(String name, String src, String dest, boolean overwrite) {
54 this(name, String.format("Copy node '%s:%s' to '%s:%s'. " + (overwrite ? "Overwrite" : "Do not overwrite") + " if node already exists",
55 RepositoryConstants.CONFIG, src, RepositoryConstants.CONFIG, dest),
56 RepositoryConstants.CONFIG, src, dest, overwrite);
57 }
58
59 public CopyNodeTask(String name, String description, String workspaceName, String src, String dest, boolean overwrite) {
60 super(name, description);
61 this.workspaceName = workspaceName;
62 this.src = src;
63 this.dest = dest;
64 this.overwrite = overwrite;
65 }
66
67 @Override
68 protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException {
69 Session session = installContext.getJCRSession(workspaceName);
70 if (session.nodeExists(dest)) {
71 if (overwrite) {
72 session.getNode(dest).remove();
73 }
74 else {
75 installContext.error("Can't copy " + src + " to " + dest + " because the target node already exists.", null);
76 return;
77 }
78 }
79 Node srcNode = session.getNode(src);
80
81 try {
82 NodeUtil.copyInSession(srcNode, dest);
83 } catch (RepositoryException e) {
84 installContext.error("Can't copy " + src + " to " + dest, e);
85 }
86 }
87
88 }