1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
57
58
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
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 }