b0b6ae56dda8605c4b6aa5788cdc5113a0bb37fd
[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
9 @Component({
10     selector: 'app-action-attributes',
11     templateUrl: './action-attributes.component.html',
12     styleUrls: ['./action-attributes.component.css']
13 })
14 export class ActionAttributesComponent implements OnInit {
15
16     inputs = [];
17     outputs = [];
18     actionAttributesSideBar: boolean;
19     inputActionAttribute = new InputActionAttribute();
20     outputActionAttribute = new OutputActionAttribute();
21     isInputOtherType: boolean;
22     isOutputOtherType: boolean;
23     outputOtherType = '';
24     inputOtherType = '';
25     actionName = '';
26     designerState: DesignerDashboardState;
27     isFunctionAttributeActive = false;
28     functions: FunctionsState;
29     steps: string[];
30     suggestedInputs: string[] = [];
31     suggestedOutputs: string[] = [];
32
33     tempInputs: string[] = [];
34     tempOutputs: string[] = [];
35     currentInterfaceName: string;
36     functionAndAttributesInput: Map<string, string[]> = new Map<string, string[]>();
37     private currentTargetFunctionName: any;
38     private functionAndAttributesOutput: Map<string, string[]> = new Map<string, string[]>();
39
40     constructor(private designerStore: DesignerStore, private functionsStore: FunctionsStore) {
41
42     }
43
44     ngOnInit() {
45         this.designerStore.state$.subscribe(designerState => {
46             this.designerState = designerState;
47             if (this.designerState && this.designerState.actionName) {
48                 this.actionName = this.designerState.actionName;
49                 console.log(this.actionName);
50                 const action = this.designerState.template.workflows[this.actionName] as Action;
51                 if (action.steps) {
52                     const steps = Object.keys(action.steps);
53                     this.isFunctionAttributeActive = steps && steps.length > 0;
54                     this.steps = steps;
55                     this.suggestedOutputs = [];
56                     this.suggestedInputs = [];
57                 }
58
59                 this.inputs = [];
60                 if (action.inputs) {
61                     const namesOfInput = Object.keys(action.inputs);
62                     this.inputs = this.extractFields(namesOfInput, action.inputs);
63                 }
64                 this.outputs = [];
65                 if (action.outputs) {
66                     const namesOfOutput = Object.keys(action.outputs);
67                     this.outputs = this.extractFields(namesOfOutput, action.outputs);
68                 }
69             }
70         });
71
72         this.functionsStore.state$.subscribe(functions => {
73             this.functions = functions;
74         });
75     }
76
77
78     private extractFields(namesOfOutput: string[], container: {}) {
79         const fields = [];
80         for (const nameOutput of namesOfOutput) {
81             const fieldAttribute = new OutputActionAttribute();
82             fieldAttribute.name = nameOutput;
83             fieldAttribute.description = container[nameOutput].description;
84             fieldAttribute.required = container[nameOutput].required;
85             fieldAttribute.type = container[nameOutput].type;
86             const insertedOutputActionAttribute = Object.assign({}, fieldAttribute);
87             fields.push(insertedOutputActionAttribute);
88         }
89         return fields;
90     }
91
92     addInput(input: InputActionAttribute) {
93         if (input && input.type && input.name) {
94             const insertedInputActionAttribute = Object.assign({}, input);
95             this.inputs.push(insertedInputActionAttribute);
96         }
97     }
98
99     addOutput(output: OutputActionAttribute) {
100         console.log(output);
101         if (output && output.type && output.name) {
102             const insertedOutputActionAttribute = Object.assign({}, output);
103             this.outputs.push(insertedOutputActionAttribute);
104         }
105     }
106
107     setInputType(type: string) {
108         this.inputActionAttribute.type = type;
109         this.isInputOtherType = this.checkIfTypeIsOther(type);
110     }
111
112     setInputRequired(isRequired) {
113         this.inputActionAttribute.required = isRequired;
114     }
115
116     setOutputRequired(isRequired) {
117         this.outputActionAttribute.required = isRequired;
118     }
119
120     setOutputType(type: string) {
121         this.outputActionAttribute.type = type;
122         this.isOutputOtherType = this.checkIfTypeIsOther(type);
123     }
124
125     checkIfTypeIsOther(type) {
126         return type.includes('Other');
127     }
128
129     submitAttributes() {
130         this.addInput(this.inputActionAttribute);
131         this.addOutput(this.outputActionAttribute);
132         this.clearFormInputs();
133         this.designerStore.setInputsAndOutputsToSpecificWorkflow(this.storeInputs(this.inputs)
134             , this.storeOutputs(this.outputs), this.actionName);
135     }
136
137     private clearFormInputs() {
138         this.inputActionAttribute = new InputActionAttribute();
139         this.outputActionAttribute = new OutputActionAttribute();
140         this.outputOtherType = '';
141         this.inputOtherType = '';
142     }
143
144     private storeInputs(InputActionAttributes: InputActionAttribute[]) {
145
146         let inputs = '';
147         InputActionAttributes.forEach(input => {
148             inputs += this.appendAttributes(input);
149
150         });
151         if (inputs.endsWith(',')) {
152             inputs = inputs.substr(0, inputs.length - 1);
153         }
154         return this.convertToObject('{' + inputs + '}');
155     }
156
157     private storeOutputs(OutputActionAttributes: OutputActionAttribute[]) {
158         let outputs = '';
159         OutputActionAttributes.forEach(output => {
160             outputs += this.appendAttributes(output);
161         });
162         if (outputs.endsWith(',')) {
163             outputs = outputs.substr(0, outputs.length - 1);
164         }
165         return this.convertToObject('{' + outputs + '}');
166     }
167
168     private appendAttributes(output: OutputActionAttribute) {
169         return '"' + output.name + '" : {\n' +
170             '            "required" : ' + output.required + ',\n' +
171             '            "type" : "' + output.type + '",\n' +
172             '            "description" : "' + output.description + '"\n' +
173             '          },';
174     }
175
176     setInputAndOutputs(targetName) {
177         console.log(targetName);
178         const nodeTemplate = this.designerState.template.node_templates[targetName];
179         console.log(this.designerState.template.node_templates);
180         console.log(nodeTemplate);
181         /* tslint:disable:no-string-literal */
182         console.log(nodeTemplate['type']);
183         this.functions.serverFunctions
184             /* tslint:disable:no-string-literal */
185             .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
186             .forEach(currentFunction => {
187                 console.log(currentFunction);
188                 /* tslint:disable:no-string-literal */
189                 if (currentFunction['definition'] && currentFunction['definition']['interfaces']) {
190                     const interfaces = Object.keys(currentFunction['definition']['interfaces']);
191                     if (interfaces && interfaces.length > 0) {
192                         const interfaceName = interfaces[0];
193                         console.log(interfaceName);
194                         this.currentInterfaceName = interfaceName;
195
196                         if (!this.functionAndAttributesInput.has(targetName)) {
197                             this.currentTargetFunctionName = targetName;
198                             this.functionAndAttributesInput.set(targetName, []);
199                         }
200
201                         if (!this.functionAndAttributesOutput.has(targetName)) {
202                             this.currentTargetFunctionName = targetName;
203                             this.functionAndAttributesOutput.set(targetName, []);
204                         }
205
206                         if (nodeTemplate['interfaces'] &&
207                             nodeTemplate['interfaces'][interfaceName]['operations'] &&
208                             nodeTemplate['interfaces'][interfaceName]['operations']['process']
209                         ) {
210                             console.log('here');
211                             if (nodeTemplate['interfaces'][interfaceName]['operations']['process']['inputs']) {
212                                 /* tslint:disable:no-string-literal */
213                                 this.suggestedInputs = Object.keys(nodeTemplate['interfaces']
214                                     [interfaceName]['operations']['process']['inputs']);
215                             }
216                             if (nodeTemplate['interfaces'][interfaceName]['operations']['process']['outputs']) {
217                                 /* tslint:disable:no-string-literal */
218                                 this.suggestedOutputs = Object.keys(nodeTemplate['interfaces']
219                                     [interfaceName]['operations']['process']['outputs']);
220                                 console.log(this.suggestedInputs);
221                             }
222
223                         }
224                     }
225                 }
226             });
227         console.log(nodeTemplate);
228     }
229
230     printSomethings() {
231         console.log('something');
232     }
233
234     addTempInput(suggestedInput: string) {
235         this.addAttribute(this.tempInputs, suggestedInput);
236         this.deleteAttribute(this.suggestedInputs, suggestedInput);
237         this.addAttribute(this.functionAndAttributesInput.get(this.currentTargetFunctionName), suggestedInput);
238     }
239
240     addTempOutput(suggestedOutput: string) {
241         this.addAttribute(this.tempOutputs, suggestedOutput);
242         this.deleteAttribute(this.suggestedOutputs, suggestedOutput);
243         this.addAttribute(this.functionAndAttributesOutput.get(this.currentTargetFunctionName), suggestedOutput);
244     }
245
246     deleteAttribute(container: string[], suggestedAttribute: string) {
247         if (container && suggestedAttribute && container.includes(suggestedAttribute)) {
248             const index: number = container.indexOf(suggestedAttribute);
249             if (index !== -1) {
250                 container.splice(index, 1);
251             }
252         }
253     }
254
255     addAttribute(container: string[], suggestedAttribute: string) {
256         if (container && suggestedAttribute && !container.includes(suggestedAttribute)) {
257             container.push(suggestedAttribute);
258         }
259     }
260
261
262     submitTempAttributes() {
263         this.writeSelectedAttribute(this.functionAndAttributesInput, 'inputs');
264         this.writeSelectedAttribute(this.functionAndAttributesOutput, 'outputs');
265     }
266
267     private writeSelectedAttribute(map: Map<string, string[]>, attributeType: string) {
268         map.forEach((value, key) => {
269             const nodeTemplate = this.getNodeTemplate(key);
270             this.functions.serverFunctions
271                 /* tslint:disable:no-string-literal */
272                 .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
273                 .forEach(currentFunction => {
274
275                     if (currentFunction['definition'] && currentFunction['definition']['interfaces']
276                         [Object.keys(currentFunction['definition'] && currentFunction['definition']['interfaces'])]
277                         ['operations']['process'][attributeType]) {
278                         let newAttributes = '';
279                         const attributes = currentFunction['definition'] && currentFunction['definition']['interfaces']
280                             [Object.keys(currentFunction['definition'] && currentFunction['definition']['interfaces'])]
281                             ['operations']['process'][attributeType];
282                         value.forEach(attribute => {
283                             newAttributes += '"' + attribute + '": ' + this.convertToString(attributes[attribute]) + ',';
284                         });
285                         if (value.length > 0) {
286                             newAttributes = this.removeTheLastComma(newAttributes);
287                             const originalAttributes = this.convertToString(this.designerState.template.workflows[this.actionName]
288                                 [attributeType]);
289                             console.log(originalAttributes.substr(0, originalAttributes.length - 1) + ',' + newAttributes + '}');
290                             this.designerState.template.workflows[this.actionName][attributeType] =
291                                 this.convertToObject(originalAttributes.substr(0, originalAttributes.length - 1)
292                                     + ',' + newAttributes + '}');
293                         }
294                     }
295                 });
296         });
297     }
298
299     private removeTheLastComma = (newInputs: string) => {
300         if (newInputs.endsWith(',')) {
301             newInputs = newInputs.substr(0, newInputs.length - 1);
302         }
303         return newInputs;
304     }
305
306     private convertToString = object => JSON.stringify(object);
307
308     private convertToObject = stringValue => JSON.parse(stringValue);
309
310     private getNodeTemplate = (value: string) => this.designerState.template.node_templates[value];
311
312 }