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