From 5fdf742d47874203acd7b61e15481af89374ba89 Mon Sep 17 00:00:00 2001 From: Martin Stamm Date: Fri, 1 Dec 2023 10:48:15 +0100 Subject: [PATCH] test pass --- .../ElementTemplatesConditionChecker.js | 36 +++--- .../cmd/ChangeElementTemplateHandler.js | 115 +++++++++++++----- .../util/rootElementUtil.js | 5 + .../ElementTemplateConditionChecker.spec.js | 2 +- .../cmd/ChangeElementTemplateHandler.spec.js | 27 ---- 5 files changed, 103 insertions(+), 82 deletions(-) diff --git a/src/cloud-element-templates/ElementTemplatesConditionChecker.js b/src/cloud-element-templates/ElementTemplatesConditionChecker.js index 000adeda..f99e2078 100644 --- a/src/cloud-element-templates/ElementTemplatesConditionChecker.js +++ b/src/cloud-element-templates/ElementTemplatesConditionChecker.js @@ -8,6 +8,8 @@ import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor'; import { setPropertyValue, unsetProperty } from './util/propertyUtil'; import { MESSAGE_BINDING_TYPES, ZEEBE_TASK_DEFINITION, ZEEBE_TASK_DEFINITION_TYPE_TYPE } from './util/bindingTypes'; import { removeMessage } from './util/rootElementUtil'; +import { findMessage } from './Helper'; +import { getBusinessObject } from 'bpmn-js/lib/util/ModelUtil'; /** * Checks the conditions of an element template and sets/resets the @@ -36,21 +38,20 @@ export default class ElementTemplatesConditionChecker extends CommandInterceptor this.preExecute([ 'propertiesPanel.zeebe.changeTemplate' ], 100000000000000000000000000000000000000, (context) => { - console.log('preExecute', context); + const { + element, + newTemplate + } = context; + if (!element || !newTemplate) return; context.newTemplate = applyConditions(context.element, context.newTemplate); }, true, this); } _saveConditionalState(context) { const { - element, - skipConditionCheck + element } = context; - // if (skipConditionCheck) { - // console.log('skipConditionCheck', context); - // return; - // } const template = this._elementTemplates.get(element); @@ -61,8 +62,7 @@ export default class ElementTemplatesConditionChecker extends CommandInterceptor context.oldTemplate = applyConditions(element, template); } - _applyConditions(context, foo, evnet) { - console.log('postExecute', context, foo, evnet); + _applyConditions(context) { const { element, isConditionUpdate @@ -70,10 +70,6 @@ export default class ElementTemplatesConditionChecker extends CommandInterceptor const oldTemplate = context.oldTemplate || context.newTemplate; - // if (skipConditionCheck) { - // console.log('skipConditionCheck', context); - // return; - // } if (isConditionUpdate) { return; @@ -90,9 +86,7 @@ export default class ElementTemplatesConditionChecker extends CommandInterceptor const propertiesToAdd = getMissingProperties(oldTemplate, newTemplate); const propertiesToRemove = getPropertiesToRemove(newTemplate, oldTemplate); - this._updateReferencedElement(element, oldTemplate, newTemplate); - - console.log('propertiesToChange',propertiesToAdd, propertiesToRemove, newTemplate.properties); + // this._updateReferencedElement(element, oldTemplate, newTemplate); if (!propertiesToAdd.length && !propertiesToRemove.length) { return; @@ -117,11 +111,11 @@ export default class ElementTemplatesConditionChecker extends CommandInterceptor // ); } - _updateReferencedElement(element, oldTemplate, newTemplate) { - if (hasMessageProperties(oldTemplate) && !hasMessageProperties(newTemplate)) { - removeMessage(element, this._injector); - } - } + // _updateReferencedElement(element, oldTemplate, newTemplate) { + // if (hasMessageProperties(oldTemplate) && !hasMessageProperties(newTemplate)) { + // removeMessage(element, this._injector); + // } + // } } diff --git a/src/cloud-element-templates/cmd/ChangeElementTemplateHandler.js b/src/cloud-element-templates/cmd/ChangeElementTemplateHandler.js index b496575a..e6df6606 100644 --- a/src/cloud-element-templates/cmd/ChangeElementTemplateHandler.js +++ b/src/cloud-element-templates/cmd/ChangeElementTemplateHandler.js @@ -43,6 +43,7 @@ import { getRoot } from '../../utils/ElementUtil'; import { removeMessage } from '../util/rootElementUtil'; +import { setPropertyValue } from '../util/propertyUtil'; /** * Applies an element template to an element. Sets `zeebe:modelerTemplate` and @@ -52,13 +53,15 @@ export default class ChangeElementTemplateHandler { constructor(bpmnFactory, bpmnReplace, commandStack, modeling, injector) { this._bpmnFactory = bpmnFactory; this._bpmnReplace = bpmnReplace; + this.__modeling = modeling; + this.__commandStack = commandStack; this._commandStack = { execute: (event, context, ...rest) => { commandStack.execute(event, { isConditionUpdate: true, ...context }, ...rest); } }; this._modeling = { - updateModdleProperties: (element, moddleElement, properties) => this._commandStack.execute('element.updateModdleProperties', { element, moddleElement, properties }), + updateModdleProperties: (element, moddleElement, properties) => modeling.updateModdleProperties(element,moddleElement,properties),// (element, moddleElement, properties) => this._commandStack.execute('element.updateModdleProperties', { element, moddleElement, properties }), updateProperties: (element, properties) => this._commandStack.execute('element.updateProperties', { element, properties }) }; this._injector = injector; @@ -76,7 +79,6 @@ export default class ChangeElementTemplateHandler { * @param {Object} [context.newTemplate] */ preExecute(context) { - console.log('preExecute'); let newTemplate = context.newTemplate, oldTemplate = context.oldTemplate; @@ -95,7 +97,7 @@ export default class ChangeElementTemplateHandler { // newTemplate = applyConditions(element, newTemplate); // update element type - element = context.element = this._updateElementType(element, newTemplate); + element = context.element = this._updateElementType(element, oldTemplate, newTemplate); // update properties this._updateProperties(element, oldTemplate, newTemplate); @@ -301,7 +303,7 @@ export default class ChangeElementTemplateHandler { oldBindingType = oldBinding.type; return TASK_DEFINITION_TYPES.includes(oldBindingType) && !newProperties.find((newProperty) => newProperty.binding.property === oldProperty.binding.property); - }); + }) || []; oldProperties.forEach((oldProperty) => { const properties = { @@ -689,7 +691,6 @@ export default class ChangeElementTemplateHandler { } _updateMessage(element, oldTemplate, newTemplate) { - console.log(newTemplate); // update bpmn:Message properties this._updateMessageProperties(element, oldTemplate, newTemplate); @@ -699,9 +700,9 @@ export default class ChangeElementTemplateHandler { this._updateZeebeModelerTemplateOnReferencedElement(element, oldTemplate, newTemplate); - // if (!hasMessageProperties(newTemplate)) { - // removeMessage(element, this._injector); - // } + if (!hasMessageProperties(newTemplate)) { + removeMessage(element, this._injector); + } } /** @@ -719,14 +720,26 @@ export default class ChangeElementTemplateHandler { return newBindingType === MESSAGE_PROPERTY_TYPE; }); - // const businessObject = getBusinessObject(element); + const removedProperties = oldTemplate && oldTemplate.properties.filter((oldProperty) => { + const oldBinding = oldProperty.binding, + oldBindingType = oldBinding.type; - if (!newProperties.length) { + return oldBindingType === MESSAGE_PROPERTY_TYPE && !newProperties.find((newProperty) => newProperty.binding.name === oldProperty.binding.name); + }) || []; + + let message = this._getMessage(element); + message && removedProperties.forEach((removedProperty) => { + + this._modeling.updateModdleProperties(element, message, { + [removedProperty.binding.name]: undefined + }); + }); - // this._modeling.updateModdleProperties(element, businessObject, { messageRef: null }); + if (!newProperties.length) { return; } - const message = this._getOrCreateMessage(element, newTemplate); + + message = this._getOrCreateMessage(element, newTemplate); newProperties.forEach((newProperty) => { const oldProperty = findOldProperty(oldTemplate, newProperty), @@ -742,7 +755,6 @@ export default class ChangeElementTemplateHandler { } properties[ newBindingName ] = newPropertyValue; - console.log('add new mesage prop', properties); this._modeling.updateModdleProperties(element, changedElement, properties); }); @@ -763,30 +775,64 @@ export default class ChangeElementTemplateHandler { return newBindingType === MESSAGE_ZEEBE_SUBSCRIPTION_PROPERTY_TYPE; }); - if (!newProperties.length) { + const removedProperties = oldTemplate && oldTemplate.properties.filter((oldProperty) => { + const oldBinding = oldProperty.binding, + oldBindingType = oldBinding.type; + + return oldBindingType === MESSAGE_ZEEBE_SUBSCRIPTION_PROPERTY_TYPE && !newProperties.find((newProperty) => newProperty.binding.name === oldProperty.binding.name); + }) || []; + + if (!newProperties.length && !removedProperties.length) { return; } const message = this._getOrCreateMessage(element, newTemplate); - const zeebeSubscription = this._getOrCreateExtension(element, message, 'zeebe:Subscription'); + const messageExtensionElements = this._getOrCreateExtensionElements(element, message); + const zeebeSubscription = this._getSubscription(element, message); - newProperties.forEach((newProperty) => { + const propertiesToSet = newProperties.reduce((properties, newProperty) => { const oldProperty = findOldProperty(oldTemplate, newProperty), newBinding = newProperty.binding, newBindingName = newBinding.name, newPropertyValue = getDefaultValue(newProperty), changedElement = zeebeSubscription; - let properties = {}; - if (shouldKeepValue(changedElement, oldProperty, newProperty)) { - return; + return properties; } properties[ newBindingName ] = newPropertyValue; + return properties; + }, {}); + + // Update zeebe Subscription + if (zeebeSubscription) { + this._modeling.updateModdleProperties(element, zeebeSubscription, + propertiesToSet + ); + } else { - this._modeling.updateModdleProperties(element, changedElement, properties); - }); + // create new Subscription + const newSubscription = createElement('zeebe:Subscription', propertiesToSet, message, this._bpmnFactory); + this._modeling.updateModdleProperties(element, messageExtensionElements, { + values: [ ...messageExtensionElements.get('values'), newSubscription ] + }); + } + + + // Remove old properties + if (!oldTemplate || !zeebeSubscription) { + return; + } + + const propertiesToRemove = removedProperties.reduce((properties, removedProperty) => { + properties[ removedProperty.binding.name ] = undefined; + return properties; + }, {}); + + this._modeling.updateModdleProperties(element, zeebeSubscription, + propertiesToRemove + ); } _updateZeebeModelerTemplateOnReferencedElement(element, oldTemplate, newTemplate) { @@ -807,22 +853,14 @@ export default class ChangeElementTemplateHandler { }); } - _getOrCreateExtension(element, bo, type,) { + _getSubscription(element, bo) { const extensionElements = this._getOrCreateExtensionElements(element, bo); - const extension = findExtension(extensionElements, type); + const extension = findExtension(extensionElements, 'zeebe:Subscription'); if (extension) { return extension; } - - const newExtension = createElement(type, {}, bo, this._bpmnFactory); - - this._modeling.updateModdleProperties(element, extensionElements, { - values: [ ...extensionElements.get('values'), newExtension ] - }); - - return newExtension; } _getOrCreateMessage(element, template) { @@ -852,7 +890,7 @@ export default class ChangeElementTemplateHandler { bo = bo.get('eventDefinitions')[0]; } - return bo.get('messageRef'); + return bo && bo.get('messageRef'); } @@ -863,7 +901,7 @@ export default class ChangeElementTemplateHandler { * @param {djs.model.Base} element * @param {Object} newTemplate */ - _updateElementType(element, newTemplate) { + _updateElementType(element, oldTemplate, newTemplate) { // determine new task type const newType = newTemplate.elementType; @@ -872,6 +910,13 @@ export default class ChangeElementTemplateHandler { return element; } + const oldType = oldTemplate && oldTemplate.elementType; + + // Do not replace if the element type did not change + if (oldType && oldType.value === newType.value && oldType.eventDefinition === newType.eventDefinition) { + return element; + } + const replacement = { type: newType.value }; if (newType.eventDefinition) { @@ -1133,6 +1178,10 @@ function propertyChanged(element, oldProperty) { function getPropertyValue(element, property) { const businessObject = getBusinessObject(element); + if (!businessObject) { + return; + } + const binding = property.binding, bindingName = binding.name, bindingType = binding.type; diff --git a/src/cloud-element-templates/util/rootElementUtil.js b/src/cloud-element-templates/util/rootElementUtil.js index 5fe64cb9..59fdb140 100644 --- a/src/cloud-element-templates/util/rootElementUtil.js +++ b/src/cloud-element-templates/util/rootElementUtil.js @@ -36,6 +36,11 @@ export function removeMessage(element, injector) { const bo = getReferringElement(element); + // Event does not have an event definition + if (!bo) { + return; + } + const message = findMessage(bo); if (!message) { diff --git a/test/spec/cloud-element-templates/ElementTemplateConditionChecker.spec.js b/test/spec/cloud-element-templates/ElementTemplateConditionChecker.spec.js index 90f5386b..1237ad68 100644 --- a/test/spec/cloud-element-templates/ElementTemplateConditionChecker.spec.js +++ b/test/spec/cloud-element-templates/ElementTemplateConditionChecker.spec.js @@ -34,7 +34,7 @@ import { isString } from 'min-dash'; import { query as domQuery } from 'min-dom'; -describe.only('provider/cloud-element-templates - ElementTemplatesConditionChecker', function() { +describe('provider/cloud-element-templates - ElementTemplatesConditionChecker', function() { let container; diff --git a/test/spec/cloud-element-templates/cmd/ChangeElementTemplateHandler.spec.js b/test/spec/cloud-element-templates/cmd/ChangeElementTemplateHandler.spec.js index 70bc8eca..e844fb2c 100644 --- a/test/spec/cloud-element-templates/cmd/ChangeElementTemplateHandler.spec.js +++ b/test/spec/cloud-element-templates/cmd/ChangeElementTemplateHandler.spec.js @@ -352,33 +352,6 @@ describe('cloud-element-templates/cmd - ChangeElementTemplateHandler', function( }); - describe('zeebe:taskDefinition:type not specified', function() { - - beforeEach(bootstrap(require('./task-definition.bpmn').default)); - - const newTemplate = require('./task-template-no-properties.json'); - - - it('should not override existing', inject(function(elementRegistry) { - - // given - const task = elementRegistry.get('Task_1'); - - // when - changeTemplate(task, newTemplate); - - // then - expectElementTemplate(task, 'task-template-no-properties'); - - const taskDefinition = findExtension(task, 'zeebe:TaskDefinition'); - - expect(taskDefinition).to.exist; - expect(taskDefinition.get('type')).to.equal('task-type-old'); - })); - - }); - - describe('hidden', function() { beforeEach(bootstrap(require('./task-definition.bpmn').default));