5ab4b43fa45280cb52fd650d37c8bfd4628331dd
[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                     this.getNodeType(this.functionName);
66                 }
67             });
68
69         this.packageCreationStore.state$
70             .subscribe(cbaPackage => {
71                 this.cbaPackage = cbaPackage;
72                 console.log('File name =>================== ');
73                 console.log(this.cbaPackage.templates.files);
74                 this.cbaPackage.templates.files.forEach((value, key) => {
75                     console.log('File name => ' + key);
76                     const templateAndMapping = new TemplateAndMapping();
77                     templateAndMapping.isTemplate = true;
78                     const isFromTemplate = true;
79                     this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
80                 });
81
82                 this.cbaPackage.mapping.files.forEach((value, key) => {
83                     const templateAndMapping = new TemplateAndMapping();
84                     templateAndMapping.isMapping = true;
85                     const isFromTemplate = false;
86                     this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
87                 });
88             });
89
90
91     }
92
93     toNodeProcess(nodeTemplate, functionName) {
94         this.requiredInputs = new Map<string, {}>();
95         this.requiredOutputs = new Map<string, {}>();
96         this.OptionalInputs = new Map<string, {}>();
97         this.optionalOutputs = new Map<string, {}>();
98         console.log(nodeTemplate);
99         this.currentFuncion['instance-name'] = functionName;
100         // tslint:disable-next-line: no-string-literal
101         this.currentFuncion['type'] = nodeTemplate['type'];
102         if (Object.keys(nodeTemplate.interfaces).length > 0) {
103             const nodeName = Object.keys(nodeTemplate.interfaces)[0];
104             // tslint:disable-next-line: no-string-literal
105             const inputs = nodeTemplate.interfaces[nodeName]['operations']['process']['inputs'];
106             // tslint:disable-next-line: no-string-literal
107             const outputs = nodeTemplate.interfaces[nodeName]['operations']['process']['outputs'];
108
109             if (inputs) {
110                 for (const [key, value] of Object.entries(inputs)) {
111                     console.log(key + '-' + value);
112                     this.currentFuncion.inputs[key] = value;
113                 }
114             }
115             if (outputs) {
116                 for (const [key, value] of Object.entries(outputs)) {
117                     console.log(key + '-' + value);
118                     this.currentFuncion.outputs[key] = value;
119                 }
120             }
121         }
122     }
123     ngOnDestroy() {
124         this.ngUnsubscribe.next();
125         this.ngUnsubscribe.complete();
126     }
127
128     displayFunctionData() {
129
130         // tslint:disable-next-line: variable-name
131         const node_templates = {};
132         // tslint:disable-next-line: no-string-literal
133         const type = this.currentFuncion['type'];
134         const instanceName = this.currentFuncion['instance-name'];
135         // insert selected templates in nodeTemplates.artifacts
136         this.selectedTemplates.forEach((value, key) => {
137             console.log(key);
138             console.log(value);
139
140             if (value.isMapping) {
141                 this.nodeTemplates.artifacts[key + '-mapping'] = {
142                     type: 'artifact-mapping-resource',
143                     file: 'Templates/' + key + '-mapping.json'
144                 };
145             }
146
147             if (value.isTemplate) {
148                 this.nodeTemplates.artifacts[key + '-template'] = {
149                     type: 'artifact-template-resource',
150                     file: 'Templates/' + key + '-template.vtl'
151                 };
152             }
153         });
154         // instantiate the final node_template object to save
155
156         this.nodeTemplates.type = type;
157         node_templates[this.currentFuncion['instance-name']] = this.nodeTemplates;
158
159         delete this.currentFuncion['instance-name'];
160         // tslint:disable-next-line: no-string-literal
161         delete this.currentFuncion['type'];
162
163         this.nodeTemplates.interfaces = {
164             [this.interfaceChildName]: {
165                 operations: {
166                     process: {
167                         ...this.currentFuncion,
168                     }
169                 }
170             }
171         };
172
173         console.log(this.currentFuncion);
174         console.log(node_templates);
175         // tslint:disable-next-line: no-unused-expression
176         this.designerStore.addNodeTemplate(instanceName, type, node_templates[instanceName]);
177     }
178     // Template logic
179     private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
180         const nameOfFile = isFromTemplate ?
181             key.split('/')[1].split('.')[0].split('-template')[0]
182             : key.split('/')[1].split('.')[0].split('-mapping')[0];
183         // const fullName = nameOfFile + ',' + key.split('.');
184         if (this.templateAndMappingMap.has(nameOfFile)) {
185             const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
186             !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
187             this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
188         } else {
189             this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
190         }
191
192     }
193
194     addTemplates() { }
195     setArtifact(predefined: boolean) {
196         if (predefined) {
197
198         } else {
199             this.currentFuncion.inputs['artifact-prefix-names'] = { get_input: 'template-prefix' };
200         }
201     }
202     addToInputs(optionalInput) {
203         this.requiredInputs.set(optionalInput, this.OptionalInputs.get(optionalInput));
204         this.OptionalInputs.delete(optionalInput);
205     }
206
207     setTemplate(file: string) {
208         if (this.selectedTemplates.has(file)) {
209             this.selectedTemplates.delete(file);
210         } else {
211             this.selectedTemplates.set(file, this.templateAndMappingMap.get(file));
212         }
213         console.log(this.selectedTemplates);
214     }
215
216     getKeys(map: Map<string, any>) {
217         return Array.from(map.keys());
218     }
219     getValue(file: string, map: Map<string, any>) {
220         return map.get(file);
221     }
222
223     getObjectKey(object) {
224         // console.log(object);
225         return Object.keys(object);
226     }
227     getObjectValue(object) {
228         return Object.values(object);
229     }
230     getNodeType(nodeName: string) {
231         this.functionStore.state$
232             .subscribe(state => {
233                 console.log(state);
234                 const functions = state.serverFunctions;
235                 // tslint:disable-next-line: prefer-for-of
236                 for (let i = 0; i < functions.length; i++) {
237                     if (functions[i].modelName === nodeName) {
238                         // tslint:disable: no-string-literal
239                         console.log(functions[i].definition['interfaces']);
240                         this.getInputFields(functions[i].definition['interfaces'], 'outputs');
241                         this.getInputFields(functions[i].definition['interfaces'], 'inputs');
242                         break;
243                     }
244                 }
245             });
246     }
247
248     getInputFields(interfaces, type) {
249
250         if (type === 'inputs') {
251             this.requiredInputs = new Map<string, {}>();
252             this.OptionalInputs = new Map<string, {}>();
253         } else {
254             this.requiredOutputs = new Map<string, {}>();
255             this.optionalOutputs = new Map<string, {}>();
256
257         }
258         const nodeName = Object.keys(interfaces)[0];
259         this.interfaceChildName = nodeName;
260         console.log(interfaces[nodeName]['operations']['process'][type]);
261         const fields = interfaces[nodeName]['operations']['process'][type];
262         this.artifactPrefix = false;
263         for (const [key, value] of Object.entries(fields)) {
264             if (key === 'artifact-prefix-names') {
265                 this.artifactPrefix = true;
266             } else if (value['required']) {
267                 console.log('This field is required = ' + key);
268                 if (type === 'inputs') {
269                     this.requiredInputs.set(key, Object.assign({}, value));
270                 } else {
271                     this.requiredOutputs.set(key, Object.assign({}, value));
272                 }
273             } else {
274                 console.log('This field is Optional ' + key);
275                 if (type === 'inputs') {
276                     this.OptionalInputs.set(key, Object.assign({}, value));
277                 } else {
278                     this.optionalOutputs.set(key, Object.assign({}, value));
279                 }
280             }
281         }
282
283         // console.log(this.requiredOutputs);
284     }
285
286
287 }