Skip to content

Commit

Permalink
feat: handle template updates for message events
Browse files Browse the repository at this point in the history
Closes #890
  • Loading branch information
barmac committed Apr 25, 2023
1 parent 8d94193 commit 0d50f43
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/provider/cloud-element-templates/ReferencedElementBehavior.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getBusinessObject, isAny } from 'bpmn-js/lib/util/ModelUtil';
import { getBusinessObject, is, isAny } from 'bpmn-js/lib/util/ModelUtil';
import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
import { isString } from 'min-dash';

Expand Down Expand Up @@ -77,11 +77,14 @@ export class ReferencedElementBehavior extends CommandInterceptor {

/**
* Remove referenced element when template is removed.
* Keep referenced element when template is replaced.
*/
_handleReplacement(context) {
const { oldShape } = context;
const { oldShape, newShape } = context;
const oldTemplate = getTemplateId(oldShape),
newTemplate = getTemplateId(newShape);

if (!canHaveReferencedElement(oldShape)) {
if (!canHaveReferencedElement(oldShape) || !oldTemplate) {
return;
}

Expand All @@ -92,7 +95,12 @@ export class ReferencedElementBehavior extends CommandInterceptor {
return;
}

this._removeRootElement(message);
if (!canHaveReferencedElement(newShape) || !newTemplate) {
this._removeRootElement(message);
return;
}

this._addMessage(newShape, message);
}

_handleRemoval(context) {
Expand Down Expand Up @@ -127,6 +135,14 @@ export class ReferencedElementBehavior extends CommandInterceptor {
rootElements: rootElements.filter(e => e !== rootElement)
});
}

_addMessage(element, message) {
const bo = getReferringElement(element);

this._modeling.updateModdleProperties(element, bo, {
'messageRef': message
});
}
}

ReferencedElementBehavior.$inject = [
Expand All @@ -147,6 +163,16 @@ function canHaveReferencedElement(element) {
]);
}

function getReferringElement(element) {
const bo = getBusinessObject(element);

if (is(bo, 'bpmn:Event')) {
return bo.get('eventDefinitions')[0];
}

return bo;
}

function isLabel(element) {
return element.type === 'label';
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<bpmn:intermediateCatchEvent id="MessageEvent" name="Event" zeebe:modelerTemplate="messageEventTemplate">
<bpmn:messageEventDefinition id="MessageEventDefinition_0klf6zr" messageRef="Message_1" />
</bpmn:intermediateCatchEvent>
<bpmn:intermediateCatchEvent id="MessageEvent_2" zeebe:modelerTemplate="updateTemplate" zeebe:modelerTemplateVersion="1">
<bpmn:messageEventDefinition id="MessageEventDefinition_0ki3umf" messageRef="Message_2" />
</bpmn:intermediateCatchEvent>
<bpmn:group id="Group_1" categoryValueRef="CategoryValue_025ggwq" zeebe:modelerTemplate="qux" />
<bpmn:textAnnotation id="TextAnnotation_1" zeebe:modelerTemplate="qux">
<bpmn:text>Text Annotation</bpmn:text>
Expand All @@ -25,6 +28,11 @@
<zeebe:subscription correlationKey="=correlationKey" />
</bpmn:extensionElements>
</bpmn:message>
<bpmn:message id="Message_2" name="version_1" zeebe:modelerTemplate="updateTemplate">
<bpmn:extensionElements>
<zeebe:subscription correlationKey="=correlationKey" />
</bpmn:extensionElements>
</bpmn:message>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1">
<bpmndi:BPMNShape id="Activity_1srimyz_di" bpmnElement="Task_1">
Expand All @@ -42,6 +50,9 @@
<bpmndi:BPMNShape id="Event_0cwpnu8_di" bpmnElement="MessageEvent">
<dc:Bounds x="182" y="402" width="36" height="36" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_0rxdmt7_di" bpmnElement="MessageEvent_2">
<dc:Bounds x="182" y="542" width="36" height="36" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Group_1_di" bpmnElement="Group_1">
<dc:Bounds x="450" y="130" width="300" height="300" />
<bpmndi:BPMNLabel>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[
{
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
"id": "foo",
"name":"Foo",
"appliesTo": [ "bpmn:Task" ],
"properties": []
},
{
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
"id": "foo",
"name":"Foo 1",
"version": 1,
Expand All @@ -22,6 +24,7 @@
]
},
{
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
"id": "foo",
"name":"Foo 2",
"version": 2,
Expand All @@ -38,26 +41,30 @@
]
},
{
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
"id": "bar",
"name":"Bar 1",
"version": 1,
"appliesTo": [ "bpmn:Task" ],
"properties": []
},
{
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
"id": "bar",
"name":"Bar 2",
"version": 2,
"appliesTo": [ "bpmn:Task" ],
"properties": []
},
{
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
"id": "baz",
"name":"Baz",
"appliesTo": [ "bpmn:Task" ],
"properties": []
},
{
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
"id": "icon",
"name":"Icon",
"appliesTo": [ "bpmn:Task" ],
Expand All @@ -67,12 +74,14 @@
}
},
{
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
"id": "qux",
"name":"Qux",
"appliesTo": [ "bpmn:BaseElement" ],
"properties": []
},
{
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
"name": "Message Event Template",
"id": "messageEventTemplate",
"appliesTo": [
Expand Down Expand Up @@ -101,5 +110,69 @@
}
}
]
},
{
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
"name": "Message Event Template - outdated",
"id": "updateTemplate",
"version": 1,
"appliesTo": [
"bpmn:IntermediateCatchEvent",
"bpmn:IntermediateThrowEvent"
],
"elementType": {
"value": "bpmn:IntermediateCatchEvent",
"eventDefinition": "bpmn:MessageEventDefinition"
},
"properties": [
{
"type": "String",
"value": "version_1",
"binding": {
"type": "bpmn:Message#property",
"name": "name"
}
},
{
"type": "Hidden",
"value": "=correlationKey",
"binding": {
"type": "bpmn:Message#zeebe:subscription#property",
"name": "correlationKey"
}
}
]
},
{
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
"name": "Message Event Template - new",
"id": "updateTemplate",
"version": 2,
"appliesTo": [
"bpmn:IntermediateCatchEvent",
"bpmn:IntermediateThrowEvent"
],
"elementType": {
"value": "bpmn:IntermediateCatchEvent",
"eventDefinition": "bpmn:MessageEventDefinition"
},
"properties": [
{
"type": "String",
"value": "version_2",
"binding": {
"type": "bpmn:Message#property",
"name": "name"
}
},
{
"type": "Hidden",
"value": "=correlationKey",
"binding": {
"type": "bpmn:Message#zeebe:subscription#property",
"name": "correlationKey"
}
}
]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
unlinkTemplate,
updateTemplate
} from 'src/provider/cloud-element-templates/util/templateUtil';
import { findMessage } from 'src/provider/cloud-element-templates/Helper';

import TestContainer from 'mocha-test-container-support';

Expand Down Expand Up @@ -180,6 +181,54 @@ describe('provider/cloud-element-template - templateUtil', function() {
expect(taskBo.modelerTemplate).to.eql('foo');
expect(taskBo.modelerTemplateVersion).to.eql(2);
}));


it('should update message event template', inject(function(elementRegistry, injector) {

// given
const newTemplate = templates.find(
template => template.id === 'updateTemplate' && template.version === 2);
let event = elementRegistry.get('MessageEvent_2');

// when
updateTemplate(event, newTemplate, injector);

// then
event = elementRegistry.get('MessageEvent_2');
const eventBo = getBusinessObject(event);

expect(eventBo.modelerTemplate).to.eql('updateTemplate');
expect(eventBo.modelerTemplateVersion).to.eql(2);

const message = findMessage(eventBo);
expect(message.name).to.eql('version_2');
}));


it('should update message event template but keep user-edited name',
inject(function(elementRegistry, modeling, injector) {

// given
const newTemplate = templates.find(
template => template.id === 'updateTemplate' && template.version === 2);
let event = elementRegistry.get('MessageEvent_2'),
eventBo = getBusinessObject(event);
modeling.updateModdleProperties(event, findMessage(eventBo), { name: 'user_edited' });

// when
updateTemplate(event, newTemplate, injector);

// then
event = elementRegistry.get('MessageEvent_2');
eventBo = getBusinessObject(event);

expect(eventBo.modelerTemplate).to.eql('updateTemplate');
expect(eventBo.modelerTemplateVersion).to.eql(2);

const message = findMessage(eventBo);
expect(message.name).to.eql('user_edited');
})
);
});


Expand Down

0 comments on commit 0d50f43

Please sign in to comment.