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.rest.delivery.jcr.filter;
35
36 import info.magnolia.jcr.decoration.ContentDecoratorNodeIterator;
37 import info.magnolia.jcr.decoration.ContentDecoratorNodeWrapper;
38 import info.magnolia.jcr.predicate.JCRMgnlPropertyHidingPredicate;
39 import info.magnolia.jcr.wrapper.ChildFilteringContentDecorator;
40 import info.magnolia.jcr.wrapper.PropertyFilteringContentDecorator;
41 import info.magnolia.rest.delivery.jcr.decorator.ReferenceExpandDecorator;
42 import info.magnolia.rest.delivery.jcr.decorator.ReferenceExpandWrapper;
43 import info.magnolia.rest.reference.ReferenceDefinition;
44
45 import java.util.ArrayList;
46 import java.util.List;
47
48 import javax.jcr.Node;
49 import javax.jcr.NodeIterator;
50 import javax.jcr.RepositoryException;
51
52
53
54
55 public class FilteringContentDecoratorBuilder {
56 private List<String> childNodeTypes = new ArrayList<>();
57 private boolean includeSystemProperties;
58 private int depth;
59 private List<ReferenceDefinition> references = new ArrayList<>();
60
61 public FilteringContentDecoratorBuilder childNodeTypes(List<String> childrenNodeTypes) {
62 this.childNodeTypes.clear();
63 if (childrenNodeTypes != null) {
64 this.childNodeTypes.addAll(childrenNodeTypes);
65 }
66 return this;
67 }
68
69 public FilteringContentDecoratorBuilder includeSystemProperties(boolean includeSystemProperties) {
70 this.includeSystemProperties = includeSystemProperties;
71 return this;
72 }
73
74 public FilteringContentDecoratorBuilder depth(int depth) {
75 this.depth = depth;
76 return this;
77 }
78
79
80
81
82 @Deprecated
83 public FilteringContentDecoratorBuilder references(List<ReferenceDefinition> references) {
84 this.references.clear();
85 if (references != null) {
86 this.references.addAll(references);
87 }
88 return this;
89 }
90
91 public Node wrapNode(Node node) {
92 if (!childNodeTypes.isEmpty()) {
93 node = new ContentDecoratorNodeWrapper<>(node, new ChildFilteringContentDecorator(new NodeTypesPredicate(childNodeTypes), true));
94 }
95
96 if (!includeSystemProperties) {
97 node = new ContentDecoratorNodeWrapper<>(node, new PropertyFilteringContentDecorator(new JCRMgnlPropertyHidingPredicate()));
98 }
99
100 if (depth >= 0) {
101 ChildFilteringContentDecorator depthDecorator = new ChildFilteringContentDecorator(new DepthFilteringPredicate(getDepth(node), getDepth(node) + depth), true);
102 node = new ContentDecoratorNodeWrapper<>(node, depthDecorator);
103 }
104
105
106 if (!references.isEmpty()) {
107 FilteringContentDecoratorBuilder expandDecorators = new FilteringContentDecoratorBuilder();
108 expandDecorators
109 .includeSystemProperties(includeSystemProperties)
110 .depth(-1);
111
112 node = new ReferenceExpandWrapper(node, references, expandDecorators);
113 }
114
115 return node;
116 }
117
118 public NodeIterator wrapNodeIterator(NodeIterator nodeIterator) {
119
120 if (!childNodeTypes.isEmpty()) {
121 nodeIterator = new ChildNodeTypesFilteringNodeIterator(nodeIterator, childNodeTypes);
122 }
123
124 if (!includeSystemProperties) {
125 nodeIterator = new ContentDecoratorNodeIterator(nodeIterator, new PropertyFilteringContentDecorator(new JCRMgnlPropertyHidingPredicate()));
126 }
127
128 if (depth >= 0) {
129 nodeIterator = new DepthFilteringDecoratorNodeIterator(nodeIterator, depth);
130 }
131
132
133 if (!references.isEmpty()) {
134 FilteringContentDecoratorBuilder expandDecorators = new FilteringContentDecoratorBuilder();
135 expandDecorators
136 .includeSystemProperties(includeSystemProperties)
137 .depth(-1);
138
139 nodeIterator = new ContentDecoratorNodeIterator(nodeIterator, new ReferenceExpandDecorator(references, expandDecorators));
140 }
141
142 return nodeIterator;
143 }
144
145 private int getDepth(Node node) {
146 try {
147 return node.getDepth();
148 } catch (RepositoryException e) {
149 return 0;
150 }
151 }
152 }