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} 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) {
77 public getTemplateVariable(fileContent: string) {
78 const variables: string[] = [];
79 const stringsSlittedByBraces = fileContent.split('${');
80 const stringsDefaultByDollarSignOnly = fileContent.split('"$');
82 for (let i = 1; i < stringsSlittedByBraces.length; i++) {
83 const element = stringsSlittedByBraces[i];
85 const firstElement = element.split('}')[0];
86 if (!variables.includes(firstElement)) {
87 variables.push(firstElement);
89 console.log(firstElement);
94 for (let i = 1; i < stringsDefaultByDollarSignOnly.length; i++) {
95 const element = stringsDefaultByDollarSignOnly[i];
96 if (element && !element.includes('$')) {
97 const firstElement = element.split('"')[0]
99 .replace('}', '').trim();
100 if (!variables.includes(firstElement)) {
101 variables.push(firstElement);
108 public dropped(files: NgxFileDropEntry[]) {
110 for (const droppedFile of files) {
111 // Is it a file? & Not added before
112 if (droppedFile.fileEntry.isFile && !this.fileNames.has(droppedFile.fileEntry.name)) {
113 const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
114 this.uploadedFiles.push(fileEntry);
115 this.fileNames.add(fileEntry.name);
122 if (this.allowedExt.includes('.csv')) {
125 this.setTemplateFilesToStore();
130 for (const droppedFile of this.uploadedFiles) {
131 droppedFile.file((file: File) => {
132 const fileReader = new FileReader();
133 fileReader.onload = (e) => {
134 this.variables = fileReader.result.toString().split(',');
135 console.log(this.variables);
136 this.getMappingTableFromTemplate(null);
138 fileReader.readAsText(file);
141 this.uploadedFiles = [];
144 private convertDictionaryToMap(resourceDictionaries: ResourceDictionary[]): Mapping[] {
145 const mapArray: Mapping[] = [];
146 for (const resourceDictionary of resourceDictionaries) {
147 this.MappingAdapter = new MappingAdapter(resourceDictionary);
148 mapArray.push(this.MappingAdapter.ToMapping());
150 console.log(mapArray);
154 setTemplateFilesToStore() {
155 for (const droppedFile of this.uploadedFiles) {
156 droppedFile.file((file: File) => {
157 const fileReader = new FileReader();
158 fileReader.onload = (e) => {
159 this.templateFileContent = fileReader.result.toString();
160 this.variables = this.getTemplateVariable(this.templateFileContent);
163 fileReader.readAsText(file);
166 this.uploadedFiles = [];
169 textChanges(code: any, fileName: string) {
170 // this.packageCreationStore.addTemplate(fileName, code);
171 this.templateFileContent = code;
174 public fileOver(event) {
178 public fileLeave(event) {
182 resetTheUploadedFiles() {
183 this.uploadedFiles = [];
187 this.showListViewParent.emit('tell parent to open create views');
190 getMappingTableFromTemplate(e) {
194 if (this.variables && this.variables.length > 0) {
196 this.packageCreationStore.getTemplateAndMapping(this.variables).subscribe(res => {
197 this.resourceDictionaryRes = res;
198 console.log(this.resourceDictionaryRes);
206 // Save Mapping to Store
207 if (this.resourceDictionaryRes && this.resourceDictionaryRes.length > 0) {
208 const mapArray = this.convertDictionaryToMap(this.resourceDictionaryRes);
209 this.packageCreationStore.addMapping('Templates/' + this.fileName + '-mapping.json',
210 this.packageCreationUtils.transformToJson(this.jsonConvert.serialize(mapArray)));
211 this.resourceDictionaryRes = [];
213 // Save Template to store
214 if (this.templateFileContent) {
215 this.packageCreationStore.addTemplate('Templates/' + this.fileName + '-template' + this.getFileExtension(),
216 this.templateFileContent);
217 this.templateFileContent = '';
225 if (this.dtElement.dtInstance) {
226 console.log('rerender');
227 this.dtElement.dtInstance.then((dtInstance: DataTables.Api) => {
228 dtInstance.destroy();
229 this.dtElement.dtOptions = this.dtOptions;
230 this.dtElement.dtTrigger.next();
234 this.dtTrigger.next();
238 ngOnDestroy(): void {
239 // Do not forget to unsubscribe the event
240 this.dtTrigger.unsubscribe();