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
'use strict';
 
var getBusinessObject = require('bpmn-js/lib/util/ModelUtil').getBusinessObject;
 
var is = require('bpmn-js/lib/util/ModelUtil').is,
    isAny = require('bpmn-js/lib/features/modeling/util/ModelingUtil').isAny;
 
var find = require('lodash/find');
 
 
var TEMPLATE_ATTR = 'activiti:modelerTemplate';
 
/**
 * The BPMN 2.0 extension attribute name under
 * which the element template is stored.
 *
 * @type {String}
 */
module.exports.TEMPLATE_ATTR = TEMPLATE_ATTR;
 
 
/**
 * Get template id for a given diagram element.
 *
 * @param {djs.model.Base} element
 *
 * @return {String}
 */
function getTemplateId(element) {
 
  var bo = getBusinessObject(element);
 
  if (bo) {
    return bo.get(TEMPLATE_ATTR);
  }
}
 
module.exports.getTemplateId = getTemplateId;
 
 
/**
 * Get template of a given element.
 *
 * @param {Element} element
 * @param {ElementTemplates} elementTemplates
 *
 * @return {TemplateDefinition}
 */
function getTemplate(element, elementTemplates) {
  var id = getTemplateId(element);
 
  return id && elementTemplates.get(id);
}
 
module.exports.getTemplate = getTemplate;
 
/**
 * Get default template for a given element.
 *
 * @param {Element} element
 * @param {ElementTemplates} elementTemplates
 *
 * @return {TemplateDefinition}
 */
function getDefaultTemplate(element, elementTemplates) {
 
  // return first default template, if any exists
  return (
    elementTemplates.getAll().filter(function(t) {
      return isAny(element, t.appliesTo) && t.isDefault;
    })
  )[0];
}
 
module.exports.getDefaultTemplate = getDefaultTemplate;
 
 
/**
 * Find extension with given type in
 * BPMN element, diagram element or ExtensionElement.
 *
 * @param {ModdleElement|djs.model.Base} element
 * @param {String} type
 *
 * @return {ModdleElement} the extension
 */
function findExtension(element, type) {
  var bo = getBusinessObject(element);
 
  var extensionElements;
 
  if (is(bo, 'bpmn:ExtensionElements')) {
    extensionElements = bo;
  } else {
    extensionElements = bo.extensionElements;
  }
 
  if (!extensionElements) {
    return null;
  }
 
  return find(extensionElements.get('values'), function(e) {
    return is(e, type);
  });
}
 
module.exports.findExtension = findExtension;
 
 
function findExtensions(element, types) {
  var extensionElements = getExtensionElements(element);
 
  if (!extensionElements) {
    return [];
  }
 
  return extensionElements.get('values').filter(function(e) {
    return isAny(e, types);
  });
}
 
module.exports.findExtensions = findExtensions;
 
 
function findActivitiInOut(element, binding) {
 
  var extensionElements = getExtensionElements(element);
 
  if (!extensionElements) {
    return;
  }
 
  var matcher;
 
  if (binding.type === 'activiti:in') {
    matcher = function(e) {
      return is(e, 'activiti:In') && isInOut(e, binding);
    };
  } else
  if (binding.type === 'activiti:out') {
    matcher = function(e) {
      return is(e, 'activiti:Out') && isInOut(e, binding);
    };
  } else
  if (binding.type === 'activiti:in:businessKey') {
    matcher = function(e) {
      return is(e, 'activiti:In') && 'businessKey' in e;
    };
  }
 
  return find(extensionElements.get('values'), matcher);
}
 
module.exports.findActivitiInOut = findActivitiInOut;
 
function findActivitiProperty(activitiProperties, binding) {
  return find(activitiProperties.get('values'), function(p) {
    return p.name === binding.name;
  });
}
 
module.exports.findActivitiProperty = findActivitiProperty;
 
 
function findInputParameter(inputOutput, binding) {
  var parameters = inputOutput.get('inputParameters');
 
  return find(parameters, function(p) {
    return p.name === binding.name;
  });
}
 
module.exports.findInputParameter = findInputParameter;
 
 
function findOutputParameter(inputOutput, binding) {
  var parameters = inputOutput.get('outputParameters');
 
  return find(parameters, function(p) {
    var value = p.value;
 
    if (!binding.scriptFormat) {
      return value === binding.source;
    }
 
    var definition = p.definition;
 
    if (!definition || binding.scriptFormat !== definition.scriptFormat) {
      return false;
    }
 
    return definition.value === binding.source;
  });
}
 
module.exports.findOutputParameter = findOutputParameter;
 
 
 
// helpers /////////////////////////////////
 
function getExtensionElements(element) {
  var bo = getBusinessObject(element);
 
  if (is(bo, 'bpmn:ExtensionElements')) {
    return bo;
  } else {
    return bo.extensionElements;
  }
}
 
 
function isInOut(element, binding) {
 
  if (binding.type === 'activiti:in') {
    // find based on target attribute
    if (binding.target) {
      return element.target === binding.target;
    }
  }
 
  if (binding.type === 'activiti:out') {
    // find based on source / sourceExpression
    if (binding.source) {
      return element.source === binding.source;
    }
 
    if (binding.sourceExpression) {
      return element.sourceExpression === binding.sourceExpression;
    }
  }
 
  // find based variables / local combination
  if (binding.variables) {
    return element.variables === 'all' && (
      binding.variables !== 'local' || element.local
    );
  }
}