fixing bugs import inputs and outputs from functions into actions
[ccsdk/cds.git] / cds-ui / designer-client / src / app / modules / feature-modules / packages / designer / action-attributes / action-attributes.component.ts
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 import {PackageCreationStore} from '../../package-creation/package-creation.store';
9 import {CBAPackage} from '../../package-creation/mapping-models/CBAPacakge.model';
10
11 @Component({
12     selector: 'app-action-attributes',
13     templateUrl: './action-attributes.component.html',
14     styleUrls: ['./action-attributes.component.css']
15 })
16 export class ActionAttributesComponent implements OnInit {
17
18     inputs = [];
19     outputs = [];
20     newInputs = [];
21     newOutputs = [];
22     actionAttributesSideBar: boolean;
23     inputActionAttribute = new InputActionAttribute();
24     outputActionAttribute = new OutputActionAttribute();
25     isInputOtherType: boolean;
26     isOutputOtherType: boolean;
27     outputOtherType = '';
28     inputOtherType = '';
29     actionName = '';
30     designerState: DesignerDashboardState;
31     isFunctionAttributeActive = false;
32     functions: FunctionsState;
33     steps: string[];
34     suggestedInputs: string[] = [];
35     suggestedOutputs: string[] = [];
36
37     tempInputs: string[] = [];
38     tempOutputs: string[] = [];
39     currentInterfaceName: string;
40     functionAndAttributesInput: Map<string, string[]> = new Map<string, string[]>();
41     private currentTargetFunctionName: any;
42     private functionAndAttributesOutput: Map<string, string[]> = new Map<string, string[]>();
43     suggestedAttributes: string[] = [];
44     selectedFunctionName = '';
45     selectedAttributeName = '';
46     isNotComponentResourceResolution = true;
47     currentArtifacts: string[] = [];
48     isParametersHidden = true;
49     cbaPackage: CBAPackage;
50     suggestedMappingParameters: string[] = [];
51     selectedParameterList: string[] = [];
52     currentSuggestedArtifact: string;
53     suggestedDeletedInput: any = {};
54     suggestedEditedAttribute: any = {};
55
56     constructor(private designerStore: DesignerStore,
57                 private functionsStore: FunctionsStore,
58                 private packageCreationStore: PackageCreationStore) {
59
60     }
61
62     ngOnInit() {
63         console.log('is paramters hidden' + this.isParametersHidden);
64         console.log('is artifacts hidden' + this.isNotComponentResourceResolution);
65         this.designerStore.state$.subscribe(designerState => {
66             this.designerState = designerState;
67             if (this.designerState && this.designerState.actionName) {
68                 this.actionName = this.designerState.actionName;
69                 console.log(this.actionName);
70                 const action = this.designerState.template.workflows[this.actionName] as Action;
71                 if (action.steps) {
72                     const steps = Object.keys(action.steps);
73                     this.isFunctionAttributeActive = steps && steps.length > 0;
74                     this.steps = steps;
75                     this.suggestedOutputs = [];
76                     this.suggestedInputs = [];
77                 }
78
79                 this.inputs = [];
80                 if (action.inputs) {
81                     const namesOfInput = Object.keys(action.inputs);
82                     this.inputs = this.extractFields(namesOfInput, action.inputs);
83                 }
84                 this.outputs = [];
85                 if (action.outputs) {
86                     const namesOfOutput = Object.keys(action.outputs);
87                     this.outputs = this.extractFields(namesOfOutput, action.outputs);
88                 }
89             }
90         });
91
92         this.functionsStore.state$.subscribe(functions => {
93             this.functions = functions;
94         });
95
96         this.packageCreationStore.state$
97             .subscribe(cbaPackage => {
98                 this.cbaPackage = cbaPackage;
99
100             });
101     }
102
103
104     private extractFields(namesOfOutput: string[], container: {}) {
105         const fields = [];
106         for (const nameOutput of namesOfOutput) {
107             const fieldAttribute = new OutputActionAttribute();
108             fieldAttribute.name = nameOutput;
109             fieldAttribute.description = container[nameOutput].description;
110             fieldAttribute.required = container[nameOutput].required;
111             fieldAttribute.type = container[nameOutput].type;
112             fieldAttribute.value = container[nameOutput].value;
113             console.log(fieldAttribute.value);
114             const insertedOutputActionAttribute = Object.assign({}, fieldAttribute);
115             fields.push(insertedOutputActionAttribute);
116         }
117         return fields;
118     }
119
120     addInput(input: InputActionAttribute) {
121         console.log(input);
122         if (input && input.type && input.name) {
123             const insertedInputActionAttribute = Object.assign({}, input);
124             this.newInputs.push(insertedInputActionAttribute);
125         }
126     }
127
128     addOutput(output: OutputActionAttribute) {
129         console.log(output);
130         if (output && output.type && output.name) {
131             const insertedOutputActionAttribute = Object.assign({}, output);
132             this.newOutputs.push(insertedOutputActionAttribute);
133         }
134     }
135
136     setInputType(type: string) {
137         this.inputActionAttribute.type = type;
138         this.isInputOtherType = this.checkIfTypeIsOther(type);
139     }
140
141     setInputRequired(isRequired) {
142         this.inputActionAttribute.required = isRequired;
143     }
144
145     setOutputRequired(isRequired) {
146         this.outputActionAttribute.required = isRequired;
147     }
148
149     setOutputType(type: string) {
150         this.outputActionAttribute.type = type;
151         this.isOutputOtherType = this.checkIfTypeIsOther(type);
152     }
153
154     checkIfTypeIsOther(type) {
155         return type.includes('Other');
156     }
157
158     submitAttributes() {
159         this.addInput(this.inputActionAttribute);
160         if (this.selectedFunctionName && this.selectedAttributeName) {
161             console.log(this.getValue());
162             this.outputActionAttribute.value =
163                 this.getValue();
164         }
165         this.addOutput(this.outputActionAttribute);
166         this.clearFormInputs();
167         this.storeOutputs(this.newOutputs);
168         this.storeInputs((this.newInputs));
169         this.newInputs.forEach(input => {
170             if (!this.inputs.includes(input)) {
171                 this.inputs.push(input);
172             }
173         });
174
175         this.newOutputs.forEach(output => {
176             if (!this.outputs.includes(output)) {
177                 this.outputs.push(output);
178             }
179         });
180     }
181
182     private getValue() {
183         let value = '["' + this.selectedFunctionName + '", "' + '","' + this.selectedAttributeName;
184
185         if (!this.isParametersHidden) {
186             let currentSelected = '';
187             for (const selectedParameter of this.selectedParameterList) {
188                 currentSelected += '"' + selectedParameter + '",';
189             }
190             value += '","' + this.currentSuggestedArtifact + '",'
191                 + currentSelected.substr(0, currentSelected.length - 2) + '';
192         } else if (!this.isNotComponentResourceResolution && this.currentSuggestedArtifact) {
193             value += '","' + this.currentSuggestedArtifact + '';
194
195         }
196
197         return value += '"]';
198     }
199
200     public clearFormInputs() {
201         console.log('trying to clear ');
202         this.inputActionAttribute = new InputActionAttribute();
203         this.outputActionAttribute = new OutputActionAttribute();
204         this.outputOtherType = '';
205         this.inputOtherType = '';
206     }
207
208     private storeInputs(InputActionAttributes: InputActionAttribute[]) {
209
210         let inputs = '';
211         InputActionAttributes.forEach(input => {
212             inputs += this.appendAttributes(input);
213             console.log(inputs);
214         });
215         if (inputs.length > 0) {
216             this.writeAttribute(inputs, 'inputs');
217         }
218     }
219
220     private storeOutputs(OutputActionAttributes: OutputActionAttribute[]) {
221         let outputs = '';
222         OutputActionAttributes.forEach(output => {
223             outputs += this.appendOutputAttributes(output);
224         });
225         if (outputs.length > 0) {
226             this.writeAttribute(outputs, 'outputs');
227         }
228
229     }
230
231     private appendAttributes(inputActionAttribute: InputActionAttribute) {
232         const entrySchema: string = this.getEntrySchema(inputActionAttribute);
233         const input = '"' + inputActionAttribute.name + '" : {\n' +
234             '            "required" : ' + inputActionAttribute.required + ',\n' +
235             '            "type" : "' + inputActionAttribute.type + '",\n' +
236             '            "description" : "' + inputActionAttribute.description + '"';
237         return input + entrySchema;
238     }
239
240     private getEntrySchema(inputActionAttribute: InputActionAttribute) {
241         return inputActionAttribute.type.includes('list') ?
242             ',\n\t' + '"entry_schema" : {\n' +
243             '              "type" : "string"\n' +
244             '            }\n},'
245             :
246             '\n },';
247     }
248
249     setInputAndOutputs(targetName) {
250         console.log(targetName);
251         const nodeTemplate = this.designerState.template.node_templates[targetName];
252         console.log(this.designerState.template.node_templates);
253         console.log(nodeTemplate);
254         /* tslint:disable:no-string-literal */
255         console.log(nodeTemplate['type']);
256         this.functions.serverFunctions
257             /* tslint:disable:no-string-literal */
258             .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
259             .forEach(currentFunction => {
260                 console.log(currentFunction);
261                 /* tslint:disable:no-string-literal */
262                 if (currentFunction['definition'] && currentFunction['definition']['interfaces']) {
263                     const interfaces = Object.keys(currentFunction['definition']['interfaces']);
264                     if (interfaces && interfaces.length > 0) {
265                         const interfaceName = interfaces[0];
266                         console.log(interfaceName);
267                         this.currentInterfaceName = interfaceName;
268
269                         if (!this.functionAndAttributesInput.has(targetName)) {
270                             this.currentTargetFunctionName = targetName;
271                             this.functionAndAttributesInput.set(targetName, []);
272                         }
273
274                         if (!this.functionAndAttributesOutput.has(targetName)) {
275                             this.currentTargetFunctionName = targetName;
276                             this.functionAndAttributesOutput.set(targetName, []);
277                         }
278
279                         if (nodeTemplate['interfaces'] &&
280                             nodeTemplate['interfaces'][interfaceName]['operations'] &&
281                             nodeTemplate['interfaces'][interfaceName]['operations']['process']
282                         ) {
283                             console.log('here');
284                             if (nodeTemplate['interfaces'][interfaceName]['operations']['process']['inputs']) {
285                                 /* tslint:disable:no-string-literal */
286                                 this.suggestedInputs = Object.keys(nodeTemplate['interfaces']
287                                     [interfaceName]['operations']['process']['inputs']);
288                                 this.filterTwoCollections(this.suggestedInputs, this.inputs);
289
290                             }
291                             if (nodeTemplate['interfaces'][interfaceName]['operations']['process']['outputs']) {
292                                 /* tslint:disable:no-string-literal */
293                                 this.suggestedOutputs = Object.keys(nodeTemplate['interfaces']
294                                     [interfaceName]['operations']['process']['outputs']);
295                                 this.filterTwoCollections(this.suggestedOutputs, this.outputs);
296                             }
297
298                         }
299                     }
300                 }
301             });
302         console.log(nodeTemplate);
303     }
304
305     private filterTwoCollections(suggestedInputs: string[], inputs: any[]) {
306         inputs.forEach(input => {
307             if (suggestedInputs.includes(input.name)) {
308                 this.deleteAttribute(suggestedInputs, input.name);
309             }
310         });
311     }
312
313     printSomethings() {
314         this.setInputAndOutputs(
315             this.designerState.template.workflows[this.actionName]['steps'][this.steps[0]]['target']
316         );
317     }
318
319     addTempInput(suggestedInput: string) {
320         this.addAttribute(this.tempInputs, suggestedInput);
321         this.deleteAttribute(this.suggestedInputs, suggestedInput);
322         this.addAttribute(this.functionAndAttributesInput.get(this.currentTargetFunctionName), suggestedInput);
323     }
324
325     addTempOutput(suggestedOutput: string) {
326         this.addAttribute(this.tempOutputs, suggestedOutput);
327         this.deleteAttribute(this.suggestedOutputs, suggestedOutput);
328         this.addAttribute(this.functionAndAttributesOutput.get(this.currentTargetFunctionName), suggestedOutput);
329     }
330
331     deleteAttribute(container: string[], suggestedAttribute: string) {
332         if (container && suggestedAttribute && container.includes(suggestedAttribute)) {
333             const index: number = container.indexOf(suggestedAttribute);
334             console.log(index);
335             if (index !== -1) {
336                 container.splice(index, 1);
337             }
338         }
339     }
340
341     addAttribute(container: string[], suggestedAttribute: string) {
342         if (container && suggestedAttribute && !container.includes(suggestedAttribute)) {
343             container.push(suggestedAttribute);
344         }
345     }
346
347
348     submitTempAttributes() {
349         this.writeSelectedAttribute(this.functionAndAttributesInput, 'inputs');
350         this.writeSelectedAttribute(this.functionAndAttributesOutput, 'outputs');
351     }
352
353     private writeSelectedAttribute(map: Map<string, string[]>, attributeType: string) {
354         map.forEach((value, key) => {
355             const nodeTemplate = this.getNodeTemplate(key);
356             this.functions.serverFunctions
357                 /* tslint:disable:no-string-literal */
358                 .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
359                 .forEach(currentFunction => {
360
361                     if (currentFunction['definition'] && currentFunction['definition']['interfaces']
362                         [Object.keys(currentFunction['definition'] && currentFunction['definition']['interfaces'])]
363                         ['operations']['process'][attributeType]) {
364                         let newAttributes = '';
365                         const attributes = currentFunction['definition'] && currentFunction['definition']['interfaces']
366                             [Object.keys(currentFunction['definition'] && currentFunction['definition']['interfaces'])]
367                             ['operations']['process'][attributeType];
368                         value.forEach(attribute => {
369                             newAttributes += '"' + attribute + '": ' + this.convertToString(attributes[attribute]) + ',';
370                         });
371                         if (value.length > 0) {
372                             this.writeAttribute(newAttributes, attributeType);
373                         }
374                     }
375                 });
376         });
377     }
378
379     private writeAttribute(newAttributes: string, attributeType: string) {
380         newAttributes = this.removeTheLastComma(newAttributes);
381         console.log(newAttributes);
382         let originalAttributes = this.convertToString(this.designerState.template.workflows[this.actionName]
383             [attributeType]);
384         this.createAttributeTypeIfNotExisted(originalAttributes, attributeType);
385         originalAttributes = this.convertToString(this.designerState.template.workflows[this.actionName]
386             [attributeType]);
387         if (originalAttributes.length > 2) {
388             this.designerState.template.workflows[this.actionName][attributeType] =
389                 this.convertToObject(originalAttributes.substr(0, originalAttributes.length - 1)
390                     + ',' + newAttributes + '}');
391         } else {
392             this.designerState.template.workflows[this.actionName][attributeType] =
393                 this.convertToObject(originalAttributes.substr(0, originalAttributes.length - 1)
394                     + newAttributes + '}');
395         }
396         /* console.log(originalAttributes.substr(0, originalAttributes.length - 1) + ',' + newAttributes + '}');
397          this.designerState.template.workflows[this.actionName][attributeType] =
398              this.convertToObject(originalAttributes.substr(0, originalAttributes.length - 1)
399                  + ',' + newAttributes + '}');*/
400     }
401
402     private removeTheLastComma(newInputs: string): string {
403         if (newInputs.endsWith(',')) {
404             newInputs = newInputs.substr(0, newInputs.length - 1);
405         }
406         return newInputs;
407     }
408
409     private convertToString = object => JSON.stringify(object);
410
411     private convertToObject = stringValue => JSON.parse(stringValue);
412
413     private getNodeTemplate = (value: string) => this.designerState.template.node_templates[value];
414
415
416     getAttributesAndOutputs(functionName: string) {
417         this.suggestedAttributes = [];
418         console.log(functionName);
419
420         const nodeTemplate = this.designerState.template.node_templates[functionName];
421         if (nodeTemplate['type'].includes('component-resource-resolution')) {
422             this.isNotComponentResourceResolution = false;
423             this.isParametersHidden = true;
424         } else {
425             this.isNotComponentResourceResolution = true;
426             this.isParametersHidden = true;
427         }
428         /* tslint:disable:no-string-literal */
429         console.log(nodeTemplate['type']);
430         this.functions.serverFunctions
431             /* tslint:disable:no-string-literal */
432             .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
433             .forEach(currentFunction => {
434                 if (currentFunction.definition['attributes']) {
435                     Object.keys(currentFunction.definition['attributes']).forEach(attribute => {
436                         this.suggestedAttributes.push(attribute);
437                         this.suggestedAttributes.push('assignment-map');
438                     });
439                 }
440                 console.log(this.suggestedAttributes);
441
442                 this.selectedFunctionName = functionName;
443
444
445             });
446     }
447
448     addTempOutputAttr(suggestedOutputAndAttribute: string) {
449         console.log('ssss');
450         this.selectedAttributeName = suggestedOutputAndAttribute;
451         this.currentArtifacts = [];
452         const nodeTemplate = this.designerState.template.node_templates[this.selectedFunctionName];
453         if (nodeTemplate['artifacts']
454         ) {
455             Object.keys(nodeTemplate['artifacts']).forEach(key => {
456                 const mappingName = key.split('-')[0];
457                 if (!this.currentArtifacts.includes(mappingName)) {
458                     this.currentArtifacts.push(mappingName);
459                 }
460             });
461         }
462         console.log('happend');
463
464     }
465
466
467     private appendOutputAttributes(outputActionAttribute: OutputActionAttribute) {
468         const entrySchema: string = this.getEntrySchema(outputActionAttribute);
469         const output = '"' + outputActionAttribute.name + '" : {\n' +
470             '            "required" : ' + outputActionAttribute.required + ',\n' +
471             '            "type" : "' + outputActionAttribute.type + '",\n' +
472             '            "description" : "' + outputActionAttribute.description + '",\n' +
473             '            "value\" :' + '{\n' +
474             '             "get_attribute" : ' + outputActionAttribute.value + '\n' +
475             '            }\n';
476
477         return output + entrySchema;
478
479     }
480
481     addArtifactFile(suggestedArtifact: string) {
482         this.currentSuggestedArtifact = suggestedArtifact;
483         this.isParametersHidden = !this.selectedAttributeName.includes('assignment-map');
484         if (!this.isParametersHidden) {
485             this.suggestedMappingParameters = this.getSuggestedMappingParameters(suggestedArtifact);
486         }
487     }
488
489     private getSuggestedMappingParameters(suggestedArtifact: string) {
490         const suggestedMappingParameters = [];
491
492         this.cbaPackage.mapping.files.forEach(((value, key) => {
493             if (key.includes(suggestedArtifact)) {
494
495                 JSON.parse(value).forEach(value2 => {
496                     suggestedMappingParameters.push(value2['name']);
497                 });
498             }
499         }));
500         return suggestedMappingParameters;
501     }
502
503     addSuggestedMappingParameter(suggestedMappingParameter: string) {
504         this.addAttribute(this.selectedParameterList, suggestedMappingParameter);
505         this.deleteAttribute(this.suggestedMappingParameters, suggestedMappingParameter);
506
507     }
508
509     editAttribute(input: any) {
510         this.suggestedEditedAttribute = input;
511     }
512
513     private createAttributeTypeIfNotExisted(originalAttributes: string, attributeType: string) {
514         if (!originalAttributes) {
515             this.designerState.template.workflows[this.actionName][attributeType] = {};
516         }
517     }
518
519     private checkIfTypeIsList(type: string) {
520         return type.includes('list');
521     }
522
523     markDeletedInput(input: any) {
524         this.suggestedDeletedInput = input;
525     }
526
527     deleteActionAttribute() {
528         delete this.designerState.template.workflows[this.actionName]
529             ['inputs'][this.suggestedDeletedInput.name];
530         this.deleteAttribute(this.inputs, this.suggestedDeletedInput);
531
532         delete this.designerState.template.workflows[this.actionName]
533             ['outputs'][this.suggestedDeletedInput.name];
534         this.deleteAttribute(this.outputs, this.suggestedDeletedInput);
535     }
536 }