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 java.util.Arrays;
39 import java.util.List;
40
41 import javax.jcr.RepositoryException;
42 import javax.jcr.Session;
43
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47
48
49
50
51
52 public class RemovePropertiesTask extends AbstractRepositoryTask {
53
54 private static final Logger log = LoggerFactory.getLogger(RemovePropertiesTask.class);
55
56 private final String workspaceName;
57 private final List<String> pathsToRemove;
58 private final boolean displayInfoMessageForMissingNodes;
59
60 public RemovePropertiesTask(String name, String workspaceName, List<String> pathsToRemove, boolean displayInfoMessageForMissingNodes) {
61 this(name, String.format("Remove properties: '%s' from '%s' workspace.", Arrays.toString(pathsToRemove.toArray()), workspaceName),
62 workspaceName, pathsToRemove, displayInfoMessageForMissingNodes);
63 }
64
65 public RemovePropertiesTask(String name, String description, String workspaceName, List<String> pathsToRemove, boolean displayInfoMessageForMissingNodes) {
66 super(name, description);
67 this.pathsToRemove = pathsToRemove;
68 this.workspaceName = workspaceName;
69 this.displayInfoMessageForMissingNodes = displayInfoMessageForMissingNodes;
70 }
71
72 @Override
73 protected void doExecute(InstallContext ctx) throws RepositoryException, TaskExecutionException {
74 Session session = ctx.getJCRSession(this.workspaceName);
75
76 for (String path : this.pathsToRemove) {
77 if (!session.propertyExists(path)) {
78 if (this.displayInfoMessageForMissingNodes) {
79 ctx.info("Could not remove property '" + path + "' from workspace '" + this.workspaceName + "'. Property does not exist.");
80 }
81 log.debug("Could not remove property '{}' from workspace '{}'. Property does not exist.", path, this.workspaceName);
82 } else {
83 session.getProperty(path).remove();
84 }
85 }
86 }
87 }