372fbca0347887870de1fd463a3390b5a6186390
[ccsdk/cds.git] /
1 import { Component, EventEmitter, OnInit, Output } from '@angular/core';
2 import { PackageCreationStore } from '../../package-creation.store';
3 import { Mapping, Template } from '../../mapping-models/CBAPacakge.model';
4 import { TemplateInfo, TemplateStore } from '../../template.store';
5 import { TemplateAndMapping } from '../TemplateAndMapping';
6 import { ActivatedRoute } from '@angular/router';
7
8
9 @Component({
10     selector: 'app-templ-mapp-listing',
11     templateUrl: './templ-mapp-listing.component.html',
12     styleUrls: ['./templ-mapp-listing.component.css']
13 })
14 export class TemplMappListingComponent implements OnInit {
15     @Output() showCreationViewParentNotification = new EventEmitter<any>();
16     private templateAndMappingMap = new Map<string, TemplateAndMapping>();
17     private templates: Template;
18     private mapping: Mapping;
19     isCreate = true;
20
21     constructor(
22         private packageCreationStore: PackageCreationStore,
23         private templateStore: TemplateStore,
24         private route: ActivatedRoute
25     ) {
26     }
27
28     ngOnInit() {
29         if (this.route.snapshot.paramMap.has('id')) {
30             this.isCreate = false;
31         }
32         this.packageCreationStore.state$.subscribe(cba => {
33             if (cba.templates) {
34                 this.templates = cba.templates;
35                 this.mapping = cba.mapping;
36                 console.log(this.mapping);
37                 let templateAndMapping;
38                 this.templateAndMappingMap.clear();
39                 this.templates.files.forEach((value, key) => {
40                     templateAndMapping = new TemplateAndMapping();
41                     templateAndMapping.isTemplate = true;
42                     const isFromTemplate = true;
43                     this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
44                 });
45                 this.mapping.files.forEach((value, key) => {
46                     templateAndMapping = new TemplateAndMapping();
47                     templateAndMapping.isMapping = true;
48                     const isFromTemplate = false;
49                     this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
50                 });
51                 console.log('hello there ');
52                 console.log(this.templateAndMappingMap);
53             }
54         });
55     }
56
57     private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
58         const nameOfFile = key.split('/')[1].split('.')[0].split('-')[0];
59         // const fullName = nameOfFile + ',' + key.split('.');
60         if (this.templateAndMappingMap.has(nameOfFile)) {
61             const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
62             !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
63             this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
64         } else {
65             this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
66         }
67
68     }
69
70     openCreationView() {
71         this.showCreationViewParentNotification.emit('tell parent to open create views');
72     }
73
74     setSourceCodeEditor(key: string) {
75         const templateKey = 'Templates/' + key + '-template.vtl';
76         this.packageCreationStore.state$.subscribe(cba => {
77             console.log('cba ------');
78             console.log(cba);
79             console.log(key);
80             console.log(this.templateAndMappingMap);
81             if (cba.templates && cba.templates.files.has(templateKey)) {
82                 const fileContent = cba.templates.getValue(templateKey.trim());
83                 console.log(fileContent);
84                 const templateInfo = new TemplateInfo();
85                 templateInfo.fileContent = fileContent;
86                 templateInfo.fileName = templateKey;
87                 this.templateStore.changeTemplateInfo(templateInfo);
88             }
89             const mappingKey = 'Templates/' + key + '-mapping.json';
90             if (cba.mapping && cba.mapping.files.has(mappingKey)) {
91                 const obj = JSON.parse(cba.mapping.getValue(mappingKey));
92                 const templateInfo = new TemplateInfo();
93                 templateInfo.mapping = obj;
94                 templateInfo.fileName = mappingKey;
95                 templateInfo.type = 'mapping';
96                 this.templateStore.changeTemplateInfo(templateInfo);
97             }
98         });
99     }
100
101     getKeys(templateAndMappingMap: Map<string, TemplateAndMapping>) {
102         return Array.from(this.templateAndMappingMap.keys());
103     }
104
105     getValue(file: string) {
106         return this.templateAndMappingMap.get(file);
107     }
108
109 }