shikeying
2023-03-17 8c1a723d62a6aa5d6266ca613ae4eb77c789db06
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
'use strict';
 
var assign = require('lodash/assign');
 
var entryFactory = require('../../../../factory/EntryFactory'),
    getBusinessObject = require('bpmn-js/lib/util/ModelUtil').getBusinessObject,
    getTemplate = require('../Helper').getTemplate,
    cmdHelper = require('../../../../helper/CmdHelper'),
    elementHelper = require('../../../../helper/ElementHelper');
 
var findExtension = require('../Helper').findExtension,
    findExtensions = require('../Helper').findExtensions,
    findInputParameter = require('../Helper').findInputParameter,
    findOutputParameter = require('../Helper').findOutputParameter,
    findActivitiProperty = require('../Helper').findActivitiProperty,
    findActivitiInOut = require('../Helper').findActivitiInOut;
 
var createActivitiProperty = require('../CreateHelper').createActivitiProperty,
    createInputParameter = require('../CreateHelper').createInputParameter,
    createOutputParameter = require('../CreateHelper').createOutputParameter,
    createActivitiIn = require('../CreateHelper').createActivitiIn,
    createActivitiOut = require('../CreateHelper').createActivitiOut,
    createActivitiInWithBusinessKey = require('../CreateHelper').createActivitiInWithBusinessKey,
    createActivitiFieldInjection = require('../CreateHelper').createActivitiFieldInjection;
 
var ACTIVITI_PROPERTY_TYPE = 'activiti:property',
    ACTIVITI_INPUT_PARAMETER_TYPE = 'activiti:inputParameter',
    ACTIVITI_OUTPUT_PARAMETER_TYPE = 'activiti:outputParameter',
    ACTIVITI_IN_TYPE = 'activiti:in',
    ACTIVITI_OUT_TYPE = 'activiti:out',
    ACTIVITI_IN_BUSINESS_KEY_TYPE = 'activiti:in:businessKey',
    ACTIVITI_EXECUTION_LISTENER_TYPE = 'activiti:executionListener',
    ACTIVITI_FIELD = 'activiti:field';
 
var BASIC_MODDLE_TYPES = [
  'Boolean',
  'Integer',
  'String'
];
 
var EXTENSION_BINDING_TYPES = [
  ACTIVITI_PROPERTY_TYPE,
  ACTIVITI_INPUT_PARAMETER_TYPE,
  ACTIVITI_OUTPUT_PARAMETER_TYPE,
  ACTIVITI_IN_TYPE,
  ACTIVITI_OUT_TYPE,
  ACTIVITI_IN_BUSINESS_KEY_TYPE,
  ACTIVITI_FIELD
];
 
var IO_BINDING_TYPES = [
  ACTIVITI_INPUT_PARAMETER_TYPE,
  ACTIVITI_OUTPUT_PARAMETER_TYPE
];
 
var IN_OUT_BINDING_TYPES = [
  ACTIVITI_IN_TYPE,
  ACTIVITI_OUT_TYPE,
  ACTIVITI_IN_BUSINESS_KEY_TYPE
];
 
/**
 * Injects custom properties into the given group.
 *
 * @param {djs.model.Base} element
 * @param {ElementTemplates} elementTemplates
 * @param {BpmnFactory} bpmnFactory
 * @param {Function} translate
 */
module.exports = function(element, elementTemplates, bpmnFactory, translate) {
 
  var template = getTemplate(element, elementTemplates);
 
  if (!template) {
    return [];
  }
 
  var renderCustomField = function(id, p, idx) {
    var propertyType = p.type;
 
    var entryOptions = {
      id: id,
      description: p.description,
      label: p.label ? translate(p.label) : p.label,
      modelProperty: id,
      get: propertyGetter(id, p),
      set: propertySetter(id, p, bpmnFactory),
      validate: propertyValidator(id, p, translate)
    };
 
    var entry;
 
    if (propertyType === 'Boolean') {
      entry = entryFactory.checkbox(entryOptions);
    }
 
    if (propertyType === 'String') {
      entry = entryFactory.textField(entryOptions);
    }
 
    if (propertyType === 'Text') {
      entry = entryFactory.textBox(entryOptions);
    }
 
    if (propertyType === 'Dropdown') {
      entryOptions.selectOptions = p.choices;
 
      entry = entryFactory.selectBox(entryOptions);
    }
 
    return entry;
  };
 
  var groups = [];
  var id, entry;
 
  var customFieldsGroup = {
    id: 'customField',
    label: translate('Custom Fields'),
    entries: []
  };
  template.properties.forEach(function(p, idx) {
 
    id = 'custom-' + template.id + '-' + idx;
 
    entry = renderCustomField(id, p, idx);
    if (entry) {
      customFieldsGroup.entries.push(entry);
    }
  });
  if (customFieldsGroup.entries.length > 0) {
    groups.push(customFieldsGroup);
  }
 
  if (template.scopes) {
    for (var scopeName in template.scopes) {
 
      var scope = template.scopes[scopeName];
      var idScopeName = scopeName.replace(/:/g, '_');
 
      var customScopeFieldsGroup = {
        id: 'customField-' + idScopeName,
        label: translate('Custom Fields for scope: ') + scopeName,
        entries: []
      };
 
      scope.properties.forEach(function(p, idx) {
 
        var propertyId = 'custom-' + template.id + '-' + idScopeName + '-' + idx;
 
        var scopedProperty = propertyWithScope(p, scopeName);
 
        entry = renderCustomField(propertyId, scopedProperty, idx);
        if (entry) {
          customScopeFieldsGroup.entries.push(entry);
        }
      });
 
      if (customScopeFieldsGroup.entries.length > 0) {
        groups.push(customScopeFieldsGroup);
      }
    }
  }
 
  return groups;
};
 
 
// getters, setters and validators ///////////////
 
 
/**
 * Return a getter that retrieves the given property.
 *
 * @param {String} name
 * @param {PropertyDescriptor} property
 *
 * @return {Function}
 */
function propertyGetter(name, property) {
 
  /* getter */
  return function get(element) {
    var value = getPropertyValue(element, property);
 
    return objectWithKey(name, value);
  };
}
 
/**
 * Return a setter that updates the given property.
 *
 * @param {String} name
 * @param {PropertyDescriptor} property
 * @param {BpmnFactory} bpmnFactory
 *
 * @return {Function}
 */
function propertySetter(name, property, bpmnFactory) {
 
  /* setter */
  return function set(element, values) {
 
    var value = values[name];
 
    return setPropertyValue(element, property, value, bpmnFactory);
  };
}
 
/**
 * Return a validator that ensures the property is ok.
 *
 * @param {String} name
 * @param {PropertyDescriptor} property
 * @param {Function} translate
 *
 * @return {Function}
 */
function propertyValidator(name, property, translate) {
 
  /* validator */
  return function validate(element, values) {
    var value = values[name];
 
    var error = validateValue(value, property, translate);
 
    if (error) {
      return objectWithKey(name, error);
    }
  };
}
 
 
// get, set and validate helpers ///////////////////
 
/**
 * Return the value of the specified property descriptor,
 * on the passed diagram element.
 *
 * @param {djs.model.Base} element
 * @param {PropertyDescriptor} property
 *
 * @return {Any}
 */
function getPropertyValue(element, property) {
 
  var bo = getBusinessObject(element);
 
  var binding = property.binding,
      scope = property.scope;
 
  var bindingType = binding.type,
      bindingName = binding.name;
 
  var propertyValue = property.value || '';
 
  if (scope) {
    bo = findExtension(bo, scope.name);
    if (!bo) {
      return propertyValue;
    }
  }
 
  // property
  if (bindingType === 'property') {
 
    var value = bo.get(bindingName);
 
    if (bindingName === 'conditionExpression') {
      if (value) {
        return value.body;
      } else {
        // return defined default
        return propertyValue;
      }
    } else {
      // return value; default to defined default
      return typeof value !== 'undefined' ? value : propertyValue;
    }
  }
 
  var activitiProperties,
      activitiProperty;
 
  if (bindingType === ACTIVITI_PROPERTY_TYPE) {
    if (scope) {
      activitiProperties = bo.get('properties');
    } else {
      activitiProperties = findExtension(bo, 'activiti:Properties');
    }
 
    if (activitiProperties) {
      activitiProperty = findActivitiProperty(activitiProperties, binding);
 
      if (activitiProperty) {
        return activitiProperty.value;
      }
    }
 
    return propertyValue;
  }
 
  var inputOutput,
      ioParameter;
 
  if (IO_BINDING_TYPES.indexOf(bindingType) !== -1) {
 
    if (scope) {
      inputOutput = bo.get('inputOutput');
    } else {
      inputOutput = findExtension(bo, 'activiti:InputOutput');
    }
 
    if (!inputOutput) {
      // ioParameter cannot exist yet, return property value
      return propertyValue;
    }
  }
 
  // activiti input parameter
  if (bindingType === ACTIVITI_INPUT_PARAMETER_TYPE) {
    ioParameter = findInputParameter(inputOutput, binding);
 
    if (ioParameter) {
      if (binding.scriptFormat) {
        if (ioParameter.definition) {
          return ioParameter.definition.value;
        }
      } else {
        return ioParameter.value || '';
      }
    }
 
    return propertyValue;
  }
 
  // activiti output parameter
  if (binding.type === ACTIVITI_OUTPUT_PARAMETER_TYPE) {
    ioParameter = findOutputParameter(inputOutput, binding);
 
    if (ioParameter) {
      return ioParameter.name;
    }
 
    return propertyValue;
  }
 
 
  var ioElement;
 
  if (IN_OUT_BINDING_TYPES.indexOf(bindingType) != -1) {
    ioElement = findActivitiInOut(bo, binding);
 
    if (ioElement) {
      if (bindingType === ACTIVITI_IN_BUSINESS_KEY_TYPE) {
        return ioElement.businessKey;
      } else
      if (bindingType === ACTIVITI_OUT_TYPE) {
        return ioElement.target;
      } else
      if (bindingType === ACTIVITI_IN_TYPE) {
        return ioElement[binding.expression ? 'sourceExpression' : 'source'];
      }
    }
 
    return propertyValue;
  }
 
  if (bindingType === ACTIVITI_EXECUTION_LISTENER_TYPE) {
    var executionListener;
    if (scope) {
      executionListener = bo.get('executionListener');
    } else {
      executionListener = findExtension(bo, 'activiti:ExecutionListener');
    }
 
    return executionListener.script.value;
  }
 
  var fieldInjection;
  if (ACTIVITI_FIELD === bindingType) {
    var fieldInjections = findExtensions(bo, [ 'activiti:Field' ]);
    fieldInjections.forEach(function(item) {
      if (item.name === binding.name) {
        fieldInjection = item;
      }
    });
    if (fieldInjection) {
      return fieldInjection.string || fieldInjection.expression;
    } else {
      return '';
    }
  }
 
  throw unknownPropertyBinding(property);
}
 
module.exports.getPropertyValue = getPropertyValue;
 
 
/**
 * Return an update operation that changes the diagram
 * element's custom property to the given value.
 *
 * The response of this method will be processed via
 * {@link PropertiesPanel#applyChanges}.
 *
 * @param {djs.model.Base} element
 * @param {PropertyDescriptor} property
 * @param {String} value
 * @param {BpmnFactory} bpmnFactory
 *
 * @return {Object|Array<Object>} results to be processed
 */
function setPropertyValue(element, property, value, bpmnFactory) {
  var bo = getBusinessObject(element);
 
  var binding = property.binding,
      scope = property.scope;
 
  var bindingType = binding.type,
      bindingName = binding.name;
 
  var propertyValue;
 
  var updates = [];
 
  var extensionElements;
 
  if (EXTENSION_BINDING_TYPES.indexOf(bindingType) !== -1) {
    extensionElements = bo.get('extensionElements');
 
    // create extension elements, if they do not exist (yet)
    if (!extensionElements) {
      extensionElements = elementHelper.createElement('bpmn:ExtensionElements', null, element, bpmnFactory);
 
      updates.push(cmdHelper.updateBusinessObject(
        element, bo, objectWithKey('extensionElements', extensionElements)
      ));
    }
  }
 
  if (scope) {
    bo = findExtension(bo, scope.name);
    if (!bo) {
      bo = elementHelper.createElement(scope.name, null, element, bpmnFactory);
 
      updates.push(cmdHelper.addElementsTolist(
        bo, extensionElements, 'values', [ bo ]
      ));
    }
  }
 
  // property
  if (bindingType === 'property') {
 
    if (bindingName === 'conditionExpression') {
 
      propertyValue = elementHelper.createElement('bpmn:FormalExpression', {
        body: value,
        language: binding.scriptFormat
      }, bo, bpmnFactory);
    } else {
 
      var moddlePropertyDescriptor = bo.$descriptor.propertiesByName[bindingName];
 
      var moddleType = moddlePropertyDescriptor.type;
 
      // make sure we only update String, Integer, Real and
      // Boolean properties (do not accidentally override complex objects...)
      if (BASIC_MODDLE_TYPES.indexOf(moddleType) === -1) {
        throw new Error('cannot set moddle type <' + moddleType + '>');
      }
 
      if (moddleType === 'Boolean') {
        propertyValue = !!value;
      } else
      if (moddleType === 'Integer') {
        propertyValue = parseInt(value, 10);
 
        if (isNaN(propertyValue)) {
          // do not write NaN value
          propertyValue = undefined;
        }
      } else {
        propertyValue = value;
      }
    }
 
    if (propertyValue !== undefined) {
      updates.push(cmdHelper.updateBusinessObject(
        element, bo, objectWithKey(bindingName, propertyValue)
      ));
    }
  }
 
  // activiti:property
  var activitiProperties,
      existingActivitiProperty,
      newActivitiProperty;
 
  if (bindingType === ACTIVITI_PROPERTY_TYPE) {
 
    if (scope) {
      activitiProperties = bo.get('properties');
    } else {
      activitiProperties = findExtension(extensionElements, 'activiti:Properties');
    }
 
    if (!activitiProperties) {
      activitiProperties = elementHelper.createElement('activiti:Properties', null, bo, bpmnFactory);
 
      if (scope) {
        updates.push(cmdHelper.updateBusinessObject(
          element, bo, { properties: activitiProperties }
        ));
      }
      else {
        updates.push(cmdHelper.addElementsTolist(
          element, extensionElements, 'values', [ activitiProperties ]
        ));
      }
    }
 
    existingActivitiProperty = findActivitiProperty(activitiProperties, binding);
 
    newActivitiProperty = createActivitiProperty(binding, value, bpmnFactory);
 
    updates.push(cmdHelper.addAndRemoveElementsFromList(
      element,
      activitiProperties,
      'values',
      null,
      [ newActivitiProperty ],
      existingActivitiProperty ? [ existingActivitiProperty ] : []
    ));
  }
 
  // activiti:inputParameter
  // activiti:outputParameter
  var inputOutput,
      existingIoParameter,
      newIoParameter;
 
  if (IO_BINDING_TYPES.indexOf(bindingType) !== -1) {
 
    if (scope) {
      inputOutput = bo.get('inputOutput');
    } else {
      inputOutput = findExtension(extensionElements, 'activiti:InputOutput');
    }
 
    // create inputOutput element, if it do not exist (yet)
    if (!inputOutput) {
      inputOutput = elementHelper.createElement('activiti:InputOutput', null, bo, bpmnFactory);
 
      if (scope) {
        updates.push(cmdHelper.updateBusinessObject(
          element, bo, { inputOutput: inputOutput }
        ));
      }
      else {
        updates.push(cmdHelper.addElementsTolist(
          element, extensionElements, 'values', inputOutput
        ));
      }
    }
  }
 
  if (bindingType === ACTIVITI_INPUT_PARAMETER_TYPE) {
 
    existingIoParameter = findInputParameter(inputOutput, binding);
 
    newIoParameter = createInputParameter(binding, value, bpmnFactory);
 
    updates.push(cmdHelper.addAndRemoveElementsFromList(
      element,
      inputOutput,
      'inputParameters',
      null,
      [ newIoParameter ],
      existingIoParameter ? [ existingIoParameter ] : []
    ));
  }
 
  if (bindingType === ACTIVITI_OUTPUT_PARAMETER_TYPE) {
 
    existingIoParameter = findOutputParameter(inputOutput, binding);
 
    newIoParameter = createOutputParameter(binding, value, bpmnFactory);
 
    updates.push(cmdHelper.addAndRemoveElementsFromList(
      element,
      inputOutput,
      'outputParameters',
      null,
      [ newIoParameter ],
      existingIoParameter ? [ existingIoParameter ] : []
    ));
  }
 
 
  // activiti:in
  // activiti:out
  // activiti:in:businessKey
  var existingInOut,
      newInOut;
 
  if (IN_OUT_BINDING_TYPES.indexOf(bindingType) !== -1) {
 
    existingInOut = findActivitiInOut(bo, binding);
 
    if (bindingType === ACTIVITI_IN_TYPE) {
      newInOut = createActivitiIn(binding, value, bpmnFactory);
    } else
    if (bindingType === ACTIVITI_OUT_TYPE) {
      newInOut = createActivitiOut(binding, value, bpmnFactory);
    } else {
      newInOut = createActivitiInWithBusinessKey(binding, value, bpmnFactory);
    }
 
    updates.push(cmdHelper.addAndRemoveElementsFromList(
      element,
      extensionElements,
      'values',
      null,
      [ newInOut ],
      existingInOut ? [ existingInOut ] : []
    ));
  }
 
  if (bindingType === ACTIVITI_FIELD) {
    var existingFieldInjections = findExtensions(bo, [ 'activiti:Field' ]);
    var newFieldInjections = [];
 
    if (existingFieldInjections.length > 0) {
      existingFieldInjections.forEach(function(item) {
        if (item.name === binding.name) {
          newFieldInjections.push(createActivitiFieldInjection(binding, value, bpmnFactory));
        } else {
          newFieldInjections.push(item);
        }
      });
    } else {
      newFieldInjections.push(createActivitiFieldInjection(binding, value, bpmnFactory));
    }
 
    updates.push(cmdHelper.addAndRemoveElementsFromList(
      element,
      extensionElements,
      'values',
      null,
      newFieldInjections,
      existingFieldInjections ? existingFieldInjections : []
    ));
  }
 
  if (updates.length) {
    return updates;
  }
 
  // quick warning for better debugging
  console.warn('no update', element, property, value);
}
 
module.exports.setPropertyValue = setPropertyValue;
 
/**
 * Validate value of a given property.
 *
 * @param {String} value
 * @param {PropertyDescriptor} property
 * @param {Function} translate
 *
 * @return {Object} with validation errors
 */
function validateValue(value, property, translate) {
 
  var constraints = property.constraints || {};
 
  if (constraints.notEmpty && isEmpty(value)) {
    return translate('Must not be empty');
  }
 
  if (constraints.maxLength && value.length > constraints.maxLength) {
    return translate('Must have max length {length}', { length: constraints.maxLength });
  }
 
  if (constraints.minLength && value.length < constraints.minLength) {
    return translate('Must have min length {length}', { length: constraints.minLength });
  }
 
  var pattern = constraints.pattern,
      message;
 
  if (pattern) {
 
    if (typeof pattern !== 'string') {
      message = pattern.message;
      pattern = pattern.value;
    }
 
    if (!matchesPattern(value, pattern)) {
      return message || translate('Must match pattern {pattern}', { pattern: pattern });
    }
  }
}
 
 
// misc helpers ///////////////////////////////
 
function propertyWithScope(property, scopeName) {
  if (!scopeName) {
    return property;
  }
 
  return assign({}, property, {
    scope: {
      name: scopeName
    }
  });
}
 
/**
 * Return an object with a single key -> value association.
 *
 * @param {String} key
 * @param {Any} value
 *
 * @return {Object}
 */
function objectWithKey(key, value) {
  var obj = {};
 
  obj[key] = value;
 
  return obj;
}
 
/**
 * Does the given string match the specified pattern?
 *
 * @param {String} str
 * @param {String} pattern
 *
 * @return {Boolean}
 */
function matchesPattern(str, pattern) {
  var regexp = new RegExp(pattern);
 
  return regexp.test(str);
}
 
function isEmpty(str) {
  return !str || /^\s*$/.test(str);
}
 
/**
 * Create a new {@link Error} indicating an unknown
 * property binding.
 *
 * @param {PropertyDescriptor} property
 *
 * @return {Error}
 */
function unknownPropertyBinding(property) {
  var binding = property.binding;
 
  return new Error('unknown binding: <' + binding.type + '>');
}