wf composition
[sdc/sdc-workflow-designer.git] / workflow-designer-ui / src / main / frontend / src / features / version / composition / custom-properties-provider / provider / camunda / parts / implementation / WorkflowImplementationType.js
1 var entryFactory = require('bpmn-js-properties-panel/lib/factory/EntryFactory'),
2     cmdHelper = require('bpmn-js-properties-panel/lib/helper/CmdHelper'),
3     extensionElementsHelper = require('bpmn-js-properties-panel/lib/helper/ExtensionElementsHelper'),
4     elementHelper = require('bpmn-js-properties-panel/lib/helper/ElementHelper');
5
6 var assign = require('lodash.assign');
7 var map = require('lodash.map');
8 import { implementationType } from './implementationConstants';
9
10 var DEFAULT_DELEGATE_PROPS = ['class', 'expression', 'delegateExpression'];
11
12 var DELEGATE_PROPS = {
13     'camunda:class': undefined,
14     'camunda:expression': undefined,
15     'camunda:delegateExpression': undefined,
16     'camunda:resultVariable': undefined
17 };
18
19 var DMN_CAPABLE_PROPS = {
20     'camunda:decisionRef': undefined,
21     'camunda:decisionRefBinding': 'latest',
22     'camunda:decisionRefVersion': undefined,
23     'camunda:mapDecisionResult': 'resultList',
24     'camunda:decisionRefTenantId': undefined
25 };
26
27 var EXTERNAL_CAPABLE_PROPS = {
28     'camunda:type': undefined,
29     'camunda:topic': undefined
30 };
31
32 const ACTIVITY_PROPS = {};
33
34 ACTIVITY_PROPS[implementationType] = undefined;
35
36 module.exports = function(element, bpmnFactory, options, translate) {
37     var DEFAULT_OPTIONS = [
38         { value: 'class', name: translate('Java Class') },
39         { value: 'expression', name: translate('Expression') },
40         { value: 'delegateExpression', name: translate('Delegate Expression') }
41     ];
42
43     var DMN_OPTION = [{ value: 'dmn', name: translate('DMN') }];
44
45     var EXTERNAL_OPTION = [{ value: 'external', name: translate('External') }];
46
47     var CONNECTOR_OPTION = [
48         { value: 'connector', name: translate('Connector') }
49     ];
50
51     var SCRIPT_OPTION = [{ value: 'script', name: translate('Script') }];
52
53     var ACTIVITY_OPTION = [
54         { value: 'workflowActivity', name: translate('Activity') }
55     ];
56
57     var getType = options.getImplementationType,
58         getBusinessObject = options.getBusinessObject;
59
60     var hasDmnSupport = options.hasDmnSupport,
61         hasExternalSupport = options.hasExternalSupport,
62         hasServiceTaskLikeSupport = options.hasServiceTaskLikeSupport,
63         hasScriptSupport = options.hasScriptSupport;
64
65     var entries = [];
66
67     var selectOptions = DEFAULT_OPTIONS.concat([]);
68
69     if (hasDmnSupport) {
70         selectOptions = selectOptions.concat(DMN_OPTION);
71     }
72
73     if (hasExternalSupport) {
74         selectOptions = selectOptions.concat(EXTERNAL_OPTION);
75     }
76
77     if (hasServiceTaskLikeSupport) {
78         selectOptions = selectOptions.concat(CONNECTOR_OPTION);
79     }
80
81     if (hasScriptSupport) {
82         selectOptions = selectOptions.concat(SCRIPT_OPTION);
83     }
84
85     selectOptions = selectOptions.concat(ACTIVITY_OPTION);
86
87     selectOptions.push({ value: '' });
88
89     entries.push(
90         entryFactory.selectBox({
91             id: 'implementation',
92             label: translate('Implementation'),
93             selectOptions: selectOptions,
94             modelProperty: 'implType',
95
96             get: function(element) {
97                 return {
98                     implType: getType(element) || ''
99                 };
100             },
101
102             set: function(element, values) {
103                 var bo = getBusinessObject(element);
104                 var oldType = getType(element);
105                 var newType = values.implType;
106
107                 var props = assign({}, DELEGATE_PROPS);
108
109                 if (DEFAULT_DELEGATE_PROPS.indexOf(newType) !== -1) {
110                     var newValue = '';
111                     if (DEFAULT_DELEGATE_PROPS.indexOf(oldType) !== -1) {
112                         newValue = bo.get('camunda:' + oldType);
113                     }
114
115                     props['camunda:' + newType] = newValue;
116                 }
117
118                 if (hasDmnSupport) {
119                     props = assign(props, DMN_CAPABLE_PROPS);
120                     if (newType === 'dmn') {
121                         props['camunda:decisionRef'] = '';
122                     }
123                 }
124
125                 if (hasExternalSupport) {
126                     props = assign(props, EXTERNAL_CAPABLE_PROPS);
127                     if (newType === 'external') {
128                         props['camunda:type'] = 'external';
129                         props['camunda:topic'] = '';
130                     }
131                 }
132
133                 if (hasScriptSupport) {
134                     props['camunda:script'] = undefined;
135
136                     if (newType === 'script') {
137                         props['camunda:script'] = elementHelper.createElement(
138                             'camunda:Script',
139                             {},
140                             bo,
141                             bpmnFactory
142                         );
143                     }
144                 }
145
146                 props = assign(props, ACTIVITY_PROPS);
147                 props[implementationType.ACTIVITY] = undefined;
148                 var commands = [];
149                 if (newType === 'workflowActivity') {
150                     props[implementationType.ACTIVITY] = '';
151                     props[implementationType.ACTIVITY_RESOURCE] = '';
152                 } else {
153                     var inputsOutputs = extensionElementsHelper.getExtensionElements(
154                         bo,
155                         'camunda:InputOutput'
156                     );
157                     commands.push(
158                         map(inputsOutputs, function(inputOutput) {
159                             return extensionElementsHelper.removeEntry(
160                                 bo,
161                                 element,
162                                 inputOutput
163                             );
164                         })
165                     );
166                 }
167
168                 commands.push(
169                     cmdHelper.updateBusinessObject(element, bo, props)
170                 );
171
172                 if (hasServiceTaskLikeSupport) {
173                     var connectors = extensionElementsHelper.getExtensionElements(
174                         bo,
175                         'camunda:Connector'
176                     );
177                     commands.push(
178                         map(connectors, function(connector) {
179                             return extensionElementsHelper.removeEntry(
180                                 bo,
181                                 element,
182                                 connector
183                             );
184                         })
185                     );
186
187                     if (newType === 'connector') {
188                         var extensionElements = bo.get('extensionElements');
189                         if (!extensionElements) {
190                             extensionElements = elementHelper.createElement(
191                                 'bpmn:ExtensionElements',
192                                 { values: [] },
193                                 bo,
194                                 bpmnFactory
195                             );
196                             commands.push(
197                                 cmdHelper.updateBusinessObject(element, bo, {
198                                     extensionElements: extensionElements
199                                 })
200                             );
201                         }
202                         var connector = elementHelper.createElement(
203                             'camunda:Connector',
204                             {},
205                             extensionElements,
206                             bpmnFactory
207                         );
208                         commands.push(
209                             cmdHelper.addAndRemoveElementsFromList(
210                                 element,
211                                 extensionElements,
212                                 'values',
213                                 'extensionElements',
214                                 [connector],
215                                 []
216                             )
217                         );
218                     }
219                 }
220
221                 return commands;
222             }
223         })
224     );
225
226     return entries;
227 };