/
CreateChoreoTaskBehavior.js
251 lines (216 loc) · 8.69 KB
/
CreateChoreoTaskBehavior.js
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
import inherits from 'inherits';
import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
import {
getBandHeight
} from '../../../util/BandUtil';
import {
is
} from 'bpmn-js/lib/util/ModelUtil';
import {
assign, flatten
} from 'min-dash';
import {
createMessageFlowSemantics,
unlinkMessageFlowSemantics,
linkMessageFlowSemantics,
createMessageShape
} from '../../../util/MessageUtil';
import {
getConnectedElements,
isChoreoActivity,
isInitiating
} from '../../../util/DiagramWalkerUtil';
function chooseParticipants(hints, allParticipants) {
// In the default case no source shape was given, e.g. because the the activity was dragged from the side pallet.
const recommended = {
initiator: allParticipants[0],
receiver: allParticipants[1]
};
if (hints && hints.sourceShape) {
let source = hints.sourceShape;
const precedingActivities = getConnectedElements(hints.sourceShape, 'incoming', isChoreoActivity);
if (isChoreoActivity(source) || precedingActivities.length === 1) {
// We reverse the participants roles compared to the previous activity.
const participants = isChoreoActivity(source) ? source.bandShapes : precedingActivities[0].bandShapes;
recommended.receiver = participants.find(p => isInitiating(p)).businessObject;
recommended.initiator = participants.find(p => !isInitiating(p)).businessObject;
} else if (precedingActivities.length > 1) {
// If there are more than two preceding activities, e.g., due to a join we select the most used participants.
const participants = flatten(precedingActivities.map(p => p.bandShapes.map(bs => bs.businessObject)));
const count = {};
participants.forEach(bo => {
count[bo.id] = (count[bo.id] || 0) + 1;
});
let list = allParticipants.map(bo => [bo, count[bo.id]]);
list.sort((a, b) => b[1] - a[1]);
recommended.initiator = list[0][0];
recommended.receiver = list[1][0];
}
}
return recommended;
}
/**
* Behavior when creating a choreography task.
* @constructor
* @param {Injector} injector
* @param {BpmnFactory} bpmnFactory
* @param {Canvas} canvas
* @param {ElementFactory} elementFactory
* @param {BPMNModdle} model
*/
export default function CreateChoreoTaskBehavior(injector, bpmnFactory, canvas, elementFactory, model) {
injector.invoke(CommandInterceptor, this);
this.preExecuted('shape.create', function(event) {
// To accommodate for shapes that were copied we need to do some extra differentiation
let context = event.context;
let shape = context.shape;
let position = context.position;
// shapes that were created by copy already have participants.
if (is(shape, 'bpmn:ChoreographyActivity') && shape.type !== 'label' && !shape.oldBusinessObject) {
// Create a completely new ChoreoActivity
// get the participants in the choreography
let choreo = canvas.getRootElement();
let participants = choreo.businessObject.get('participants');
// create participants if we do not have two yet
while (participants.length < 2) {
let participant = bpmnFactory.create('bpmn:Participant');
participants.push(participant);
participant.name = 'Participant ' + participants.length;
}
const recommended = chooseParticipants(context.hints, participants);
let partA = recommended.initiator;
let partB = recommended.receiver;
// set the properties of the choreo activity business object
let activity = shape.businessObject;
activity.get('participantRef').push(partA);
activity.get('participantRef').push(partB);
activity.set('initiatingParticipantRef', partA);
activity.set('name', 'New Activity');
// find out the bounds of the band shapes
let boundsA = {
x: position.x - shape.width / 2,
y: position.y - shape.height / 2,
width: shape.width,
height: getBandHeight(partA)
};
let boundsB = {
x: position.x - shape.width / 2,
y: position.y + shape.height / 2 - getBandHeight(partB),
width: shape.width,
height: getBandHeight(partB)
};
// create the participant bands
let diA = bpmnFactory.createDiShape('bpmndi:BPMNShape', boundsA, {
choreographyActivityShape: activity.di,
bpmnElement: partA,
participantBandKind: 'top_initiating',
isMessageVisible: false
});
let shapeA = elementFactory.createShape(assign({
type: 'bpmn:Participant',
id: model.ids.nextPrefixed('ParticipantBand_', partA),
businessObject: partA,
diBand: diA,
activityShape: shape
}, boundsA));
let diB = bpmnFactory.createDiShape('bpmndi:BPMNShape', boundsB, {
choreographyActivityShape: activity.di,
bpmnElement: partB,
participantBandKind: 'bottom_non_initiating',
isMessageVisible: false
});
let shapeB = elementFactory.createShape(assign({
type: 'bpmn:Participant',
id: model.ids.nextPrefixed('ParticipantBand_', partB),
businessObject: partB,
diBand: diB,
activityShape: shape
}, boundsB));
shape.bandShapes = [shapeA, shapeB];
// remember some stuff in the context for later linking
assign(context, {
createdShapeA: shapeA,
createdShapeB: shapeB
});
// create initiating message flow and shape (invisible)
if (is(shape, 'bpmn:ChoreographyTask')) {
let messageFlow = createMessageFlowSemantics(
injector,
activity,
activity.get('initiatingParticipantRef')
);
let messageShape = createMessageShape(injector, shapeA, messageFlow);
messageShape.hidden = true;
assign(context, {
createdMessageFlow: messageFlow,
createdMessageShape: messageShape
});
}
}
});
this.executed('shape.create', function(event) {
let context = event.context;
let shape = context.shape;
// Same as above: new vs. copied
if (is(shape, 'bpmn:ChoreographyActivity') && shape.type !== 'label' && !shape.oldBusinessObject) {
// add the bands to the BPMNDiagram semantic object
let choreo = canvas.getRootElement().businessObject;
let bpmnDiagram = choreo.di;
bpmnDiagram.get('planeElement').push(context.createdShapeA.diBand);
bpmnDiagram.get('planeElement').push(context.createdShapeB.diBand);
context.createdShapeA.diBand.$parent = bpmnDiagram;
context.createdShapeB.diBand.$parent = bpmnDiagram;
// add bands to canvas
canvas.addShape(context.createdShapeA, shape);
canvas.addShape(context.createdShapeB, shape);
// properly link elements regarding choreography tasks
if (is(shape, 'bpmn:ChoreographyTask')) {
linkMessageFlowSemantics(injector, shape.businessObject, context.createdMessageFlow);
canvas.addShape(context.createdMessageShape, context.createdShapeA);
}
}
});
this.reverted('shape.create', function(event) {
let context = event.context;
let shape = context.shape;
if (is(shape, 'bpmn:ChoreographyActivity') && shape.type !== 'label') {
if (!shape.oldBusinessObject) {
// remove the bands from the BPMNDiagram semantic object
let choreo = canvas.getRootElement().businessObject;
let bpmnDiagram = choreo.di;
bpmnDiagram.get('planeElement').pop();
bpmnDiagram.get('planeElement').pop();
// remove bands from canvas
canvas.removeShape(context.createdShapeA);
canvas.removeShape(context.createdShapeB);
// properly unlink elements regarding choreography tasks
if (is(shape, 'bpmn:ChoreographyTask')) {
unlinkMessageFlowSemantics(injector, shape.businessObject, context.createdMessageFlow);
canvas.removeShape(context.createdMessageShape);
}
} else {
// Delete messages, message flows, and band DIs that were created during copy paste
let choreo = canvas.getRootElement().businessObject;
let bpmnDiagram = choreo.di;
event.context.shape.bandShapes.forEach(band => {
bpmnDiagram.get('planeElement').splice(bpmnDiagram.get('planeElement').indexOf(band), 1);
});
if (is(shape, 'bpmn:ChoreographyTask')) {
const businessObject = shape.businessObject;
// slice because unlinkMessageFlow touches the messageFlowRef array
businessObject.messageFlowRef.slice().forEach(ref => {
unlinkMessageFlowSemantics(injector, businessObject, ref);
});
}
}
}
});
}
CreateChoreoTaskBehavior.$inject = [
'injector',
'bpmnFactory',
'canvas',
'elementFactory',
'moddle'
];
inherits(CreateChoreoTaskBehavior, CommandInterceptor);