VersioningTimelineEventProvider.java
5.47 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Workspace
* javax.jcr.version.Version
* javax.jcr.version.VersionHistory
* javax.jcr.version.VersionIterator
* javax.jcr.version.VersionManager
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.timeline.internal.providers;
import com.adobe.granite.timeline.TimelineEvent;
import com.adobe.granite.timeline.TimelineEventProvider;
import com.adobe.granite.timeline.TimelineEventType;
import com.adobe.granite.timeline.types.VersioningTimelineEventType;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.version.Version;
import javax.jcr.version.VersionHistory;
import javax.jcr.version.VersionIterator;
import javax.jcr.version.VersionManager;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
@Component
@Service
public class VersioningTimelineEventProvider
implements TimelineEventProvider {
private static final Logger log = LoggerFactory.getLogger(VersioningTimelineEventProvider.class);
public static final TimelineEventType EVENT_TYPE = new VersioningTimelineEventType();
private static final String ACTION = "Version Created";
@Override
public boolean accepts(Resource resource) {
try {
Node node = (Node)resource.adaptTo(Node.class);
return node.isNodeType("mix:versionable") || node.hasNode("jcr:content") && node.getNode("jcr:content").isNodeType("mix:versionable");
}
catch (RepositoryException e) {
log.error("error while checking node type for [{}]: ", (Object)resource.getPath(), (Object)e);
return false;
}
}
@Override
public Collection<TimelineEvent> getEvents(Resource resource) {
ArrayList<TimelineEvent> entries = new ArrayList<TimelineEvent>();
Node node = (Node)resource.adaptTo(Node.class);
try {
if (!node.isNodeType("mix:versionable") && node.hasNode("jcr:content") && node.getNode("jcr:content").isNodeType("mix:versionable")) {
node = node.getNode("jcr:content");
}
long start = System.currentTimeMillis();
VersionManager versionManager = node.getSession().getWorkspace().getVersionManager();
VersionHistory versionHistory = versionManager.getVersionHistory(node.getPath());
VersionIterator allVersions = versionHistory.getAllVersions();
log.debug(">> retrieved all versions for [{}] in [{}ms]", (Object)node.getPath(), (Object)(System.currentTimeMillis() - start));
long startTimelineEntries = System.currentTimeMillis();
while (allVersions.hasNext()) {
Version v = allVersions.nextVersion();
if ("jcr:rootVersion".equals(v.getName())) continue;
entries.add(new VersioningTimelineEvent(v, resource));
}
log.debug(">> converted versions to timeline events for [{}] in [{}ms]", (Object)node.getPath(), (Object)(System.currentTimeMillis() - startTimelineEntries));
}
catch (RepositoryException e) {
log.error("error while getting version history for [{}]: ", (Object)resource.getPath(), (Object)e);
}
return entries;
}
@Override
public TimelineEventType getType() {
return EVENT_TYPE;
}
private static class VersioningTimelineEvent
implements TimelineEvent {
private final Version version;
private final Resource resource;
private VersioningTimelineEvent(Version version, Resource resource) {
this.version = version;
this.resource = resource;
}
public String getAction() {
return "Version Created";
}
public String getDescription() {
try {
return this.version.getName();
}
catch (RepositoryException e) {
log.error("error while getting version description for [{}]: ", (Object)this.resource.getPath(), (Object)e);
return null;
}
}
public String getOrigin() {
try {
return this.version.getPath();
}
catch (RepositoryException e) {
log.error("error while getting version path for [{}]: ", (Object)this.resource.getPath(), (Object)e);
return null;
}
}
public long getTime() {
try {
return this.version.getCreated().getTimeInMillis();
}
catch (RepositoryException e) {
log.error("error while getting version date for [{}]: ", (Object)this.resource.getPath(), (Object)e);
return 0;
}
}
public TimelineEventType getType() {
return VersioningTimelineEventProvider.EVENT_TYPE;
}
public String getUserID() {
return null;
}
}
}