4eee1f30cf98313e433220eb9d1e49bc4df8da41
[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
10 @Component({
11     selector: 'app-functions-attribute',
12     templateUrl: './functions-attribute.component.html',
13     styleUrls: ['./functions-attribute.component.css']
14 })
15 export class FunctionsAttributeComponent implements OnInit, OnDestroy {
16
17     ngUnsubscribe = new Subject();
18     designerDashboardState: DecodeSuccessCallback;
19     cbaPackage: CBAPackage;
20     templateAndMappingMap = new Map<string, TemplateAndMapping>();
21     selectedTemplates = new Map<string, TemplateAndMapping>();
22     fileToDelete: string;
23     requiredInputs = new Map<string, {}>();
24     requiredOutputs = new Map<string, {}>();
25     OptionalInputs = new Map<string, {}>();
26     optionalOutputs = new Map<string, {}>();
27     artifactPrefix = false;
28
29     constructor(
30         private designerStore: DesignerStore,
31         private packageCreationStore: PackageCreationStore,
32         private functionStore: FunctionsStore
33     ) {
34     }
35
36     ngOnInit() {
37         this.designerStore.state$
38             .pipe(
39                 distinctUntilChanged((a: any, b: any) => JSON.stringify(a) === JSON.stringify(b)),
40                 takeUntil(this.ngUnsubscribe))
41             .subscribe(designerDashboardState => {
42                 this.designerDashboardState = designerDashboardState;
43             });
44
45         this.packageCreationStore.state$
46             .subscribe(cbaPackage => {
47                 this.cbaPackage = cbaPackage;
48                 console.log('File name =>================== ');
49                 console.log(this.cbaPackage.templates.files);
50                 this.cbaPackage.templates.files.forEach((value, key) => {
51                     console.log('File name => ' + key);
52                     const templateAndMapping = new TemplateAndMapping();
53                     templateAndMapping.isTemplate = true;
54                     const isFromTemplate = true;
55                     this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
56                 });
57
58                 this.cbaPackage.mapping.files.forEach((value, key) => {
59                     const templateAndMapping = new TemplateAndMapping();
60                     templateAndMapping.isMapping = true;
61                     const isFromTemplate = false;
62                     this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
63                 });
64             });
65         this.getNodeType('component-resource-resolution');
66
67     }
68
69     ngOnDestroy() {
70         this.ngUnsubscribe.next();
71         this.ngUnsubscribe.complete();
72     }
73     // Template logic
74
75     private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
76         const nameOfFile = isFromTemplate ?
77             key.split('/')[1].split('.')[0].split('-template')[0]
78             : key.split('/')[1].split('.')[0].split('-mapping')[0];
79         // const fullName = nameOfFile + ',' + key.split('.');
80         if (this.templateAndMappingMap.has(nameOfFile)) {
81             const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
82             !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
83             this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
84         } else {
85             this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
86         }
87
88     }
89
90     addTemplates() { }
91     addToInputs(optionalInput) {
92         this.requiredInputs.set(optionalInput, this.OptionalInputs.get(optionalInput));
93         this.OptionalInputs.delete(optionalInput);
94     }
95
96     setTemplate(file: string) {
97         if (this.selectedTemplates.has(file)) {
98             this.selectedTemplates.delete(file);
99         } else {
100             this.selectedTemplates.set(file, this.templateAndMappingMap.get(file));
101         }
102         console.log(this.selectedTemplates);
103     }
104
105     getKeys(map: Map<string, any>) {
106         return Array.from(map.keys());
107     }
108     getValue(file: string, map: Map<string, any>) {
109         return map.get(file);
110     }
111
112     getObjectKey(object) {
113         // console.log(object);
114         return Object.keys(object);
115     }
116     getObjectValue(object) {
117         return Object.values(object);
118     }
119     getNodeType(nodeName: string) {
120         this.functionStore.state$
121             .subscribe(state => {
122                 console.log(state);
123                 const functions = state.serverFunctions;
124                 // tslint:disable-next-line: prefer-for-of
125                 for (let i = 0; i < functions.length; i++) {
126                     if (functions[i].modelName === nodeName) {
127                         // tslint:disable: no-string-literal
128                         console.log(functions[i].definition['interfaces']);
129                         this.getInputFields(functions[i].definition['interfaces'], 'ResourceResolutionComponent', 'inputs');
130                         this.getInputFields(functions[i].definition['interfaces'], 'ResourceResolutionComponent', 'outputs');
131                         break;
132                     }
133                 }
134             });
135     }
136
137     getInputFields(interfaces, nodeName, type) {
138         console.log(interfaces[nodeName]['operations']['process'][type]);
139         const fields = interfaces[nodeName]['operations']['process'][type];
140
141         for (const [key, value] of Object.entries(fields)) {
142             if (key === 'artifact-prefix-names') {
143                 this.artifactPrefix = true;
144             } else if (value['required']) {
145                 console.log('This field is required = ' + key);
146                 if (type === 'inputs') {
147                     this.requiredInputs.set(key, Object.assign({}, value));
148                 } else {
149                     this.requiredOutputs.set(key, Object.assign({}, value));
150                 }
151             } else {
152                 console.log('This field is Optional ' + key);
153                 if (type === 'inputs') {
154                     this.OptionalInputs.set(key, Object.assign({}, value));
155                 } else {
156                     this.optionalOutputs.set(key, Object.assign({}, value));
157                 }
158             }
159         }
160
161         // console.log(this.requiredOutputs);
162     }
163
164
165 }