View Javadoc

1   /**
2    * This file Copyright (c) 2009-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.Content;
37  import info.magnolia.cms.core.HierarchyManager;
38  import info.magnolia.cms.core.ItemType;
39  import info.magnolia.cms.core.NodeData;
40  import info.magnolia.cms.core.search.QueryManager;
41  import info.magnolia.cms.security.AccessDeniedException;
42  import info.magnolia.cms.security.AccessManager;
43  
44  import javax.jcr.RepositoryException;
45  import javax.jcr.Workspace;
46  
47  /**
48   * A {@link HierarchyManager} wrapping an other hierarchy manager. Subclassed to manipulate the hierarchy.
49   * @version $Revision: $ ($Author: $)
50   */
51  public abstract class HierarchyManagerWrapper implements HierarchyManager {
52  
53      private final HierarchyManager wrappedHM;
54  
55      protected HierarchyManagerWrapper(HierarchyManager wrappedHM) {
56          this.wrappedHM = wrappedHM;
57      }
58  
59      public HierarchyManager getWrappedHierarchyManager() {
60          return wrappedHM;
61      }
62  
63      /**
64       * @deprecated since 4.3 use getWrappedHierarchyManager() instead
65       */
66      @Deprecated
67      public HierarchyManager getDelegate() {
68          return wrappedHM;
69      }
70  
71      public String toString() {
72          final StringBuilder buffer = new StringBuilder();
73          buffer.append(getClass().getSimpleName());
74          buffer.append(" for ");
75          buffer.append(getWrappedHierarchyManager().toString());
76          return buffer.toString();
77      }
78  
79  
80      /**
81       * Override this method to have hierarchy manager wrap every piece of content it returns.
82       * @param content unwrapped content
83       * @return wrapped content (or content passed in if not overriden)
84       */
85      protected Content wrap(Content content) {
86          return content;
87      }
88  
89      /**
90       * Override this method to have hierarchy manager wrap every piece of node data it returns.
91       * @param nodeData unwrapped node data
92       * @return wrapped node data (or node data passed in if not overriden)
93       */
94      protected NodeData wrap(NodeData nodeData) {
95          return nodeData;
96      }
97  
98      /**
99       * Override this method to alter all paths passed into the various hierarchy manager methods.
100      * @param path unaltered path
101      * @return wrapped path (or the one passed in if not overridden)
102      */
103     protected String transformPath(String path) {
104         return path;
105     }
106 
107     // ---- below are only generated wrappedHM methods
108     public AccessManager getAccessManager() {
109         return getWrappedHierarchyManager().getAccessManager();
110     }
111 
112     public QueryManager getQueryManager() {
113         return getWrappedHierarchyManager().getQueryManager();
114     }
115 
116     public Content createContent(String path, String label, String contentType) throws RepositoryException {
117         path = transformPath(path);
118         return wrap(getWrappedHierarchyManager().createContent(path, label, contentType));
119     }
120 
121     public Content getContent(String path) throws RepositoryException {
122         path = transformPath(path);
123         return wrap(getWrappedHierarchyManager().getContent(path));
124     }
125 
126     public Content getContent(String path, boolean create, ItemType type) throws RepositoryException {
127         path = transformPath(path);
128         return wrap(getWrappedHierarchyManager().getContent(path, create, type));
129     }
130 
131     public NodeData getNodeData(String path) throws RepositoryException {
132         path = transformPath(path);
133         return wrap(getWrappedHierarchyManager().getNodeData(path));
134     }
135 
136     /**
137      * @deprecated since 4.0 - use getContent().isNodeType() instead. (not used currently)
138       */
139      @Deprecated
140     public Content getPage(String path, String templateName) throws RepositoryException {
141         path = transformPath(path);
142         return wrap(getWrappedHierarchyManager().getPage(path, templateName));
143     }
144 
145     public void delete(String path) throws RepositoryException {
146         path = transformPath(path);
147         getWrappedHierarchyManager().delete(path);
148     }
149 
150     public Content getRoot() throws RepositoryException {
151         return wrap(getWrappedHierarchyManager().getRoot());
152     }
153 
154     /**
155      * @deprecated since 4.0 - use getContent().isNodeType() instead.
156       */
157      @Deprecated
158     public boolean isPage(String path) throws AccessDeniedException {
159         path = transformPath(path);
160         return getWrappedHierarchyManager().isPage(path);
161     }
162 
163     public boolean isExist(String path) {
164         path = transformPath(path);
165         return getWrappedHierarchyManager().isExist(path);
166     }
167 
168     public boolean isGranted(String path, long permissions) {
169         path = transformPath(path);
170         return getWrappedHierarchyManager().isGranted(path, permissions);
171     }
172 
173     /**
174      * @deprecated since 4.0 - use getContent().isNodeType() instead.
175       */
176      @Deprecated
177     public boolean isNodeType(String path, String type) {
178         path = transformPath(path);
179         return getWrappedHierarchyManager().isNodeType(path, type);
180     }
181 
182     /**
183     * @deprecated since 4.0 - use getContent().isNodeType() instead. (not used currently)
184      */
185     @Deprecated
186     public boolean isNodeType(String path, ItemType type) {
187         path = transformPath(path);
188         return getWrappedHierarchyManager().isNodeType(path, type);
189     }
190 
191     public boolean isNodeData(String path) throws AccessDeniedException {
192         path = transformPath(path);
193         return getWrappedHierarchyManager().isNodeData(path);
194     }
195 
196     public Content getContentByUUID(String uuid) throws RepositoryException {
197         return wrap(getWrappedHierarchyManager().getContentByUUID(uuid));
198     }
199 
200     public Workspace getWorkspace() {
201         return getWrappedHierarchyManager().getWorkspace();
202     }
203 
204     public void moveTo(String source, String destination) throws RepositoryException {
205         source = transformPath(source);
206         destination = transformPath(destination);
207         getWrappedHierarchyManager().moveTo(source, destination);
208     }
209 
210     public void copyTo(String source, String destination) throws RepositoryException {
211         source = transformPath(source);
212         destination = transformPath(destination);
213         getWrappedHierarchyManager().copyTo(source, destination);
214     }
215 
216     public void save() throws RepositoryException {
217         getWrappedHierarchyManager().save();
218     }
219 
220     public boolean hasPendingChanges() throws RepositoryException {
221         return getWrappedHierarchyManager().hasPendingChanges();
222     }
223 
224     public void refresh(boolean keepChanges) throws RepositoryException {
225         getWrappedHierarchyManager().refresh(keepChanges);
226     }
227 
228     public String getName() {
229         return getWrappedHierarchyManager().getName();
230     }
231 }