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