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.jcr.wrapper;
35
36 import javax.jcr.Node;
37 import javax.jcr.Property;
38 import javax.jcr.RepositoryException;
39 import javax.jcr.Value;
40
41 import info.magnolia.exception.RuntimeRepositoryException;
42 import info.magnolia.jcr.decoration.NodePredicateContentDecorator;
43 import info.magnolia.jcr.predicate.AbstractPredicate;
44
45
46
47
48
49
50 public class ChannelVisibilityContentDecorator extends NodePredicateContentDecorator {
51
52 private static class ChannelVisibilityPredicate extends AbstractPredicate<Node> {
53
54 public static final String CHANNEL_PROPERTY_NAME = "channels";
55
56 private final String currentChannel;
57
58 private ChannelVisibilityPredicate(String currentChannel) {
59 this.currentChannel = currentChannel;
60 }
61
62 @Override
63 public boolean evaluateTyped(Node node) {
64 try {
65 if (currentChannel == null || currentChannel.equalsIgnoreCase("all")) {
66 return true;
67 }
68 if (node.hasProperty(CHANNEL_PROPERTY_NAME)) {
69 Property channel = node.getProperty(CHANNEL_PROPERTY_NAME);
70 if (channel.isMultiple()) {
71 Value[] values = channel.getValues();
72 if (values.length == 0) {
73 return true;
74 }
75 for (Value value : values) {
76 if (value.getString().equals(currentChannel)) {
77 return true;
78 }
79 }
80 return false;
81 }
82 }
83 return true;
84
85 } catch (RepositoryException e) {
86 throw new RuntimeRepositoryException(e);
87 }
88 }
89 }
90
91 public ChannelVisibilityContentDecorator(String currentChannel) {
92 super(new ChannelVisibilityPredicate(currentChannel));
93 }
94 }