1 import { Component, EventEmitter, OnInit, Output, OnDestroy, 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 { MappingAdapter, Mapping } from '../../mapping-models/mappingAdapter.model';
9 import { PackageCreationUtils } from '../../package-creation.utils';
10 import { JsonConvert } from 'json2typescript';
13 selector: 'app-templ-mapp-creation',
14 templateUrl: './templ-mapp-creation.component.html',
15 styleUrls: ['./templ-mapp-creation.component.css']
17 export class TemplMappCreationComponent implements OnInit, OnDestroy {
18 @Output() showListViewParent = new EventEmitter<any>();
20 public uploadedFiles: FileSystemFileEntry[] = [];
21 private fileNames: Set<string> = new Set();
22 private jsonConvert = new JsonConvert();
23 public files: NgxFileDropEntry[] = [];
25 templateInfo = new TemplateInfo();
26 private variables: string[] = [];
27 private mappingFileValues = [];
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 resourceDictionaryRes: ResourceDictionary[] = [];
33 allowedExt = ['.vtl'];
34 @ViewChild(DataTableDirective, { static: false })
35 dtElement: DataTableDirective;
36 MappingAdapter: MappingAdapter;
38 templateFileContent: string;
39 templateExt = 'Velcoity';
43 private packageCreationStore: PackageCreationStore,
44 private templateStore: TemplateStore,
45 private packageCreationUtils: PackageCreationUtils
50 this.templateStore.state$.subscribe(templateInfo => {
51 console.log(templateInfo);
52 this.templateInfo = templateInfo;
53 this.fileName = templateInfo.fileName.split('/')[1];
57 pagingType: 'full_numbers',
65 switch (this.templateExt) {
66 case 'Velcoity': return '.vtl';
67 case 'Koltin': return '.ktl';
68 case 'Jinja': return '.j2';
69 default: return '.vtl';
72 public getTemplateVariable(fileContent: string) {
73 const variables: string[] = [];
74 const stringsSlittedByBraces = fileContent.split('${');
75 const stringsDefaultByDollarSignOnly = fileContent.split('"$');
77 for (let i = 1; i < stringsSlittedByBraces.length; i++) {
78 const element = stringsSlittedByBraces[i];
80 const firstElement = element.split('}')[0];
81 if (!variables.includes(firstElement)) {
82 variables.push(firstElement);
84 console.log(firstElement);
89 for (let i = 1; i < stringsDefaultByDollarSignOnly.length; i++) {
90 const element = stringsDefaultByDollarSignOnly[i];
91 if (element && !element.includes('$')) {
92 const firstElement = element.split('"')[0]
94 .replace('}', '').trim();
95 if (!variables.includes(firstElement)) {
96 variables.push(firstElement);
103 public dropped(files: NgxFileDropEntry[]) {
105 for (const droppedFile of files) {
106 // Is it a file? & Not added before
107 if (droppedFile.fileEntry.isFile && !this.fileNames.has(droppedFile.fileEntry.name)) {
108 const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
109 this.uploadedFiles.push(fileEntry);
110 this.fileNames.add(fileEntry.name);
116 removeFile(fileIndex: number) {
117 /*const filename = 'Definitions/' + this.uploadedFiles[fileIndex].name;
118 this.packageCreationStore.removeFileFromDefinition(filename);
119 this.uploadedFiles.splice(fileIndex, 1);*/
123 if (this.allowedExt.includes('.csv')) {
126 this.setTemplateFilesToStore();
131 for (const droppedFile of this.uploadedFiles) {
132 droppedFile.file((file: File) => {
133 const fileReader = new FileReader();
134 fileReader.onload = (e) => {
135 this.variables = fileReader.result.toString().split(',');
136 console.log(this.variables);
137 this.getMappingTableFromTemplate(null);
139 fileReader.readAsText(file);
142 this.uploadedFiles = [];
145 private convertDictionaryToMap(resourceDictionaries: ResourceDictionary[]): Mapping[] {
146 const mapArray: Mapping[] = [];
147 for (const resourceDictionary of resourceDictionaries) {
148 this.MappingAdapter = new MappingAdapter(resourceDictionary);
149 mapArray.push(this.MappingAdapter.ToMapping());
151 console.log(mapArray);
155 setTemplateFilesToStore() {
156 for (const droppedFile of this.uploadedFiles) {
157 droppedFile.file((file: File) => {
158 const fileReader = new FileReader();
159 fileReader.onload = (e) => {
160 this.templateFileContent = fileReader.result.toString();
161 this.variables = this.getTemplateVariable(this.templateFileContent);
164 fileReader.readAsText(file);
167 this.uploadedFiles = [];
170 textChanges(code: any, fileName: string) {
171 // this.packageCreationStore.addTemplate(fileName, code);
172 this.templateFileContent = code;
175 public fileOver(event) {
179 public fileLeave(event) {
183 resetTheUploadedFiles() {
184 this.uploadedFiles = [];
188 this.showListViewParent.emit('tell parent to open create views');
191 getMappingTableFromTemplate(e) {
192 if (e) { e.preventDefault(); }
193 if (this.variables && this.variables.length > 0) {
195 this.packageCreationStore.getTemplateAndMapping(this.variables).subscribe(res => {
196 this.resourceDictionaryRes = res;
197 console.log(this.resourceDictionaryRes);
205 // Save Mapping to Store
206 if (this.resourceDictionaryRes) {
207 const mapArray = this.convertDictionaryToMap(this.resourceDictionaryRes);
208 this.packageCreationStore.addMapping('Templates/' + this.fileName + '-mapping.json',
209 this.packageCreationUtils.transformToJson(this.jsonConvert.serialize(mapArray)));
211 // Save Template to store
212 if (this.templateFileContent) {
213 this.packageCreationStore.addTemplate('Templates/' + this.fileName + '-template' + this.getFileExtension(),
214 this.templateFileContent);
222 if (this.dtElement.dtInstance) {
223 console.log('rerender');
224 this.dtElement.dtInstance.then((dtInstance: DataTables.Api) => {
225 dtInstance.destroy();
226 this.dtElement.dtOptions = this.dtOptions;
227 this.dtElement.dtTrigger.next();
231 this.dtTrigger.next();
235 ngOnDestroy(): void {
236 // Do not forget to unsubscribe the event
237 this.dtTrigger.unsubscribe();