1 import { Component, EventEmitter, OnDestroy, OnInit, Output, ViewChild } 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';
14 selector: 'app-templ-mapp-creation',
15 templateUrl: './templ-mapp-creation.component.html',
16 styleUrls: ['./templ-mapp-creation.component.css']
18 export class TemplMappCreationComponent implements OnInit, OnDestroy {
19 @Output() showListViewParent = new EventEmitter<any>();
21 public uploadedFiles: FileSystemFileEntry[] = [];
22 private fileNames: Set<string> = new Set();
23 private jsonConvert = new JsonConvert();
24 public files: NgxFileDropEntry[] = [];
26 templateInfo = new TemplateInfo();
27 private variables: string[] = [];
28 dtOptions: DataTables.Settings = {};
29 // We use this trigger because fetching the list of persons can be quite long,
30 // thus we ensure the data is fetched before rendering
31 dtTrigger = new Subject();
32 resTableDtTrigger = new Subject();
33 resourceDictionaryRes: ResourceDictionary[] = [];
34 allowedExt = ['.vtl'];
35 @ViewChild(DataTableDirective, { static: false })
36 dtElement: DataTableDirective;
37 MappingAdapter: MappingAdapter;
39 templateFileContent: string;
40 templateExt = 'Velcoity';
41 dependancies = new Map<string, Array<string>>();
42 dependanciesSource = new Map<string, string>();
50 private packageCreationStore: PackageCreationStore,
51 private templateStore: TemplateStore,
52 private packageCreationUtils: PackageCreationUtils,
53 private toastr: ToastrService
58 this.templateStore.state$.subscribe(templateInfo => {
59 console.log('----------');
60 console.log(templateInfo);
61 this.templateInfo = templateInfo;
62 this.fileName = templateInfo.fileName.split('/')[1];
64 this.fileName = this.fileName.split('-')[0];
66 if (templateInfo.type === 'mapping') {
67 this.mappingRes = templateInfo.mapping;
68 this.currentMapping = templateInfo;
69 this.resourceDictionaryRes = [];
70 this.resTableDtTrigger.next();
73 this.templateFileContent = templateInfo.fileContent;
74 this.currentTemplate = templateInfo;
79 pagingType: 'full_numbers',
87 switch (this.templateExt) {
99 public getTemplateVariable(fileContent: string) {
100 const variables: string[] = [];
101 const stringsSlittedByBraces = fileContent.split('${');
102 const stringsDefaultByDollarSignOnly = fileContent.split('"$');
104 for (let i = 1; i < stringsSlittedByBraces.length; i++) {
105 const element = stringsSlittedByBraces[i];
107 const firstElement = element.split('}')[0];
108 if (!variables.includes(firstElement)) {
109 variables.push(firstElement);
111 console.log(firstElement);
116 for (let i = 1; i < stringsDefaultByDollarSignOnly.length; i++) {
117 const element = stringsDefaultByDollarSignOnly[i];
118 if (element && !element.includes('$')) {
119 const firstElement = element.split('"')[0]
121 .replace('}', '').trim();
122 if (!variables.includes(firstElement)) {
123 variables.push(firstElement);
130 public dropped(files: NgxFileDropEntry[]) {
132 for (const droppedFile of files) {
133 // Is it a file? & Not added before
134 if (droppedFile.fileEntry.isFile && !this.fileNames.has(droppedFile.fileEntry.name)) {
135 const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
136 this.uploadedFiles.push(fileEntry);
137 this.fileNames.add(fileEntry.name);
144 this.dependancies.clear();
145 this.dependanciesSource.clear();
146 if (this.allowedExt.includes('.csv')) {
149 this.setTemplateFilesToStore();
154 for (const droppedFile of this.uploadedFiles) {
155 droppedFile.file((file: File) => {
156 const fileReader = new FileReader();
157 fileReader.onload = (e) => {
158 this.variables = fileReader.result.toString().split(',');
159 console.log(this.variables);
160 this.getMappingTableFromTemplate(null);
162 fileReader.readAsText(file);
165 this.uploadedFiles = [];
168 private convertDictionaryToMap(resourceDictionaries: ResourceDictionary[]): Mapping[] {
169 const mapArray: Mapping[] = [];
170 for (const resourceDictionary of resourceDictionaries) {
171 this.MappingAdapter = new MappingAdapter(resourceDictionary, this.dependancies, this.dependanciesSource);
172 mapArray.push(this.MappingAdapter.ToMapping());
174 console.log(mapArray);
178 setTemplateFilesToStore() {
179 for (const droppedFile of this.uploadedFiles) {
180 droppedFile.file((file: File) => {
181 const fileReader = new FileReader();
182 fileReader.onload = (e) => {
183 this.templateFileContent = fileReader.result.toString();
184 this.variables = this.getTemplateVariable(this.templateFileContent);
187 fileReader.readAsText(file);
190 this.uploadedFiles = [];
193 textChanges(code: any, fileName: string) {
194 // this.packageCreationStore.addTemplate(fileName, code);
195 this.templateFileContent = code;
198 public fileOver(event) {
202 public fileLeave(event) {
206 resetTheUploadedFiles() {
207 this.uploadedFiles = [];
211 this.showListViewParent.emit('tell parent to open create views');
214 getMappingTableFromTemplate(e) {
215 this.resourceDictionaryRes = [];
219 if (this.variables && this.variables.length > 0) {
221 this.packageCreationStore.getTemplateAndMapping(this.variables).subscribe(res => {
222 this.mappingRes = [];
223 this.resourceDictionaryRes = res;
224 console.log(this.resourceDictionaryRes);
231 if (!this.dependanciesSource.has(key)) {
232 this.dependanciesSource.set(key, map.key);
238 this.templateFileContent = '';
239 this.resourceDictionaryRes = [];
240 this.mappingRes = [];
241 this.currentMapping = {};
242 this.currentTemplate = {};
246 // check file duplication
247 if (!(this.packageCreationStore.fileExist('Templates/' + this.fileName + '-mapping.json')
248 || this.packageCreationStore.fileExist('Templates/' + this.fileName + '-template' + this.getFileExtension()))) {
249 // Save Mapping to Store
250 if (this.resourceDictionaryRes && this.resourceDictionaryRes.length > 0) {
251 const mapArray = this.convertDictionaryToMap(this.resourceDictionaryRes);
252 this.packageCreationStore.addMapping('Templates/' + this.fileName + '-mapping.json',
253 this.packageCreationUtils.transformToJson(this.jsonConvert.serialize(mapArray)));
254 this.resourceDictionaryRes = [];
256 // Save Template to store
257 if (this.templateFileContent) {
258 this.packageCreationStore.addTemplate('Templates/' + this.fileName + '-template' + this.getFileExtension(),
259 this.templateFileContent);
260 this.templateFileContent = '';
263 this.toastr.success('File is created', 'success');
265 console.log('this file already exist');
266 this.toastr.error('File name already exist', 'Error');
269 this.toastr.error('Add the file name', 'Error');
274 selectSource(dict, e) {
275 const source = e.target.value;
276 let keyDepend = null;
278 keyDepend = dict.definition.sources[source].properties['key-dependencies'] || null;
283 this.dependancies.set(dict.name, keyDepend);
285 // this.dependancies.delete(dict.name);
286 // this.dependanciesSource.delete(dict.name);
288 this.dependanciesSource.set(dict.name, source);
289 console.log(this.dependancies);
290 console.log(this.dependanciesSource);
293 getKeys(map: Map<string, any>) {
294 return Array.from(map.keys());
298 return this.dependancies.get(key);
302 if (this.dtElement.dtInstance) {
303 console.log('rerender');
304 this.dtElement.dtInstance.then((dtInstance: DataTables.Api) => {
305 dtInstance.destroy();
306 this.dtElement.dtOptions = this.dtOptions;
307 this.dtElement.dtTrigger.next();
311 this.dtTrigger.next();
315 ngOnDestroy(): void {
316 // Do not forget to unsubscribe the event
317 this.dtTrigger.unsubscribe();
321 class DependancyVal {
328 this.source = source;
329 this.keyDepend = keyDepend;