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