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