7103552d0d7a40fc4e3ec42e3d6453aa0e6a8ad9
[ccsdk/cds.git] /
1 import { Component, OnDestroy, OnInit } from '@angular/core';
2 import { DesignerStore } from '../designer.store';
3 import { PackageCreationStore } from '../../package-creation/package-creation.store';
4 import { Subject } from 'rxjs';
5 import { distinctUntilChanged, takeUntil } from 'rxjs/operators';
6 import { CBAPackage } from '../../package-creation/mapping-models/CBAPacakge.model';
7 import { TemplateAndMapping } from '../../package-creation/template-mapping/TemplateAndMapping';
8 import { FunctionsStore } from '../functions.store';
9 import { NodeProcess, NodeTemplate } from '../model/desinger.nodeTemplate.model';
10 import { DesignerDashboardState } from '../model/designer.dashboard.state';
11 import { Action } from '../action-attributes/models/Action';
12
13 @Component({
14     selector: 'app-functions-attribute',
15     templateUrl: './functions-attribute.component.html',
16     styleUrls: ['./functions-attribute.component.css']
17 })
18 export class FunctionsAttributeComponent implements OnInit, OnDestroy {
19
20     ngUnsubscribe = new Subject();
21     designerDashboardState: DecodeSuccessCallback;
22     cbaPackage: CBAPackage;
23     templateAndMappingMap = new Map<string, TemplateAndMapping>();
24     selectedTemplates = new Map<string, TemplateAndMapping>();
25     fileToDelete: string;
26     requiredInputs = new Map<string, {}>();
27     requiredOutputs = new Map<string, {}>();
28     OptionalInputs = new Map<string, {}>();
29     optionalOutputs = new Map<string, {}>();
30     artifactPrefix = false;
31     currentFuncion = new NodeProcess();
32     nodeTemplates = new NodeTemplate('');
33     designerState: DesignerDashboardState;
34     actionName = '';
35     functionName = '';
36     interfaceChildName = '';
37
38
39     constructor(
40         private designerStore: DesignerStore,
41         private packageCreationStore: PackageCreationStore,
42         private functionStore: FunctionsStore
43     ) {
44     }
45
46     ngOnInit() {
47         this.designerStore.state$
48             .pipe(
49                 distinctUntilChanged((a: any, b: any) => JSON.stringify(a) === JSON.stringify(b)),
50                 takeUntil(this.ngUnsubscribe))
51             .subscribe(designerDashboardState => {
52                 this.designerDashboardState = designerDashboardState;
53                 this.designerState = designerDashboardState;
54                 this.actionName = this.designerState.actionName;
55                 const action = this.designerState.template.workflows[this.actionName] as Action;
56
57                 console.log(action);
58                 if (action) {
59                     const child = Object.keys(action.steps)[0];
60                     this.functionName = action.steps[child].target;
61                     console.log(this.designerState.template.node_templates[this.functionName]);
62                     //  this.currentFuncion = this.designerState.template.node_templates[this.functionName];
63                     // reset inouts&outputs
64                     this.toNodeProcess(this.designerState.template.node_templates[this.functionName], this.functionName);
65                     const type = this.designerState.template.node_templates[this.functionName].type;
66                     this.getNodeType(type);
67                 }
68             });
69
70         this.packageCreationStore.state$
71             .subscribe(cbaPackage => {
72                 this.cbaPackage = cbaPackage;
73                 console.log('File name =>================== ');
74                 console.log(this.cbaPackage.templates.files);
75                 this.cbaPackage.templates.files.forEach((value, key) => {
76                     console.log('File name => ' + key);
77                     const templateAndMapping = new TemplateAndMapping();
78                     templateAndMapping.isTemplate = true;
79                     const isFromTemplate = true;
80                     this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
81                 });
82
83                 this.cbaPackage.mapping.files.forEach((value, key) => {
84                     const templateAndMapping = new TemplateAndMapping();
85                     templateAndMapping.isMapping = true;
86                     const isFromTemplate = false;
87                     this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
88                 });
89             });
90
91
92     }
93
94     toNodeProcess(nodeTemplate, functionName) {
95         this.requiredInputs = new Map<string, {}>();
96         this.requiredOutputs = new Map<string, {}>();
97         this.OptionalInputs = new Map<string, {}>();
98         this.optionalOutputs = new Map<string, {}>();
99         console.log(nodeTemplate);
100         this.currentFuncion['instance-name'] = functionName;
101         // tslint:disable-next-line: no-string-literal
102         this.currentFuncion['type'] = nodeTemplate['type'];
103         if (nodeTemplate.interfaces && Object.keys(nodeTemplate.interfaces).length > 0) {
104             const nodeName = Object.keys(nodeTemplate.interfaces)[0];
105             // tslint:disable-next-line: no-string-literal
106             const inputs = nodeTemplate.interfaces[nodeName]['operations']['process']['inputs'];
107             // tslint:disable-next-line: no-string-literal
108             const outputs = nodeTemplate.interfaces[nodeName]['operations']['process']['outputs'];
109
110             if (inputs) {
111                 for (const [key, value] of Object.entries(inputs)) {
112                     console.log(key + '-' + value);
113                     this.currentFuncion.inputs[key] = value;
114                 }
115             }
116             if (outputs) {
117                 for (const [key, value] of Object.entries(outputs)) {
118                     console.log(key + '-' + value);
119                     this.currentFuncion.outputs[key] = value;
120                 }
121             }
122         }
123     }
124     ngOnDestroy() {
125         this.ngUnsubscribe.next();
126         this.ngUnsubscribe.complete();
127     }
128
129     saveFunctionData() {
130
131         // tslint:disable-next-line: variable-name
132         const node_templates = {};
133         const finalFunctionData = this.currentFuncion;
134         // tslint:disable-next-line: no-string-literal
135         const type = finalFunctionData['type'];
136         const instanceName = finalFunctionData['instance-name'];
137         // insert selected templates in nodeTemplates.artifacts
138         this.selectedTemplates.forEach((value, key) => {
139             console.log(key);
140             console.log(value);
141
142             if (value.isMapping) {
143                 this.nodeTemplates.artifacts[key + '-mapping'] = {
144                     type: 'artifact-mapping-resource',
145                     file: 'Templates/' + key + '-mapping.json'
146                 };
147             }
148
149             if (value.isTemplate) {
150                 this.nodeTemplates.artifacts[key + '-template'] = {
151                     type: 'artifact-template-resource',
152                     file: 'Templates/' + key + '-template.vtl'
153                 };
154             }
155         });
156         // instantiate the final node_template object to save
157
158         this.nodeTemplates.type = type;
159         node_templates[finalFunctionData['instance-name']] = this.nodeTemplates;
160
161         delete finalFunctionData['instance-name'];
162         // tslint:disable-next-line: no-string-literal
163         delete finalFunctionData['type'];
164
165         this.nodeTemplates.interfaces = {
166             [this.interfaceChildName]: {
167                 operations: {
168                     process: {
169                         ...finalFunctionData,
170                     }
171                 }
172             }
173         };
174
175         console.log(finalFunctionData);
176         console.log(node_templates);
177         // tslint:disable-next-line: no-unused-expression
178         this.designerStore.addNodeTemplate(instanceName, type, node_templates[instanceName]);
179     }
180     // Template logic
181     private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
182         const nameOfFile = isFromTemplate ?
183             key.split('/')[1].split('.')[0].split('-template')[0]
184             : key.split('/')[1].split('.')[0].split('-mapping')[0];
185         // const fullName = nameOfFile + ',' + key.split('.');
186         if (this.templateAndMappingMap.has(nameOfFile)) {
187             const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
188             !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
189             this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
190         } else {
191             this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
192         }
193
194     }
195
196     addTemplates() { }
197     setArtifact(predefined: boolean) {
198         if (predefined) {
199
200         } else {
201             this.currentFuncion.inputs['artifact-prefix-names'] = { get_input: 'template-prefix' };
202         }
203     }
204     addToInputs(optionalInput) {
205         this.requiredInputs.set(optionalInput, this.OptionalInputs.get(optionalInput));
206         this.OptionalInputs.delete(optionalInput);
207     }
208
209     setTemplate(file: string) {
210         if (this.selectedTemplates.has(file)) {
211             this.selectedTemplates.delete(file);
212         } else {
213             this.selectedTemplates.set(file, this.templateAndMappingMap.get(file));
214         }
215         console.log(this.selectedTemplates);
216     }
217
218     getKeys(map: Map<string, any>) {
219         return Array.from(map.keys());
220     }
221     getValue(file: string, map: Map<string, any>) {
222         return map.get(file);
223     }
224
225     getObjectKey(object) {
226         // console.log(object);
227         return Object.keys(object);
228     }
229     getObjectValue(object) {
230         return Object.values(object);
231     }
232     getNodeType(nodeName: string) {
233         this.functionStore.state$
234             .subscribe(state => {
235                 console.log(state);
236                 console.log(nodeName);
237                 const functions = state.serverFunctions;
238                 // tslint:disable-next-line: prefer-for-of
239                 for (let i = 0; i < functions.length; i++) {
240                     if (functions[i].modelName === nodeName) {
241                         // tslint:disable: no-string-literal
242                         console.log(functions[i].definition['interfaces']);
243                         this.getInputFields(functions[i].definition['interfaces'], 'outputs');
244                         this.getInputFields(functions[i].definition['interfaces'], 'inputs');
245                         break;
246                     }
247                 }
248             });
249     }
250
251     getInputFields(interfaces, type) {
252
253         if (type === 'inputs') {
254             this.requiredInputs = new Map<string, {}>();
255             this.OptionalInputs = new Map<string, {}>();
256         } else {
257             this.requiredOutputs = new Map<string, {}>();
258             this.optionalOutputs = new Map<string, {}>();
259
260         }
261         const nodeName = Object.keys(interfaces)[0];
262         this.interfaceChildName = nodeName;
263         console.log(nodeName + ' ------ ' + type);
264         console.log(interfaces[nodeName]['operations']['process'][type]);
265         const fields = interfaces[nodeName]['operations']['process'][type];
266         this.artifactPrefix = false;
267         for (const [key, value] of Object.entries(fields)) {
268             if (key === 'artifact-prefix-names') {
269                 this.artifactPrefix = true;
270             } else if (value['required']) {
271                 console.log('This field is required = ' + key);
272                 if (type === 'inputs') {
273                     this.requiredInputs.set(key, Object.assign({}, value));
274                 } else {
275                     this.requiredOutputs.set(key, Object.assign({}, value));
276                 }
277             } else {
278                 console.log('This field is Optional ' + key);
279                 if (type === 'inputs') {
280                     this.OptionalInputs.set(key, Object.assign({}, value));
281                 } else {
282                     this.optionalOutputs.set(key, Object.assign({}, value));
283                 }
284             }
285         }
286
287         // console.log(this.requiredOutputs);
288     }
289
290
291 }