Skip to content

Commit

Permalink
test pass
Browse files Browse the repository at this point in the history
  • Loading branch information
marstamm committed Dec 1, 2023
1 parent 63043ff commit 5fdf742
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 82 deletions.
36 changes: 15 additions & 21 deletions src/cloud-element-templates/ElementTemplatesConditionChecker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
import { setPropertyValue, unsetProperty } from './util/propertyUtil';

Check failure on line 8 in src/cloud-element-templates/ElementTemplatesConditionChecker.js

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, 16, diagram-js@11.9 bpmn-js@11.5)

'setPropertyValue' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 8 in src/cloud-element-templates/ElementTemplatesConditionChecker.js

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, 16, diagram-js@11.9 bpmn-js@11.5)

'unsetProperty' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 8 in src/cloud-element-templates/ElementTemplatesConditionChecker.js

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, 16)

'setPropertyValue' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 8 in src/cloud-element-templates/ElementTemplatesConditionChecker.js

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, 16)

'unsetProperty' is defined but never used. Allowed unused vars must match /^_/u
import { MESSAGE_BINDING_TYPES, ZEEBE_TASK_DEFINITION, ZEEBE_TASK_DEFINITION_TYPE_TYPE } from './util/bindingTypes';
import { removeMessage } from './util/rootElementUtil';

Check failure on line 10 in src/cloud-element-templates/ElementTemplatesConditionChecker.js

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, 16, diagram-js@11.9 bpmn-js@11.5)

'removeMessage' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 10 in src/cloud-element-templates/ElementTemplatesConditionChecker.js

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, 16)

'removeMessage' is defined but never used. Allowed unused vars must match /^_/u
import { findMessage } from './Helper';

Check failure on line 11 in src/cloud-element-templates/ElementTemplatesConditionChecker.js

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, 16, diagram-js@11.9 bpmn-js@11.5)

'findMessage' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 11 in src/cloud-element-templates/ElementTemplatesConditionChecker.js

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, 16)

'findMessage' is defined but never used. Allowed unused vars must match /^_/u
import { getBusinessObject } from 'bpmn-js/lib/util/ModelUtil';

Check failure on line 12 in src/cloud-element-templates/ElementTemplatesConditionChecker.js

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, 16, diagram-js@11.9 bpmn-js@11.5)

'getBusinessObject' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 12 in src/cloud-element-templates/ElementTemplatesConditionChecker.js

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, 16)

'getBusinessObject' is defined but never used. Allowed unused vars must match /^_/u

/**
* Checks the conditions of an element template and sets/resets the
Expand Down Expand Up @@ -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);

Expand All @@ -61,19 +62,14 @@ 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
} = context;

const oldTemplate = context.oldTemplate || context.newTemplate;

// if (skipConditionCheck) {
// console.log('skipConditionCheck', context);
// return;
// }

if (isConditionUpdate) {
return;
Expand All @@ -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;
Expand All @@ -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);
// }
// }
}


Expand Down
115 changes: 82 additions & 33 deletions src/cloud-element-templates/cmd/ChangeElementTemplateHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
getRoot
} from '../../utils/ElementUtil';
import { removeMessage } from '../util/rootElementUtil';
import { setPropertyValue } from '../util/propertyUtil';

Check failure on line 46 in src/cloud-element-templates/cmd/ChangeElementTemplateHandler.js

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, 16, diagram-js@11.9 bpmn-js@11.5)

'setPropertyValue' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 46 in src/cloud-element-templates/cmd/ChangeElementTemplateHandler.js

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, 16)

'setPropertyValue' is defined but never used. Allowed unused vars must match /^_/u

/**
* Applies an element template to an element. Sets `zeebe:modelerTemplate` and
Expand All @@ -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;
Expand All @@ -76,7 +79,6 @@ export default class ChangeElementTemplateHandler {
* @param {Object} [context.newTemplate]
*/
preExecute(context) {
console.log('preExecute');
let newTemplate = context.newTemplate,
oldTemplate = context.oldTemplate;

Expand All @@ -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);
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -689,7 +691,6 @@ export default class ChangeElementTemplateHandler {
}

_updateMessage(element, oldTemplate, newTemplate) {
console.log(newTemplate);

// update bpmn:Message properties
this._updateMessageProperties(element, oldTemplate, newTemplate);
Expand All @@ -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);
}
}

/**
Expand All @@ -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),
Expand All @@ -742,7 +755,6 @@ export default class ChangeElementTemplateHandler {
}

properties[ newBindingName ] = newPropertyValue;
console.log('add new mesage prop', properties);

this._modeling.updateModdleProperties(element, changedElement, properties);
});
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -852,7 +890,7 @@ export default class ChangeElementTemplateHandler {
bo = bo.get('eventDefinitions')[0];
}

return bo.get('messageRef');
return bo && bo.get('messageRef');
}


Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/cloud-element-templates/util/rootElementUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 5fdf742

Please sign in to comment.