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.rendering.template.configured;
35
36 import info.magnolia.cms.core.MgnlNodeType;
37 import info.magnolia.jcr.RuntimeRepositoryException;
38 import info.magnolia.jcr.predicate.AbstractPredicate;
39 import info.magnolia.jcr.util.NodeUtil;
40 import info.magnolia.objectfactory.Components;
41 import info.magnolia.rendering.template.InheritanceConfiguration;
42 import org.apache.commons.lang.StringUtils;
43
44 import javax.jcr.Node;
45 import javax.jcr.RepositoryException;
46 import java.util.Comparator;
47
48
49
50
51
52
53 public class ConfiguredInheritance implements InheritanceConfiguration {
54
55 public static final String COMPONENTS_ALL = "all";
56 public static final String COMPONENTS_NONE = "none";
57 public static final String COMPONENTS_FILTERED = "filtered";
58
59 public static final String PROPERTIES_ALL = "all";
60 public static final String PROPERTIES_NONE = "none";
61
62 private boolean enabled = false;
63 private String components = COMPONENTS_FILTERED;
64 private String properties = PROPERTIES_ALL;
65 private Class<? extends AbstractPredicate<Node>> predicateClass;
66 private Class<? extends Comparator<Node>> nodeComparatorClass;
67
68 @Override
69 public boolean isEnabled() {
70 return enabled;
71 }
72
73 public void setEnabled(boolean enabled) {
74 this.enabled = enabled;
75 }
76
77 public void setComponents(String components) {
78 this.components = components;
79 }
80
81 public void setProperties(String properties) {
82 this.properties = properties;
83 }
84
85 @Override
86 public boolean isInheritsProperties() {
87 return isEnabled() && StringUtils.equalsIgnoreCase(StringUtils.trim(properties), PROPERTIES_ALL);
88 }
89
90 @Override
91 public boolean isInheritsComponents() {
92 return isEnabled() && (StringUtils.equalsIgnoreCase(StringUtils.trim(components), COMPONENTS_ALL) || StringUtils.equalsIgnoreCase(StringUtils.trim(components), COMPONENTS_FILTERED));
93 }
94
95 @Override
96 public AbstractPredicate<Node> getComponentPredicate() {
97 if (!isEnabled()) {
98 return new InheritNothingInheritancePredicate();
99 }
100 if (predicateClass != null) {
101 return Components.newInstance(predicateClass);
102 }
103 if (StringUtils.equalsIgnoreCase(StringUtils.trim(components), COMPONENTS_ALL)) {
104 return new AllComponentsInheritancePredicate();
105 }
106 if (StringUtils.equalsIgnoreCase(StringUtils.trim(components), COMPONENTS_FILTERED)) {
107 return new FilteredComponentInheritancePredicate();
108 }
109 return new InheritNothingInheritancePredicate();
110 }
111
112 public void setPredicateClass(Class<? extends AbstractPredicate<Node>> predicateClass) {
113 this.predicateClass = predicateClass;
114 }
115
116 @Override
117 public Comparator<Node> getComponentComparator() {
118 if (nodeComparatorClass != null) {
119 return Components.newInstance(nodeComparatorClass);
120 }
121 return new NodeDepthComparator();
122 }
123
124 public void setNodeComparatorClass(Class<? extends Comparator<Node>> nodeComparatorClass) {
125 this.nodeComparatorClass = nodeComparatorClass;
126 }
127
128
129
130
131
132 public static class FilteredComponentInheritancePredicate extends AbstractPredicate<Node> {
133
134 public static final String INHERITED_PROPERTY_NAME = "inheritable";
135
136 @Override
137 public boolean evaluateTyped(Node node) {
138 try {
139 return NodeUtil.isNodeType(node, MgnlNodeType.NT_COMPONENT) && (node.hasProperty(INHERITED_PROPERTY_NAME) && Boolean.parseBoolean(node.getProperty(INHERITED_PROPERTY_NAME).getString()));
140 } catch (RepositoryException e) {
141 throw new RuntimeRepositoryException(e);
142 }
143 }
144 }
145
146
147
148
149 public static class AllComponentsInheritancePredicate extends AbstractPredicate<Node> {
150
151 @Override
152 public boolean evaluateTyped(Node node) {
153 try {
154 return NodeUtil.isNodeType(node, MgnlNodeType.NT_COMPONENT);
155 } catch (RepositoryException e) {
156 throw new RuntimeRepositoryException(e);
157 }
158 }
159 }
160
161
162
163
164 public static class InheritNothingInheritancePredicate extends AbstractPredicate<Node> {
165
166 @Override
167 public boolean evaluateTyped(Node node) {
168 return false;
169 }
170 }
171
172
173
174
175 public static class NodeDepthComparator implements Comparator<Node> {
176 @Override
177 public int compare(Node lhs, Node rhs) {
178 try {
179 return rhs.getDepth() - lhs.getDepth();
180 } catch (RepositoryException e) {
181 throw new RuntimeRepositoryException(e);
182 }
183 }
184 }
185 }