20305e853ac6aafe3c73f950ede8b9730535eebb
[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
11 @Component({
12     selector: 'app-functions-attribute',
13     templateUrl: './functions-attribute.component.html',
14     styleUrls: ['./functions-attribute.component.css']
15 })
16 export class FunctionsAttributeComponent implements OnInit, OnDestroy {
17
18     ngUnsubscribe = new Subject();
19     designerDashboardState: DecodeSuccessCallback;
20     cbaPackage: CBAPackage;
21     templateAndMappingMap = new Map<string, TemplateAndMapping>();
22     selectedTemplates = new Map<string, TemplateAndMapping>();
23     fileToDelete: string;
24     requiredInputs = new Map<string, {}>();
25     requiredOutputs = new Map<string, {}>();
26     OptionalInputs = new Map<string, {}>();
27     optionalOutputs = new Map<string, {}>();
28     artifactPrefix = false;
29     currentFuncion = new NodeProcess();
30     nodeTemplates = new NodeTemplate('');
31
32     constructor(
33         private designerStore: DesignerStore,
34         private packageCreationStore: PackageCreationStore,
35         private functionStore: FunctionsStore
36     ) {
37     }
38
39     ngOnInit() {
40         this.designerStore.state$
41             .pipe(
42                 distinctUntilChanged((a: any, b: any) => JSON.stringify(a) === JSON.stringify(b)),
43                 takeUntil(this.ngUnsubscribe))
44             .subscribe(designerDashboardState => {
45                 this.designerDashboardState = designerDashboardState;
46             });
47
48         this.packageCreationStore.state$
49             .subscribe(cbaPackage => {
50                 this.cbaPackage = cbaPackage;
51                 console.log('File name =>================== ');
52                 console.log(this.cbaPackage.templates.files);
53                 this.cbaPackage.templates.files.forEach((value, key) => {
54                     console.log('File name => ' + key);
55                     const templateAndMapping = new TemplateAndMapping();
56                     templateAndMapping.isTemplate = true;
57                     const isFromTemplate = true;
58                     this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
59                 });
60
61                 this.cbaPackage.mapping.files.forEach((value, key) => {
62                     const templateAndMapping = new TemplateAndMapping();
63                     templateAndMapping.isMapping = true;
64                     const isFromTemplate = false;
65                     this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
66                 });
67             });
68         this.getNodeType('component-resource-resolution');
69
70     }
71
72     ngOnDestroy() {
73         this.ngUnsubscribe.next();
74         this.ngUnsubscribe.complete();
75     }
76
77     displayFunctionData() {
78
79         // tslint:disable-next-line: variable-name
80         const node_templates = {};
81         const type = 'component-resource-resolution';
82         const instanceName = this.currentFuncion['instance-name'];
83         // insert selected templates in nodeTemplates.artifacts
84         this.selectedTemplates.forEach((value, key) => {
85             console.log(key);
86             console.log(value);
87
88             if (value.isMapping) {
89                 this.nodeTemplates.artifacts[key + '-mapping'] = {
90                     type: 'artifact-mapping-resource',
91                     file: 'Templates/' + key + '-mapping.json'
92                 };
93             }
94
95             if (value.isTemplate) {
96                 this.nodeTemplates.artifacts[key + '-template'] = {
97                     type: 'artifact-template-resource',
98                     file: 'Templates/' + key + '-template.vtl'
99                 };
100             }
101         });
102         // instantiate the final node_template object to save
103
104         this.nodeTemplates.type = 'component-resource-resolution';
105         node_templates[this.currentFuncion['instance-name']] = this.nodeTemplates;
106
107         delete this.currentFuncion['instance-name'];
108
109         this.nodeTemplates.interfaces = {
110             ResourceResolutionComponent: {
111                 operations: {
112                     process: {
113                         ...this.currentFuncion,
114                     }
115                 }
116             }
117         };
118
119         console.log(this.currentFuncion);
120         console.log(node_templates);
121         // tslint:disable-next-line: no-unused-expression
122         this.designerStore.addNodeTemplate(instanceName, type, node_templates[instanceName]);
123     }
124     // Template logic
125     private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
126         const nameOfFile = isFromTemplate ?
127             key.split('/')[1].split('.')[0].split('-template')[0]
128             : key.split('/')[1].split('.')[0].split('-mapping')[0];
129         // const fullName = nameOfFile + ',' + key.split('.');
130         if (this.templateAndMappingMap.has(nameOfFile)) {
131             const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
132             !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
133             this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
134         } else {
135             this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
136         }
137
138     }
139
140     addTemplates() { }
141     setArtifact(predefined: boolean) {
142         if (predefined) {
143
144         } else {
145             this.currentFuncion.inputs['artifact-prefix-names'] = { get_input: 'template-prefix' };
146         }
147     }
148     addToInputs(optionalInput) {
149         this.requiredInputs.set(optionalInput, this.OptionalInputs.get(optionalInput));
150         this.OptionalInputs.delete(optionalInput);
151     }
152
153     setTemplate(file: string) {
154         if (this.selectedTemplates.has(file)) {
155             this.selectedTemplates.delete(file);
156         } else {
157             this.selectedTemplates.set(file, this.templateAndMappingMap.get(file));
158         }
159         console.log(this.selectedTemplates);
160     }
161
162     getKeys(map: Map<string, any>) {
163         return Array.from(map.keys());
164     }
165     getValue(file: string, map: Map<string, any>) {
166         return map.get(file);
167     }
168
169     getObjectKey(object) {
170         // console.log(object);
171         return Object.keys(object);
172     }
173     getObjectValue(object) {
174         return Object.values(object);
175     }
176     getNodeType(nodeName: string) {
177         this.functionStore.state$
178             .subscribe(state => {
179                 console.log(state);
180                 const functions = state.serverFunctions;
181                 // tslint:disable-next-line: prefer-for-of
182                 for (let i = 0; i < functions.length; i++) {
183                     if (functions[i].modelName === nodeName) {
184                         // tslint:disable: no-string-literal
185                         console.log(functions[i].definition['interfaces']);
186                         this.getInputFields(functions[i].definition['interfaces'], 'ResourceResolutionComponent', 'inputs');
187                         this.getInputFields(functions[i].definition['interfaces'], 'ResourceResolutionComponent', 'outputs');
188                         break;
189                     }
190                 }
191             });
192     }
193
194     getInputFields(interfaces, nodeName, type) {
195         console.log(interfaces[nodeName]['operations']['process'][type]);
196         const fields = interfaces[nodeName]['operations']['process'][type];
197
198         for (const [key, value] of Object.entries(fields)) {
199             if (key === 'artifact-prefix-names') {
200                 this.artifactPrefix = true;
201             } else if (value['required']) {
202                 console.log('This field is required = ' + key);
203                 if (type === 'inputs') {
204                     this.requiredInputs.set(key, Object.assign({}, value));
205                 } else {
206                     this.requiredOutputs.set(key, Object.assign({}, value));
207                 }
208             } else {
209                 console.log('This field is Optional ' + key);
210                 if (type === 'inputs') {
211                     this.OptionalInputs.set(key, Object.assign({}, value));
212                 } else {
213                     this.optionalOutputs.set(key, Object.assign({}, value));
214                 }
215             }
216         }
217
218         // console.log(this.requiredOutputs);
219     }
220
221
222 }