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.pages.app.availability;
35
36 import info.magnolia.jcr.util.NodeTypes;
37 import info.magnolia.jcr.util.NodeUtil;
38 import info.magnolia.jcr.util.SessionUtil;
39 import info.magnolia.ui.api.availability.AbstractAvailabilityRule;
40 import info.magnolia.ui.vaadin.integration.jcr.JcrItemId;
41 import info.magnolia.ui.vaadin.integration.jcr.JcrPropertyItemId;
42
43 import javax.jcr.Node;
44 import javax.jcr.RepositoryException;
45
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49
50
51
52 public class PageHasSubpagesRule extends AbstractAvailabilityRule {
53
54 private static final Logger log = LoggerFactory.getLogger(PageHasSubpagesRule.class);
55
56 @Override
57 public boolean isAvailableForItem(Object itemId) {
58
59 if (itemId instanceof JcrItemId && !(itemId instanceof JcrPropertyItemId)) {
60 JcrItemId jcrItemId = (JcrItemId) itemId;
61 Node node = SessionUtil.getNodeByIdentifier(jcrItemId.getWorkspace(), jcrItemId.getUuid());
62 if (node != null) {
63 try {
64
65 if (NodeUtil.isNodeType(node, NodeTypes.Page.NAME)) {
66
67 return NodeUtil.getNodes(node, NodeTypes.Page.NAME).iterator().hasNext();
68 }
69 } catch (RepositoryException e) {
70 String path = "unknown";
71 try {
72 path = node.getPath();
73 } catch (RepositoryException e1) {
74
75 }
76 log.warn("Error evaluating availability for node [{}], returning false: {}", path, e.getMessage());
77 }
78 }
79 }
80 return false;
81 }
82 }