56c1d0e6461096d2569b4fd606794888a6982697
[ccsdk/cds.git] /
1 import {Component, OnInit} from '@angular/core';
2 import {InputActionAttribute, OutputActionAttribute} from './models/InputActionAttribute';
3 import {DesignerStore} from '../designer.store';
4 import {DesignerDashboardState} from '../model/designer.dashboard.state';
5 import {Action} from './models/Action';
6 import {FunctionsStore} from '../functions.store';
7 import {FunctionsState} from '../model/functions.state';
8
9 @Component({
10     selector: 'app-action-attributes',
11     templateUrl: './action-attributes.component.html',
12     styleUrls: ['./action-attributes.component.css']
13 })
14 export class ActionAttributesComponent implements OnInit {
15
16     inputs = [];
17     outputs = [];
18     actionAttributesSideBar: boolean;
19     inputActionAttribute = new InputActionAttribute();
20     outputActionAttribute = new OutputActionAttribute();
21     isInputOtherType: boolean;
22     isOutputOtherType: boolean;
23     outputOtherType = '';
24     inputOtherType = '';
25     actionName = '';
26     designerState: DesignerDashboardState;
27     isFunctionAttributeActive = false;
28     functions: FunctionsState;
29     steps: string[];
30     suggestedInputs: string[] = [];
31     suggestedOutputs: string[] = [];
32
33     tempInputs: string[] = [];
34     tempOutputs: string[] = [];
35     currentInterfaceName: string;
36     functionAndAttributesInput: Map<string, string[]> = new Map<string, string[]>();
37     private currentTargetFunctionName: any;
38     private functionAndAttributesOutput: Map<string, string[]> = new Map<string, string[]>();
39
40     constructor(private designerStore: DesignerStore, private functionsStore: FunctionsStore) {
41
42     }
43
44     ngOnInit() {
45         this.designerStore.state$.subscribe(designerState => {
46             this.designerState = designerState;
47             if (this.designerState && this.designerState.actionName) {
48                 this.actionName = this.designerState.actionName;
49                 const action = this.designerState.template.workflows[this.actionName] as Action;
50                 if (action.steps) {
51                     const steps = Object.keys(action.steps);
52                     if (steps && steps.length > 0) {
53                         this.isFunctionAttributeActive = true;
54                     } else {
55                         this.isFunctionAttributeActive = false;
56                     }
57                     this.steps = steps;
58                     this.suggestedOutputs = [];
59                     this.suggestedInputs = [];
60                     /*steps.forEach(step => {
61                         const target = action.steps[step].target;
62                         this.getInputs(target);
63                     });*/
64                 }
65
66
67                 this.inputs = [];
68                 if (action.inputs) {
69                     const namesOfInput = Object.keys(action.inputs);
70                     this.inputs = this.extractFields(namesOfInput, action.inputs);
71                 }
72                 this.outputs = [];
73                 if (action.outputs) {
74                     const namesOfOutput = Object.keys(action.outputs);
75                     this.outputs = this.extractFields(namesOfOutput, action.outputs);
76                 }
77             }
78         });
79
80         this.functionsStore.state$.subscribe(functions => {
81             this.functions = functions;
82         });
83     }
84
85
86     private extractFields(namesOfOutput: string[], container: {}) {
87         const fields = [];
88         for (const nameOutput of namesOfOutput) {
89             const fieldAttribute = new OutputActionAttribute();
90             fieldAttribute.name = nameOutput;
91             fieldAttribute.description = container[nameOutput].description;
92             fieldAttribute.required = container[nameOutput].required;
93             fieldAttribute.type = container[nameOutput].type;
94             const insertedOutputActionAttribute = Object.assign({}, fieldAttribute);
95             fields.push(insertedOutputActionAttribute);
96         }
97         return fields;
98     }
99
100     addInput(input: InputActionAttribute) {
101         if (input && input.type && input.name) {
102             const insertedInputActionAttribute = Object.assign({}, input);
103             this.inputs.push(insertedInputActionAttribute);
104         }
105     }
106
107     addOutput(output: OutputActionAttribute) {
108         console.log(output);
109         if (output && output.type && output.name) {
110             const insertedOutputActionAttribute = Object.assign({}, output);
111             this.outputs.push(insertedOutputActionAttribute);
112         }
113     }
114
115     setInputType(type: string) {
116         this.inputActionAttribute.type = type;
117         this.isInputOtherType = this.checkIfTypeIsOther(type);
118     }
119
120     setInputRequired(isRequired) {
121         this.inputActionAttribute.required = isRequired;
122     }
123
124     setOutputRequired(isRequired) {
125         this.outputActionAttribute.required = isRequired;
126     }
127
128     setOutputType(type: string) {
129         this.outputActionAttribute.type = type;
130         this.isOutputOtherType = this.checkIfTypeIsOther(type);
131     }
132
133     checkIfTypeIsOther(type) {
134         return type.includes('Other');
135     }
136
137     submitAttributes() {
138         this.addInput(this.inputActionAttribute);
139         this.addOutput(this.outputActionAttribute);
140         this.clearFormInputs();
141         this.designerStore.setInputsAndOutputsToSpecificWorkflow(this.storeInputs(this.inputs)
142             , this.storeOutputs(this.outputs), this.actionName);
143     }
144
145     private clearFormInputs() {
146         this.inputActionAttribute = new InputActionAttribute();
147         this.outputActionAttribute = new OutputActionAttribute();
148         this.outputOtherType = '';
149         this.inputOtherType = '';
150     }
151
152     private storeInputs(InputActionAttributes: InputActionAttribute[]) {
153
154         let inputs = '';
155         InputActionAttributes.forEach(input => {
156             inputs += this.appendAttributes(input);
157
158         });
159         if (inputs.endsWith(',')) {
160             inputs = inputs.substr(0, inputs.length - 1);
161         }
162         return this.convertToObject('{' + inputs + '}');
163     }
164
165     private storeOutputs(OutputActionAttributes: OutputActionAttribute[]) {
166         let outputs = '';
167         OutputActionAttributes.forEach(output => {
168             outputs += this.appendAttributes(output);
169         });
170         if (outputs.endsWith(',')) {
171             outputs = outputs.substr(0, outputs.length - 1);
172         }
173         return this.convertToObject('{' + outputs + '}');
174     }
175
176     private appendAttributes(output: OutputActionAttribute) {
177         return '"' + output.name + '" : {\n' +
178             '            "required" : ' + output.required + ',\n' +
179             '            "type" : "' + output.type + '",\n' +
180             '            "description" : "' + output.description + '"\n' +
181             '          },';
182     }
183
184     setInputAndOutputs(targetName) {
185         console.log(targetName);
186         const nodeTemplate = this.designerState.template.node_templates[targetName];
187         console.log(this.designerState.template.node_templates);
188         console.log(nodeTemplate);
189         /* tslint:disable:no-string-literal */
190         console.log(nodeTemplate['type']);
191         this.functions.serverFunctions
192             /* tslint:disable:no-string-literal */
193             .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
194             .forEach(currentFunction => {
195                 console.log(currentFunction);
196                 /* tslint:disable:no-string-literal */
197                 if (currentFunction['definition'] && currentFunction['definition']['interfaces']) {
198                     const interfaces = Object.keys(currentFunction['definition']['interfaces']);
199                     if (interfaces && interfaces.length > 0) {
200                         const interfaceName = interfaces[0];
201                         console.log(interfaceName);
202                         this.currentInterfaceName = interfaceName;
203
204                         if (!this.functionAndAttributesInput.has(targetName)) {
205                             this.currentTargetFunctionName = targetName;
206                             this.functionAndAttributesInput.set(targetName, []);
207                         }
208
209                         if (!this.functionAndAttributesOutput.has(targetName)) {
210                             this.currentTargetFunctionName = targetName;
211                             this.functionAndAttributesOutput.set(targetName, []);
212                         }
213
214                         if (nodeTemplate['interfaces'] &&
215                             nodeTemplate['interfaces'][interfaceName]['operations'] &&
216                             nodeTemplate['interfaces'][interfaceName]['operations']['process']
217                         ) {
218                             console.log('here');
219                             if (nodeTemplate['interfaces'][interfaceName]['operations']['process']['inputs']) {
220                                 /* tslint:disable:no-string-literal */
221                                 this.suggestedInputs = Object.keys(nodeTemplate['interfaces']
222                                     [interfaceName]['operations']['process']['inputs']);
223                             }
224                             if (nodeTemplate['interfaces'][interfaceName]['operations']['process']['outputs']) {
225                                 /* tslint:disable:no-string-literal */
226                                 this.suggestedOutputs = Object.keys(nodeTemplate['interfaces']
227                                     [interfaceName]['operations']['process']['outputs']);
228                                 console.log(this.suggestedInputs);
229                             }
230
231                         }
232                     }
233                 }
234             });
235         console.log(nodeTemplate);
236     }
237
238     printSomethings() {
239         console.log('something');
240     }
241
242     addTempInput(suggestedInput: string) {
243         this.addAttribute(this.tempInputs, suggestedInput);
244         this.deleteAttribute(this.suggestedInputs, suggestedInput);
245         this.addAttribute(this.functionAndAttributesInput.get(this.currentTargetFunctionName), suggestedInput);
246     }
247
248     addTempOutput(suggestedOutput: string) {
249         this.addAttribute(this.tempOutputs, suggestedOutput);
250         this.deleteAttribute(this.suggestedOutputs, suggestedOutput);
251         this.addAttribute(this.functionAndAttributesOutput.get(this.currentTargetFunctionName), suggestedOutput);
252     }
253
254     deleteAttribute(container: string[], suggestedAttribute: string) {
255         if (container && suggestedAttribute && container.includes(suggestedAttribute)) {
256             const index: number = container.indexOf(suggestedAttribute);
257             if (index !== -1) {
258                 container.splice(index, 1);
259             }
260         }
261     }
262
263     addAttribute(container: string[], suggestedAttribute: string) {
264         if (container && suggestedAttribute && !container.includes(suggestedAttribute)) {
265             container.push(suggestedAttribute);
266         }
267     }
268
269
270     submitTempAttributes() {
271         this.writeSelectedAttributeInputs();
272         this.writeSelectedAttributeOutputs();
273     }
274
275     private writeSelectedAttributeOutputs() {
276         this.functionAndAttributesOutput.forEach((key, value) => {
277             const nodeTemplate = this.getNodeTemplate(value);
278             this.functions.serverFunctions
279                 /* tslint:disable:no-string-literal */
280                 .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
281                 .forEach(currentFunction => {
282                     if (currentFunction['definition'] && currentFunction['definition']['interfaces']
283                         [Object.keys(currentFunction['definition'] && currentFunction['definition']['interfaces'])]
284                         ['operations']['process']['outputs']) {
285                         let newOutputs = '';
286                         const outputs = currentFunction['definition'] && currentFunction['definition']['interfaces']
287                             [Object.keys(currentFunction['definition'] && currentFunction['definition']['interfaces'])]
288                             ['operations']['process']['outputs'];
289                         key.forEach(attribute => {
290                             newOutputs += '"' + attribute + '": ' + this.convertToString(outputs[attribute]) + ',';
291                         });
292                         if (key.length > 0) {
293                             newOutputs = this.removeTheLastComma(newOutputs);
294                             const originalOutputs = this.convertToString(this.designerState.template.workflows[this.actionName]['outputs']);
295                             console.log(originalOutputs.substr(0, originalOutputs.length - 1) + ',' + newOutputs + '}');
296                             this.designerState.template.workflows[this.actionName]['outputs'] =
297                                 this.convertToObject(originalOutputs.substr(0, originalOutputs.length - 1) + ',' + newOutputs + '}');
298                         }
299                     }
300                 });
301         });
302     }
303
304
305     private writeSelectedAttributeInputs() {
306         this.functionAndAttributesInput.forEach((key, value) => {
307             const nodeTemplate = this.getNodeTemplate(value);
308             this.functions.serverFunctions
309                 /* tslint:disable:no-string-literal */
310                 .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
311                 .forEach(currentFunction => {
312                     if (currentFunction['definition'] && currentFunction['definition']['interfaces']
313                         [Object.keys(currentFunction['definition'] && currentFunction['definition']['interfaces'])]
314                         ['operations']['process']['inputs']) {
315                         let newInputs = '';
316                         const inputs = currentFunction['definition'] && currentFunction['definition']['interfaces']
317                             [Object.keys(currentFunction['definition'] && currentFunction['definition']['interfaces'])]
318                             ['operations']['process']['inputs'];
319                         key.forEach(attribute => {
320                             newInputs += '"' + attribute + '": ' + this.convertToString(inputs[attribute]) + ',';
321                         });
322                         if (key.length > 0) {
323                             newInputs = this.removeTheLastComma(newInputs);
324                             const originalInputs = this.convertToString(this.designerState.template.workflows[this.actionName]['inputs']);
325                             console.log(originalInputs.substr(0, originalInputs.length - 1) + ',' + newInputs + '}');
326                             this.designerState.template.workflows[this.actionName]['inputs'] =
327                                 this.convertToObject(originalInputs.substr(0, originalInputs.length - 1) + ',' + newInputs + '}');
328                         }
329                     }
330
331
332                 });
333         });
334     }
335
336     private removeTheLastComma = (newInputs: string) => {
337         if (newInputs.endsWith(',')) {
338             newInputs = newInputs.substr(0, newInputs.length - 1);
339         }
340         return newInputs;
341     }
342
343     private convertToString = object => JSON.stringify(object);
344
345     private convertToObject = stringValue => JSON.parse(stringValue);
346
347     private getNodeTemplate = (value: string) => this.designerState.template.node_templates[value];
348
349 }