RandomParticipantChooser.java
2.93 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.workflow.WorkflowException
* com.day.cq.workflow.WorkflowSession
* com.day.cq.workflow.exec.ParticipantStepChooser
* com.day.cq.workflow.exec.WorkItem
* com.day.cq.workflow.metadata.MetaDataMap
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
*/
package com.day.cq.workflow.impl.process;
import com.day.cq.workflow.WorkflowException;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.exec.ParticipantStepChooser;
import com.day.cq.workflow.exec.WorkItem;
import com.day.cq.workflow.metadata.MetaDataMap;
import java.util.Random;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
@Component
@Service
@Properties(value={@Property(name="service.description", value={"A sample implementation of a dynamic participant chooser."}), @Property(name="chooser.label", value={"Random Participant Chooser"})})
public class RandomParticipantChooser
implements ParticipantStepChooser {
public String getParticipant(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException {
String participant = null;
String[] availableParticipants = this.getAvailableParticipants(args);
if (availableParticipants != null && availableParticipants.length > 0) {
int index = this.getRandomIndex(availableParticipants.length);
participant = availableParticipants[index];
}
return participant;
}
private int getRandomIndex(int numberOfParticipants) {
Random random = new Random();
return random.nextInt(numberOfParticipants);
}
private String[] getAvailableParticipants(MetaDataMap args) {
String[] availableParticipants = new String[]{};
if (args == null) {
return availableParticipants;
}
if (args.get(Arguments.PROCESS_ARGS.name(), String.class) != null) {
String processArgs = (String)args.get(Arguments.PROCESS_ARGS.name(), String.class);
String[] processArgsSplit = processArgs.split(",");
availableParticipants = new String[processArgsSplit.length];
for (int i = 0; i < processArgsSplit.length; ++i) {
availableParticipants[i] = processArgsSplit[i].trim();
}
} else {
availableParticipants = args.get(Arguments.PARTICIPANTS.name(), String[].class) != null ? (String[])args.get(Arguments.PARTICIPANTS.name(), String[].class) : new String[]{};
}
return availableParticipants;
}
public static enum Arguments {
PROCESS_ARGS,
PARTICIPANTS;
private Arguments() {
}
}
}