summaryrefslogtreecommitdiffstats
path: root/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/templ-mapp-creation/templ-mapp-creation.component.ts
blob: 4087dee3e9e7ce07d3f4f3d1bd0f91287b22774e (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
import { AfterViewInit, Component, EventEmitter, OnDestroy, OnInit, Output, ViewChild } from '@angular/core';
import { FileSystemFileEntry, NgxFileDropEntry } from 'ngx-file-drop';
import { PackageCreationStore } from '../../package-creation.store';
import { TemplateInfo, TemplateStore } from '../../template.store';
import { Subject } from 'rxjs';
import { ResourceDictionary } from '../../mapping-models/ResourceDictionary.model';
import { DataTableDirective } from 'angular-datatables';
import { Mapping, MappingAdapter } from '../../mapping-models/mappingAdapter.model';
import { PackageCreationUtils } from '../../package-creation.utils';
import { JsonConvert, Any } from 'json2typescript';
import { ToastrService } from 'ngx-toastr';
import { SharedService } from '../shared-service';
import { XmlParser } from '../utils/ParserFactory/XmlParser';
import { TourService } from 'ngx-tour-md-menu';
import { PackageCreationService } from '../../package-creation.service';
import { ParserFactory } from '../utils/ParserFactory/ParserFactory';
import { TemplateType, FileExtension } from '../utils/TemplateType';
import { MatPaginator, MatSort, MatTableDataSource } from '@angular/material';
import { ChangeDetectorRef } from '@angular/core';
declare var $: any;


@Component({
    selector: 'app-templ-mapp-creation',
    templateUrl: './templ-mapp-creation.component.html',
    styleUrls: ['./templ-mapp-creation.component.css']
})
export class TemplMappCreationComponent implements OnInit, OnDestroy, AfterViewInit {
    @Output() showListView = new EventEmitter<any>();
    @Output() showCreationView = new EventEmitter<any>();
    public uploadedFiles: FileSystemFileEntry[] = [];
    fileNames: Set<string> = new Set();
    jsonConvert = new JsonConvert();
    public files: NgxFileDropEntry[] = [];
    fileName: any;
    templateInfo = new TemplateInfo();
    variables: string[] = [];
    dtOptions: DataTables.Settings = {};
    initDtOptions: DataTables.Settings = {};
    // We use this trigger because fetching the list of persons can be quite long,
    // thus we ensure the data is fetched before rendering
    dtTrigger = new Subject();
    resTableDtTrigger = new Subject();
    resourceDictionaryRes: ResourceDictionary[] = [];
    allowedExt = ['.vtl'];
    @ViewChild(DataTableDirective, { static: false })
    dtElement: DataTableDirective;
    MappingAdapter: MappingAdapter;
    mapping = new Map();
    templateFileContent: string;
    templateExt = 'vtl';
    dependancies = new Map<string, Array<string>>();
    dependanciesSource = new Map<string, string>();
    mappingRes: Mapping[] = [];
    currentTemplate: any;
    currentMapping: any;
    edit = false;
    templatesExist = false;
    fileToDelete: any = {};
    parserFactory: ParserFactory;
    selectedProps: Set<string>;
    resColumns: string[] = [
        'Required', 'Template Input',
        'name', 'Dictionary Name',
        'dictionary-source', 'dependencies',
        'default', 'Velocity', 'Data Type',
        'Entry Schema'
    ];
    initColumn: string[] = ['select', ...this.resColumns];



    // displayedColumns: string[] = ['id', 'name', 'progress', 'color'];
    dataSource: MatTableDataSource<{}>;
    initDataSource: MatTableDataSource<{}>;
    @ViewChild(MatPaginator, { static: true }) initPaginator: MatPaginator;
    @ViewChild(MatSort, { static: true }) initSort: MatSort;

    constructor(
        private packageCreationStore: PackageCreationStore,
        private templateStore: TemplateStore,
        private packageCreationUtils: PackageCreationUtils,
        private toastr: ToastrService,
        private sharedService: SharedService,
        private packageCreationService: PackageCreationService,
        private tourService: TourService,
        private cdr: ChangeDetectorRef
    ) {
    }

    ngAfterViewInit() {
        try {
            this.initDataSource.paginator = this.initPaginator;
            this.initDataSource.sort = this.initSort;
        } catch (e) { }
    }

    ngOnInit() {

        this.selectedProps = new Set<string>();
        this.parserFactory = new ParserFactory();
        this.templateStore.state$.subscribe(templateInfo => {
            // init Template&mapping vars
            console.log('Oninit');
            console.log(templateInfo);
            this.templateInfo = templateInfo;
            this.fileToDelete = templateInfo.fileName;
            this.fileName = templateInfo.fileName.split('/')[1];
            if (this.fileName) {
                this.fileName = this.fileName.substr(0, this.fileName.lastIndexOf('-'));
            }
            if (templateInfo.type === 'mapping' || templateInfo.type.includes('mapping')) {
                console.log(templateInfo.mapping);
                this.mappingRes = templateInfo.mapping;
                this.currentMapping = Object.assign({}, templateInfo);
                this.resourceDictionaryRes = [];
                // Assign the data to the data source for the table to render
                this.rerender();
            } else {
                this.mappingRes = [];
                this.currentMapping = Any;
                this.resourceDictionaryRes = [];
            }
            this.templateFileContent = templateInfo.fileContent;
            this.templateExt = this.templateInfo.ext || this.templateExt;
            this.currentTemplate = Object.assign({}, templateInfo);

            if (templateInfo.type === 'template' || templateInfo.type.includes('template')) {
                console.log('template extension ' + this.templateExt);
                this.currentTemplate.fileName = 'Templates/' + this.fileName + '-template.' + this.templateExt;
                console.log(this.currentTemplate.fileName);
            } else {
                this.currentTemplate = Any;
            }

        });


        this.sharedService.isEdit().subscribe(res => {
            console.log('------------------------....');
            this.templatesExist = this.packageCreationStore.state.templates.files.size > 0
                || this.packageCreationStore.state.mapping.files.size > 0;
            console.log(res);
            this.edit = res;

            if (!this.edit) {
                console.log('remove ----');
                this.currentMapping = {};
                this.currentTemplate = {};
                this.fileName = '';
                this.templateFileContent = '';
                this.resourceDictionaryRes = [];
                this.mappingRes = [];
            }
        });

    }

    initApplyFilter(event: Event) {
        const filterValue = (event.target as HTMLInputElement).value;
        this.initDataSource.filter = filterValue.trim().toLowerCase();
        if (this.initDataSource.paginator) {
            this.initDataSource.paginator.firstPage();
        }
    }
    setProp(e, propName, index) {
        if (propName === 'input-param') {
            this.mappingRes[index][propName] = e.checked;

        } else {
            // tslint:disable-next-line: no-string-literal
            this.mappingRes[index]['property'][propName] = e.checked;
        }
        //  console.log(this.mappingRes[index]);
    }
    selectProp(value) {
        console.log(value);
        if (this.selectedProps.has(value)) {
            this.selectedProps.delete(value);
        } else {
            this.selectedProps.add(value);
        }
    }

    removeProps() {
        console.log(this.selectedProps);
        this.selectedProps.forEach(prop => {
            this.mappingRes.forEach((res, index) => {
                if (res.name === prop) {
                    console.log('delete...');
                    this.mappingRes.splice(index, 1);
                    this.selectedProps.delete(prop);
                    this.rerender();
                }
            });
        });
    }
    selectAllProps() {
        // if all items are already selected, unselect them
        if (this.mappingRes.length === this.selectedProps.size) {
            this.selectedProps = new Set<string>();
        } else {
            this.mappingRes.forEach(prop => {
                // console.log(prop);
                this.selectedProps.add(prop.name);
            });
        }

    }
    reMap() {
        let currentResDictionary = [];
        if (this.selectedProps && this.selectedProps.size > 0) {
            console.log('base');
            this.packageCreationService.getTemplateAndMapping([...this.selectedProps]).subscribe(res => {
                let message = 'Re-Auto mapping';
                //  this.mappingRes = [];
                currentResDictionary = this.convertDictionaryToMap(res);
                console.log(currentResDictionary);
                if (currentResDictionary && currentResDictionary.length <= 0) {
                    message = 'No values for those attributes';
                }

                // Replcae new values with the old ones
                currentResDictionary.forEach(curr => {
                    for (let i = 0; i < this.mappingRes.length; i++) {
                        if (this.mappingRes[i].name === curr.name) {
                            this.mappingRes[i] = curr;
                        }
                    }
                });
                this.rerender();
                this.toastr.success(message, 'Success');
                this.selectedProps = new Set();
            }, err => {
                this.toastr.error('Error');
            });
        }

    }

    getFileExtension() {
        switch (this.templateExt) {
            case 'vtl':
                return '.vtl';
            case 'kt':
                return '.ktl';
            case 'j2':
                return '.j2';
            default:
                return '.vtl';
        }
    }

    fileExtensionFromString(filename: string): string {
        const fileExtension = filename.substring(filename.lastIndexOf('.') + 1);
        return fileExtension;
    }

    public getTemplateVariable(fileContent: string) {
        // TODO: implement factory Pattern for parser
        console.log('start parsing........ ' + this.templateExt);
        const parser = this.parserFactory.getParser(fileContent, this.templateExt);
        return parser.getVariables(fileContent);
    }

    public dropped(files: NgxFileDropEntry[]) {
        this.files = files;
        for (const droppedFile of files) {
            // Is it a file? & Not added before
            if (droppedFile.fileEntry.isFile) {
                const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
                this.uploadedFiles.push(fileEntry);
                this.fileNames.add(fileEntry.name);

            }
        }
    }
    removeFile(index) {
        this.uploadedFiles.splice(index, 1);
    }

    confirmDelete() {
        // Delete from templates
        this.sharedService.deleteFromList(this.fileName);
        this.packageCreationStore.state.templates.files.delete(this.fileToDelete);
        // Delete from Mapping
        this.packageCreationStore.state.mapping.files.delete(this.fileToDelete);
        if (
            this.packageCreationStore.state.templates.files.size > 0 ||
            this.packageCreationStore.state.mapping.files.size > 0
        ) {
            this.openListView();
        }

    }
    uploadFile() {
        this.dependancies.clear();
        this.dependanciesSource.clear();
        if (this.allowedExt.includes('.csv') || this.allowedExt.includes('.xml')) {
            this.fetchkeysfromfile();
        } else {
            this.setTemplateFilesToStore();
        }
        $('.btn-cancel').click();
    }

    fetchkeysfromfile() {
        for (const droppedFile of this.uploadedFiles) {
            droppedFile.file((file: File) => {
                const fileReader = new FileReader();
                fileReader.onload = (e) => {
                    const fileExt = this.fileExtensionFromString(droppedFile.name);
                    if (fileExt === 'csv') {
                        this.variables = fileReader.result.toString().split(',');
                    } else {
                        const parser = new XmlParser();
                        this.variables = parser.getVariables(fileReader.result.toString());
                    }
                    console.log('variables = ' + this.variables);
                    this.getMappingTableFromTemplate(null);

                };
                fileReader.readAsText(file);
            });
        }
        this.uploadedFiles = [];
    }

    private convertDictionaryToMap(resourceDictionaries: ResourceDictionary[]): Mapping[] {
        const mapArray: Mapping[] = [];
        for (const resourceDictionary of resourceDictionaries) {
            this.MappingAdapter = new MappingAdapter(resourceDictionary, this.dependancies, this.dependanciesSource);
            mapArray.push(this.MappingAdapter.ToMapping());
        }
        console.log(mapArray);
        return mapArray;
    }

    private finalMapping(mappingArray: Mapping[]): Mapping[] {
        const mapArray: Mapping[] = [];
        for (const mapping of mappingArray) {
            this.MappingAdapter = new MappingAdapter(null, this.dependancies, this.dependanciesSource);
            mapArray.push(this.MappingAdapter.finalize(mapping));
            console.log(mapping);
        }
        console.log(mapArray);
        return mapArray;
    }

    setTemplateFilesToStore() {
        for (const droppedFile of this.uploadedFiles) {
            droppedFile.file((file: File) => {
                const fileReader = new FileReader();
                fileReader.onload = (e) => {
                    this.templateFileContent = fileReader.result.toString();
                    // this.variables = this.getTemplateVariable(this.templateFileContent);
                    // console.log(this.variables);

                };
                fileReader.readAsText(file);
            });
        }
        this.uploadedFiles = [];
    }

    textChanges(code: any, fileName: string) {
        //  this.packageCreationStore.addTemplate(fileName, code);
        // this.templateFileContent = code;
    }

    public fileOver(event) {
        console.log(event);
    }

    public fileLeave(event) {
        console.log(event);
    }
    //
    resetTheUploadedFiles() {
        this.uploadedFiles = [];
    }

    openListView() {
        console.log('open List view');
        this.showListView.emit('tell parent to open create views');
    }

    openCreationView() {
        console.log('close creation view');
        this.showCreationView.emit('close create form and open list');
    }

    identify(index, item) {
        return item.name;
    }
    setVelocity(index, value) {
        // tslint:disable-next-line: no-string-literal
        this.mappingRes[index].property['metadata'] = {
            'transform-template': value
        };
        console.log(this.resourceDictionaryRes[index]);
    }

    getMappingTableFromTemplate(e) {
        console.log('-' + this.templateFileContent + '-');
        this.resourceDictionaryRes = [];
        if (e) {
            e.preventDefault();
        }
        this.variables = this.getTemplateVariable(this.templateFileContent);
        console.log('variables = ' + this.variables);
        if (this.variables && this.variables.length > 0) {
            console.log('base');
            this.packageCreationService.getTemplateAndMapping(this.variables).subscribe(res => {
                let message = 'Attributes are Fetched';
                this.mappingRes = [];
                this.resourceDictionaryRes = res;
                console.log(this.resourceDictionaryRes);
                this.mappingRes = this.convertDictionaryToMap(this.resourceDictionaryRes);
                console.log(this.mappingRes);
                this.rerender();
                if (this.resourceDictionaryRes && this.resourceDictionaryRes.length <= 0) {
                    message = 'No values for those attributes';
                }
                this.toastr.success(message, 'Success');
            }, err => {
                this.toastr.error('Error');
            });
        } else {
            this.toastr.error('Empty or Invalid file format. Validate your file first');
        }
    }

    initMap(key, map) {
        if (!this.dependanciesSource.has(key)) {
            this.dependanciesSource.set(key, map.key);
        }
        return map.key;
    }
    clear() {
        this.fileName = '';
        this.templateFileContent = '';
        this.resourceDictionaryRes = [];
        this.mappingRes = [];
        this.currentMapping = {};
        this.currentTemplate = {};
        //   this.closeCreationForm();
    }
    cancel() {
        this.openListView();
    }

    objectToString(object) {
        if (object) {
            return JSON.stringify(object);
        }
        return '';
    }
    saveToStore() {
        const filename = this.fileName;
        if (filename) {
            // check file duplication
            console.log('----------- mode ' + this.edit);
            const fileContent = this.templateFileContent;
            if (
                (!(this.packageCreationStore.fileExist('Templates/' + filename + '-mapping.json')
                    || this.packageCreationStore.fileExist('Templates/' + filename + '-template' + this.getFileExtension())))
                || this.edit
            ) {
                // Save Mapping to Store
                if (this.mappingRes && this.mappingRes.length > 0) {
                    const mapArray = this.finalMapping(this.mappingRes);
                    console.log(mapArray);
                    //  this.packageCreationUtils.transformToJson(this.jsonConvert.serialize(mapArray)))
                    if (this.edit) {
                        this.packageCreationStore.addMapping('Templates/' + filename + '-mapping.json',
                            JSON.stringify(mapArray));
                    } else {
                        this.packageCreationStore.addMapping('Templates/' + filename + '-mapping.json',
                            this.packageCreationUtils.transformToJson(this.jsonConvert.serialize(mapArray)));
                    }
                    this.mappingRes = [];
                }
                // Save Template to store
                // if (this.templateFileContent) {
                this.packageCreationStore.addTemplate('Templates/' + filename + '-template' + this.getFileExtension(),
                    fileContent);
                this.templateFileContent = '';
                //  }
                this.fileName = '';
                this.toastr.success('File is created', 'success');
                this.openListView();
                if (localStorage.getItem('tour-guide') !== 'end' && localStorage.getItem('tour-guide') !== 'false') {
                    this.tourService.goto('tm-templateEdit');
                }
            } else {
                console.log('this file already exist');
                this.toastr.error('File name already exist', 'Error');
            }
        } else {
            this.toastr.error('Add the file name', 'Error');
        }
    }


    selectSource(dict, e) {
        const source = e.target.value;
        let keyDepend = null;
        this.dependancies.set(dict.name, null);
        try {
            keyDepend = dict.sources[source].properties['key-dependencies'] || null;
        } catch (e) { }
        console.log(dict);
        console.log(source);
        if (keyDepend) {
            this.dependancies.set(dict.name, keyDepend);
        } else {
            // this.dependancies.delete(dict.name);
            // this.dependanciesSource.delete(dict.name);
        }
        this.dependanciesSource.set(dict.name, source);
        console.log(this.dependancies);
        console.log(this.dependanciesSource);
    }

    getKeys(map: Map<string, any>) {
        return Array.from(map.keys());
    }

    getValue(key) {
        return this.dependancies.get(key);
    }

    rerender(): void {
        this.initDataSource = new MatTableDataSource(this.mappingRes);
        /*
        Hint: the intial page size for the table will be the result size; I did that because the table doesn't load element in DOM,
        as result some function are not working well like save and you have to move to other pages to fix that.
        */
        this.initPaginator.pageSize = this.mappingRes.length;
        this.initDataSource.paginator = this.initPaginator;
        this.initDataSource.sort = this.initSort;
    }

    ngOnDestroy(): void {
        // Do not forget to unsubscribe the event
        this.dtTrigger.unsubscribe();
        this.resTableDtTrigger.unsubscribe();
        // this.templateStore.unsubscribe();
    }
}