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.module.InstallContext;
37
38 import javax.jcr.Property;
39 import javax.jcr.RepositoryException;
40 import javax.jcr.Session;
41
42 import org.apache.commons.lang3.StringUtils;
43
44
45
46
47 public class MovePropertyTask extends AbstractRepositoryTask {
48
49 private final String workspaceName;
50 private final String srcProperty;
51 private final String destProperty;
52 private final boolean overwrite;
53
54 public MovePropertyTask(String name, String workspaceName, String srcProperty, String destProperty, boolean overwrite) {
55 this(name, String.format("Move property '%s:%s' to '%s:%s'.", workspaceName, srcProperty, workspaceName, destProperty),
56 workspaceName, srcProperty, destProperty, overwrite);
57 }
58
59 public MovePropertyTask(String name, String description, String workspaceName, String srcProperty, String destProperty, boolean overwrite) {
60 super(name, description);
61 this.workspaceName = workspaceName;
62 this.srcProperty = srcProperty;
63 this.destProperty = StringUtils.removeEnd(destProperty, "/");
64 this.overwrite = overwrite;
65 }
66
67 @Override
68 protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException {
69
70 Session session = installContext.getJCRSession(workspaceName);
71
72 if (session.propertyExists(destProperty)) {
73 if (overwrite) {
74 session.getProperty(destProperty).remove();
75 } else {
76 installContext.error("Can't move property '" + srcProperty + "' to '" + destProperty + "' because this property already exists.", null);
77 return;
78 }
79 }
80
81 Property property = session.getProperty(srcProperty);
82
83 try {
84 session.getNode(StringUtils.substringBeforeLast(destProperty, "/")).setProperty(StringUtils.substringAfterLast(destProperty, "/"), property.getValue());
85 property.remove();
86 } catch (RepositoryException e) {
87 installContext.error("Can't copy property " + property + " to " + destProperty, e);
88 }
89 }
90
91 }