Skip to content

Commit

Permalink
fix: do not remove documentation and execution listeners when templat…
Browse files Browse the repository at this point in the history
…e is removed

Closes #114
  • Loading branch information
jarekdanielak committed Aug 28, 2024
1 parent 15e1f50 commit 977003b
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 3 deletions.
39 changes: 36 additions & 3 deletions src/element-templates/cmd/RemoveElementTemplateHandler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getLabel, setLabel } from 'bpmn-js/lib/features/label-editing/LabelUtil';
import { getShapeIdFromPlane, isPlane } from 'bpmn-js/lib/util/DrilldownUtil';
import { getBusinessObject, is } from 'bpmn-js/lib/util/ModelUtil';
import { getBusinessObject, is, isAny } from 'bpmn-js/lib/util/ModelUtil';

export default class RemoveElementTemplateHandler {
constructor(
Expand All @@ -10,7 +10,8 @@ export default class RemoveElementTemplateHandler {
canvas,
bpmnFactory,
replace,
commandStack
commandStack,
moddleCopy
) {
this._modeling = modeling;
this._elementFactory = elementFactory;
Expand All @@ -19,6 +20,7 @@ export default class RemoveElementTemplateHandler {
this._bpmnFactory = bpmnFactory;
this._replace = replace;
this._commandStack = commandStack;
this._moddleCopy = moddleCopy;
}

preExecute(context) {
Expand Down Expand Up @@ -121,6 +123,8 @@ export default class RemoveElementTemplateHandler {
newBo = bpmnFactory.create(bo.$type),
label = getLabel(element);

this._copyNonTemplateProperties(bo, newBo);

if (!label) {
return newBo;
}
Expand All @@ -133,6 +137,34 @@ export default class RemoveElementTemplateHandler {

return newBo;
}

/**
* Copy documentation and execution listeners from source to target business object.
*
* NOTE: Mutetes the target business object.
* @param {ModdleElement} sourceBusinessObject
* @param {ModdleElement} targetBusinessObject
*/
_copyNonTemplateProperties(sourceBusinessObject, targetBusinessObject) {
console.log('_copyNonTemplateProperties');
const copy = this._moddleCopy;
const bpmnFactory = this._bpmnFactory;

const { documentation, extensionElements } = sourceBusinessObject;

if (documentation) {
const docs = copy.copyProperty(documentation, targetBusinessObject, 'documentation');
targetBusinessObject.documentation = docs;
}

if (extensionElements && extensionElements.values) {
const exts = copy.copyProperty(extensionElements, targetBusinessObject, 'extensionElements');
const executionListenerTypes = [ 'zeebe:ExecutionListeners', 'camunda:ExecutionListener' ];
const executionListeners = exts.values.filter((value) => isAny(value, executionListenerTypes));

targetBusinessObject.extensionElements = bpmnFactory.create('bpmn:ExtensionElements', { values: executionListeners });
}
}
}


Expand All @@ -143,5 +175,6 @@ RemoveElementTemplateHandler.$inject = [
'canvas',
'bpmnFactory',
'replace',
'commandStack'
'commandStack',
'moddleCopy'
];
32 changes: 32 additions & 0 deletions test/spec/cloud-element-templates/ElementTemplates.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,38 @@ describe('provider/cloud-element-templates - ElementTemplates', function() {
});
}));


it('should keep documentation', inject(function(elementRegistry, elementTemplates) {

// given
let task = elementRegistry.get('Task_1');

// assume
const bo = getBusinessObject(task);
expect(bo.documentation[0].text).to.exist;

// when
task = elementTemplates.removeTemplate(task);

// then
const newBo = getBusinessObject(task);
expect(newBo.documentation[0].text).to.exist;
}));


it('should keep execution listeners', inject(function(elementRegistry, elementTemplates) {

// given
let task = elementRegistry.get('Task_5');

// when
task = elementTemplates.removeTemplate(task);

// then
const bo = getBusinessObject(task);
expect(bo.extensionElements.values[0].$type).to.equal('zeebe:ExecutionListeners');
}));

});


Expand Down
32 changes: 32 additions & 0 deletions test/spec/element-templates/ElementTemplates.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,38 @@ describe('provider/element-templates - ElementTemplates', function() {
expect(taskBo.flowElements).to.have.length(1);
}));


it('should keep documentation', inject(function(elementRegistry, elementTemplates) {

// given
let task = elementRegistry.get('Task_1');

// assume
const bo = getBusinessObject(task);
expect(bo.documentation[0].text).to.exist;

// when
task = elementTemplates.removeTemplate(task);

// then
const newBo = getBusinessObject(task);
expect(newBo.documentation[0].text).to.exist;
}));


it('should keep execution listeners', inject(function(elementRegistry, elementTemplates) {

// given
let task = elementRegistry.get('Task_5');

// when
task = elementTemplates.removeTemplate(task);

// then
const bo = getBusinessObject(task);
expect(bo.extensionElements.values[0].$type).to.equal('camunda:ExecutionListener');
}));

});


Expand Down

0 comments on commit 977003b

Please sign in to comment.