f33965044e1beb81e6707c245329d161d3f147ab
[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 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                 this.inputs = [];
79                 if (action.inputs) {
80                     const namesOfInput = Object.keys(action.inputs);
81                     this.inputs = this.extractFields(namesOfInput, action.inputs);
82                 }
83                 this.outputs = [];
84                 if (action.outputs) {
85                     const namesOfOutput = Object.keys(action.outputs);
86                     this.outputs = this.extractFields(namesOfOutput, action.outputs);
87                 }
88             }
89         });
90
91         this.functionsStore.state$.subscribe(functions => {
92             this.functions = functions;
93         });
94
95         this.packageCreationStore.state$
96             .subscribe(cbaPackage => {
97                 this.cbaPackage = cbaPackage;
98
99             });
100     }
101
102
103     private extractFields(namesOfOutput: string[], container: {}) {
104         const fields = [];
105         for (const nameOutput of namesOfOutput) {
106             const fieldAttribute = new OutputActionAttribute();
107             fieldAttribute.name = nameOutput;
108             fieldAttribute.description = container[nameOutput].description;
109             fieldAttribute.required = container[nameOutput].required;
110             fieldAttribute.type = container[nameOutput].type;
111             fieldAttribute.value = container[nameOutput].value;
112             console.log(fieldAttribute.value);
113             const insertedOutputActionAttribute = Object.assign({}, fieldAttribute);
114             fields.push(insertedOutputActionAttribute);
115         }
116         return fields;
117     }
118
119     addInput(input: InputActionAttribute) {
120         console.log(input);
121         if (input && input.type && input.name) {
122             const insertedInputActionAttribute = Object.assign({}, input);
123             if (!this.newInputs.some(obj => obj.name === input.name)) {
124                 this.newInputs.push(insertedInputActionAttribute);
125             }
126         }
127     }
128
129     addOutput(output: OutputActionAttribute) {
130         console.log(output);
131         if (output && output.type && output.name) {
132             const insertedOutputActionAttribute = Object.assign({}, output);
133             this.newOutputs.push(insertedOutputActionAttribute);
134         }
135     }
136
137     setInputType(type: string) {
138         this.inputActionAttribute.type = type;
139         this.isInputOtherType = this.checkIfTypeIsOther(type);
140     }
141
142     setInputRequired(isRequired) {
143         this.inputActionAttribute.required = isRequired;
144     }
145
146     setOutputRequired(isRequired) {
147         this.outputActionAttribute.required = isRequired;
148     }
149
150     setOutputType(type: string) {
151         this.outputActionAttribute.type = type;
152         this.isOutputOtherType = this.checkIfTypeIsOther(type);
153     }
154
155     checkIfTypeIsOther(type) {
156         return type.includes('Other');
157     }
158
159     submitAttributes() {
160         this.addInput(this.inputActionAttribute);
161         if (this.selectedFunctionName && this.selectedAttributeName) {
162             console.log(this.getValue());
163             this.outputActionAttribute.value =
164                 this.getValue();
165         }
166         this.addOutput(this.outputActionAttribute);
167         this.clearFormInputs();
168         this.storeOutputs(this.newOutputs);
169         this.storeInputs((this.newInputs));
170         this.newInputs.forEach(input => {
171             if (!this.inputs.includes(input.name)) {
172                 this.inputs.push(input);
173             }
174         });
175
176         this.newOutputs.forEach(output => {
177             if (!this.outputs.includes(output)) {
178                 this.outputs.push(output);
179             }
180         });
181         this.newInputs = [];
182         this.newOutputs = [];
183     }
184
185     private getValue() {
186         let value = '["' + this.selectedFunctionName + '", "' + '","' + this.selectedAttributeName;
187
188         if (!this.isParametersHidden) {
189             let currentSelected = '';
190             for (const selectedParameter of this.selectedParameterList) {
191                 currentSelected += '"' + selectedParameter + '",';
192             }
193             value += '","' + this.currentSuggestedArtifact + '",'
194                 + currentSelected.substr(0, currentSelected.length - 2) + '';
195         } else if (!this.isNotComponentResourceResolution && this.currentSuggestedArtifact) {
196             value += '","' + this.currentSuggestedArtifact + '';
197
198         }
199
200         return value += '"]';
201     }
202
203     public clearFormInputs() {
204         console.log('trying to clear ');
205         this.inputActionAttribute = new InputActionAttribute();
206         this.outputActionAttribute = new OutputActionAttribute();
207         this.outputOtherType = '';
208         this.inputOtherType = '';
209     }
210
211     private storeInputs(InputActionAttributes: InputActionAttribute[]) {
212
213         let inputs = '';
214         InputActionAttributes.forEach(input => {
215             inputs += this.appendAttributes(input);
216             console.log(inputs);
217         });
218         if (inputs.length > 0) {
219             this.writeAttribute(inputs, 'inputs');
220         }
221     }
222
223     private storeOutputs(OutputActionAttributes: OutputActionAttribute[]) {
224         let outputs = '';
225         OutputActionAttributes.forEach(output => {
226             outputs += this.appendOutputAttributes(output);
227         });
228         if (outputs.length > 0) {
229             this.writeAttribute(outputs, 'outputs');
230         }
231
232     }
233
234     private appendAttributes(inputActionAttribute: InputActionAttribute) {
235         const entrySchema: string = this.getEntrySchema(inputActionAttribute);
236         const input = '"' + inputActionAttribute.name + '" : {\n' +
237             '            "required" : ' + inputActionAttribute.required + ',\n' +
238             '            "type" : "' + inputActionAttribute.type + '",\n' +
239             '            "description" : "' + inputActionAttribute.description + '"';
240         return input + entrySchema;
241     }
242
243     private getEntrySchema(inputActionAttribute: InputActionAttribute) {
244         return inputActionAttribute.type.includes('list') ?
245             ',\n\t' + '"entry_schema" : {\n' +
246             '              "type" : "string"\n' +
247             '            }\n},'
248             :
249             '\n },';
250     }
251
252     setInputAndOutputs(targetName) {
253         console.log(targetName);
254         const nodeTemplate = this.designerState.template.node_templates[targetName];
255         console.log(this.designerState.template.node_templates);
256         console.log(nodeTemplate);
257         /* tslint:disable:no-string-literal */
258         console.log(nodeTemplate['type']);
259         this.functions.serverFunctions
260             /* tslint:disable:no-string-literal */
261             .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
262             .forEach(currentFunction => {
263                 console.log(currentFunction);
264                 /* tslint:disable:no-string-literal */
265                 if (currentFunction['definition'] && currentFunction['definition']['interfaces']) {
266                     const interfaces = Object.keys(currentFunction['definition']['interfaces']);
267                     if (interfaces && interfaces.length > 0) {
268                         const interfaceName = interfaces[0];
269                         console.log(interfaceName);
270                         this.currentInterfaceName = interfaceName;
271
272                         if (!this.functionAndAttributesInput.has(targetName)) {
273                             this.currentTargetFunctionName = targetName;
274                             this.functionAndAttributesInput.set(targetName, []);
275                         }
276
277                         if (!this.functionAndAttributesOutput.has(targetName)) {
278                             this.currentTargetFunctionName = targetName;
279                             this.functionAndAttributesOutput.set(targetName, []);
280                         }
281
282                         if (nodeTemplate['interfaces'] &&
283                             nodeTemplate['interfaces'][interfaceName]['operations'] &&
284                             nodeTemplate['interfaces'][interfaceName]['operations']['process']
285                         ) {
286                             console.log('here');
287                             if (nodeTemplate['interfaces'][interfaceName]['operations']['process']['inputs']) {
288                                 /* tslint:disable:no-string-literal */
289                                 this.suggestedInputs = Object.keys(nodeTemplate['interfaces']
290                                     [interfaceName]['operations']['process']['inputs']);
291                                 this.filterTwoCollections(this.suggestedInputs, this.inputs);
292
293                             }
294                             if (nodeTemplate['interfaces'][interfaceName]['operations']['process']['outputs']) {
295                                 /* tslint:disable:no-string-literal */
296                                 this.suggestedOutputs = Object.keys(nodeTemplate['interfaces']
297                                     [interfaceName]['operations']['process']['outputs']);
298                                 this.filterTwoCollections(this.suggestedOutputs, this.outputs);
299                             }
300
301                         }
302                     }
303                 }
304             });
305         console.log(nodeTemplate);
306     }
307
308     private filterTwoCollections(suggestedInputs: string[], inputs: any[]) {
309         inputs.forEach(input => {
310             if (suggestedInputs.includes(input.name)) {
311                 this.deleteAttribute(suggestedInputs, input.name);
312             }
313         });
314     }
315
316     printSomethings() {
317         this.setInputAndOutputs(
318             this.designerState.template.workflows[this.actionName]['steps'][this.steps[0]]['target']
319         );
320     }
321
322     addTempInput(suggestedInput: string) {
323         this.addAttribute(this.tempInputs, suggestedInput);
324         this.deleteAttribute(this.suggestedInputs, suggestedInput);
325         this.addAttribute(this.functionAndAttributesInput.get(this.currentTargetFunctionName), suggestedInput);
326     }
327
328     addTempOutput(suggestedOutput: string) {
329         this.addAttribute(this.tempOutputs, suggestedOutput);
330         this.deleteAttribute(this.suggestedOutputs, suggestedOutput);
331         this.addAttribute(this.functionAndAttributesOutput.get(this.currentTargetFunctionName), suggestedOutput);
332     }
333
334     deleteAttribute(container: string[], suggestedAttribute: string) {
335         if (container && suggestedAttribute && container.includes(suggestedAttribute)) {
336             const index: number = container.indexOf(suggestedAttribute);
337             console.log(index);
338             if (index !== -1) {
339                 container.splice(index, 1);
340             }
341         }
342     }
343
344     addAttribute(container: string[], suggestedAttribute: string) {
345         if (container && suggestedAttribute && !container.includes(suggestedAttribute)) {
346             container.push(suggestedAttribute);
347         }
348     }
349
350
351     submitTempAttributes() {
352         this.writeSelectedAttribute(this.functionAndAttributesInput, 'inputs');
353         this.writeSelectedAttribute(this.functionAndAttributesOutput, 'outputs');
354     }
355
356     private writeSelectedAttribute(map: Map<string, string[]>, attributeType: string) {
357         map.forEach((value, key) => {
358             const nodeTemplate = this.getNodeTemplate(key);
359             this.functions.serverFunctions
360                 /* tslint:disable:no-string-literal */
361                 .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
362                 .forEach(currentFunction => {
363
364                     if (currentFunction['definition'] && currentFunction['definition']['interfaces']
365                         [Object.keys(currentFunction['definition'] && currentFunction['definition']['interfaces'])]
366                         ['operations']['process'][attributeType]) {
367                         let newAttributes = '';
368                         const attributes = currentFunction['definition'] && currentFunction['definition']['interfaces']
369                             [Object.keys(currentFunction['definition'] && currentFunction['definition']['interfaces'])]
370                             ['operations']['process'][attributeType];
371                         value.forEach(attribute => {
372                             newAttributes += '"' + attribute + '": ' + this.convertToString(attributes[attribute]) + ',';
373                         });
374                         if (value.length > 0) {
375                             this.writeAttribute(newAttributes, attributeType);
376                         }
377                     }
378                 });
379         });
380     }
381
382     private writeAttribute(newAttributes: string, attributeType: string) {
383         newAttributes = this.removeTheLastComma(newAttributes);
384         console.log(newAttributes);
385         let originalAttributes = this.convertToString(this.designerState.template.workflows[this.actionName]
386             [attributeType]);
387         this.createAttributeTypeIfNotExisted(originalAttributes, attributeType);
388         originalAttributes = this.convertToString(this.designerState.template.workflows[this.actionName]
389             [attributeType]);
390         if (originalAttributes.length > 2) {
391             this.designerState.template.workflows[this.actionName][attributeType] =
392                 this.convertToObject(originalAttributes.substr(0, originalAttributes.length - 1)
393                     + ',' + newAttributes + '}');
394         } else {
395             this.designerState.template.workflows[this.actionName][attributeType] =
396                 this.convertToObject(originalAttributes.substr(0, originalAttributes.length - 1)
397                     + newAttributes + '}');
398         }
399
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                     });
438                 }
439                 console.log(this.suggestedAttributes);
440
441                 this.selectedFunctionName = functionName;
442
443
444             });
445     }
446
447     addTempOutputAttr(suggestedOutputAndAttribute: string) {
448         console.log('ssss');
449         this.selectedAttributeName = suggestedOutputAndAttribute;
450         this.currentArtifacts = [];
451         const nodeTemplate = this.designerState.template.node_templates[this.selectedFunctionName];
452         if (nodeTemplate['artifacts']
453         ) {
454             Object.keys(nodeTemplate['artifacts']).forEach(key => {
455                 const mappingName = key.split('-')[0];
456                 if (!this.currentArtifacts.includes(mappingName)) {
457                     this.currentArtifacts.push(mappingName);
458                 }
459             });
460         }
461         console.log('happend');
462
463     }
464
465
466     private appendOutputAttributes(outputActionAttribute: OutputActionAttribute) {
467         const entrySchema: string = this.getEntrySchema(outputActionAttribute);
468         const output = '"' + outputActionAttribute.name + '" : {\n' +
469             '            "required" : ' + outputActionAttribute.required + ',\n' +
470             '            "type" : "' + outputActionAttribute.type + '",\n' +
471             '            "description" : "' + outputActionAttribute.description + '",\n' +
472             '            "value\" :' + '{\n' +
473             '             "get_attribute" : ' + outputActionAttribute.value + '\n' +
474             '            }\n';
475
476         return output + entrySchema;
477
478     }
479
480     addArtifactFile(suggestedArtifact: string) {
481         this.currentSuggestedArtifact = suggestedArtifact;
482         this.isParametersHidden = !this.selectedAttributeName.includes('assignment-map');
483         if (!this.isParametersHidden) {
484             this.suggestedMappingParameters = this.getSuggestedMappingParameters(suggestedArtifact);
485         }
486     }
487
488     private getSuggestedMappingParameters(suggestedArtifact: string) {
489         const suggestedMappingParameters = [];
490
491         this.cbaPackage.mapping.files.forEach(((value, key) => {
492             if (key.includes(suggestedArtifact)) {
493
494                 JSON.parse(value).forEach(value2 => {
495                     suggestedMappingParameters.push(value2['name']);
496                 });
497             }
498         }));
499         return suggestedMappingParameters;
500     }
501
502     addSuggestedMappingParameter(suggestedMappingParameter: string) {
503         this.addAttribute(this.selectedParameterList, suggestedMappingParameter);
504         this.deleteAttribute(this.suggestedMappingParameters, suggestedMappingParameter);
505
506     }
507
508     editAttribute(input: any) {
509         this.suggestedEditedAttribute = input;
510     }
511
512     private createAttributeTypeIfNotExisted(originalAttributes: string, attributeType: string) {
513         if (!originalAttributes) {
514             this.designerState.template.workflows[this.actionName][attributeType] = {};
515         }
516     }
517
518     private checkIfTypeIsList(type: string) {
519         return type.includes('list');
520     }
521
522     markDeletedInput(input: any) {
523         this.suggestedDeletedInput = input;
524     }
525
526     deleteActionAttribute() {
527         delete this.designerState.template.workflows[this.actionName]
528             ['inputs'][this.suggestedDeletedInput.name];
529         this.deleteAttribute(this.inputs, this.suggestedDeletedInput);
530
531         delete this.designerState.template.workflows[this.actionName]
532             ['outputs'][this.suggestedDeletedInput.name];
533         this.deleteAttribute(this.outputs, this.suggestedDeletedInput);
534     }
535 }