Skip to content

Commit

Permalink
fix(Condition): use update logic for applying condtions
Browse files Browse the repository at this point in the history
fixes #32
  • Loading branch information
marstamm committed Dec 1, 2023
1 parent 53b864e commit e270ba0
Show file tree
Hide file tree
Showing 7 changed files with 502 additions and 107 deletions.
96 changes: 62 additions & 34 deletions src/cloud-element-templates/ElementTemplatesConditionChecker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import {
import { isObject } from 'min-dash';
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 { ZEEBE_TASK_DEFINITION, ZEEBE_TASK_DEFINITION_TYPE_TYPE } from './util/bindingTypes';

const HIGH_PRIORITY = 2500;

/**
* Checks the conditions of an element template and sets/resets the
Expand All @@ -33,6 +33,23 @@ export default class ElementTemplatesConditionChecker extends CommandInterceptor
'propertiesPanel.zeebe.changeTemplate',
'element.move'
], this._applyConditions, true, this);

// Apply Conditions before changing properties. This persists the template so we can check if conditions apply
// after upgrading the template.
this.preExecute([ 'propertiesPanel.zeebe.changeTemplate' ], HIGH_PRIORITY, this._applyCondtions , true, this);
}

_applyCondtions(context) {
const {
element,
newTemplate
} = context;

if (!element || !newTemplate) {
return;
}

context.newTemplate = applyConditions(context.element, context.newTemplate);
}

_saveConditionalState(context) {
Expand All @@ -52,35 +69,38 @@ export default class ElementTemplatesConditionChecker extends CommandInterceptor
_applyConditions(context) {
const {
element,
oldTemplate
hints = {}
} = context;


if (hints.skipConditionUpdate) {
return;
}

const template = this._elementTemplates.get(element);

// New Template is persisted before applying default values,
// new conditions might apply after the defaults are present.
const oldTemplate = context.oldTemplate || context.newTemplate;

if (!template || !oldTemplate || template.id !== oldTemplate.id) {
return;
}

const newTemplate = applyConditions(element, template);

const propertiesToAdd = getMissingProperties(oldTemplate, newTemplate);
const propertiesToRemove = getPropertiesToRemove(newTemplate, oldTemplate);

this._updateReferencedElement(element, oldTemplate, newTemplate);

propertiesToAdd.forEach(property =>
setPropertyValue(this._bpmnFactory, this._commandStack, element, property, property.value)
);
if (!hasDifferentPropertyBindings(newTemplate, oldTemplate)) {
return;
}

propertiesToRemove.forEach(property =>
unsetProperty(this._commandStack, element, property)
);
}
const changeContext = {
element,
newTemplate,
oldTemplate,
hints: { skipConditionUpdate: true }
};

_updateReferencedElement(element, oldTemplate, newTemplate) {
if (hasMessageProperties(oldTemplate) && !hasMessageProperties(newTemplate)) {
removeMessage(element, this._injector);
}
this._commandStack.execute('propertiesPanel.zeebe.changeTemplate', changeContext);
}
}

Expand All @@ -96,6 +116,28 @@ ElementTemplatesConditionChecker.$inject = [

// helpers

function hasDifferentPropertyBindings(sourceTemplate, targetTemplate) {
return hasNewProperties(sourceTemplate, targetTemplate) || hasRemovedProperties(sourceTemplate, targetTemplate);
}

function hasNewProperties(sourceTemplate, targetTemplate) {
let properties = targetTemplate.properties;

return properties.some(targetProp =>!(
sourceTemplate.properties.find(sourceProp => compareProps(sourceProp, targetProp))
));
}

function hasRemovedProperties(oldTemplate, newTemplate) {
const oldProperties = getMissingProperties(newTemplate, oldTemplate);

// ensure XML properties are mantained for properties with
// different conditions but same bindings
return oldProperties.some(property =>
!findPropertyWithBinding(newTemplate, property)
);
}

function getMissingProperties(sourceTemplate, targetTemplate) {

let properties = targetTemplate.properties;
Expand All @@ -118,16 +160,6 @@ function findPropertyWithBinding(template, prop1) {
);
}

function getPropertiesToRemove(newTemplate, oldTemplate) {
const oldProperties = getMissingProperties(newTemplate, oldTemplate);

// ensure XML properties are mantained for properties with
// different conditions but same bindings
return oldProperties.filter(property =>
!findPropertyWithBinding(newTemplate, property)
);
}

function normalizeReplacer(key, value) {

if (isObject(value)) {
Expand Down Expand Up @@ -168,7 +200,3 @@ function normalizeBinding(binding) {
function equals(a, b) {
return JSON.stringify(a, normalizeReplacer) === JSON.stringify(b, normalizeReplacer);
}

function hasMessageProperties(template) {
return template.properties.some(p => MESSAGE_BINDING_TYPES.includes(p.binding.type));
}
4 changes: 4 additions & 0 deletions src/cloud-element-templates/Helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ export function findMessage(businessObject) {
businessObject = eventDefinitions[0];
}

if (!businessObject) {
return;
}

return businessObject.get('messageRef');
}

Expand Down
Loading

0 comments on commit e270ba0

Please sign in to comment.