View Javadoc

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