Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: fix data store removal behavior #1686

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
93 changes: 63 additions & 30 deletions lib/features/modeling/behavior/DataStoreBehavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import {
import { isAny } from '../util/ModelingUtil';

import UpdateSemanticParentHandler from '../cmd/UpdateSemanticParentHandler';
import { isLabel } from '../../../util/LabelUtil';


/**
* BPMN specific data store behavior
*/
export default function DataStoreBehavior(
canvas, commandStack, elementRegistry,
eventBus) {
eventBus,
modeling) {

CommandInterceptor.call(this, eventBus);

Expand All @@ -39,7 +41,9 @@ export default function DataStoreBehavior(
function updateDataStoreParent(dataStore, newDataStoreParent) {
var dataStoreBo = dataStore.businessObject || dataStore;

newDataStoreParent = newDataStoreParent || getFirstParticipantWithProcessRef();
if (typeof newDataStoreParent === 'undefined') {
newDataStoreParent = getFirstParticipantWithProcessRef();
}

if (newDataStoreParent) {
var newDataStoreParentBo = newDataStoreParent.businessObject || newDataStoreParent;
Expand All @@ -50,6 +54,16 @@ export default function DataStoreBehavior(
newSemanticParent: newDataStoreParentBo.processRef || newDataStoreParentBo,
newDiParent: getDi(newDataStoreParent)
});
} else {
dataStore = elementRegistry.get(dataStore.id);

// data store may already be deleted at this point
if (dataStore) {

// remove data store which has no valid attach
// candidate anymore
modeling.removeShape(dataStore);
}
}
}

Expand Down Expand Up @@ -101,13 +115,15 @@ export default function DataStoreBehavior(
shape = context.shape,
parent = shape.parent;

if (!isDataStore(shape)) {
return;
}

if (is(shape, 'bpmn:DataStoreReference') &&
shape.type !== 'label' &&
is(parent, 'bpmn:Collaboration')) {

updateDataStoreParent(shape);
if (!is(parent, 'bpmn:Collaboration')) {
return;
}

updateDataStoreParent(shape);
});


Expand All @@ -118,22 +134,19 @@ export default function DataStoreBehavior(
oldParent = context.oldParent,
parent = shape.parent;

if (is(oldParent, 'bpmn:Collaboration')) {
if (!isDataStore(shape)) {
return;
}

if (is(oldParent, 'bpmn:Collaboration') || !is(parent, 'bpmn:Collaboration')) {

// do nothing if not necessary
return;
}

if (is(shape, 'bpmn:DataStoreReference') &&
shape.type !== 'label' &&
is(parent, 'bpmn:Collaboration')) {

var participant = is(oldParent, 'bpmn:Participant') ?
oldParent :
getAncestor(oldParent, 'bpmn:Participant');
var participant = getClosesParent(oldParent, 'bpmn:Participant');

updateDataStoreParent(shape, participant);
}
updateDataStoreParent(shape, participant);
});


Expand All @@ -143,16 +156,29 @@ export default function DataStoreBehavior(
shape = context.shape,
rootElement = canvas.getRootElement();

if (isAny(shape, [ 'bpmn:Participant', 'bpmn:SubProcess' ])
&& is(rootElement, 'bpmn:Collaboration')) {
getDataStores(rootElement)
.filter(function(dataStore) {
return isDescendant(dataStore, shape);
})
.forEach(function(dataStore) {
updateDataStoreParent(dataStore);
});
if (!is(rootElement, 'bpmn:Collaboration')) {
return;
}

if (isDataStore(shape)) {
updateDataStoreParent(shape, null);
}

if (!isAny(shape, [ 'bpmn:Participant', 'bpmn:SubProcess' ])) {
return;
}

getDataStores(rootElement)
.filter(function(dataStore) {
return isDescendant(dataStore, shape);
})
.forEach(function(dataStore) {

console.log('updateDataStoreParent', dataStore, shape, rootElement);

updateDataStoreParent(dataStore);
});

});

// update data store parents on collaboration -> process
Expand All @@ -165,6 +191,8 @@ export default function DataStoreBehavior(

dataStores.forEach(function(dataStore) {

console.log('update daastore parent!');

if (is(newRoot, 'bpmn:Process')) {
updateDataStoreParent(dataStore, newRoot);
}
Expand All @@ -178,6 +206,7 @@ DataStoreBehavior.$inject = [
'commandStack',
'elementRegistry',
'eventBus',
'modeling'
];

inherits(DataStoreBehavior, CommandInterceptor);
Expand All @@ -200,13 +229,17 @@ function isDescendant(descendant, ancestor) {
return false;
}

function getAncestor(element, type) {
function getClosesParent(element, type) {

while (element.parent) {
if (is(element.parent, type)) {
return element.parent;
while (element) {
if (is(element, type)) {
return element;
}

element = element.parent;
}
}

function isDataStore(shape) {
return is(shape, 'bpmn:DataStoreReference') && !isLabel(shape);
}
95 changes: 82 additions & 13 deletions test/spec/features/modeling/behavior/DataStoreBehaviorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import modelingModule from 'lib/features/modeling';
import coreModule from 'lib/core';


describe('features/modeling/behavior - data store', function() {
describe.only('features/modeling/behavior - data store', function() {

var testModules = [ coreModule, modelingModule ];

Expand Down Expand Up @@ -373,7 +373,7 @@ describe('features/modeling/behavior - data store', function() {
});


describe('collaboration', function() {
describe.only('collaboration', function() {

describe('update parent on participant removed', function() {
var processDiagramXML = require('./DataStoreBehavior.remove-participant.bpmn');
Expand Down Expand Up @@ -430,52 +430,121 @@ describe('features/modeling/behavior - data store', function() {
});


describe('collaboration -> process', function() {
describe('should remove when clearing collaboration diagram', function() {

var processDiagramXML = require('./DataStoreBehavior.collaboration.bpmn');

var dataStoreShape,
participant;
var participant,
dataStore,
collaboration;

beforeEach(bootstrapModeler(processDiagramXML, {
modules: testModules
}));

beforeEach(inject(function(canvas, elementRegistry, modeling) {

// given
participant = elementRegistry.get('Participant');
dataStore = elementRegistry.get('DataStoreReference');
collaboration = canvas.getRootElement();

// assume
expect(dataStore.parent).to.equal(collaboration);

// when
modeling.removeElements([
participant,
dataStore
]);
}));


it('should do', inject(function() {

// then
expect(participant.parent).not.to.exist;
expect(dataStore.parent).not.to.exist;
}));


it('should undo', inject(function(commandStack) {

// when
commandStack.undo();

// then
expect(participant.parent).to.equal(collaboration);
expect(dataStore.parent).to.equal(collaboration);
}));


it('should redo', inject(function(elementRegistry, modeling, commandStack) {

// when
commandStack.undo();
commandStack.redo();

// then
expect(participant.parent).not.to.exist;
expect(dataStore.parent).not.to.exist;
}));

});


describe('should remove with last participant', function() {

var processDiagramXML = require('./DataStoreBehavior.collaboration.bpmn');

var dataStore,
participant,
collaboration;

beforeEach(bootstrapModeler(processDiagramXML, { modules: testModules }));

beforeEach(inject(function(elementRegistry, modeling) {
dataStoreShape = elementRegistry.get('DataStoreReference');
beforeEach(inject(function(elementRegistry, modeling, canvas) {

dataStore = elementRegistry.get('DataStoreReference');
participant = elementRegistry.get('Participant');
collaboration = canvas.getRootElement();

// assume
expect(dataStore.parent).to.equal(collaboration);

// when
modeling.removeShape(participant);
}));


it('should do', inject(function(canvas) {
var rootElement = canvas.getRootElement();

// then
expect(dataStoreShape.businessObject.$parent).to.eql(rootElement.businessObject);
expect(dataStore.businessObject.$parent).not.to.exist;
expect(dataStore.parent).not.to.exist;
}));


it('should undo', inject(function(canvas, commandStack) {
it.only('should undo', inject(function(canvas, commandStack) {

// when
commandStack.undo();

// then
expect(dataStoreShape.businessObject.$parent).to.eql(participant.businessObject.processRef);
expect(dataStore.businessObject.$parent).to.eql(participant.businessObject.processRef);
expect(dataStore.parent).to.equal(collaboration);
}));


it('should redo', inject(function(canvas, commandStack) {
var rootElement = canvas.getRootElement();

// when
commandStack.undo();
commandStack.redo();

// then
expect(dataStoreShape.businessObject.$parent).to.eql(rootElement.businessObject);
expect(dataStore.businessObject.$parent).not.to.exist;
expect(dataStore.parent).not.to.exist;
}));

});
Expand Down