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