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.HierarchyManager;
39 import info.magnolia.cms.core.ItemType;
40 import info.magnolia.cms.core.MetaData;
41 import info.magnolia.cms.core.NodeData;
42 import info.magnolia.cms.core.version.ContentVersion;
43 import info.magnolia.cms.security.AccessDeniedException;
44
45 import java.util.ArrayList;
46 import java.util.Collection;
47 import java.util.Comparator;
48
49 import javax.jcr.Node;
50 import javax.jcr.RepositoryException;
51 import javax.jcr.Workspace;
52 import javax.jcr.lock.Lock;
53 import javax.jcr.nodetype.NodeType;
54 import javax.jcr.version.Version;
55 import javax.jcr.version.VersionHistory;
56 import javax.jcr.version.VersionIterator;
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 public abstract class ContentWrapper extends AbstractContent {
79
80 private Content wrappedContent;
81
82
83
84
85 public ContentWrapper() {
86 }
87
88 public ContentWrapper(Content wrappedContent) {
89 this.wrappedContent = wrappedContent;
90 }
91
92 public Content getWrappedContent() {
93 return this.wrappedContent;
94 }
95
96
97
98
99 public void setWrappedContent(Content wrappedContent) {
100 this.wrappedContent = wrappedContent;
101 }
102
103
104
105
106
107 protected Content wrap(Content node) {
108 return node;
109 }
110
111
112
113
114 protected NodeData wrap(NodeData nodeData) {
115 return nodeData;
116 }
117
118
119
120
121
122 protected Collection<Content> wrapContentNodes(Collection<Content> collection) {
123 ArrayList<Content> wrapped = new ArrayList<Content>();
124 for (Content content : collection) {
125 wrapped.add(wrap(content));
126 }
127 return wrapped;
128 }
129
130
131
132
133
134 protected Collection<NodeData> wrapNodeDatas(Collection<NodeData> collection) {
135 ArrayList<NodeData> wrapped = new ArrayList<NodeData>();
136 for (NodeData nodeData : collection) {
137 wrapped.add(wrap(nodeData));
138 }
139 return wrapped;
140 }
141
142 @Override
143 public String toString() {
144 final StringBuilder buffer = new StringBuilder();
145 buffer.append(getClass().getSimpleName());
146 buffer.append(" for ");
147 buffer.append(super.toString());
148 return buffer.toString();
149 }
150
151 @Override
152 public Collection<Content> getChildren(ContentFilter filter, String namePattern, Comparator<Content> orderCriteria) {
153 Content content = getWrappedContent();
154 if (content instanceof AbstractContent) {
155
156 Collection<Content> children = ((AbstractContent) content).getChildren(ContentUtil.ALL_NODES_CONTENT_FILTER, namePattern, orderCriteria);
157
158 Collection<Content> wrappedChildren = wrapContentNodes(children);
159
160 Collection<Content> filteredChildren = new ArrayList<Content>();
161 for (Content wrappedChild : wrappedChildren) {
162 if (filter.accept(wrappedChild)) {
163 filteredChildren.add(wrappedChild);
164 }
165 }
166 return filteredChildren;
167 }
168 throw new IllegalStateException("This wrapper supports only wrapping AbstractContent objects by default. Please override this method.");
169 }
170
171 @Override
172 public NodeData newNodeDataInstance(String name, int type, boolean createIfNotExisting) throws AccessDeniedException, RepositoryException {
173 Content content = getWrappedContent();
174 if (content instanceof AbstractContent) {
175 return wrap(((AbstractContent) content).newNodeDataInstance(name, type, createIfNotExisting));
176 }
177 throw new IllegalStateException("This wrapper supports only wrapping AbstractContent objects by default. Please override this method.");
178 }
179
180
181
182
183
184 @Override
185 public void addMixin(String type) throws RepositoryException {
186 this.getWrappedContent().addMixin(type);
187 }
188
189 @Override
190 public Version addVersion() throws RepositoryException {
191 return this.getWrappedContent().addVersion();
192 }
193
194 @Override
195 public Version addVersion(Rule rule) throws RepositoryException {
196 return this.getWrappedContent().addVersion(rule);
197 }
198
199 @Override
200 public Content createContent(String name, String contentType) throws RepositoryException {
201 return wrap(this.getWrappedContent().createContent(name, contentType));
202 }
203
204 @Override
205 public void delete() throws RepositoryException {
206 this.getWrappedContent().delete();
207 }
208
209 @Override
210 public void deleteNodeData(String name) throws RepositoryException {
211 this.getWrappedContent().deleteNodeData(name);
212 }
213
214 @Override
215 public VersionIterator getAllVersions() throws RepositoryException {
216 return this.getWrappedContent().getAllVersions();
217 }
218
219 @Override
220 public Content getAncestor(int level) throws RepositoryException {
221 return wrap(this.getWrappedContent().getAncestor(level));
222 }
223
224 @Override
225 public Collection<Content> getAncestors() throws RepositoryException {
226 return wrapContentNodes(this.getWrappedContent().getAncestors());
227 }
228
229 @Override
230 public ContentVersion getBaseVersion() throws RepositoryException {
231 return this.getWrappedContent().getBaseVersion();
232 }
233
234
235
236
237 @Override
238 public Content getChildByName(String namePattern) {
239 return wrap(this.getWrappedContent().getChildByName(namePattern));
240 }
241
242 @Override
243 public Content getContent(String name) throws RepositoryException {
244 return wrap(this.getWrappedContent().getContent(name));
245 }
246
247 @Override
248 public String getHandle() {
249 return this.getWrappedContent().getHandle();
250 }
251
252 @Override
253 public int getIndex() throws RepositoryException {
254 return this.getWrappedContent().getIndex();
255 }
256
257 @Override
258 public ItemType getItemType() throws RepositoryException {
259 return this.getWrappedContent().getItemType();
260 }
261
262 @Override
263 public Node getJCRNode() {
264 return this.getWrappedContent().getJCRNode();
265 }
266
267 @Override
268 public int getLevel() throws RepositoryException {
269 return this.getWrappedContent().getLevel();
270 }
271
272 @Override
273 public Lock getLock() throws RepositoryException {
274 return this.getWrappedContent().getLock();
275 }
276
277 @Override
278 public MetaData getMetaData() {
279 return this.getWrappedContent().getMetaData();
280 }
281
282 @Override
283 public NodeType[] getMixinNodeTypes() throws RepositoryException {
284 return this.getWrappedContent().getMixinNodeTypes();
285 }
286
287 @Override
288 public String getName() {
289 return this.getWrappedContent().getName();
290 }
291
292 @Override
293 public Collection<NodeData> getNodeDataCollection(String namePattern) {
294 return wrapNodeDatas(this.getWrappedContent().getNodeDataCollection(namePattern));
295 }
296
297 @Override
298 public NodeType getNodeType() throws RepositoryException {
299 return this.getWrappedContent().getNodeType();
300 }
301
302 @Override
303 public String getNodeTypeName() throws RepositoryException {
304 return this.getWrappedContent().getNodeTypeName();
305 }
306
307 @Override
308 public Content getParent() throws RepositoryException {
309 return wrap(this.getWrappedContent().getParent());
310 }
311
312 @Override
313 public String getTemplate() {
314 return this.getWrappedContent().getTemplate();
315 }
316
317 @Override
318 public String getTitle() {
319 return this.getWrappedContent().getTitle();
320 }
321
322 @Override
323 public String getUUID() {
324 return this.getWrappedContent().getUUID();
325 }
326
327 @Override
328 public ContentVersion getVersionedContent(String versionName) throws RepositoryException {
329 return this.getWrappedContent().getVersionedContent(versionName);
330 }
331
332 @Override
333 public ContentVersion getVersionedContent(Version version) throws RepositoryException {
334 return this.getWrappedContent().getVersionedContent(version);
335 }
336
337 @Override
338 public VersionHistory getVersionHistory() throws RepositoryException {
339 return this.getWrappedContent().getVersionHistory();
340 }
341
342 @Override
343 public Workspace getWorkspace() throws RepositoryException {
344 return this.getWrappedContent().getWorkspace();
345 }
346
347 @Override
348 public boolean hasContent(String name) throws RepositoryException {
349 return getWrappedContent().hasContent(name);
350 }
351
352 @Override
353 public boolean hasMetaData() {
354 return this.getWrappedContent().hasMetaData();
355 }
356
357 @Override
358 public boolean holdsLock() throws RepositoryException {
359 return this.getWrappedContent().holdsLock();
360 }
361
362 @Override
363 public boolean isGranted(long permissions) {
364 return this.getWrappedContent().isGranted(permissions);
365 }
366
367 @Override
368 public boolean isLocked() throws RepositoryException {
369 return this.getWrappedContent().isLocked();
370 }
371
372 @Override
373 public boolean isModified() {
374 return this.getWrappedContent().isModified();
375 }
376
377 @Override
378 public boolean isNodeData(String path) throws RepositoryException {
379 return this.getWrappedContent().isNodeData(path);
380 }
381
382 @Override
383 public boolean isNodeType(String type) {
384 return this.getWrappedContent().isNodeType(type);
385 }
386
387 @Override
388 public Lock lock(boolean isDeep, boolean isSessionScoped, long yieldFor) throws RepositoryException {
389 return this.getWrappedContent().lock(isDeep, isSessionScoped, yieldFor);
390 }
391
392 @Override
393 public Lock lock(boolean isDeep, boolean isSessionScoped) throws RepositoryException {
394 return this.getWrappedContent().lock(isDeep, isSessionScoped);
395 }
396
397 @Override
398 public void orderBefore(String srcName, String beforeName) throws RepositoryException {
399 this.getWrappedContent().orderBefore(srcName, beforeName);
400 }
401
402 @Override
403 public void refresh(boolean keepChanges) throws RepositoryException {
404 this.getWrappedContent().refresh(keepChanges);
405 }
406
407 @Override
408 public void removeMixin(String type) throws RepositoryException {
409 this.getWrappedContent().removeMixin(type);
410 }
411
412 @Override
413 public void removeVersionHistory() throws RepositoryException {
414 this.getWrappedContent().removeVersionHistory();
415 }
416
417 @Override
418 public void restore(String versionName, boolean removeExisting) throws RepositoryException {
419 this.getWrappedContent().restore(versionName, removeExisting);
420 }
421
422 @Override
423 public void restore(Version version, boolean removeExisting) throws RepositoryException {
424 this.getWrappedContent().restore(version, removeExisting);
425 }
426
427 @Override
428 public void restore(Version version, String relPath, boolean removeExisting) throws RepositoryException {
429 this.getWrappedContent().restore(version, relPath, removeExisting);
430 }
431
432 @Override
433 public void restoreByLabel(String versionLabel, boolean removeExisting) throws RepositoryException {
434 this.getWrappedContent().restoreByLabel(versionLabel, removeExisting);
435 }
436
437 @Override
438 public void save() throws RepositoryException {
439 this.getWrappedContent().save();
440 }
441
442 @Override
443 public void unlock() throws RepositoryException {
444 this.getWrappedContent().unlock();
445 }
446
447 @Override
448 public void updateMetaData() throws RepositoryException {
449 this.getWrappedContent().updateMetaData();
450 }
451
452 @Override
453 public HierarchyManager getHierarchyManager() {
454 return this.getWrappedContent().getHierarchyManager();
455 }
456
457 @Override
458 public boolean hasMixin(String mixinName) throws RepositoryException {
459 return this.getWrappedContent().hasMixin(mixinName);
460 }
461 }