View Javadoc

1   /**
2    * This file Copyright (c) 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.core.version;
35  
36  import info.magnolia.cms.core.ItemType;
37  import info.magnolia.jcr.wrapper.DelegateNodeWrapper;
38  import info.magnolia.jcr.wrapper.DelegatePropertyWrapper;
39  import info.magnolia.jcr.wrapper.PropertyWrappingNodeWrapper;
40  
41  import java.util.Calendar;
42  
43  import javax.jcr.AccessDeniedException;
44  import javax.jcr.Item;
45  import javax.jcr.ItemNotFoundException;
46  import javax.jcr.Node;
47  import javax.jcr.PathNotFoundException;
48  import javax.jcr.Property;
49  import javax.jcr.RepositoryException;
50  import javax.jcr.Session;
51  import javax.jcr.nodetype.NodeType;
52  import javax.jcr.version.Version;
53  import javax.jcr.version.VersionHistory;
54  
55  /**
56   * Wrapper for version of the node exposing frozen node content as its own as used to happen in old Content API.
57   *
58   * @version $Id$
59   */
60  public class VersionedNode extends PropertyWrappingNodeWrapper implements Version {
61  
62  
63      private final Version version;
64      private final Node baseNode;
65  
66      private class VersionedNodeChild extends PropertyWrappingNodeWrapper implements Node {
67  
68          private final DelegateNodeWrapper versionedParent;
69  
70          public VersionedNodeChild(VersionedNode versionedNode, Node node) {
71              super(node);
72              this.versionedParent = versionedNode;
73          }
74  
75          public VersionedNodeChild(VersionedNodeChild versionedNode, Node node) {
76              super(node);
77              this.versionedParent = versionedNode;
78          }
79  
80          @Override
81          public int getDepth() throws RepositoryException {
82              return this.versionedParent.getDepth() + 1;
83          }
84  
85          @Override
86          public Item getAncestor(int depth) throws ItemNotFoundException, AccessDeniedException, RepositoryException {
87              // at some point we will enter the real hierarchy, so its easiest to loop
88              Node parent = getParent();
89              while(parent.getDepth() > depth){
90                  parent = parent.getParent();
91              }
92              return parent;
93          }
94  
95  
96  
97          @Override
98          public Node getParent() throws ItemNotFoundException,
99          AccessDeniedException, RepositoryException {
100             return this.versionedParent;
101         }
102 
103         @Override
104         public String getPath() throws RepositoryException {
105             return this.versionedParent.getPath() + "/" + getName();
106         }
107 
108         @Override
109         public NodeType getPrimaryNodeType() throws RepositoryException {
110             return getSession().getWorkspace().getNodeTypeManager().getNodeType(getWrappedNode().getProperty(ItemType.JCR_FROZEN_PRIMARY_TYPE).getString());
111         }
112 
113         @Override
114         public boolean isNodeType(String nodeTypeName) throws RepositoryException {
115             return getPrimaryNodeType().isNodeType(nodeTypeName);
116         }
117 
118         @Override
119         public Property wrapProperty(Property property) {
120             return new DelegatePropertyWrapper(property) {
121                 @Override
122                 public String getPath() throws RepositoryException {
123                     return VersionedNodeChild.this.getPath() + "/" + getName();
124                 }
125             };
126         }
127 
128         @Override
129         public Node wrapNode(Node node) {
130             return new VersionedNodeChild(this, node);
131         }
132     }
133 
134     public VersionedNode(Version versionedNode, Node baseNode) throws PathNotFoundException, RepositoryException {
135         super(versionedNode.getNode(ItemType.JCR_FROZENNODE));
136         this.version = versionedNode;
137         this.baseNode = baseNode;
138     }
139 
140     @Override
141     public int getDepth() throws RepositoryException {
142         return this.baseNode.getDepth();
143     }
144 
145     @Override
146     public Item getAncestor(int depth) throws ItemNotFoundException, AccessDeniedException, RepositoryException {
147         return this.baseNode.getAncestor(depth);
148     }
149 
150     public Version unwrap() {
151         return version;
152     }
153 
154     @Override
155     public VersionHistory getContainingHistory() throws RepositoryException {
156         return version.getContainingHistory();
157     }
158 
159     @Override
160     public Calendar getCreated() throws RepositoryException {
161         return version.getCreated();
162     }
163 
164     @Override
165     public Node getFrozenNode() throws RepositoryException {
166         return deepUnwrap(getClass());
167     }
168 
169     @Override
170     public Version getLinearPredecessor() throws RepositoryException {
171         return version.getLinearPredecessor();
172     }
173 
174     @Override
175     public Version getLinearSuccessor() throws RepositoryException {
176         return version.getLinearSuccessor();
177     }
178 
179     @Override
180     public Version[] getPredecessors() throws RepositoryException {
181         return null;
182     }
183 
184     @Override
185     public Version[] getSuccessors() throws RepositoryException {
186         return version.getSuccessors();
187     }
188 
189     @Override
190     public Node wrapNode(Node node) {
191         return new VersionedNodeChild(this, node);
192     }
193 
194     @Override
195     public Property wrapProperty(Property property) {
196         return new DelegatePropertyWrapper(property) {
197             @Override
198             public String getPath() throws RepositoryException {
199                 return getPath() + "/" + getName();
200             }
201         };
202     }
203 
204     @Override
205     public String getPath() throws RepositoryException {
206         return baseNode.getPath();
207     }
208 
209     @Override
210     public Node getParent() throws ItemNotFoundException,
211     AccessDeniedException, RepositoryException {
212         return baseNode.getParent();
213     }
214 
215     @Override
216     public boolean isNodeType(String nodeTypeName) throws RepositoryException {
217         return baseNode.isNodeType(nodeTypeName);
218     }
219 
220     @Override
221     public NodeType getPrimaryNodeType() throws RepositoryException {
222         return baseNode.getPrimaryNodeType();
223     }
224 
225     public Node getBaseNode() {
226         return baseNode;
227     }
228 
229     @Override
230     public Session getSession() throws RepositoryException {
231         return baseNode.getSession();
232     }
233 }