fix(Import): forgive broken di (missing bpmnElement attr)

Related to #8
This commit is contained in:
Nico Rehwaldt 2014-03-21 16:46:56 +01:00
parent 1597af78c2
commit 8055914d41
4 changed files with 135 additions and 52 deletions

View File

@ -1,6 +1,6 @@
var _ = require('lodash');
function BpmnTraverser(visitor) {
function BpmnTraverser(handler) {
var elementDiMap = {};
var elementGfxMap = {};
@ -19,8 +19,8 @@ function BpmnTraverser(visitor) {
function visit(element, di, ctx) {
// call visitor
var gfx = visitor(element, di, ctx);
// call handler
var gfx = handler.element(element, di, ctx);
// and log returned result
elementGfxMap[element.id] = gfx;
@ -36,6 +36,9 @@ function BpmnTraverser(visitor) {
}
}
function logError(message, context) {
handler.error(message, context);
}
////// DI handling ////////////////////////////
@ -45,7 +48,11 @@ function BpmnTraverser(visitor) {
function registerDi(element) {
var bpmnElement = element.bpmnElement;
elementDiMap[bpmnElement.id] = element;
if (bpmnElement) {
elementDiMap[bpmnElement.id] = element;
} else {
logError('no bpmnElement for <' + element.$type + '#' + element.id + '>', { element: element });
}
}
function getDi(bpmnElement) {

View File

@ -8,29 +8,36 @@ function importBpmnDiagram(diagram, definitions, done) {
var shapes = {};
var visitor = function(element, di, parent) {
var visitor = {
var shape;
element: function(element, di, parent) {
if (di.$type === 'bpmndi:BPMNShape') {
var bounds = di.bounds;
var shape;
shape = { id: element.id, type: element.$type, x: bounds.x, y: bounds.y, width: bounds.width, height: bounds.height, parent: parent };
if (di.$type === 'bpmndi:BPMNShape') {
var bounds = di.bounds;
canvas.addShape(shape);
} else {
shape = { id: element.id, type: element.$type, x: bounds.x, y: bounds.y, width: bounds.width, height: bounds.height, parent: parent };
var waypoints = _.collect(di.waypoint, function(p) {
return { x: p.x, y: p.y };
});
canvas.addShape(shape);
} else {
shape = { id: element.id, type: element.$type, waypoints: waypoints };
canvas.addConnection(shape);
var waypoints = _.collect(di.waypoint, function(p) {
return { x: p.x, y: p.y };
});
shape = { id: element.id, type: element.$type, waypoints: waypoints };
canvas.addConnection(shape);
}
shapes[element.id] = shape;
return shape;
},
error: function(message, context) {
console.warn('[import]', message, context);
}
shapes[element.id] = shape;
return shape;
};
var walker = new BpmnTreeWalker(visitor);

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<bpmn2:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd" id="test" targetNamespace="http://bpmn.io/schema/bpmn">
<bpmn2:process id="Process_1" isExecutable="false">
<bpmn2:startEvent id="StartEvent_1"/>
</bpmn2:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1">
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_2">
<dc:Bounds height="36.0" width="36.0" x="412.0" y="240.0"/>
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn2:definitions>

View File

@ -22,44 +22,74 @@ describe('BpmnTreeWalker', function() {
}
/**
* A simple handler that records what a tree walker processes
*/
function RecordingHandler() {
var elements = [];
var errors = [];
function handleElement(element, di, ctx) {
var id = element.id;
elements.push({ id: id, parent: ctx });
return id;
}
function handleError(message, context) {
errors.push({ message: message, context: context });
}
this.element = handleElement;
this.error = handleError;
this._errors = errors;
this._elements = elements;
}
function readBpmn(file, fn, done) {
readFile(file, 'bpmn:Definitions', function(err, definitions) {
if (err) {
return done(err);
}
fn(definitions);
done();
});
}
/**
* Expect a certain render order when parsing the given file contents.
*
* Call done when done.
*/
function expectWalkOrder(file, expectedOrder, done) {
readBpmn(file, function(definitions) {
var handler = new RecordingHandler();
new BpmnTreeWalker(handler).handleDefinitions(definitions);
expect(handler._elements).toDeepEqual(expectedOrder);
expect(handler._errors.length).toBe(0);
}, done);
}
beforeEach(Matchers.add);
var bpmnModel = BpmnModel.instance();
describe('should walk', function() {
/**
* Expect a certain render order when parsing the given file contents.
*
* Call done when done.
*/
function expectWalkOrder(file, expectedOrder, done) {
readFile(file, 'bpmn:Definitions', function(err, definitions) {
if (err) {
done(err);
return;
}
var actualOrder = [];
var visitor = function(element, di, ctx) {
var id = element.id;
actualOrder.push({ id: id, parent: ctx });
return id;
};
new BpmnTreeWalker(visitor).handleDefinitions(definitions);
expect(actualOrder).toDeepEqual(expectedOrder);
done();
});
}
it('process / sub process', function(done) {
// given
@ -195,4 +225,30 @@ describe('BpmnTreeWalker', function() {
});
});
describe('should handle errors', function() {
it('missing bpmnElement on di', function(done) {
// given
readBpmn('error/di-no-bpmn-element.bpmn', function(definitions) {
// when
var handler = new RecordingHandler();
new BpmnTreeWalker(handler).handleDefinitions(definitions);
expect(handler._elements).toDeepEqual([]);
expect(handler._errors.length).toBe(1);
var error = handler._errors[0];
expect(error.message).toBe('no bpmnElement for <bpmndi:BPMNShape#_BPMNShape_StartEvent_2>');
}, done);
});
});
});