View Javadoc

1   /**
2    * This file Copyright (c) 2011-2013 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.jcr.decoration;
35  
36  import info.magnolia.jcr.wrapper.DelegateNodeWrapper;
37  
38  import java.io.InputStream;
39  import java.math.BigDecimal;
40  import java.util.Calendar;
41  
42  import javax.jcr.AccessDeniedException;
43  import javax.jcr.Binary;
44  import javax.jcr.Item;
45  import javax.jcr.ItemExistsException;
46  import javax.jcr.ItemNotFoundException;
47  import javax.jcr.Node;
48  import javax.jcr.NodeIterator;
49  import javax.jcr.PathNotFoundException;
50  import javax.jcr.Property;
51  import javax.jcr.PropertyIterator;
52  import javax.jcr.RepositoryException;
53  import javax.jcr.Session;
54  import javax.jcr.Value;
55  import javax.jcr.ValueFormatException;
56  import javax.jcr.lock.LockException;
57  import javax.jcr.nodetype.ConstraintViolationException;
58  import javax.jcr.nodetype.NoSuchNodeTypeException;
59  import javax.jcr.version.VersionException;
60  
61  /**
62   * Node wrapper that applies wrappers and filtering by delegating to a {@link ContentDecorator}.
63   *
64   * @param <D> implementation of decorator.
65   */
66  public class ContentDecoratorNodeWrapper<D extends ContentDecorator> extends DelegateNodeWrapper {
67  
68      private final D contentDecorator;
69  
70      public ContentDecoratorNodeWrapper(Node node, D contentDecorator) {
71          this.contentDecorator = contentDecorator;
72          setWrappedNode(node);
73      }
74  
75      @Override
76      public void setWrappedNode(Node node) {
77          if (!contentDecorator.isMultipleWrapEnabled()) {
78              super.setWrappedNode(node);
79          } else {
80              this.wrapped = node;
81          }
82      }
83  
84      public D getContentDecorator() {
85          return contentDecorator;
86      }
87  
88      @Override
89      public Session getSession() throws RepositoryException {
90          return wrapSession(super.getSession());
91      }
92  
93      @Override
94      public Item getAncestor(int depth) throws ItemNotFoundException, AccessDeniedException, RepositoryException {
95          Item item = super.getAncestor(depth);
96          if (item.isNode()) {
97              return wrapNode((Node) item);
98          } else {
99              return wrapProperty((Property) item);
100         }
101     }
102 
103     @Override
104     public Node getParent() throws ItemNotFoundException, AccessDeniedException, RepositoryException {
105         return wrapNode(super.getParent());
106     }
107 
108     @Override
109     public Node addNode(String relPath) throws ItemExistsException, PathNotFoundException, VersionException, ConstraintViolationException, LockException, RepositoryException {
110         return wrapNode(super.addNode(relPath));
111     }
112 
113     @Override
114     public Node addNode(String relPath, String primaryNodeTypeName) throws ItemExistsException, PathNotFoundException, NoSuchNodeTypeException, LockException, VersionException, ConstraintViolationException, RepositoryException {
115         return wrapNode(super.addNode(relPath, primaryNodeTypeName));
116     }
117 
118     @Override
119     public Node getNode(String relPath) throws PathNotFoundException, RepositoryException {
120         Node node = super.getNode(relPath);
121         if (!contentDecorator.evaluateNode(node)) {
122             throw new PathNotFoundException(relPath);
123         }
124         return wrapNode(node);
125     }
126 
127     @Override
128     public NodeIterator getNodes() throws RepositoryException {
129         return wrapNodeIterator(super.getNodes());
130     }
131 
132     @Override
133     public NodeIterator getNodes(String namePattern) throws RepositoryException {
134         return wrapNodeIterator(super.getNodes(namePattern));
135     }
136 
137     @Override
138     public NodeIterator getNodes(String[] nameGlobs) throws RepositoryException {
139         return wrapNodeIterator(super.getNodes(nameGlobs));
140     }
141 
142     @Override
143     public boolean hasNode(String relPath) throws RepositoryException {
144         return super.hasNode(relPath) && contentDecorator.evaluateNode(super.getNode(relPath));
145     }
146 
147     @Override
148     public boolean hasNodes() throws RepositoryException {
149         return getNodes().hasNext();
150     }
151 
152     @Override
153     public PropertyIterator getProperties() throws RepositoryException {
154         return wrapPropertyIterator(super.getProperties());
155     }
156 
157     @Override
158     public PropertyIterator getProperties(String namePattern) throws RepositoryException {
159         return wrapPropertyIterator(super.getProperties(namePattern));
160     }
161 
162     @Override
163     public PropertyIterator getProperties(String[] nameGlobs) throws RepositoryException {
164         return wrapPropertyIterator(super.getProperties(nameGlobs));
165     }
166 
167     @Override
168     public PropertyIterator getWeakReferences() throws RepositoryException {
169         return wrapPropertyIterator(super.getWeakReferences());
170     }
171 
172     @Override
173     public Property getProperty(String relPath) throws PathNotFoundException, RepositoryException {
174         return wrapProperty(super.getProperty(relPath));
175     }
176 
177     @Override
178     public PropertyIterator getReferences(String name) throws RepositoryException {
179         return wrapPropertyIterator(super.getReferences(name));
180     }
181 
182     @Override
183     public PropertyIterator getWeakReferences(String name) throws RepositoryException {
184         return wrapPropertyIterator(super.getWeakReferences(name));
185     }
186 
187     @Override
188     public PropertyIterator getReferences() throws RepositoryException {
189         return wrapPropertyIterator(super.getReferences());
190     }
191 
192     @Override
193     public Property setProperty(String name, BigDecimal value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
194         return wrapProperty(super.setProperty(name, value));
195     }
196 
197     @Override
198     public Property setProperty(String name, Binary value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
199         return wrapProperty(super.setProperty(name, value));
200     }
201 
202     @Override
203     public Property setProperty(String name, boolean value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
204         return wrapProperty(super.setProperty(name, value));
205     }
206 
207     @Override
208     public Property setProperty(String name, Calendar value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
209         return wrapProperty(super.setProperty(name, value));
210     }
211 
212     @Override
213     public Property setProperty(String name, double value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
214         return wrapProperty(super.setProperty(name, value));
215     }
216 
217     @Override
218     public Property setProperty(String name, InputStream value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
219         return wrapProperty(super.setProperty(name, value));
220     }
221 
222     @Override
223     public Property setProperty(String name, long value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
224         return wrapProperty(super.setProperty(name, value));
225     }
226 
227     @Override
228     public Property setProperty(String name, Node value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
229         return wrapProperty(super.setProperty(name, value));
230     }
231 
232     @Override
233     public Property setProperty(String name, String value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
234         return wrapProperty(super.setProperty(name, value));
235     }
236 
237     @Override
238     public Property setProperty(String name, String value, int type) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
239         return wrapProperty(super.setProperty(name, value, type));
240     }
241 
242     @Override
243     public Property setProperty(String name, String[] values) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
244         return wrapProperty(super.setProperty(name, values));
245     }
246 
247     @Override
248     public Property setProperty(String name, String[] values, int type) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
249         return wrapProperty(super.setProperty(name, values, type));
250     }
251 
252     @Override
253     public Property setProperty(String name, Value value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
254         return wrapProperty(super.setProperty(name, value));
255     }
256 
257     @Override
258     public Property setProperty(String name, Value value, int type) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
259         return wrapProperty(super.setProperty(name, value, type));
260     }
261 
262     @Override
263     public Property setProperty(String name, Value[] values) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
264         return wrapProperty(super.setProperty(name, values));
265     }
266 
267     @Override
268     public Property setProperty(String name, Value[] values, int type) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
269         return wrapProperty(super.setProperty(name, values, type));
270     }
271 
272     protected Session wrapSession(Session session) {
273         return contentDecorator.wrapSession(session);
274     }
275 
276     protected Node wrapNode(Node node) {
277         return contentDecorator.wrapNode(node);
278     }
279 
280     protected NodeIterator wrapNodeIterator(NodeIterator nodeIterator) {
281         return contentDecorator.wrapNodeIterator(nodeIterator);
282     }
283 
284     protected Property wrapProperty(Property property) {
285         return contentDecorator.wrapProperty(property);
286     }
287 
288     protected PropertyIterator wrapPropertyIterator(PropertyIterator propertyIterator) {
289         return contentDecorator.wrapPropertyIterator(propertyIterator);
290     }
291 
292     @Override
293     public boolean equals(Object obj) {
294         if (obj == null) {
295             return false;
296         }
297 
298         if (!(obj instanceof ContentDecoratorNodeWrapper)) {
299             return false;
300         }
301         ContentDecoratorNodeWrapper that = (ContentDecoratorNodeWrapper) obj;
302         return (this.wrapped == null ? that.wrapped == null : this.wrapped.equals(that.wrapped))
303                 && (this.contentDecorator == null ? that.contentDecorator == null : this.contentDecorator.equals(that.contentDecorator));
304     }
305 
306     @Override
307     public int hashCode() {
308         return (this.wrapped == null ? 7 : this.wrapped.hashCode()) + (this.contentDecorator == null ? 6 : this.contentDecorator.hashCode());
309     }
310 }