/
WorkflowHelper.java
510 lines (461 loc) · 21 KB
/
WorkflowHelper.java
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
* Copyright 2006 Wyona
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.wyona.org/licenses/APACHE-LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wyona.yanel.core.workflow;
import java.io.InputStream;
import java.util.Date;
import org.apache.log4j.Logger;
import org.wyona.yanel.core.Resource;
import org.wyona.yanel.core.api.attributes.VersionableV2;
import org.wyona.yanel.core.api.attributes.ViewableV2;
import org.wyona.yanel.core.api.attributes.WorkflowableV1;
import org.wyona.yanel.core.attributes.versionable.RevisionInformation;
import org.wyona.yanel.core.attributes.viewable.View;
import org.wyona.yanel.core.util.DateUtil;
import org.wyona.yanel.core.util.ResourceAttributeHelper;
import org.wyona.yarep.core.Node;
import org.wyona.yarep.core.Property;
import org.wyona.yarep.core.Revision;
/**
* This class provides some helper methods related to workflow for resources
* which are WorkflowableV1, VersionableV2, and ViewableV2 and which implement workflow
* according to the following conditions:
* - there is a resource config property 'workflow-schema' which points to the schema
* - the workflow information is stored as properties of a repository node in the repository
* of the resource with the path of the resource.
*
*/
public class WorkflowHelper {
private static Logger log = Logger.getLogger(WorkflowHelper.class);
protected static final String LIVE_REVISION_PROPERTY = "live-revision";
protected static final String WORKFLOW_DATE_PROPERTY = "workflow-date";
protected static final String WORKFLOW_STATE_PROPERTY = "workflow-state";
protected static final String WORKFLOW_SCHEMA_PROPERTY_NAME = "workflow-schema";
/**
* Execute workflow transition for a specific revision of a particular resource
*/
public static void doTransition(Resource resource, String transitionID, String revision) throws WorkflowException {
boolean foundTransition = false;
String currentState = null;
WorkflowableV1 workflowable = (WorkflowableV1)resource;
Workflow workflow = getWorkflow(resource);
currentState = workflowable.getWorkflowState(revision);
if (currentState == null) {
currentState = workflow.getInitialState();
}
Transition[] transitions = workflow.getLeavingTransitions(currentState);
for (int i = 0; i < transitions.length; i++) {
if (transitions[i].getID().equals(transitionID)) {
Condition[] conditions = transitions[i].getConditions();
for (int j = 0; j < conditions.length; j++) {
if (!conditions[j].isComplied(workflowable, workflow, revision)) {
throw new WorkflowException("Workflow condition not complied for state '" + currentState + "' and transition '" + transitionID + "': " + conditions[j].getMessage());
}
}
String newState = transitions[i].getDestinationState();
workflowable.setWorkflowState(newState, revision);
Action[] actions = transitions[i].getActions();
for (int j = 0; j < actions.length; j++) {
actions[j].execute(workflowable, workflow, revision);
}
foundTransition = true;
}
}
if (!foundTransition) {
throw new WorkflowException("Invalid workflow transition: " + transitionID +
" for state: " + currentState);
}
}
/* INFO: It's better to hide the persistence and hence use doTransition(Resource, String, String)
public static void doTransition(Node node, String transitionID, Workflow workflow) throws WorkflowException {
boolean foundTransition = false;
String currentState = null;
currentState = getWorkflowState(node);
if (currentState == null) {
currentState = workflow.getInitialState();
}
Transition[] transitions = workflow.getLeavingTransitions(currentState);
for (int i = 0; i < transitions.length; i++) {
if (transitions[i].getID().equals(transitionID)) {
// INFO: The isComplied() method is based on workflowable instead node
Condition[] conditions = transitions[i].getConditions();
for (int j = 0; j < conditions.length; j++) {
if (!conditions[j].isComplied(workflowable, workflow, revision)) {
throw new WorkflowException("Workflow condition not complied for state '" + currentState + "' and transition '" + transitionID + "': " + conditions[j].getMessage());
}
}
String newState = transitions[i].getDestinationState();
setWorkflowState(node, newState);
// INFO: The execute() method is based on workflowable instead node
Action[] actions = transitions[i].getActions();
for (int j = 0; j < actions.length; j++) {
actions[j].execute(workflowable, workflow, revision);
}
foundTransition = true;
}
}
if (!foundTransition) {
throw new WorkflowException("Invalid workflow transition: " + transitionID +
" for state: " + currentState);
}
}
*/
/*public static boolean canDoTransition(Resource resource, String transitionID, String revision)
throws WorkflowException {
WorkflowableV1 workflowable = (WorkflowableV1)resource;
Workflow workflow = getWorkflow(resource);
String currentState = workflowable.getWorkflowState(revision);
if (currentState == null) {
currentState = workflow.getInitialState();
}
Transition[] transitions = workflow.getLeavingTransitions(currentState);
for (int j = 0; j < transitions.length; j++) {
Condition[] conditions = transitions[j].getConditions();
for (int k = 0; k < conditions.length; k++) {
if (!conditions[k].isComplied(workflowable, workflow, revision)) {
return true;
}
}
}
return false;
}*/
/**
* Get revision which is marked as live
*/
public static String getLiveRevision(Resource resource) throws WorkflowException {
try {
WorkflowableV1 workflowable = (WorkflowableV1)resource;
if (getWorkflow(resource) == null) {
RevisionInformation[] revisions = ((VersionableV2)resource).getRevisions();
if (revisions == null || revisions.length == 0) {
return null;
}
return revisions[revisions.length-1].getName();
}
return workflowable.getWorkflowVariable(LIVE_REVISION_PROPERTY);
} catch (Exception e) {
log.error(e, e);
throw new WorkflowException(e.getMessage(), e);
}
}
/**
* Indicates whether the given resource is live, i.e. if the workflow variable
* "live-revision" has a defined value.
* If a resource does not have an associated workflow, it is considered as live.
* @param resource
* @return true if live, false otherwise
* @throws WorkflowException
*/
public static boolean isLive(Resource resource) throws WorkflowException {
try {
WorkflowableV1 workflowable = (WorkflowableV1)resource;
if (getWorkflow(resource) == null || workflowable.getWorkflowVariable(LIVE_REVISION_PROPERTY) != null) {
return true;
}
return false;
} catch (Exception e) {
log.error(e, e);
throw new WorkflowException(e.getMessage(), e);
}
}
public static View getLiveView(Resource resource, String viewid) throws Exception {
if (((WorkflowableV1)resource).isLive()) {
String liveRevision = getLiveRevision(resource);
if (liveRevision != null) {
return ((VersionableV2)resource).getView(viewid, liveRevision);
}
// if there are no revisions, show the normal view:
return ((ViewableV2)resource).getView(viewid);
}
return null;
}
/**
* Check if resource is associated with workflow
*/
public static boolean hasWorkflow(Resource resource) throws WorkflowException {
try {
if (resource.getResourceConfigProperty(WORKFLOW_SCHEMA_PROPERTY_NAME) != null) {
return true;
} else {
log.warn("Resource configuration does not contain a '" + WORKFLOW_SCHEMA_PROPERTY_NAME + "' property!");
}
} catch (Exception e) {
log.warn(e.getMessage());
}
return false;
}
/**
* Get workflow which is associated with resource
* @param resource Resource
* @return Workflow instance
*/
public static Workflow getWorkflow(Resource resource) throws WorkflowException {
try {
String workflowSchema = resource.getResourceConfigProperty(WORKFLOW_SCHEMA_PROPERTY_NAME);
if (workflowSchema == null) {
return null;
}
WorkflowBuilder builder = new WorkflowBuilder();
if (resource.getRealm().getRepository().existsNode(workflowSchema)) {
String targetEnv = resource.getYanel().getTargetEnvironment();
InputStream stream = resource.getRealm().getRepository().getNode(workflowSchema).getInputStream();
// TODO: Cache result of workflow builder for performance reasons
return builder.buildWorkflow(stream, targetEnv);
} else {
throw new WorkflowException("No such workflow instance '" + workflowSchema + "' within repository: " + resource.getRealm().getRepository() + " (configured within resource config with request path: " + resource.getPath() + ")");
}
} catch (Exception e) {
log.error(e, e);
throw new WorkflowException(e.getMessage(), e);
}
}
/**
* Get introspection part re workflow and versioning
*/
public static String getWorkflowIntrospection(Resource resource) throws WorkflowException {
try {
if (!ResourceAttributeHelper.hasAttributeImplemented(resource, "Versionable", "2")) throw new WorkflowException("Resource '" + resource.getClass().getName() + "' has not VersionableV2 interface implemented!");
RevisionInformation[] revisions = ((VersionableV2)resource).getRevisions();
WorkflowableV1 workflowable = (WorkflowableV1)resource;
String liveRevision = getLiveRevision(resource);
if (revisions != null && revisions.length > 0) {
StringBuffer sb = new StringBuffer();
sb.append("<versions xmlns=\"http://www.wyona.org/neutron/2.0\">");
for (int i = revisions.length - 1; i >= 0; i--) {
sb.append("<version url=\"?yanel.resource.revision=" + revisions[i].getName() + "\">");
sb.append("<comment>" + revisions[i].getComment() + "</comment>");
sb.append("<date>" + revisions[i].getDate() + "</date>");
sb.append("<user>" + revisions[i].getUser() + "</user>");
sb.append("<revision>" + revisions[i].getName() + "</revision>");
Workflow workflow = getWorkflow(resource);
if (workflow != null) {
String state = workflowable.getWorkflowState(revisions[i].getName());
if (state == null) {
state = workflow.getInitialState();
}
Date workflowDate = workflowable.getWorkflowDate(revisions[i].getName());
String date = "";
if (workflowDate != null) {
date = DateUtil.format(workflowDate);
}
Transition[] transitions = workflow.getLeavingTransitions(state);
sb.append("<workflow>");
if (revisions[i].getName().equals(liveRevision)) {
state += " (LIVE)"; // TODO: this is a hack
}
sb.append("<state date=\"" + date + "\">" + state + "</state>");
sb.append("<transitions>");
transitions: for (int j = 0; j < transitions.length; j++) {
Condition[] conditions = transitions[j].getConditions();
for (int k = 0; k < conditions.length; k++) {
if (!conditions[k].isComplied(workflowable, workflow, revisions[i].getName())) {
log.warn("Condition (" + conditions[k].getClass().getName() + ") failed for workflow transition '" + transitions[j].getID() + "' (Revision: " + revisions[i].getName() + ")");
continue transitions; // jump to next transition
}
}
sb.append("<transition id=\"" + transitions[j].getID() + "\" to=\"" + transitions[j].getDestinationState() + "\" url=\"?yanel.resource.workflow.transition=" + transitions[j].getID() + "&yanel.resource.revision=" + revisions[i].getName() + "\" method=\"POST\">");
String[] languages = transitions[j].getDescriptionLanguages();
for (int l = 0; l < languages.length; l++) {
String description = transitions[j].getDescription(languages[l]);
sb.append("<description xml:lang=\"" + languages[l] + "\">" + description + "</description>");
}
sb.append("</transition>");
}
sb.append("</transitions>");
sb.append("<!-- NOTE: Workflow history not implemented by Yanel yet! -->");
sb.append("<history/>");
sb.append("</workflow>");
}
sb.append("</version>");
}
sb.append("</versions>");
return sb.toString();
}
return "<!-- No revisions! -->";
} catch (Exception e) {
log.error(e, e);
throw new WorkflowException(e.getMessage(), e);
}
}
/**
*
*/
public static String getWorkflowVariable(Resource resource, String name) throws WorkflowException {
try {
return getWorkflowVariable(resource.getRealm().getRepository().getNode(resource.getPath()), name);
} catch (Exception e) {
log.error(e, e);
throw new WorkflowException(e.getMessage(), e);
}
}
/**
* Get workflow variable value, e.g. value of live-revision
* @param name Name of workflow variable
*/
public static String getWorkflowVariable(Node node, String name) throws WorkflowException {
try {
if (!node.hasProperty(name)) {
log.info("No such property: " + name);
return null;
}
Property property = node.getProperty(name);
if (property != null) {
return property.getString();
}
return null;
} catch (Exception e) {
log.error(e, e);
throw new WorkflowException(e.getMessage(), e);
}
}
/**
*
*/
public static void setWorkflowVariable(Resource resource, String name, String value) throws WorkflowException {
try {
setWorkflowVariable(resource.getRealm().getRepository().getNode(resource.getPath()), name, value);
} catch (Exception e) {
log.error(e, e);
throw new WorkflowException(e.getMessage(), e);
}
}
/**
*
*/
public static void setWorkflowVariable(Node node, String name, String value) throws WorkflowException {
try {
node.setProperty(name, value);
} catch (Exception e) {
log.error(e, e);
throw new WorkflowException(e.getMessage(), e);
}
}
public static void removeWorkflowVariable(Resource resource, String name) throws WorkflowException {
try {
removeWorkflowVariable(resource.getRealm().getRepository().getNode(resource.getPath()), name);
} catch (Exception e) {
log.error(e, e);
throw new WorkflowException(e.getMessage(), e);
}
}
public static void removeWorkflowVariable(Node node, String name) throws WorkflowException {
try {
node.removeProperty(name);
} catch (Exception e) {
log.error(e, e);
throw new WorkflowException(e.getMessage(), e);
}
}
/**
*
*/
public static String getWorkflowState(Resource resource, String revision) throws WorkflowException {
try {
return getWorkflowState(resource.getRealm().getRepository().getNode(resource.getPath()), revision);
} catch (Exception e) {
log.error(e, e);
throw new WorkflowException(e.getMessage(), e);
}
}
/**
*
*/
public static String getWorkflowState(Node node, String revision) throws WorkflowException {
try {
return getWorkflowState(node.getRevision(revision));
} catch (Exception e) {
log.error(e, e);
throw new WorkflowException(e.getMessage(), e);
}
}
/**
*
*/
private static String getWorkflowState(Node node) throws WorkflowException {
try {
Property stateProp = node.getProperty(WORKFLOW_STATE_PROPERTY);
if (stateProp != null) {
return stateProp.getString();
}
return null;
} catch (Exception e) {
log.error(e, e);
throw new WorkflowException(e.getMessage(), e);
}
}
/**
*
*/
public static void setWorkflowState(Resource resource, String state, String revision) throws WorkflowException {
try {
setWorkflowState(resource.getRealm().getRepository().getNode(resource.getPath()), state, revision);
} catch (Exception e) {
log.error(e, e);
throw new WorkflowException(e.getMessage(), e);
}
}
/**
* @deprecated Use {@link #setWorkflowState(Resource, String, String)} instead
* Set workflow state (and date)
*/
public static void setWorkflowState(Node node, String state, String revision) throws WorkflowException {
try {
setWorkflowState(node.getRevision(revision), state);
} catch (Exception e) {
log.error(e, e);
throw new WorkflowException(e.getMessage(), e);
}
}
/**
* Set workflow state (and date)
*/
private static void setWorkflowState(Node node, String state) throws WorkflowException {
try {
log.debug("Set workflow state: " + state);
node.setProperty(WORKFLOW_STATE_PROPERTY, state);
node.setProperty(WORKFLOW_DATE_PROPERTY, new Date());
// TODO: write workflow history
} catch (Exception e) {
log.error(e, e);
throw new WorkflowException(e.getMessage(), e);
}
}
/**
* @param revision Revision of resource
*/
public static Date getWorkflowDate(Resource resource, String revision) throws WorkflowException {
try {
return getWorkflowDate(resource.getRealm().getRepository().getNode(resource.getPath()), revision);
} catch (Exception e) {
log.error(e, e);
throw new WorkflowException(e.getMessage(), e);
}
}
/**
* @deprecated Use {@link #getWorkflowDate(Resource, String)} instead
*/
public static Date getWorkflowDate(Node node, String revision) throws WorkflowException {
try {
Property dateProp = node.getRevision(revision).getProperty(WORKFLOW_DATE_PROPERTY);
if (dateProp != null) {
return dateProp.getDate();
}
return null;
} catch (Exception e) {
log.error(e, e);
throw new WorkflowException(e.getMessage(), e);
}
}
}