View Javadoc

1   /**
2    * This file Copyright (c) 2007-2014 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * A base class to implement content wrappers. All returned content objects, including collections, are also wrapped by calling the wrapping methods.
60   * <p>
61   * The following methods you might want to override:
62   * <ul>
63   * <li>{@link #getChildren(info.magnolia.cms.core.Content.ContentFilter, String, java.util.Comparator)}</li>
64   * <li>{@link #hasContent(String)}</li>
65   * <li>{@link #getContent(String)}</li>
66   * <li>{@link #getNodeData(String)}</li>
67   * <li>{@link #getNodeDataCollection(String)}</li>
68   * <li>{@link #wrap(Content)}</li>
69   * <li>{@link #wrap(NodeData)}</li>
70   * </ul>
71   *
72   * This default implementation assumes that the wrapped content is of type {@link AbstractContent}. If not you have to override the following methods:
73   * <ul>
74   * <li>{@link #getChildren(info.magnolia.cms.core.Content.ContentFilter, String, java.util.Comparator)}</li>
75   * <li>{@link #newNodeDataInstance(String, int, boolean)}</li>
76   * </ul>
77   *
78   * @author pbaerfuss
79   * @version $Id$
80   *
81   */
82  public abstract class ContentWrapper extends AbstractContent {
83  
84      private Content wrappedContent;
85  
86      /**
87       * @deprecated since 4.3 - use {@link #ContentWrapper(info.magnolia.cms.core.Content)} instead.
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      * @deprecated since 4.3 - use {@link #ContentWrapper(info.magnolia.cms.core.Content)} instead.
102      */
103     public void setWrappedContent(Content wrappedContent) {
104         this.wrappedContent = wrappedContent;
105     }
106 
107     /**
108      * Override if a wrapper wants to wrap returned content objects. This method is called by getContent(), getParent(), ...
109      * The default implementation does nothing.
110      */
111     protected Content wrap(Content node) {
112         return node;
113     }
114 
115     /**
116      * Override if a wrapper wants to wrap returned node data objects. The default implementation returns the original value.
117      */
118     protected NodeData wrap(NodeData nodeData) {
119         return nodeData;
120     }
121 
122     /**
123      * Override if a wrapper wants to wrap returned collections as well (by getChildren(..), ...
124      * Delegates to {@link #wrap(Content)}
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      * Override if a wrapper wants to wrap returned collections as well (by getChildren(..), ...
136      * Delegates to {@link #wrap(Content)}
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             // first get the children from the wrapped content
160             Collection<Content> children = ((AbstractContent) content).getChildren(ContentUtil.ALL_NODES_CONTENT_FILTER, namePattern, orderCriteria);
161             // wrap the children
162             Collection<Content> wrappedChildren = wrapContentNodes(children);
163             // now we can apply the filter which might depend on the behavior of the wrapper
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     // --- only wrapping methods below
185     // --- - methods returning Content should wrap it using #wrap()
186     // --- - methods returning Collection<Content> should wrap it with #wrapContentNodes()
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      * @deprecated since 4.3, either use {@link #getContent(String)} or {@link #getChildren(String)}
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 }