1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.jcr;
25
26 import java.util.ArrayList;
27 import java.util.Calendar;
28 import java.util.List;
29 import javax.jcr.ItemNotFoundException;
30 import javax.jcr.Property;
31 import javax.jcr.RepositoryException;
32 import javax.jcr.Value;
33 import javax.jcr.version.Version;
34 import org.modeshape.graph.property.Name;
35
36
37
38
39 class JcrVersionNode extends JcrNode implements Version {
40
41 private static final Version[] EMPTY_VERSION_ARRAY = new Version[0];
42
43 JcrVersionNode( AbstractJcrNode node ) {
44 super(node.cache, node.nodeId, node.location);
45
46 assert !node.isRoot() : "Versions should always be located in the /jcr:system/jcr:versionStorage subgraph";
47 }
48
49
50
51
52 @Override
53 public JcrVersionHistoryNode getContainingHistory() throws RepositoryException {
54 return new JcrVersionHistoryNode(getParent());
55 }
56
57
58
59
60 @Override
61 public Calendar getCreated() throws RepositoryException {
62 return getProperty(JcrLexicon.CREATED).getDate();
63 }
64
65
66
67
68
69
70
71
72
73 public AbstractJcrNode getFrozenNode() throws RepositoryException {
74 return getNode(JcrLexicon.FROZEN_NODE);
75 }
76
77
78
79
80 @Override
81 public Version[] getPredecessors() throws RepositoryException {
82 return getNodesForProperty(JcrLexicon.PREDECESSORS);
83 }
84
85
86
87
88
89 @Override
90 public Version[] getSuccessors() throws RepositoryException {
91 return getNodesForProperty(JcrLexicon.SUCCESSORS);
92 }
93
94 private final Version[] getNodesForProperty( Name propertyName ) throws RepositoryException {
95 assert JcrLexicon.SUCCESSORS.equals(propertyName) || JcrLexicon.PREDECESSORS.equals(propertyName);
96
97 Property references = getProperty(propertyName);
98
99 if (references == null) return EMPTY_VERSION_ARRAY;
100
101 Value[] values = references.getValues();
102
103 List<JcrVersionNode> versions = new ArrayList<JcrVersionNode>(values.length);
104
105 for (int i = 0; i < values.length; i++) {
106 String uuid = values[i].getString();
107
108 AbstractJcrNode node = session().getNodeByUUID(uuid);
109 versions.add((JcrVersionNode)node);
110 }
111
112 return versions.toArray(EMPTY_VERSION_ARRAY);
113
114 }
115
116 private final JcrVersionNode getFirstNodeForProperty( Name propertyName ) throws RepositoryException {
117 assert JcrLexicon.SUCCESSORS.equals(propertyName) || JcrLexicon.PREDECESSORS.equals(propertyName);
118
119 Property references = getProperty(propertyName);
120
121 if (references == null) return null;
122
123 Value[] values = references.getValues();
124 if (values.length == 0) return null;
125
126 String uuid = values[0].getString();
127 AbstractJcrNode node = session().getNodeByUUID(uuid);
128
129 return (JcrVersionNode)node;
130 }
131
132 boolean isSuccessorOf( JcrVersionNode other ) throws RepositoryException {
133 if (!other.hasProperty(JcrLexicon.SUCCESSORS)) return false;
134
135 Value[] successors = other.getProperty(JcrLexicon.SUCCESSORS).getValues();
136
137 String uuidString = uuid().toString();
138 for (int i = 0; i < successors.length; i++) {
139 if (uuidString.equals(successors[i].getString())) {
140 return true;
141 }
142 }
143
144 return false;
145 }
146
147 @Override
148 public JcrVersionNode getLinearPredecessor() throws RepositoryException {
149 return getFirstNodeForProperty(JcrLexicon.PREDECESSORS);
150 }
151
152 @Override
153 public JcrVersionNode getLinearSuccessor() throws RepositoryException {
154 return getFirstNodeForProperty(JcrLexicon.SUCCESSORS);
155 }
156
157 }