11429ead60bf70f407fcef7d715fdcfe81208383
[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
37     constructor(private designerStore: DesignerStore, private functionsStore: FunctionsStore) {
38
39     }
40
41     ngOnInit() {
42         this.designerStore.state$.subscribe(designerState => {
43             this.designerState = designerState;
44             if (this.designerState && this.designerState.actionName) {
45                 this.actionName = this.designerState.actionName;
46                 const action = this.designerState.template.workflows[this.actionName] as Action;
47                 if (action.steps) {
48                     const steps = Object.keys(action.steps);
49                     if (steps && steps.length > 0) {
50                         this.isFunctionAttributeActive = true;
51                     } else {
52                         this.isFunctionAttributeActive = false;
53                     }
54                     this.steps = steps;
55                     this.suggestedOutputs = [];
56                     this.suggestedInputs = [];
57                     /*steps.forEach(step => {
58                         const target = action.steps[step].target;
59                         this.getInputs(target);
60                     });*/
61                 }
62
63
64                 this.inputs = [];
65                 if (action.inputs) {
66                     const namesOfInput = Object.keys(action.inputs);
67                     this.inputs = this.extractFields(namesOfInput, action.inputs);
68                 }
69                 this.outputs = [];
70                 if (action.outputs) {
71                     const namesOfOutput = Object.keys(action.outputs);
72                     this.outputs = this.extractFields(namesOfOutput, action.outputs);
73                 }
74             }
75         });
76
77         this.functionsStore.state$.subscribe(functions => {
78             this.functions = functions;
79         });
80     }
81
82
83     private extractFields(namesOfOutput: string[], container: {}) {
84         const fields = [];
85         for (const nameOutput of namesOfOutput) {
86             const fieldAttribute = new OutputActionAttribute();
87             fieldAttribute.name = nameOutput;
88             fieldAttribute.description = container[nameOutput].description;
89             fieldAttribute.required = container[nameOutput].required;
90             fieldAttribute.type = container[nameOutput].type;
91             const insertedOutputActionAttribute = Object.assign({}, fieldAttribute);
92             fields.push(insertedOutputActionAttribute);
93         }
94         return fields;
95     }
96
97     addInput(input: InputActionAttribute) {
98         if (input && input.type && input.name) {
99             const insertedInputActionAttribute = Object.assign({}, input);
100             this.inputs.push(insertedInputActionAttribute);
101         }
102     }
103
104     addOutput(output: OutputActionAttribute) {
105         console.log(output);
106         if (output && output.type && output.name) {
107             const insertedOutputActionAttribute = Object.assign({}, output);
108             this.outputs.push(insertedOutputActionAttribute);
109         }
110     }
111
112     setInputType(type: string) {
113         this.inputActionAttribute.type = type;
114         this.isInputOtherType = this.checkIfTypeIsOther(type);
115     }
116
117     setInputRequired(isRequired) {
118         this.inputActionAttribute.required = isRequired;
119     }
120
121     setOutputRequired(isRequired) {
122         this.outputActionAttribute.required = isRequired;
123     }
124
125     setOutputType(type: string) {
126         this.outputActionAttribute.type = type;
127         this.isOutputOtherType = this.checkIfTypeIsOther(type);
128     }
129
130     checkIfTypeIsOther(type) {
131         return type.includes('Other');
132     }
133
134     submitAttributes() {
135         this.addInput(this.inputActionAttribute);
136         this.addOutput(this.outputActionAttribute);
137         this.clearFormInputs();
138         this.designerStore.setInputsAndOutputsToSpecificWorkflow(this.storeInputs(this.inputs)
139             , this.storeOutputs(this.outputs), this.actionName);
140     }
141
142     private clearFormInputs() {
143         this.inputActionAttribute = new InputActionAttribute();
144         this.outputActionAttribute = new OutputActionAttribute();
145         this.outputOtherType = '';
146         this.inputOtherType = '';
147     }
148
149     private storeInputs(InputActionAttributes: InputActionAttribute[]) {
150
151         let inputs = '';
152         InputActionAttributes.forEach(input => {
153             inputs += this.appendAttributes(input);
154
155         });
156         if (inputs.endsWith(',')) {
157             inputs = inputs.substr(0, inputs.length - 1);
158         }
159         return JSON.parse('{' + inputs + '}');
160     }
161
162     private storeOutputs(OutputActionAttributes: OutputActionAttribute[]) {
163         let outputs = '';
164         OutputActionAttributes.forEach(output => {
165             outputs += this.appendAttributes(output);
166         });
167         if (outputs.endsWith(',')) {
168             outputs = outputs.substr(0, outputs.length - 1);
169         }
170         return JSON.parse('{' + outputs + '}');
171     }
172
173     private appendAttributes(output: OutputActionAttribute) {
174         return '"' + output.name + '" : {\n' +
175             '            "required" : ' + output.required + ',\n' +
176             '            "type" : "' + output.type + '",\n' +
177             '            "description" : "' + output.description + '"\n' +
178             '          },';
179     }
180
181     setInputAndOutputs(targetName) {
182         console.log(targetName);
183         const nodeTemplate = this.designerState.template.node_templates[targetName];
184         console.log(this.designerState.template.node_templates);
185         console.log(nodeTemplate);
186         /* tslint:disable:no-string-literal */
187         console.log(nodeTemplate['type']);
188         this.functions.serverFunctions
189             /* tslint:disable:no-string-literal */
190             .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
191             .forEach(currentFunction => {
192                 console.log(currentFunction);
193                 /* tslint:disable:no-string-literal */
194                 if (currentFunction['definition'] && currentFunction['definition']['interfaces']) {
195                     const interfaces = Object.keys(currentFunction['definition']['interfaces']);
196                     if (interfaces && interfaces.length > 0) {
197                         const interfaceName = interfaces[0];
198                         console.log(interfaceName);
199                         this.currentInterfaceName = interfaceName;
200
201                         if (nodeTemplate['interfaces'] &&
202                             nodeTemplate['interfaces'][interfaceName]['operations'] &&
203                             nodeTemplate['interfaces'][interfaceName]['operations']['process']
204                         ) {
205                             console.log('here');
206                             if (nodeTemplate['interfaces'][interfaceName]['operations']['process']['inputs']) {
207                                 /* tslint:disable:no-string-literal */
208                                 this.suggestedInputs = Object.keys(nodeTemplate['interfaces']
209                                     [interfaceName]['operations']['process']['inputs']);
210                                 console.log(this.suggestedInputs);
211                             }
212                             if (nodeTemplate['interfaces'][interfaceName]['operations']['process']['outputs']) {
213                                 /* tslint:disable:no-string-literal */
214                                 this.suggestedOutputs = Object.keys(nodeTemplate['interfaces']
215                                     [interfaceName]['operations']['process']['outputs']);
216                                 console.log(this.suggestedInputs);
217                             }
218
219                         }
220                     }
221                 }
222             });
223         console.log(nodeTemplate);
224     }
225
226     printSomethings() {
227         console.log('something');
228     }
229
230     addTempInput(suggestedInput: string) {
231         this.addAttribute(this.tempInputs, suggestedInput);
232         this.deleteAttribute(this.suggestedInputs, suggestedInput);
233     }
234
235     addTempOutput(suggestedOutput: string) {
236         this.addAttribute(this.tempOutputs, suggestedOutput);
237         this.deleteAttribute(this.suggestedOutputs, suggestedOutput);
238     }
239
240     deleteAttribute(container: string[], suggestedAttribute: string) {
241         if (container && suggestedAttribute && container.includes(suggestedAttribute)) {
242             const index: number = container.indexOf(suggestedAttribute);
243             if (index !== -1) {
244                 container.splice(index, 1);
245             }
246         }
247     }
248
249     addAttribute(container: string[], suggestedAttribute: string) {
250         if (container && suggestedAttribute && !container.includes(suggestedAttribute)) {
251             container.push(suggestedAttribute);
252         }
253     }
254
255
256     submitTempAttributes() {
257         if (this.tempInputs && this.tempInputs.length > 0) {
258             let newInputs = '';
259             this.tempInputs.forEach(tempAttribute => {
260                 const currentInputNode: string = this.designerState.template.node_templates[this.actionName]['interfaces']
261                     [this.currentInterfaceName]['operations']['process']['inputs'][tempAttribute];
262                 const currentInputNodeAsString = JSON.stringify(currentInputNode);
263                 newInputs += '"' + tempAttribute + '": ' + currentInputNodeAsString + ',';
264
265             });
266             if (newInputs.endsWith(',')) {
267                 newInputs = newInputs.substr(0, newInputs.length - 1);
268             }
269             const originalInputs = JSON.stringify(this.designerState.template.workflows[this.actionName]['inputs']);
270             console.log(originalInputs.substr(0, originalInputs.length - 1) + ',' + newInputs + '}');
271             this.designerState.template.workflows[this.actionName]['inputs'] =
272                 JSON.parse(originalInputs.substr(0, originalInputs.length - 1) + ',' + newInputs + '}');
273         }
274
275         if (this.tempOutputs && this.tempOutputs.length > 0) {
276             this.tempOutputs.forEach(tempAttribute => {
277                 const currentOutputNode = this.designerState.template.node_templates[this.actionName]['interfaces']
278                     [this.currentInterfaceName]['operations']['process']['outputs'][tempAttribute];
279                 console.log(currentOutputNode);
280             });
281         }
282     }
283 }