View Javadoc

1   /**
2    * This file Copyright (c) 2007-2011 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: ContentWrapper.java 41137 2011-01-06 18:19:25Z gjoseph $
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     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             // first get the children from the wrapped content
158             Collection<Content> children = ((AbstractContent) content).getChildren(ContentUtil.ALL_NODES_CONTENT_FILTER, namePattern, orderCriteria);
159             // wrap the children
160             Collection<Content> wrappedChildren = wrapContentNodes(children);
161             // now we can apply the filter which might depend on the behavior of the wrapper
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     // --- only wrapping methods below
183     // --- - methods returning Content should wrap it using #wrap()
184     // --- - methods returning Collection<Content> should wrap it with #wrapContentNodes()
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      * @deprecated since 4.3, either use {@link #getContent(String)} or {@link #getChildren(String)}
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 }