2572651b5dd27103fc234f953812d5a215b921ed
[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 = [];
24     requiredOutputs = [];
25     OptionalInputs = [];
26     optionalOutputs = [];
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
92     setTemplate(file: string) {
93         if (this.selectedTemplates.has(file)) {
94             this.selectedTemplates.delete(file);
95         } else {
96             this.selectedTemplates.set(file, this.templateAndMappingMap.get(file));
97         }
98         console.log(this.selectedTemplates);
99     }
100
101     getKeys(templateAndMappingMap: Map<string, TemplateAndMapping>) {
102         return Array.from(templateAndMappingMap.keys());
103     }
104     getValue(file: string) {
105         return this.templateAndMappingMap.get(file);
106     }
107
108     getObjectKey(object) {
109         // console.log(object);
110         return Object.keys(object);
111     }
112     getObjectValue(object) {
113         return Object.values(object);
114     }
115     getNodeType(nodeName: string) {
116         this.functionStore.state$
117             .subscribe(state => {
118                 console.log(state);
119                 const functions = state.serverFunctions;
120                 // tslint:disable-next-line: prefer-for-of
121                 for (let i = 0; i < functions.length; i++) {
122                     if (functions[i].modelName === nodeName) {
123                         // tslint:disable: no-string-literal
124                         console.log(functions[i].definition['interfaces']);
125                         this.getInputFields(functions[i].definition['interfaces'], 'ResourceResolutionComponent', 'inputs');
126                         this.getInputFields(functions[i].definition['interfaces'], 'ResourceResolutionComponent', 'outputs');
127                         break;
128                     }
129                 }
130             });
131     }
132
133     getInputFields(interfaces, nodeName, type) {
134         console.log(interfaces[nodeName]['operations']['process'][type]);
135         const fields = interfaces[nodeName]['operations']['process'][type];
136
137         for (const [key, value] of Object.entries(fields)) {
138             const object = {};
139             object[key] = value;
140
141             if (key === 'artifact-prefix-names') {
142                 this.artifactPrefix = true;
143             } else if (value['required']) {
144                 console.log('This field is required = ' + key);
145                 if (type === 'inputs') {
146                     this.requiredInputs.push(Object.assign({}, object));
147                 } else {
148                     this.requiredOutputs.push(Object.assign({}, object));
149                 }
150             } else {
151                 console.log('This field is Optional ' + key);
152                 if (type === 'inputs') {
153                     this.OptionalInputs.push(Object.assign({}, object));
154                 } else {
155                     this.optionalOutputs.push(Object.assign({}, object));
156                 }
157             }
158         }
159
160         // console.log(this.requiredOutputs);
161     }
162
163
164 }