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