1 import { Component, EventEmitter, OnDestroy, OnInit, Output, ViewChild, AfterViewInit, ElementRef } from '@angular/core';
2 import { FileSystemFileEntry, NgxFileDropEntry } from 'ngx-file-drop';
3 import { PackageCreationStore } from '../../package-creation.store';
4 import { TemplateInfo, TemplateStore } from '../../template.store';
5 import { Subject } from 'rxjs';
6 import { ResourceDictionary } from '../../mapping-models/ResourceDictionary.model';
7 import { DataTableDirective } from 'angular-datatables';
8 import { Mapping, MappingAdapter } from '../../mapping-models/mappingAdapter.model';
9 import { PackageCreationUtils } from '../../package-creation.utils';
10 import { JsonConvert, Any } from 'json2typescript';
11 import { ToastrService } from 'ngx-toastr';
12 import { Router, ActivatedRoute } from '@angular/router';
13 import { SharedService } from '../shared-service';
16 selector: 'app-templ-mapp-creation',
17 templateUrl: './templ-mapp-creation.component.html',
18 styleUrls: ['./templ-mapp-creation.component.css']
20 export class TemplMappCreationComponent implements OnInit, OnDestroy {
21 @Output() showListViewParent = new EventEmitter<any>();
22 @Output() openList = new EventEmitter<any>();
23 public uploadedFiles: FileSystemFileEntry[] = [];
24 private fileNames: Set<string> = new Set();
25 private jsonConvert = new JsonConvert();
26 public files: NgxFileDropEntry[] = [];
28 templateInfo = new TemplateInfo();
29 variables: string[] = [];
30 dtOptions: DataTables.Settings = {};
31 initDtOptions: DataTables.Settings = {};
32 // We use this trigger because fetching the list of persons can be quite long,
33 // thus we ensure the data is fetched before rendering
34 dtTrigger = new Subject();
35 resTableDtTrigger = new Subject();
36 resourceDictionaryRes: ResourceDictionary[] = [];
37 allowedExt = ['.vtl'];
38 @ViewChild(DataTableDirective, { static: false })
39 dtElement: DataTableDirective;
40 MappingAdapter: MappingAdapter;
42 templateFileContent: string;
43 templateExt = 'Velcoity';
44 dependancies = new Map<string, Array<string>>();
45 dependanciesSource = new Map<string, string>();
52 private packageCreationStore: PackageCreationStore,
53 private templateStore: TemplateStore,
54 private packageCreationUtils: PackageCreationUtils,
55 private toastr: ToastrService,
56 private router: ActivatedRoute,
57 private sharedService: SharedService
62 if (this.router.snapshot.paramMap.has('id')) {
63 console.log('URL contains Id');
64 this.sharedService.enableEdit();
66 this.templateStore.state$.subscribe(templateInfo => {
67 // init Template&mapping vars
68 console.log('Oninit');
69 console.log(templateInfo);
70 this.templateInfo = templateInfo;
71 this.fileName = templateInfo.fileName.split('/')[1];
73 this.fileName = this.fileName.split('-')[0];
75 if (templateInfo.type === 'mapping' || templateInfo.type.includes('mapping')) {
76 this.mappingRes = templateInfo.mapping;
77 this.currentMapping = Object.assign({}, templateInfo);
78 this.resourceDictionaryRes = [];
79 this.resTableDtTrigger.next();
82 this.currentMapping = Any;
84 this.templateFileContent = templateInfo.fileContent;
85 this.currentTemplate = Object.assign({}, templateInfo);
87 if (templateInfo.type === 'template' || templateInfo.type.includes('template')) {
88 this.currentTemplate.fileName = 'Templates/' + this.fileName + '-template.vtl';
90 this.currentTemplate = Any;
95 this.sharedService.isEdit().subscribe(res => {
96 console.log('------------------------');
101 console.log('remove ----');
102 this.currentMapping = {};
103 this.currentTemplate = {};
105 this.templateFileContent = '';
106 this.resourceDictionaryRes = [];
107 this.mappingRes = [];
111 this.initDtOptions = {
112 pagingType: 'full_numbers',
118 pagingType: 'full_numbers',
126 switch (this.templateExt) {
138 public getTemplateVariable(fileContent: string) {
139 const variables: string[] = [];
140 const stringsSlittedByBraces = fileContent.split('${');
141 const stringsDefaultByDollarSignOnly = fileContent.split('"$');
143 for (let i = 1; i < stringsSlittedByBraces.length; i++) {
144 const element = stringsSlittedByBraces[i];
146 const firstElement = element.split('}')[0];
147 if (!variables.includes(firstElement)) {
148 variables.push(firstElement);
150 console.log(firstElement);
155 for (let i = 1; i < stringsDefaultByDollarSignOnly.length; i++) {
156 const element = stringsDefaultByDollarSignOnly[i];
157 if (element && !element.includes('$')) {
158 const firstElement = element.split('"')[0]
160 .replace('}', '').trim();
161 if (!variables.includes(firstElement)) {
162 variables.push(firstElement);
169 public dropped(files: NgxFileDropEntry[]) {
171 for (const droppedFile of files) {
172 // Is it a file? & Not added before
173 if (droppedFile.fileEntry.isFile) {
174 const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
175 this.uploadedFiles.push(fileEntry);
176 this.fileNames.add(fileEntry.name);
183 this.dependancies.clear();
184 this.dependanciesSource.clear();
185 if (this.allowedExt.includes('.csv')) {
188 this.setTemplateFilesToStore();
193 for (const droppedFile of this.uploadedFiles) {
194 droppedFile.file((file: File) => {
195 const fileReader = new FileReader();
196 fileReader.onload = (e) => {
197 this.variables = fileReader.result.toString().split(',');
198 console.log(this.variables);
199 this.getMappingTableFromTemplate(null);
202 fileReader.readAsText(file);
205 this.uploadedFiles = [];
208 private convertDictionaryToMap(resourceDictionaries: ResourceDictionary[]): Mapping[] {
209 const mapArray: Mapping[] = [];
210 for (const resourceDictionary of resourceDictionaries) {
211 this.MappingAdapter = new MappingAdapter(resourceDictionary, this.dependancies, this.dependanciesSource);
212 mapArray.push(this.MappingAdapter.ToMapping());
214 console.log(mapArray);
218 setTemplateFilesToStore() {
219 for (const droppedFile of this.uploadedFiles) {
220 droppedFile.file((file: File) => {
221 const fileReader = new FileReader();
222 fileReader.onload = (e) => {
223 this.templateFileContent = fileReader.result.toString();
224 this.variables = this.getTemplateVariable(this.templateFileContent);
227 fileReader.readAsText(file);
230 this.uploadedFiles = [];
233 textChanges(code: any, fileName: string) {
234 // this.packageCreationStore.addTemplate(fileName, code);
235 this.templateFileContent = code;
238 public fileOver(event) {
242 public fileLeave(event) {
246 resetTheUploadedFiles() {
247 this.uploadedFiles = [];
251 this.showListViewParent.emit('tell parent to open create views');
254 closeCreationForm() {
255 this.openList.emit('close create form and open list');
258 getMappingTableFromTemplate(e) {
259 console.log('-' + this.templateFileContent + '-');
260 this.resourceDictionaryRes = [];
264 if (this.variables && this.variables.length > 0) {
266 this.packageCreationStore.getTemplateAndMapping(this.variables).subscribe(res => {
267 let message = 'Attributes are Fetched';
268 this.mappingRes = [];
269 this.resourceDictionaryRes = res;
270 console.log(this.resourceDictionaryRes);
272 if (this.resourceDictionaryRes && this.resourceDictionaryRes.length <= 0) {
273 message = 'No values for those attributes';
275 this.toastr.success(message, 'Success');
277 this.toastr.error('Error');
283 if (!this.dependanciesSource.has(key)) {
284 this.dependanciesSource.set(key, map.key);
290 this.templateFileContent = '';
291 this.resourceDictionaryRes = [];
292 this.mappingRes = [];
293 this.currentMapping = {};
294 this.currentTemplate = {};
295 // this.closeCreationForm();
299 // check file duplication
300 console.log('----------- mode ' + this.edit);
302 (!(this.packageCreationStore.fileExist('Templates/' + this.fileName + '-mapping.json')
303 || this.packageCreationStore.fileExist('Templates/' + this.fileName + '-template' + this.getFileExtension())))
306 // Save Mapping to Store
307 if (this.resourceDictionaryRes && this.resourceDictionaryRes.length > 0) {
308 const mapArray = this.convertDictionaryToMap(this.resourceDictionaryRes);
309 this.packageCreationStore.addMapping('Templates/' + this.fileName + '-mapping.json',
310 this.packageCreationUtils.transformToJson(this.jsonConvert.serialize(mapArray)));
311 this.resourceDictionaryRes = [];
313 // Save Template to store
314 // if (this.templateFileContent) {
315 this.packageCreationStore.addTemplate('Templates/' + this.fileName + '-template' + this.getFileExtension(),
316 this.templateFileContent);
317 this.templateFileContent = '';
320 this.toastr.success('File is created', 'success');
321 this.closeCreationForm();
323 console.log('this file already exist');
324 this.toastr.error('File name already exist', 'Error');
327 this.toastr.error('Add the file name', 'Error');
332 selectSource(dict, e) {
333 const source = e.target.value;
334 let keyDepend = null;
336 keyDepend = dict.definition.sources[source].properties['key-dependencies'] || null;
341 this.dependancies.set(dict.name, keyDepend);
343 // this.dependancies.delete(dict.name);
344 // this.dependanciesSource.delete(dict.name);
346 this.dependanciesSource.set(dict.name, source);
347 console.log(this.dependancies);
348 console.log(this.dependanciesSource);
351 getKeys(map: Map<string, any>) {
352 return Array.from(map.keys());
356 return this.dependancies.get(key);
360 this.dtTrigger.next();
362 // if (this.dtElement.dtInstance) {
363 // console.log('rerender');
364 // this.dtElement.dtInstance.then((dtInstance: DataTables.Api) => {
365 // dtInstance.destroy();
366 // this.dtElement.dtOptions = this.dtOptions;
367 // this.dtElement.dtTrigger.next();
368 // dtInstance.draw();
371 // this.dtTrigger.next();
375 ngOnDestroy(): void {
376 // Do not forget to unsubscribe the event
377 this.dtTrigger.unsubscribe();
381 class DependancyVal {
388 this.source = source;
389 this.keyDepend = keyDepend;