1c5a387d08108f3f90c681ac3124edba4abee38a
[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
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                             }
289                             if (nodeTemplate['interfaces'][interfaceName]['operations']['process']['outputs']) {
290                                 /* tslint:disable:no-string-literal */
291                                 this.suggestedOutputs = Object.keys(nodeTemplate['interfaces']
292                                     [interfaceName]['operations']['process']['outputs']);
293                                 console.log(this.suggestedInputs);
294                             }
295
296                         }
297                     }
298                 }
299             });
300         console.log(nodeTemplate);
301     }
302
303     printSomethings() {
304         console.log('something');
305     }
306
307     addTempInput(suggestedInput: string) {
308         this.addAttribute(this.tempInputs, suggestedInput);
309         this.deleteAttribute(this.suggestedInputs, suggestedInput);
310         this.addAttribute(this.functionAndAttributesInput.get(this.currentTargetFunctionName), suggestedInput);
311     }
312
313     addTempOutput(suggestedOutput: string) {
314         this.addAttribute(this.tempOutputs, suggestedOutput);
315         this.deleteAttribute(this.suggestedOutputs, suggestedOutput);
316         this.addAttribute(this.functionAndAttributesOutput.get(this.currentTargetFunctionName), suggestedOutput);
317     }
318
319     deleteAttribute(container: string[], suggestedAttribute: string) {
320         if (container && suggestedAttribute && container.includes(suggestedAttribute)) {
321             const index: number = container.indexOf(suggestedAttribute);
322             if (index !== -1) {
323                 container.splice(index, 1);
324             }
325         }
326     }
327
328     addAttribute(container: string[], suggestedAttribute: string) {
329         if (container && suggestedAttribute && !container.includes(suggestedAttribute)) {
330             container.push(suggestedAttribute);
331         }
332     }
333
334
335     submitTempAttributes() {
336         this.writeSelectedAttribute(this.functionAndAttributesInput, 'inputs');
337         this.writeSelectedAttribute(this.functionAndAttributesOutput, 'outputs');
338     }
339
340     private writeSelectedAttribute(map: Map<string, string[]>, attributeType: string) {
341         map.forEach((value, key) => {
342             const nodeTemplate = this.getNodeTemplate(key);
343             this.functions.serverFunctions
344                 /* tslint:disable:no-string-literal */
345                 .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
346                 .forEach(currentFunction => {
347
348                     if (currentFunction['definition'] && currentFunction['definition']['interfaces']
349                         [Object.keys(currentFunction['definition'] && currentFunction['definition']['interfaces'])]
350                         ['operations']['process'][attributeType]) {
351                         let newAttributes = '';
352                         const attributes = currentFunction['definition'] && currentFunction['definition']['interfaces']
353                             [Object.keys(currentFunction['definition'] && currentFunction['definition']['interfaces'])]
354                             ['operations']['process'][attributeType];
355                         value.forEach(attribute => {
356                             newAttributes += '"' + attribute + '": ' + this.convertToString(attributes[attribute]) + ',';
357                         });
358                         if (value.length > 0) {
359                             this.writeAttribute(newAttributes, attributeType);
360                         }
361                     }
362                 });
363         });
364     }
365
366     private writeAttribute(newAttributes: string, attributeType: string) {
367         newAttributes = this.removeTheLastComma(newAttributes);
368         console.log(newAttributes);
369         let originalAttributes = this.convertToString(this.designerState.template.workflows[this.actionName]
370             [attributeType]);
371         this.createAttributeTypeIfNotExisted(originalAttributes, attributeType);
372         originalAttributes = this.convertToString(this.designerState.template.workflows[this.actionName]
373             [attributeType]);
374         if (originalAttributes.length > 2) {
375             this.designerState.template.workflows[this.actionName][attributeType] =
376                 this.convertToObject(originalAttributes.substr(0, originalAttributes.length - 1)
377                     + ',' + newAttributes + '}');
378         } else {
379             this.designerState.template.workflows[this.actionName][attributeType] =
380                 this.convertToObject(originalAttributes.substr(0, originalAttributes.length - 1)
381                     + newAttributes + '}');
382         }
383         /* console.log(originalAttributes.substr(0, originalAttributes.length - 1) + ',' + newAttributes + '}');
384          this.designerState.template.workflows[this.actionName][attributeType] =
385              this.convertToObject(originalAttributes.substr(0, originalAttributes.length - 1)
386                  + ',' + newAttributes + '}');*/
387     }
388
389     private removeTheLastComma(newInputs: string): string {
390         if (newInputs.endsWith(',')) {
391             newInputs = newInputs.substr(0, newInputs.length - 1);
392         }
393         return newInputs;
394     }
395
396     private convertToString = object => JSON.stringify(object);
397
398     private convertToObject = stringValue => JSON.parse(stringValue);
399
400     private getNodeTemplate = (value: string) => this.designerState.template.node_templates[value];
401
402
403     getAttributesAndOutputs(functionName: string) {
404         this.suggestedAttributes = [];
405         console.log(functionName);
406
407         const nodeTemplate = this.designerState.template.node_templates[functionName];
408         if (nodeTemplate['type'].includes('component-resource-resolution')) {
409             this.isNotComponentResourceResolution = false;
410             this.isParametersHidden = true;
411         } else {
412             this.isNotComponentResourceResolution = true;
413             this.isParametersHidden = true;
414         }
415         /* tslint:disable:no-string-literal */
416         console.log(nodeTemplate['type']);
417         this.functions.serverFunctions
418             /* tslint:disable:no-string-literal */
419             .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
420             .forEach(currentFunction => {
421                 if (currentFunction.definition['attributes']) {
422                     Object.keys(currentFunction.definition['attributes']).forEach(attribute => {
423                         this.suggestedAttributes.push(attribute);
424                         this.suggestedAttributes.push('assignment-map');
425                     });
426                 }
427                 console.log(this.suggestedAttributes);
428
429                 this.selectedFunctionName = functionName;
430
431
432             });
433     }
434
435     addTempOutputAttr(suggestedOutputAndAttribute: string) {
436         console.log('ssss');
437         this.selectedAttributeName = suggestedOutputAndAttribute;
438         this.currentArtifacts = [];
439         const nodeTemplate = this.designerState.template.node_templates[this.selectedFunctionName];
440         if (nodeTemplate['artifacts']
441         ) {
442             Object.keys(nodeTemplate['artifacts']).forEach(key => {
443                 const mappingName = key.split('-')[0];
444                 if (!this.currentArtifacts.includes(mappingName)) {
445                     this.currentArtifacts.push(mappingName);
446                 }
447             });
448         }
449         console.log('happend');
450
451     }
452
453
454     private appendOutputAttributes(outputActionAttribute: OutputActionAttribute) {
455         const entrySchema: string = this.getEntrySchema(outputActionAttribute);
456         const output = '"' + outputActionAttribute.name + '" : {\n' +
457             '            "required" : ' + outputActionAttribute.required + ',\n' +
458             '            "type" : "' + outputActionAttribute.type + '",\n' +
459             '            "description" : "' + outputActionAttribute.description + '",\n' +
460             '            "value\" :' + '{\n' +
461             '             "get_attribute" : ' + outputActionAttribute.value + '\n' +
462             '            }\n';
463
464         return output + entrySchema;
465
466     }
467
468     addArtifactFile(suggestedArtifact: string) {
469         this.currentSuggestedArtifact = suggestedArtifact;
470         this.isParametersHidden = !this.selectedAttributeName.includes('assignment-map');
471         if (!this.isParametersHidden) {
472             this.suggestedMappingParameters = this.getSuggestedMappingParameters(suggestedArtifact);
473         }
474     }
475
476     private getSuggestedMappingParameters(suggestedArtifact: string) {
477         const suggestedMappingParameters = [];
478
479         this.cbaPackage.mapping.files.forEach(((value, key) => {
480             if (key.includes(suggestedArtifact)) {
481
482                 JSON.parse(value).forEach(value2 => {
483                     suggestedMappingParameters.push(value2['name']);
484                 });
485             }
486         }));
487         return suggestedMappingParameters;
488     }
489
490     addSuggestedMappingParameter(suggestedMappingParameter: string) {
491         this.addAttribute(this.selectedParameterList, suggestedMappingParameter);
492         this.deleteAttribute(this.suggestedMappingParameters, suggestedMappingParameter);
493
494     }
495
496     editAttribute(input: any) {
497         this.suggestedEditedAttribute = input;
498     }
499
500     private createAttributeTypeIfNotExisted(originalAttributes: string, attributeType: string) {
501         if (!originalAttributes) {
502             this.designerState.template.workflows[this.actionName][attributeType] = {};
503         }
504     }
505
506     private checkIfTypeIsList(type: string) {
507         return type.includes('list');
508     }
509
510     markDeletedInput(input: any) {
511         this.suggestedDeletedInput = input;
512     }
513
514     deleteActionAttribute() {
515         delete this.designerState.template.workflows[this.actionName]
516             ['inputs'][this.suggestedDeletedInput.name];
517         this.deleteAttribute(this.inputs, this.suggestedDeletedInput);
518
519         delete this.designerState.template.workflows[this.actionName]
520             ['outputs'][this.suggestedDeletedInput.name];
521         this.deleteAttribute(this.outputs, this.suggestedDeletedInput);
522     }
523 }