6873f8948d52411ae18696a595b27409740dbeda
[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.includes(insertedInputActionAttribute)) {
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     }
182
183     private getValue() {
184         let value = '["' + this.selectedFunctionName + '", "' + '","' + this.selectedAttributeName;
185
186         if (!this.isParametersHidden) {
187             let currentSelected = '';
188             for (const selectedParameter of this.selectedParameterList) {
189                 currentSelected += '"' + selectedParameter + '",';
190             }
191             value += '","' + this.currentSuggestedArtifact + '",'
192                 + currentSelected.substr(0, currentSelected.length - 2) + '';
193         } else if (!this.isNotComponentResourceResolution && this.currentSuggestedArtifact) {
194             value += '","' + this.currentSuggestedArtifact + '';
195
196         }
197
198         return value += '"]';
199     }
200
201     public clearFormInputs() {
202         console.log('trying to clear ');
203         this.inputActionAttribute = new InputActionAttribute();
204         this.outputActionAttribute = new OutputActionAttribute();
205         this.outputOtherType = '';
206         this.inputOtherType = '';
207     }
208
209     private storeInputs(InputActionAttributes: InputActionAttribute[]) {
210
211         let inputs = '';
212         InputActionAttributes.forEach(input => {
213             inputs += this.appendAttributes(input);
214             console.log(inputs);
215         });
216         if (inputs.length > 0) {
217             this.writeAttribute(inputs, 'inputs');
218         }
219     }
220
221     private storeOutputs(OutputActionAttributes: OutputActionAttribute[]) {
222         let outputs = '';
223         OutputActionAttributes.forEach(output => {
224             outputs += this.appendOutputAttributes(output);
225         });
226         if (outputs.length > 0) {
227             this.writeAttribute(outputs, 'outputs');
228         }
229
230     }
231
232     private appendAttributes(inputActionAttribute: InputActionAttribute) {
233         const entrySchema: string = this.getEntrySchema(inputActionAttribute);
234         const input = '"' + inputActionAttribute.name + '" : {\n' +
235             '            "required" : ' + inputActionAttribute.required + ',\n' +
236             '            "type" : "' + inputActionAttribute.type + '",\n' +
237             '            "description" : "' + inputActionAttribute.description + '"';
238         return input + entrySchema;
239     }
240
241     private getEntrySchema(inputActionAttribute: InputActionAttribute) {
242         return inputActionAttribute.type.includes('list') ?
243             ',\n\t' + '"entry_schema" : {\n' +
244             '              "type" : "string"\n' +
245             '            }\n},'
246             :
247             '\n },';
248     }
249
250     setInputAndOutputs(targetName) {
251         console.log(targetName);
252         const nodeTemplate = this.designerState.template.node_templates[targetName];
253         console.log(this.designerState.template.node_templates);
254         console.log(nodeTemplate);
255         /* tslint:disable:no-string-literal */
256         console.log(nodeTemplate['type']);
257         this.functions.serverFunctions
258             /* tslint:disable:no-string-literal */
259             .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
260             .forEach(currentFunction => {
261                 console.log(currentFunction);
262                 /* tslint:disable:no-string-literal */
263                 if (currentFunction['definition'] && currentFunction['definition']['interfaces']) {
264                     const interfaces = Object.keys(currentFunction['definition']['interfaces']);
265                     if (interfaces && interfaces.length > 0) {
266                         const interfaceName = interfaces[0];
267                         console.log(interfaceName);
268                         this.currentInterfaceName = interfaceName;
269
270                         if (!this.functionAndAttributesInput.has(targetName)) {
271                             this.currentTargetFunctionName = targetName;
272                             this.functionAndAttributesInput.set(targetName, []);
273                         }
274
275                         if (!this.functionAndAttributesOutput.has(targetName)) {
276                             this.currentTargetFunctionName = targetName;
277                             this.functionAndAttributesOutput.set(targetName, []);
278                         }
279
280                         if (nodeTemplate['interfaces'] &&
281                             nodeTemplate['interfaces'][interfaceName]['operations'] &&
282                             nodeTemplate['interfaces'][interfaceName]['operations']['process']
283                         ) {
284                             console.log('here');
285                             if (nodeTemplate['interfaces'][interfaceName]['operations']['process']['inputs']) {
286                                 /* tslint:disable:no-string-literal */
287                                 this.suggestedInputs = Object.keys(nodeTemplate['interfaces']
288                                     [interfaceName]['operations']['process']['inputs']);
289                                 this.filterTwoCollections(this.suggestedInputs, this.inputs);
290
291                             }
292                             if (nodeTemplate['interfaces'][interfaceName]['operations']['process']['outputs']) {
293                                 /* tslint:disable:no-string-literal */
294                                 this.suggestedOutputs = Object.keys(nodeTemplate['interfaces']
295                                     [interfaceName]['operations']['process']['outputs']);
296                                 this.filterTwoCollections(this.suggestedOutputs, this.outputs);
297                             }
298
299                         }
300                     }
301                 }
302             });
303         console.log(nodeTemplate);
304     }
305
306     private filterTwoCollections(suggestedInputs: string[], inputs: any[]) {
307         inputs.forEach(input => {
308             if (suggestedInputs.includes(input.name)) {
309                 this.deleteAttribute(suggestedInputs, input.name);
310             }
311         });
312     }
313
314     printSomethings() {
315         this.setInputAndOutputs(
316             this.designerState.template.workflows[this.actionName]['steps'][this.steps[0]]['target']
317         );
318     }
319
320     addTempInput(suggestedInput: string) {
321         this.addAttribute(this.tempInputs, suggestedInput);
322         this.deleteAttribute(this.suggestedInputs, suggestedInput);
323         this.addAttribute(this.functionAndAttributesInput.get(this.currentTargetFunctionName), suggestedInput);
324     }
325
326     addTempOutput(suggestedOutput: string) {
327         this.addAttribute(this.tempOutputs, suggestedOutput);
328         this.deleteAttribute(this.suggestedOutputs, suggestedOutput);
329         this.addAttribute(this.functionAndAttributesOutput.get(this.currentTargetFunctionName), suggestedOutput);
330     }
331
332     deleteAttribute(container: string[], suggestedAttribute: string) {
333         if (container && suggestedAttribute && container.includes(suggestedAttribute)) {
334             const index: number = container.indexOf(suggestedAttribute);
335             console.log(index);
336             if (index !== -1) {
337                 container.splice(index, 1);
338             }
339         }
340     }
341
342     addAttribute(container: string[], suggestedAttribute: string) {
343         if (container && suggestedAttribute && !container.includes(suggestedAttribute)) {
344             container.push(suggestedAttribute);
345         }
346     }
347
348
349     submitTempAttributes() {
350         this.writeSelectedAttribute(this.functionAndAttributesInput, 'inputs');
351         this.writeSelectedAttribute(this.functionAndAttributesOutput, 'outputs');
352     }
353
354     private writeSelectedAttribute(map: Map<string, string[]>, attributeType: string) {
355         map.forEach((value, key) => {
356             const nodeTemplate = this.getNodeTemplate(key);
357             this.functions.serverFunctions
358                 /* tslint:disable:no-string-literal */
359                 .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
360                 .forEach(currentFunction => {
361
362                     if (currentFunction['definition'] && currentFunction['definition']['interfaces']
363                         [Object.keys(currentFunction['definition'] && currentFunction['definition']['interfaces'])]
364                         ['operations']['process'][attributeType]) {
365                         let newAttributes = '';
366                         const attributes = currentFunction['definition'] && currentFunction['definition']['interfaces']
367                             [Object.keys(currentFunction['definition'] && currentFunction['definition']['interfaces'])]
368                             ['operations']['process'][attributeType];
369                         value.forEach(attribute => {
370                             newAttributes += '"' + attribute + '": ' + this.convertToString(attributes[attribute]) + ',';
371                         });
372                         if (value.length > 0) {
373                             this.writeAttribute(newAttributes, attributeType);
374                         }
375                     }
376                 });
377         });
378     }
379
380     private writeAttribute(newAttributes: string, attributeType: string) {
381         newAttributes = this.removeTheLastComma(newAttributes);
382         console.log(newAttributes);
383         let originalAttributes = this.convertToString(this.designerState.template.workflows[this.actionName]
384             [attributeType]);
385         this.createAttributeTypeIfNotExisted(originalAttributes, attributeType);
386         originalAttributes = this.convertToString(this.designerState.template.workflows[this.actionName]
387             [attributeType]);
388         if (originalAttributes.length > 2) {
389             this.designerState.template.workflows[this.actionName][attributeType] =
390                 this.convertToObject(originalAttributes.substr(0, originalAttributes.length - 1)
391                     + ',' + newAttributes + '}');
392         } else {
393             this.designerState.template.workflows[this.actionName][attributeType] =
394                 this.convertToObject(originalAttributes.substr(0, originalAttributes.length - 1)
395                     + newAttributes + '}');
396         }
397
398     }
399
400     private removeTheLastComma(newInputs: string): string {
401         if (newInputs.endsWith(',')) {
402             newInputs = newInputs.substr(0, newInputs.length - 1);
403         }
404         return newInputs;
405     }
406
407     private convertToString = object => JSON.stringify(object);
408
409     private convertToObject = stringValue => JSON.parse(stringValue);
410
411     private getNodeTemplate = (value: string) => this.designerState.template.node_templates[value];
412
413
414     getAttributesAndOutputs(functionName: string) {
415         this.suggestedAttributes = [];
416         console.log(functionName);
417
418         const nodeTemplate = this.designerState.template.node_templates[functionName];
419         if (nodeTemplate['type'].includes('component-resource-resolution')) {
420             this.isNotComponentResourceResolution = false;
421             this.isParametersHidden = true;
422         } else {
423             this.isNotComponentResourceResolution = true;
424             this.isParametersHidden = true;
425         }
426         /* tslint:disable:no-string-literal */
427         console.log(nodeTemplate['type']);
428         this.functions.serverFunctions
429             /* tslint:disable:no-string-literal */
430             .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
431             .forEach(currentFunction => {
432                 if (currentFunction.definition['attributes']) {
433                     Object.keys(currentFunction.definition['attributes']).forEach(attribute => {
434                         this.suggestedAttributes.push(attribute);
435                     });
436                 }
437                 console.log(this.suggestedAttributes);
438
439                 this.selectedFunctionName = functionName;
440
441
442             });
443     }
444
445     addTempOutputAttr(suggestedOutputAndAttribute: string) {
446         console.log('ssss');
447         this.selectedAttributeName = suggestedOutputAndAttribute;
448         this.currentArtifacts = [];
449         const nodeTemplate = this.designerState.template.node_templates[this.selectedFunctionName];
450         if (nodeTemplate['artifacts']
451         ) {
452             Object.keys(nodeTemplate['artifacts']).forEach(key => {
453                 const mappingName = key.split('-')[0];
454                 if (!this.currentArtifacts.includes(mappingName)) {
455                     this.currentArtifacts.push(mappingName);
456                 }
457             });
458         }
459         console.log('happend');
460
461     }
462
463
464     private appendOutputAttributes(outputActionAttribute: OutputActionAttribute) {
465         const entrySchema: string = this.getEntrySchema(outputActionAttribute);
466         const output = '"' + outputActionAttribute.name + '" : {\n' +
467             '            "required" : ' + outputActionAttribute.required + ',\n' +
468             '            "type" : "' + outputActionAttribute.type + '",\n' +
469             '            "description" : "' + outputActionAttribute.description + '",\n' +
470             '            "value\" :' + '{\n' +
471             '             "get_attribute" : ' + outputActionAttribute.value + '\n' +
472             '            }\n';
473
474         return output + entrySchema;
475
476     }
477
478     addArtifactFile(suggestedArtifact: string) {
479         this.currentSuggestedArtifact = suggestedArtifact;
480         this.isParametersHidden = !this.selectedAttributeName.includes('assignment-map');
481         if (!this.isParametersHidden) {
482             this.suggestedMappingParameters = this.getSuggestedMappingParameters(suggestedArtifact);
483         }
484     }
485
486     private getSuggestedMappingParameters(suggestedArtifact: string) {
487         const suggestedMappingParameters = [];
488
489         this.cbaPackage.mapping.files.forEach(((value, key) => {
490             if (key.includes(suggestedArtifact)) {
491
492                 JSON.parse(value).forEach(value2 => {
493                     suggestedMappingParameters.push(value2['name']);
494                 });
495             }
496         }));
497         return suggestedMappingParameters;
498     }
499
500     addSuggestedMappingParameter(suggestedMappingParameter: string) {
501         this.addAttribute(this.selectedParameterList, suggestedMappingParameter);
502         this.deleteAttribute(this.suggestedMappingParameters, suggestedMappingParameter);
503
504     }
505
506     editAttribute(input: any) {
507         this.suggestedEditedAttribute = input;
508     }
509
510     private createAttributeTypeIfNotExisted(originalAttributes: string, attributeType: string) {
511         if (!originalAttributes) {
512             this.designerState.template.workflows[this.actionName][attributeType] = {};
513         }
514     }
515
516     private checkIfTypeIsList(type: string) {
517         return type.includes('list');
518     }
519
520     markDeletedInput(input: any) {
521         this.suggestedDeletedInput = input;
522     }
523
524     deleteActionAttribute() {
525         delete this.designerState.template.workflows[this.actionName]
526             ['inputs'][this.suggestedDeletedInput.name];
527         this.deleteAttribute(this.inputs, this.suggestedDeletedInput);
528
529         delete this.designerState.template.workflows[this.actionName]
530             ['outputs'][this.suggestedDeletedInput.name];
531         this.deleteAttribute(this.outputs, this.suggestedDeletedInput);
532     }
533 }