View Javadoc
1   /**
2    * This file Copyright (c) 2007-2015 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  public abstract class ContentWrapper extends AbstractContent {
79  
80      private Content wrappedContent;
81  
82      /**
83       * @deprecated since 4.3 - use {@link #ContentWrapper(info.magnolia.cms.core.Content)} instead.
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       * @deprecated since 4.3 - use {@link #ContentWrapper(info.magnolia.cms.core.Content)} instead.
98       */
99      public void setWrappedContent(Content wrappedContent) {
100         this.wrappedContent = wrappedContent;
101     }
102 
103     /**
104      * Override if a wrapper wants to wrap returned content objects. This method is called by getContent(), getParent(), ...
105      * The default implementation does nothing.
106      */
107     protected Content wrap(Content node) {
108         return node;
109     }
110 
111     /**
112      * Override if a wrapper wants to wrap returned node data objects. The default implementation returns the original value.
113      */
114     protected NodeData wrap(NodeData nodeData) {
115         return nodeData;
116     }
117 
118     /**
119      * Override if a wrapper wants to wrap returned collections as well (by getChildren(..), ...
120      * Delegates to {@link #wrap(Content)}
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      * Override if a wrapper wants to wrap returned collections as well (by getChildren(..), ...
132      * Delegates to {@link #wrap(Content)}
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             // first get the children from the wrapped content
156             Collection<Content> children = ((AbstractContent) content).getChildren(ContentUtil.ALL_NODES_CONTENT_FILTER, namePattern, orderCriteria);
157             // wrap the children
158             Collection<Content> wrappedChildren = wrapContentNodes(children);
159             // now we can apply the filter which might depend on the behavior of the wrapper
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     // --- only wrapping methods below
181     // --- - methods returning Content should wrap it using #wrap()
182     // --- - methods returning Collection<Content> should wrap it with #wrapContentNodes()
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      * @deprecated since 4.3, either use {@link #getContent(String)} or {@link #getChildren(String)}
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 }