summaryrefslogtreecommitdiffstats
path: root/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/templ-mapp-listing/templ-mapp-listing.component.ts
blob: c790422c1389ed7dd11e5123af88b2ca9558db75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { Component, EventEmitter, OnInit, Output } from '@angular/core';
import { PackageCreationStore } from '../../package-creation.store';
import { Mapping, Template } from '../../mapping-models/CBAPacakge.model';
import { TemplateInfo, TemplateStore } from '../../template.store';
import { TemplateAndMapping } from '../TemplateAndMapping';
import { ActivatedRoute } from '@angular/router';
import { SharedService } from '../shared-service';


@Component({
    selector: 'app-templ-mapp-listing',
    templateUrl: './templ-mapp-listing.component.html',
    styleUrls: ['./templ-mapp-listing.component.css']
})
export class TemplMappListingComponent implements OnInit {
    @Output() showCreationViewParentNotification = new EventEmitter<any>();
    @Output() showFullView = new EventEmitter<any>();
    private templateAndMappingMap = new Map<string, TemplateAndMapping>();
    private templates: Template;
    private mapping: Mapping;
    isCreate = true;
    currentFile: string;
    edit = false;
    fileToDelete: any = {};

    constructor(
        private packageCreationStore: PackageCreationStore,
        private templateStore: TemplateStore,
        private route: ActivatedRoute,
        private sharedService: SharedService
    ) {
    }

    ngOnInit() {
        if (this.route.snapshot.paramMap.has('id')) {
            this.isCreate = false;
            this.sharedService.isEdit().subscribe(res => {
                this.edit = res;
            });

        }
        this.packageCreationStore.state$.subscribe(cba => {
            if (cba.templates) {
                this.templates = cba.templates;
                this.mapping = cba.mapping;
                console.log(this.mapping);
                let templateAndMapping;
                this.templateAndMappingMap.clear();
                this.templates.files.forEach((value, key) => {
                    templateAndMapping = new TemplateAndMapping();
                    templateAndMapping.isTemplate = true;
                    const isFromTemplate = true;
                    this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
                });
                this.mapping.files.forEach((value, key) => {
                    templateAndMapping = new TemplateAndMapping();
                    templateAndMapping.isMapping = true;
                    const isFromTemplate = false;
                    this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
                });
                console.log('hello there ');
                console.log(this.templateAndMappingMap);
            }
            this.deleteFromList();
        });
    }

    private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
        const nameOfFile = key.split('/')[1].split('.')[0].split('-')[0];
        // const fullName = nameOfFile + ',' + key.split('.');
        if (this.templateAndMappingMap.has(nameOfFile)) {
            const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
            !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
            this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
        } else {
            this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
        }

    }

    deleteFromList() {
        this.sharedService.listAction().subscribe(res => {
            console.log('response from actionList');
            console.log(res);
            if (res) {
                this.templateAndMappingMap.delete(res);
                if (this.templateAndMappingMap.size <= 0) {
                    this.openCreationView();
                }
            }
        });
    }

    openCreationView() {
        this.showCreationViewParentNotification.emit('tell parent to open create views');
        console.log('disable edit mode');
        this.sharedService.disableEdit();

    }
    FullView() {
        this.showFullView.emit('show full view');
    }

    setSourceCodeEditor(key: string) {
        this.currentFile = key;
        const templateKey = 'Templates/' + key + '-template.vtl';
        this.packageCreationStore.state$.subscribe(cba => {
            console.log('cba ------');
            console.log(cba);
            console.log(key);
            console.log(this.templateAndMappingMap);
            const templateInfo = new TemplateInfo();
            if (cba.templates && cba.templates.files.has(templateKey)) {
                const fileContent = cba.templates.getValue(templateKey.trim());
                console.log(fileContent);
                templateInfo.fileContent = fileContent;
                templateInfo.fileName = templateKey;
                templateInfo.type = 'template';
            }
            const mappingKey = 'Templates/' + key + '-mapping.json';
            if (cba.mapping && cba.mapping.files.has(mappingKey)) {
                const obj = JSON.parse(cba.mapping.getValue(mappingKey));
                templateInfo.mapping = obj;
                templateInfo.fileName = mappingKey;
                templateInfo.type += 'mapping';
            }
            this.templateStore.changeTemplateInfo(templateInfo);
            this.FullView();
            this.sharedService.enableEdit();
        });
    }

    getKeys(templateAndMappingMap: Map<string, TemplateAndMapping>) {
        return Array.from(this.templateAndMappingMap.keys());
    }

    getValue(file: string) {
        return this.templateAndMappingMap.get(file);
    }
    initDelete(file) {
        console.log(file);
        this.fileToDelete = file;
    }
    condifrmDelete() {
        console.log(this.templateAndMappingMap);
        this.templateAndMappingMap.delete(this.fileToDelete);
        if (this.templateAndMappingMap.size <= 0) {
            this.openCreationView();
        }
        // Delete from templates
        this.packageCreationStore.state.templates.files.delete('Templates/' + this.fileToDelete + '-template.vtl');
        // Delete from Mapping
        this.packageCreationStore.state.mapping.files.delete('Templates/' + this.fileToDelete + '-mapping.json');

    }

}