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 java.util.Arrays;
40 import java.util.Collection;
41
42 import javax.jcr.Node;
43 import javax.jcr.RepositoryException;
44 import javax.jcr.Session;
45
46
47
48
49 public class CheckAndModifyPropertyValueTask extends PropertyValuesTask {
50 private final String workspaceName;
51 private final String nodePath;
52 private final String propertyName;
53 private final Collection<String> expectedCurrentValues;
54 private final String newValue;
55
56 public CheckAndModifyPropertyValueTask(String nodePath, String propertyName, String expectedCurrentValue, String newValue) {
57 this("Change property value if it has certain value", String.format("Changes property '%s' to '%s' if it has value '%s'.", nodePath + "/" + propertyName, newValue, expectedCurrentValue),
58 RepositoryConstants.CONFIG, nodePath, propertyName, Arrays.asList(expectedCurrentValue), newValue);
59 }
60
61 public CheckAndModifyPropertyValueTask(String workspaceName, String nodePath, String propertyName, Collection<String> expectedCurrentValues, String newValue) {
62 this("Change property if it has certain value", String.format("Changes property '%s' to '%s if it has one of expected values.", nodePath + "/" + propertyName, newValue),
63 workspaceName, nodePath, propertyName, expectedCurrentValues, newValue);
64 }
65
66 public CheckAndModifyPropertyValueTask(String name, String description, String workspaceName, String nodePath, String propertyName, String expectedCurrentValue, String newValue) {
67 this(name, description, workspaceName, nodePath, propertyName, Arrays.asList(expectedCurrentValue), newValue);
68 }
69
70 public CheckAndModifyPropertyValueTask(String name, String description, String workspaceName, String nodePath, String propertyName, Collection<String> expectedCurrentValues, String newValue) {
71 super(name, description);
72 this.workspaceName = workspaceName;
73 this.nodePath = nodePath;
74 this.propertyName = propertyName;
75 this.expectedCurrentValues = expectedCurrentValues;
76 this.newValue = newValue;
77 }
78
79 @Override
80 public void execute(InstallContext ctx) {
81 try {
82 final Session session = ctx.getJCRSession(workspaceName);
83 final Node node = session.getNode(nodePath);
84 checkAndModifyPropertyValue(ctx, node, propertyName, expectedCurrentValues, newValue);
85 } catch (RepositoryException e) {
86 ctx.error(format("Could not check property {0} of node at {1}. Please create it with value {2}.", propertyName, nodePath, newValue), e);
87 }
88 }
89 }