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.cms.util;
35
36 import info.magnolia.cms.core.AbstractContent;
37 import info.magnolia.cms.core.Content;
38 import info.magnolia.cms.core.ItemType;
39 import info.magnolia.cms.core.NodeData;
40
41 import java.util.ArrayList;
42 import java.util.Collection;
43 import java.util.Collections;
44 import java.util.Comparator;
45 import java.util.List;
46
47 import javax.jcr.PathNotFoundException;
48 import javax.jcr.RepositoryException;
49
50 import org.apache.commons.lang3.StringUtils;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 @Deprecated
74 public class InheritanceContentWrapper extends ContentWrapper {
75
76 private static Logger log = LoggerFactory.getLogger(InheritanceContentWrapper.class);
77
78
79
80
81 private final Content start;
82
83
84
85
86 public InheritanceContentWrapper(Content./../info/magnolia/cms/core/Content.html#Content">Content wrappedContent, Content start) {
87 super(wrappedContent);
88 this.start = start;
89 }
90
91
92
93
94 public InheritanceContentWrapper(Content node) {
95 this(node, node);
96 }
97
98 @Override
99 public boolean hasContent(String name) throws RepositoryException {
100 return getContentSafely(name) != null;
101 }
102
103 @Override
104 public Content getContent(String name) throws RepositoryException {
105 Content inherited = getContentSafely(name);
106 if (inherited == null) {
107 throw new PathNotFoundException("Can't inherit a node [" + name + "] on node [" + getWrappedContent().getHandle() + "]");
108 }
109 return inherited;
110 }
111
112 @Override
113 public Collection<Content> getChildren(ContentFilter filter, String namePattern, Comparator<Content> orderCriteria) {
114 List<Content> children = new ArrayList<Content>();
115
116
117 try {
118 Content inherited = getContentSafely(findNextAnchor(), resolveInnerPath());
119 if (inherited != null) {
120 children.addAll(((AbstractContent) inherited).getChildren(filter, namePattern, orderCriteria));
121 }
122 } catch (RepositoryException e) {
123 throw new RuntimeException("Can't inherit children from " + getWrappedContent(), e);
124 }
125
126
127 children.addAll(((AbstractContent) getWrappedContent()).getChildren(filter, namePattern, orderCriteria));
128 if (orderCriteria != null) {
129 Collections.sort(children, orderCriteria);
130 }
131
132 return wrapContentNodes(children);
133 }
134
135
136
137
138 protected String resolveInnerPath() throws RepositoryException {
139 final String path;
140 InheritanceContentWrapper anchor = findAnchor();
141
142 if (anchor == null) {
143 path = this.getHandle();
144 } else {
145 path = StringUtils.substringAfter(this.getHandle(), anchor.getHandle());
146 }
147 return StringUtils.removeStart(path, "/");
148 }
149
150
151
152
153 protected Content getContentSafely(String name) throws RepositoryException {
154 if (getWrappedContent().hasContent(name)) {
155 return super.getContent(name);
156 }
157
158 String innerPath = resolveInnerPath() + "/" + name;
159 innerPath = StringUtils.removeStart(innerPath, "/");
160
161 Content inherited = getContentSafely(findNextAnchor(), innerPath);
162 return inherited;
163 }
164
165
166
167
168 protected Content getContentSafely(InheritanceContentWrapper anchor, String path) throws RepositoryException {
169 if (anchor == null) {
170 return null;
171 }
172 if (StringUtils.isEmpty(path)) {
173 return anchor;
174 }
175 return anchor.getContentSafely(path);
176 }
177
178
179
180
181 protected InheritanceContentWrapper findAnchor() throws RepositoryException {
182 if (getLevel() == 0) {
183 return null;
184 }
185 if (isAnchor()) {
186 return this;
187 }
188
189 return ((InheritanceContentWrapper) getParent()).findAnchor();
190 }
191
192
193
194
195 protected InheritanceContentWrapper findNextAnchor() throws RepositoryException {
196 final InheritanceContentWrapper currentAnchor = findAnchor();
197 if (currentAnchor != null && getLevel() > 0) {
198 return ((InheritanceContentWrapper) currentAnchor.getParent()).findAnchor();
199 }
200 return null;
201 }
202
203
204
205
206 protected boolean isAnchor() {
207 return isNodeType(ItemType.CONTENT.getSystemName());
208 }
209
210 @Override
211 public NodeData getNodeData(String name) {
212 try {
213 if (getWrappedContent().hasNodeData(name)) {
214 return getWrappedContent().getNodeData(name);
215 }
216 Content inherited = getContentSafely(findNextAnchor(), resolveInnerPath());
217 if (inherited != null) {
218 return inherited.getNodeData(name);
219 }
220 } catch (RepositoryException e) {
221 throw new RuntimeException("Can't inherit nodedata " + name + " for " + getWrappedContent(), e);
222
223 }
224
225 return super.getNodeData(name);
226 }
227
228 @Override
229 public boolean hasNodeData(String name) throws RepositoryException {
230 try {
231 if (getWrappedContent().hasNodeData(name)) {
232 return getWrappedContent().hasNodeData(name);
233 }
234 Content inherited = getContentSafely(findNextAnchor(), resolveInnerPath());
235 if (inherited != null) {
236 return inherited.hasNodeData(name);
237 }
238 } catch (RepositoryException e) {
239 throw new RuntimeException("Can't inherit nodedata " + name + " for " + getWrappedContent(), e);
240
241 }
242
243 return super.hasNodeData(name);
244 }
245
246
247
248
249 @Override
250 protected Contentef="../../../../info/magnolia/cms/core/Content.html#Content">Content wrap(Content node) {
251
252 if (node instanceof InheritanceContentWrapper) {
253 return node;
254 }
255 return new InheritanceContentWrapper(node, start);
256 }
257
258
259
260
261 public boolean isInherited() {
262 return !getWrappedContent().getHandle().startsWith(start.getHandle());
263 }
264
265 }