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 import info.magnolia.repository.RepositoryConstants;
38
39 import javax.jcr.Node;
40 import javax.jcr.RepositoryException;
41 import javax.jcr.Session;
42
43
44
45
46 public class RemovePropertyTask extends AbstractRepositoryTask {
47 private final String workspaceName;
48 private final String parentPath;
49 private final String propertyToRemove;
50
51 public RemovePropertyTask(String name, String parentPath, String propertyToRemove) {
52 this(name, String.format("Remove property '%s:%s'.", RepositoryConstants.CONFIG, parentPath + "/" + propertyToRemove),
53 RepositoryConstants.CONFIG, parentPath, propertyToRemove);
54 }
55
56 public RemovePropertyTask(String name, String description, String workspaceName, String parentPath, String propertyToRemove) {
57 super(name, description);
58 this.workspaceName = workspaceName;
59 this.parentPath = parentPath;
60 this.propertyToRemove = propertyToRemove;
61 }
62
63 @Override
64 protected void doExecute(InstallContext ctx) throws RepositoryException, TaskExecutionException {
65
66 final Session session = ctx.getJCRSession(workspaceName);
67
68 if (!session.nodeExists(parentPath)) {
69 ctx.warn("Was supposed to remove property " + propertyToRemove + " at " + parentPath + " but the node was not found in workspace " + workspaceName);
70 return;
71 }
72 final Node node = session.getNode(parentPath);
73
74 if (node.hasProperty(propertyToRemove)) {
75 node.getProperty(propertyToRemove).remove();
76 } else {
77 ctx.info(parentPath + "/" + propertyToRemove + " was supposed to be removed, but wasn't found.");
78 }
79 }
80 }