CreateVersionProcess.java
8.15 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.dam.api.Asset
* com.day.cq.dam.api.Revision
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.Revision
* com.day.cq.wcm.api.WCMException
* com.day.cq.workflow.WorkflowException
* com.day.cq.workflow.WorkflowSession
* com.day.cq.workflow.exec.WorkItem
* com.day.cq.workflow.exec.Workflow
* com.day.cq.workflow.exec.WorkflowData
* com.day.cq.workflow.exec.WorkflowProcess
* com.day.cq.workflow.metadata.MetaDataMap
* javax.jcr.Node
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.version.Version
* javax.jcr.version.VersionHistory
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.jcr.resource.JcrResourceResolverFactory
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.workflow.process;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Revision;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.workflow.WorkflowException;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.exec.WorkItem;
import com.day.cq.workflow.exec.Workflow;
import com.day.cq.workflow.exec.WorkflowData;
import com.day.cq.workflow.exec.WorkflowProcess;
import com.day.cq.workflow.metadata.MetaDataMap;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.version.Version;
import javax.jcr.version.VersionHistory;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.jcr.resource.JcrResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service
@Property(name="process.label", value={"Create Version"})
public class CreateVersionProcess
implements WorkflowProcess {
private static final Logger log = LoggerFactory.getLogger(CreateVersionProcess.class);
public static final String TYPE_JCR_PATH = "JCR_PATH";
public static final String TYPE_JCR_UUID = "JCR_UUID";
@Reference
private JcrResourceResolverFactory jcrResolverFactory = null;
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException {
try {
Session session = workflowSession.getSession();
WorkflowData data = workItem.getWorkflowData();
String path = null;
String type = data.getPayloadType();
if (type.equals("JCR_PATH") && data.getPayload() != null) {
String payloadData = (String)data.getPayload();
if (session.itemExists(payloadData)) {
path = payloadData;
}
} else if (data.getPayload() != null && type.equals("JCR_UUID")) {
Node node = session.getNodeByIdentifier((String)data.getPayload());
path = node.getPath();
}
if (path != null) {
Resource res = this.jcrResolverFactory.getResourceResolver(session).getResource(path);
String revStr = null;
if (res.adaptTo(Page.class) != null) {
Page page = (Page)res.adaptTo(Page.class);
PageManager pm = page.getPageManager();
String versionLabel = this.createUniqueVersionLabel(pm.getRevisions(page.getPath(), null), this.getAbsTime(workItem));
com.day.cq.wcm.api.Revision rev = pm.createRevision(page, versionLabel, null);
revStr = this.getPageRevision(rev, session);
} else if (res.adaptTo(Asset.class) != null) {
Asset asset = (Asset)res.adaptTo(Asset.class);
String versionLabel = this.createUniqueVersionLabel(asset.getRevisions(null), this.getAbsTime(workItem));
Revision rev = asset.createRevision(versionLabel, null);
revStr = this.getAssetRevision(rev, session);
} else {
log.warn("Cannot create version of " + path);
}
if (revStr != null) {
data.getMetaDataMap().put((Object)"resourceVersion", (Object)revStr);
if (workItem.getWorkflowData().getMetaDataMap().get("absoluteTime", String.class) != null) {
Calendar cal = this.getTime(workItem);
data.getMetaDataMap().put((Object)"comment", (Object)("Activate version " + revStr + " on " + cal.getTime().toString()));
}
workflowSession.updateWorkflowData(workItem.getWorkflow(), data);
}
} else {
log.warn("Cannot process payload. Path is null");
}
}
catch (RepositoryException e) {
throw new WorkflowException((Throwable)e);
}
catch (WCMException e) {
throw new WorkflowException((Throwable)e);
}
catch (Exception e) {
throw new WorkflowException((Throwable)e);
}
}
private String createUniqueVersionLabel(Collection<?> revisions, String versionLabelHint) throws RepositoryException {
boolean unique;
if (versionLabelHint == null) {
return null;
}
LinkedList<Version> versions = new LinkedList<Version>();
for (Object o : revisions) {
Version v = o instanceof com.day.cq.wcm.api.Revision ? ((com.day.cq.wcm.api.Revision)o).getVersion() : (o instanceof Revision ? ((Revision)o).getVersion() : null);
if (null == v) continue;
versions.add(v);
}
String versionLabel = versionLabelHint;
int count = 1;
block1 : do {
unique = true;
for (Version v : versions) {
if (!v.getContainingHistory().hasVersionLabel(versionLabel)) continue;
versionLabel = versionLabelHint + " (" + ++count + ")";
unique = false;
continue block1;
}
} while (!unique);
return versionLabel;
}
private String getPageRevision(com.day.cq.wcm.api.Revision revision, Session session) throws RepositoryException {
String vid = revision.getId();
Node v = session.getNodeByIdentifier(vid);
return v.getName();
}
private String getAssetRevision(Revision revision, Session session) throws RepositoryException {
String vid = revision.getId();
Node v = session.getNodeByIdentifier(vid);
return v.getName();
}
private String getAbsTime(WorkItem workItem) {
if (workItem.getWorkflowData().getMetaDataMap().get("absoluteTime", String.class) != null) {
Calendar cal = this.getTime(workItem);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd HH.mm.ss");
return "Scheduled Activation Time is " + formatter.format(cal.getTime());
}
return null;
}
private Calendar getTime(WorkItem workItem) {
Long time = (Long)workItem.getWorkflowData().getMetaDataMap().get("absoluteTime", Long.class);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(time);
return cal;
}
protected void bindJcrResolverFactory(JcrResourceResolverFactory jcrResourceResolverFactory) {
this.jcrResolverFactory = jcrResourceResolverFactory;
}
protected void unbindJcrResolverFactory(JcrResourceResolverFactory jcrResourceResolverFactory) {
if (this.jcrResolverFactory == jcrResourceResolverFactory) {
this.jcrResolverFactory = null;
}
}
}