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.cms.core.Content;
37 import info.magnolia.cms.core.NodeData;
38 import info.magnolia.module.InstallContext;
39 import info.magnolia.repository.RepositoryConstants;
40
41 import javax.jcr.RepositoryException;
42
43
44
45
46 public class PropertyValueDelegateTask extends NodeCheckDelegateTask {
47 private final String expectedValue;
48 private final boolean propertyMustExist;
49
50 public PropertyValueDelegateTask(String taskName, String parentPath, String propertyName, String expectedValue, boolean propertyMustExist, Task ifTrue) {
51 this(taskName, parentPath, propertyName, expectedValue, propertyMustExist, ifTrue, null);
52 }
53
54 public PropertyValueDelegateTask(String taskName, String parentPath, String propertyName, String expectedValue, boolean propertyMustExist, Task ifTrue, Task ifFalse) {
55 this(taskName, String.format("Execute task [%s] if property '%s' has value '%s' or [%s] if not.",
56 ifTrue == null ? DO_NOTHING : ifTrue.getDescription(),
57 parentPath + "/" + propertyName, expectedValue,
58 ifFalse == null ? DO_NOTHING : ifFalse.getDescription()),
59 RepositoryConstants.CONFIG, parentPath, propertyName, expectedValue, propertyMustExist, ifTrue, ifFalse);
60 }
61
62 public PropertyValueDelegateTask(String taskName, String taskDescription, String workspaceName, String nodePath, String propertyName, String expectedValue, boolean propertyMustExist, Task ifTrue) {
63 this(taskName, taskDescription, workspaceName, nodePath, propertyName, expectedValue, propertyMustExist, ifTrue, null);
64 }
65
66
67
68
69
70 public PropertyValueDelegateTask(String taskName, String taskDescription, String workspaceName, String nodePath, String propertyName, String expectedValue, boolean propertyMustExist, Task ifTrue, Task ifFalse) {
71 super(taskName, taskDescription, workspaceName, nodePath, propertyName, ifTrue, ifFalse);
72 this.expectedValue = expectedValue;
73 this.propertyMustExist = propertyMustExist;
74 }
75
76 @Override
77 protected boolean checkNode(Content node, InstallContext ctx) throws TaskExecutionException, RepositoryException {
78 if (node.hasNodeData(propertyName)) {
79 final NodeData prop = node.getNodeData(propertyName);
80 return expectedValue.equals(prop.getString());
81 } else if (propertyMustExist) {
82 throw new TaskExecutionException("Property \"" + propertyName + "\" was expected to exist at " + nodePath);
83 } else {
84 ctx.warn("Property \"" + propertyName + "\" was expected to be found at " + nodePath + " but does not exist.");
85 return false;
86 }
87 }
88 }