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