624f9c7a85fcf275a424bfd19786c39216dae0e1
[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         this.selectedTemplates.forEach((value, key) => {
79             console.log(key);
80             console.log(value);
81
82             if (value.isMapping) {
83                 this.nodeTemplates.artifacts[key + '-mapping'] = {
84                     type: 'artifact-mapping-resource',
85                     file: 'Templates/' + key + '-mapping.json'
86                 };
87             }
88
89             if (value.isTemplate) {
90                 this.nodeTemplates.artifacts[key + '-template'] = {
91                     type: 'artifact-template-resource',
92                     file: 'Templates/' + key + '-template.vtl'
93                 };
94             }
95         });
96         this.nodeTemplates.interfaces = {
97             ResourceResolutionComponent: {
98                 operations: {
99                     process: {
100                         ...this.currentFuncion
101                     }
102                 }
103             }
104         };
105         setTimeout(() => {
106             console.log(this.currentFuncion);
107             console.log(this.nodeTemplates);
108
109         }, 1500);
110     }
111     // Template logic
112     private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
113         const nameOfFile = isFromTemplate ?
114             key.split('/')[1].split('.')[0].split('-template')[0]
115             : key.split('/')[1].split('.')[0].split('-mapping')[0];
116         // const fullName = nameOfFile + ',' + key.split('.');
117         if (this.templateAndMappingMap.has(nameOfFile)) {
118             const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
119             !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
120             this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
121         } else {
122             this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
123         }
124
125     }
126
127     addTemplates() { }
128     setArtifact(predefined: boolean) {
129         if (predefined) {
130
131         } else {
132             this.currentFuncion.inputs['artifact-prefix-names'] = { get_input: 'template-prefix' };
133         }
134     }
135     addToInputs(optionalInput) {
136         this.requiredInputs.set(optionalInput, this.OptionalInputs.get(optionalInput));
137         this.OptionalInputs.delete(optionalInput);
138     }
139
140     setTemplate(file: string) {
141         if (this.selectedTemplates.has(file)) {
142             this.selectedTemplates.delete(file);
143         } else {
144             this.selectedTemplates.set(file, this.templateAndMappingMap.get(file));
145         }
146         console.log(this.selectedTemplates);
147     }
148
149     getKeys(map: Map<string, any>) {
150         return Array.from(map.keys());
151     }
152     getValue(file: string, map: Map<string, any>) {
153         return map.get(file);
154     }
155
156     getObjectKey(object) {
157         // console.log(object);
158         return Object.keys(object);
159     }
160     getObjectValue(object) {
161         return Object.values(object);
162     }
163     getNodeType(nodeName: string) {
164         this.functionStore.state$
165             .subscribe(state => {
166                 console.log(state);
167                 const functions = state.serverFunctions;
168                 // tslint:disable-next-line: prefer-for-of
169                 for (let i = 0; i < functions.length; i++) {
170                     if (functions[i].modelName === nodeName) {
171                         // tslint:disable: no-string-literal
172                         console.log(functions[i].definition['interfaces']);
173                         this.getInputFields(functions[i].definition['interfaces'], 'ResourceResolutionComponent', 'inputs');
174                         this.getInputFields(functions[i].definition['interfaces'], 'ResourceResolutionComponent', 'outputs');
175                         break;
176                     }
177                 }
178             });
179     }
180
181     getInputFields(interfaces, nodeName, type) {
182         console.log(interfaces[nodeName]['operations']['process'][type]);
183         const fields = interfaces[nodeName]['operations']['process'][type];
184
185         for (const [key, value] of Object.entries(fields)) {
186             if (key === 'artifact-prefix-names') {
187                 this.artifactPrefix = true;
188             } else if (value['required']) {
189                 console.log('This field is required = ' + key);
190                 if (type === 'inputs') {
191                     this.requiredInputs.set(key, Object.assign({}, value));
192                 } else {
193                     this.requiredOutputs.set(key, Object.assign({}, value));
194                 }
195             } else {
196                 console.log('This field is Optional ' + key);
197                 if (type === 'inputs') {
198                     this.OptionalInputs.set(key, Object.assign({}, value));
199                 } else {
200                     this.optionalOutputs.set(key, Object.assign({}, value));
201                 }
202             }
203         }
204
205         // console.log(this.requiredOutputs);
206     }
207
208
209 }