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 public String toString() {
147 final StringBuilder buffer = new StringBuilder();
148 buffer.append(getClass().getSimpleName());
149 buffer.append(" for ");
150 buffer.append(super.toString());
151 return buffer.toString();
152 }
153
154 public Collection<Content> getChildren(ContentFilter filter, String namePattern, Comparator<Content> orderCriteria) {
155 Content content = getWrappedContent();
156 if(content instanceof AbstractContent){
157
158 Collection<Content> children = ((AbstractContent) content).getChildren(ContentUtil.ALL_NODES_CONTENT_FILTER, namePattern, orderCriteria);
159
160 Collection<Content> wrappedChildren = wrapContentNodes(children);
161
162 Collection<Content> filteredChildren = new ArrayList<Content>();
163 for (Content wrappedChild : wrappedChildren) {
164 if(filter.accept(wrappedChild)){
165 filteredChildren.add(wrappedChild);
166 }
167 }
168 return filteredChildren;
169 }
170 throw new IllegalStateException("This wrapper supports only wrapping AbstractContent objects by default. Please override this method.");
171 }
172
173 @Override
174 public NodeData newNodeDataInstance(String name, int type, boolean createIfNotExisting) throws AccessDeniedException, RepositoryException {
175 Content content = getWrappedContent();
176 if(content instanceof AbstractContent){
177 return wrap(((AbstractContent)content).newNodeDataInstance(name, type, createIfNotExisting));
178 }
179 throw new IllegalStateException("This wrapper supports only wrapping AbstractContent objects by default. Please override this method.");
180 }
181
182
183
184
185
186 public void addMixin(String type) throws RepositoryException {
187 this.getWrappedContent().addMixin(type);
188 }
189
190 public Version addVersion() throws RepositoryException {
191 return this.getWrappedContent().addVersion();
192 }
193
194 public Version addVersion(Rule rule) throws RepositoryException {
195 return this.getWrappedContent().addVersion(rule);
196 }
197
198 public Content createContent(String name, String contentType) throws RepositoryException {
199 return wrap(this.getWrappedContent().createContent(name, contentType));
200 }
201
202 public void delete() throws RepositoryException {
203 this.getWrappedContent().delete();
204 }
205
206 public void deleteNodeData(String name) throws RepositoryException {
207 this.getWrappedContent().deleteNodeData(name);
208 }
209
210 public VersionIterator getAllVersions() throws RepositoryException {
211 return this.getWrappedContent().getAllVersions();
212 }
213
214 public Content getAncestor(int level) throws RepositoryException {
215 return wrap(this.getWrappedContent().getAncestor(level));
216 }
217
218 public Collection<Content> getAncestors() throws RepositoryException {
219 return wrapContentNodes(this.getWrappedContent().getAncestors());
220 }
221
222 public ContentVersion getBaseVersion() throws RepositoryException {
223 return this.getWrappedContent().getBaseVersion();
224 }
225
226
227
228
229 public Content getChildByName(String namePattern) {
230 return wrap(this.getWrappedContent().getChildByName(namePattern));
231 }
232
233 public Content getContent(String name) throws RepositoryException {
234 return wrap(this.getWrappedContent().getContent(name));
235 }
236
237 public String getHandle() {
238 return this.getWrappedContent().getHandle();
239 }
240
241 public int getIndex() throws RepositoryException {
242 return this.getWrappedContent().getIndex();
243 }
244
245 public ItemType getItemType() throws RepositoryException {
246 return this.getWrappedContent().getItemType();
247 }
248
249 public Node getJCRNode() {
250 return this.getWrappedContent().getJCRNode();
251 }
252
253 public int getLevel() throws RepositoryException {
254 return this.getWrappedContent().getLevel();
255 }
256
257 public Lock getLock() throws RepositoryException {
258 return this.getWrappedContent().getLock();
259 }
260
261 public MetaData getMetaData() {
262 return this.getWrappedContent().getMetaData();
263 }
264
265 public NodeType[] getMixinNodeTypes() throws RepositoryException {
266 return this.getWrappedContent().getMixinNodeTypes();
267 }
268
269 public String getName() {
270 return this.getWrappedContent().getName();
271 }
272
273 public Collection<NodeData> getNodeDataCollection(String namePattern) {
274 return wrapNodeDatas(this.getWrappedContent().getNodeDataCollection(namePattern));
275 }
276
277 public NodeType getNodeType() throws RepositoryException {
278 return this.getWrappedContent().getNodeType();
279 }
280
281 public String getNodeTypeName() throws RepositoryException {
282 return this.getWrappedContent().getNodeTypeName();
283 }
284
285 public Content getParent() throws RepositoryException {
286 return wrap(this.getWrappedContent().getParent());
287 }
288
289 public String getTemplate() {
290 return this.getWrappedContent().getTemplate();
291 }
292
293 public String getTitle() {
294 return this.getWrappedContent().getTitle();
295 }
296
297 public String getUUID() {
298 return this.getWrappedContent().getUUID();
299 }
300
301 public ContentVersion getVersionedContent(String versionName) throws RepositoryException {
302 return this.getWrappedContent().getVersionedContent(versionName);
303 }
304
305 public ContentVersion getVersionedContent(Version version) throws RepositoryException {
306 return this.getWrappedContent().getVersionedContent(version);
307 }
308
309 public VersionHistory getVersionHistory() throws RepositoryException {
310 return this.getWrappedContent().getVersionHistory();
311 }
312
313 public Workspace getWorkspace() throws RepositoryException {
314 return this.getWrappedContent().getWorkspace();
315 }
316
317 public boolean hasContent(String name) throws RepositoryException {
318 return getWrappedContent().hasContent(name);
319 }
320
321 public boolean hasMetaData() {
322 return this.getWrappedContent().hasMetaData();
323 }
324
325 public boolean holdsLock() throws RepositoryException {
326 return this.getWrappedContent().holdsLock();
327 }
328
329 public boolean isGranted(long permissions) {
330 return this.getWrappedContent().isGranted(permissions);
331 }
332
333 public boolean isLocked() throws RepositoryException {
334 return this.getWrappedContent().isLocked();
335 }
336
337 public boolean isModified() {
338 return this.getWrappedContent().isModified();
339 }
340
341 public boolean isNodeData(String path) throws RepositoryException {
342 return this.getWrappedContent().isNodeData(path);
343 }
344
345 public boolean isNodeType(String type) {
346 return this.getWrappedContent().isNodeType(type);
347 }
348
349 public Lock lock(boolean isDeep, boolean isSessionScoped, long yieldFor) throws RepositoryException {
350 return this.getWrappedContent().lock(isDeep, isSessionScoped, yieldFor);
351 }
352
353 public Lock lock(boolean isDeep, boolean isSessionScoped) throws RepositoryException {
354 return this.getWrappedContent().lock(isDeep, isSessionScoped);
355 }
356
357 public void orderBefore(String srcName, String beforeName) throws RepositoryException {
358 this.getWrappedContent().orderBefore(srcName, beforeName);
359 }
360
361 public void refresh(boolean keepChanges) throws RepositoryException {
362 this.getWrappedContent().refresh(keepChanges);
363 }
364
365 public void removeMixin(String type) throws RepositoryException {
366 this.getWrappedContent().removeMixin(type);
367 }
368
369 public void removeVersionHistory() throws RepositoryException {
370 this.getWrappedContent().removeVersionHistory();
371 }
372
373 public void restore(String versionName, boolean removeExisting) throws RepositoryException {
374 this.getWrappedContent().restore(versionName, removeExisting);
375 }
376
377 public void restore(Version version, boolean removeExisting) throws RepositoryException {
378 this.getWrappedContent().restore(version, removeExisting);
379 }
380
381 public void restore(Version version, String relPath, boolean removeExisting) throws RepositoryException {
382 this.getWrappedContent().restore(version, relPath, removeExisting);
383 }
384
385 public void restoreByLabel(String versionLabel, boolean removeExisting) throws RepositoryException {
386 this.getWrappedContent().restoreByLabel(versionLabel, removeExisting);
387 }
388
389 public void save() throws RepositoryException {
390 this.getWrappedContent().save();
391 }
392
393 public void unlock() throws RepositoryException {
394 this.getWrappedContent().unlock();
395 }
396
397 public void updateMetaData() throws RepositoryException {
398 this.getWrappedContent().updateMetaData();
399 }
400
401 public HierarchyManager getHierarchyManager(){
402 return this.getWrappedContent().getHierarchyManager();
403 }
404
405 public boolean hasMixin(String mixinName) throws RepositoryException {
406 return this.getWrappedContent().hasMixin(mixinName);
407 }
408 }