448899019f65afdaeb52eedff791545430b3f701
[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     currentFile: string;
21
22     constructor(
23         private packageCreationStore: PackageCreationStore,
24         private templateStore: TemplateStore,
25         private route: ActivatedRoute
26     ) {
27     }
28
29     ngOnInit() {
30         if (this.route.snapshot.paramMap.has('id')) {
31             this.isCreate = false;
32         }
33         this.packageCreationStore.state$.subscribe(cba => {
34             if (cba.templates) {
35                 this.templates = cba.templates;
36                 this.mapping = cba.mapping;
37                 console.log(this.mapping);
38                 let templateAndMapping;
39                 this.templateAndMappingMap.clear();
40                 this.templates.files.forEach((value, key) => {
41                     templateAndMapping = new TemplateAndMapping();
42                     templateAndMapping.isTemplate = true;
43                     const isFromTemplate = true;
44                     this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
45                 });
46                 this.mapping.files.forEach((value, key) => {
47                     templateAndMapping = new TemplateAndMapping();
48                     templateAndMapping.isMapping = true;
49                     const isFromTemplate = false;
50                     this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
51                 });
52                 console.log('hello there ');
53                 console.log(this.templateAndMappingMap);
54             }
55         });
56     }
57
58     private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
59         const nameOfFile = key.split('/')[1].split('.')[0].split('-')[0];
60         // const fullName = nameOfFile + ',' + key.split('.');
61         if (this.templateAndMappingMap.has(nameOfFile)) {
62             const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
63             !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
64             this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
65         } else {
66             this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
67         }
68
69     }
70
71     openCreationView() {
72         this.showCreationViewParentNotification.emit('tell parent to open create views');
73     }
74
75     setSourceCodeEditor(key: string) {
76         this.currentFile = key;
77         const templateKey = 'Templates/' + key + '-template.vtl';
78         this.packageCreationStore.state$.subscribe(cba => {
79             console.log('cba ------');
80             console.log(cba);
81             console.log(key);
82             console.log(this.templateAndMappingMap);
83             if (cba.templates && cba.templates.files.has(templateKey)) {
84                 const fileContent = cba.templates.getValue(templateKey.trim());
85                 console.log(fileContent);
86                 const templateInfo = new TemplateInfo();
87                 templateInfo.fileContent = fileContent;
88                 templateInfo.fileName = templateKey;
89                 this.templateStore.changeTemplateInfo(templateInfo);
90             }
91             const mappingKey = 'Templates/' + key + '-mapping.json';
92             if (cba.mapping && cba.mapping.files.has(mappingKey)) {
93                 const obj = JSON.parse(cba.mapping.getValue(mappingKey));
94                 const templateInfo = new TemplateInfo();
95                 templateInfo.mapping = obj;
96                 templateInfo.fileName = mappingKey;
97                 templateInfo.type = 'mapping';
98                 this.templateStore.changeTemplateInfo(templateInfo);
99             }
100         });
101     }
102
103     getKeys(templateAndMappingMap: Map<string, TemplateAndMapping>) {
104         return Array.from(this.templateAndMappingMap.keys());
105     }
106
107     getValue(file: string) {
108         return this.templateAndMappingMap.get(file);
109     }
110
111 }