View Javadoc

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