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.util.ContentUtil;
38 import info.magnolia.module.InstallContext;
39
40 import javax.jcr.Node;
41 import javax.jcr.RepositoryException;
42 import javax.jcr.Session;
43
44 import org.apache.commons.lang3.StringUtils;
45
46
47
48
49
50
51 @Deprecated
52 public abstract class NodeCheckDelegateTask extends ConditionalDelegateTask {
53 protected final String workspaceName;
54 protected final String nodePath;
55 protected final String propertyName;
56
57 public NodeCheckDelegateTask(String taskName, String taskDescription, String workspaceName, String nodePath, String propertyName, Task ifTrue, Task ifFalse) {
58 super(taskName, taskDescription, ifTrue, ifFalse);
59 this.workspaceName = workspaceName;
60 this.nodePath = nodePath;
61 this.propertyName = propertyName;
62 }
63
64 @Override
65 protected boolean condition(InstallContext ctx) throws TaskExecutionException {
66 final Session session;
67 final Node rootNode;
68 final String nodePath = StringUtils.removeStart(this.nodePath, "/");
69
70 try {
71 session = ctx.getJCRSession(workspaceName);
72 rootNode = session.getRootNode();
73 } catch (RepositoryException e) {
74 throw new TaskExecutionException(String.format("Can't check for property %s in %s.", propertyName, nodePath), e);
75 }
76
77 try {
78 if (!rootNode.hasNode(nodePath)) {
79 return false;
80 }
81 } catch (RepositoryException e) {
82 return false;
83 }
84
85 try {
86 final Node node = rootNode.getNode(nodePath);
87 final Content content = ContentUtil.asContent(node);
88 return checkNode(content, ctx);
89 } catch (RepositoryException e) {
90 throw new TaskExecutionException(String.format("Can't check for property %s in %s.", propertyName, nodePath), e);
91 }
92 }
93
94 protected abstract boolean checkNode(Content node, InstallContext ctx) throws RepositoryException, TaskExecutionException;
95 }