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.jcr.util;
35
36 import info.magnolia.cms.core.version.ContentVersion;
37 import info.magnolia.cms.core.version.VersionInfo;
38 import info.magnolia.cms.core.version.VersionManager;
39 import info.magnolia.cms.util.ContentUtil;
40 import info.magnolia.jcr.wrapper.JCRPropertiesFilteringNodeWrapper;
41 import info.magnolia.objectfactory.Components;
42
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.List;
46
47 import javax.jcr.Node;
48 import javax.jcr.RepositoryException;
49 import javax.jcr.UnsupportedRepositoryOperationException;
50 import javax.jcr.version.Version;
51 import javax.jcr.version.VersionException;
52 import javax.jcr.version.VersionHistory;
53 import javax.jcr.version.VersionIterator;
54
55 import org.apache.commons.lang.StringUtils;
56 import org.apache.jackrabbit.JcrConstants;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59
60
61
62
63 public class VersionUtil {
64
65 private static final Logger log = LoggerFactory.getLogger(VersionUtil.class);
66
67
68
69
70
71 public static String getNodeTypeName(Node node) throws RepositoryException {
72 node = NodeUtil.deepUnwrap(node, JCRPropertiesFilteringNodeWrapper.class);
73
74 if (node.hasProperty(JcrConstants.JCR_FROZENPRIMARYTYPE)) {
75 return node.getProperty(JcrConstants.JCR_FROZENPRIMARYTYPE).getString();
76 }
77 return node.getProperty(JcrConstants.JCR_PRIMARYTYPE).getString();
78 }
79
80
81
82
83
84
85
86
87 public static List<VersionInfo> getVersionInfoList(Node node) {
88 List<VersionInfo> versionList = new ArrayList<VersionInfo>();
89 VersionManager versionManager = Components.getComponent(VersionManager.class);
90
91 try {
92 VersionHistory versionHistory = versionManager.getVersionHistory(node);
93
94 if (versionHistory != null) {
95 VersionIterator versionIterator = versionHistory.getAllVersions();
96 while (versionIterator.hasNext()) {
97 final Version version = versionIterator.nextVersion();
98 final String versionName = version.getName();
99
100
101 if (StringUtils.isNotBlank(versionName) && !StringUtils.equals(versionName, "jcr:rootVersion")) {
102 final String versionComment = NodeTypes.Versionable.getComment(version.getFrozenNode());
103 final ContentVersion contentVersion = new ContentVersion(version, ContentUtil.asContent(node));
104 final String versionUser = contentVersion.getUserName();
105
106 versionList.add(new VersionInfo(versionName, version.getCreated().getTime(), versionUser, versionComment));
107 }
108 }
109 }
110 } catch (RepositoryException e) {
111 log.warn("Unable to get version history of node [{}].", new Object[] {node, e});
112 }
113
114 Collections.reverse(versionList);
115
116 return versionList;
117 }
118
119
120
121
122
123 public static Version getVersion(Node node, String versionLabel) throws RepositoryException {
124 if (StringUtils.isEmpty(versionLabel)) {
125 return null;
126 }
127
128 VersionManager versionManager = Components.getComponent(VersionManager.class);
129
130 try {
131 VersionHistory versionHistory = versionManager.getVersionHistory(node);
132 if (versionHistory == null) {
133 return null;
134 }
135 return versionHistory.getVersion(versionLabel);
136 } catch (VersionException e) {
137 log.warn("The version '{}' of the node '{}' doesn't exists.", new Object[] {versionLabel, node, e});
138 return null;
139 } catch (UnsupportedRepositoryOperationException e) {
140 return null;
141 }
142 }
143
144
145
146
147 public static String getPreviousVersionLabel(Node node, String versionLabel) throws RepositoryException {
148 if (node == null) {
149 return null;
150 }
151 Version previousVersion = getPreviousVersion(getVersion(node, versionLabel));
152 if (previousVersion != null){
153 return previousVersion.getName();
154 }
155 return null;
156 }
157
158
159
160
161 public static boolean hasPreviousVersion(Node node, String versionLabel) {
162 Version previousVersion;
163 String previousVersionName;
164
165 try {
166 previousVersion = getPreviousVersion(getVersion(node, versionLabel));
167 previousVersionName = previousVersion.getName();
168 } catch (RepositoryException e) {
169 return false;
170 }
171
172 if (previousVersion != null && !StringUtils.equals(previousVersionName, "jcr:rootVersion")) {
173 return true;
174 }
175 return false;
176 }
177
178
179
180
181 public static Version getLatestVersion(Node node) throws RepositoryException {
182 VersionManager versionManager = Components.getComponent(VersionManager.class);
183 VersionIterator versionIterator = versionManager.getAllVersions(node);
184
185 Version latest = null;
186
187 if (versionIterator == null) {
188 return latest;
189 }
190
191 while (versionIterator.hasNext()) {
192 latest = versionIterator.nextVersion();
193 }
194 return latest;
195 }
196
197
198
199
200 public static Version getPreviousVersion(Version version) throws RepositoryException {
201 if (version == null) {
202 return null;
203 }
204 Version[] predecessors = version.getPredecessors();
205 if (predecessors.length > 0) {
206 return predecessors[0];
207 }
208 return null;
209 }
210
211
212
213
214 public static String getNextVersionLabel(Node node, String versionLabel) throws RepositoryException {
215 if (node == null) {
216 return null;
217 }
218 Version nextVersion = getNextVersion(getVersion(node, versionLabel));
219 if (nextVersion != null){
220 return nextVersion.getName();
221 }
222 return null;
223 }
224
225
226
227
228 public static Version getNextVersion(Version version) throws RepositoryException {
229 if (version == null) {
230 return null;
231 }
232 Version[] successors = version.getSuccessors();
233 if (successors.length > 0) {
234 return successors[0];
235 }
236 return null;
237 }
238
239 }