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';
12 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 sharedService: SharedService
61 this.templateStore.state$.subscribe(templateInfo => {
62 // init Template&mapping vars
63 console.log('Oninit');
64 console.log(templateInfo);
65 this.templateInfo = templateInfo;
66 this.fileName = templateInfo.fileName.split('/')[1];
68 this.fileName = this.fileName.split('-')[0];
70 if (templateInfo.type === 'mapping' || templateInfo.type.includes('mapping')) {
71 this.mappingRes = templateInfo.mapping;
72 this.currentMapping = Object.assign({}, templateInfo);
73 this.resourceDictionaryRes = [];
74 this.resTableDtTrigger.next();
77 this.currentMapping = Any;
79 this.templateFileContent = templateInfo.fileContent;
80 this.currentTemplate = Object.assign({}, templateInfo);
82 if (templateInfo.type === 'template' || templateInfo.type.includes('template')) {
83 this.currentTemplate.fileName = 'Templates/' + this.fileName + '-template.vtl';
85 this.currentTemplate = Any;
90 this.sharedService.isEdit().subscribe(res => {
91 console.log('------------------------');
96 console.log('remove ----');
97 this.currentMapping = {};
98 this.currentTemplate = {};
100 this.templateFileContent = '';
101 this.resourceDictionaryRes = [];
102 this.mappingRes = [];
106 this.initDtOptions = {
107 pagingType: 'full_numbers',
113 pagingType: 'full_numbers',
121 switch (this.templateExt) {
133 public getTemplateVariable(fileContent: string) {
134 const variables: string[] = [];
135 const stringsSlittedByBraces = fileContent.split('${');
136 const stringsDefaultByDollarSignOnly = fileContent.split('"$');
138 for (let i = 1; i < stringsSlittedByBraces.length; i++) {
139 const element = stringsSlittedByBraces[i];
141 const firstElement = element.split('}')[0];
142 if (!variables.includes(firstElement)) {
143 variables.push(firstElement);
145 console.log(firstElement);
150 for (let i = 1; i < stringsDefaultByDollarSignOnly.length; i++) {
151 const element = stringsDefaultByDollarSignOnly[i];
152 if (element && !element.includes('$')) {
153 const firstElement = element.split('"')[0]
155 .replace('}', '').trim();
156 if (!variables.includes(firstElement)) {
157 variables.push(firstElement);
164 public dropped(files: NgxFileDropEntry[]) {
166 for (const droppedFile of files) {
167 // Is it a file? & Not added before
168 if (droppedFile.fileEntry.isFile) {
169 const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
170 this.uploadedFiles.push(fileEntry);
171 this.fileNames.add(fileEntry.name);
177 this.uploadedFiles.splice(index, 1);
181 this.dependancies.clear();
182 this.dependanciesSource.clear();
183 if (this.allowedExt.includes('.csv')) {
186 this.setTemplateFilesToStore();
188 $('.btn-cancel').click();
194 for (const droppedFile of this.uploadedFiles) {
195 droppedFile.file((file: File) => {
196 const fileReader = new FileReader();
197 fileReader.onload = (e) => {
198 this.variables = fileReader.result.toString().split(',');
199 console.log(this.variables);
200 this.getMappingTableFromTemplate(null);
203 fileReader.readAsText(file);
206 this.uploadedFiles = [];
209 private convertDictionaryToMap(resourceDictionaries: ResourceDictionary[]): Mapping[] {
210 const mapArray: Mapping[] = [];
211 for (const resourceDictionary of resourceDictionaries) {
212 this.MappingAdapter = new MappingAdapter(resourceDictionary, this.dependancies, this.dependanciesSource);
213 mapArray.push(this.MappingAdapter.ToMapping());
215 console.log(mapArray);
219 setTemplateFilesToStore() {
220 for (const droppedFile of this.uploadedFiles) {
221 droppedFile.file((file: File) => {
222 const fileReader = new FileReader();
223 fileReader.onload = (e) => {
224 this.templateFileContent = fileReader.result.toString();
225 this.variables = this.getTemplateVariable(this.templateFileContent);
228 fileReader.readAsText(file);
231 this.uploadedFiles = [];
234 textChanges(code: any, fileName: string) {
235 // this.packageCreationStore.addTemplate(fileName, code);
236 this.templateFileContent = code;
239 public fileOver(event) {
243 public fileLeave(event) {
247 resetTheUploadedFiles() {
248 this.uploadedFiles = [];
252 this.showListViewParent.emit('tell parent to open create views');
255 closeCreationForm() {
256 this.openList.emit('close create form and open list');
259 getMappingTableFromTemplate(e) {
260 console.log('-' + this.templateFileContent + '-');
261 this.resourceDictionaryRes = [];
265 if (this.variables && this.variables.length > 0) {
267 this.packageCreationStore.getTemplateAndMapping(this.variables).subscribe(res => {
268 let message = 'Attributes are Fetched';
269 this.mappingRes = [];
270 this.resourceDictionaryRes = res;
271 console.log(this.resourceDictionaryRes);
273 if (this.resourceDictionaryRes && this.resourceDictionaryRes.length <= 0) {
274 message = 'No values for those attributes';
276 this.toastr.success(message, 'Success');
278 this.toastr.error('Error');
284 if (!this.dependanciesSource.has(key)) {
285 this.dependanciesSource.set(key, map.key);
291 this.templateFileContent = '';
292 this.resourceDictionaryRes = [];
293 this.mappingRes = [];
294 this.currentMapping = {};
295 this.currentTemplate = {};
296 // this.closeCreationForm();
300 // check file duplication
301 console.log('----------- mode ' + this.edit);
303 (!(this.packageCreationStore.fileExist('Templates/' + this.fileName + '-mapping.json')
304 || this.packageCreationStore.fileExist('Templates/' + this.fileName + '-template' + this.getFileExtension())))
307 // Save Mapping to Store
308 if (this.resourceDictionaryRes && this.resourceDictionaryRes.length > 0) {
309 const mapArray = this.convertDictionaryToMap(this.resourceDictionaryRes);
310 this.packageCreationStore.addMapping('Templates/' + this.fileName + '-mapping.json',
311 this.packageCreationUtils.transformToJson(this.jsonConvert.serialize(mapArray)));
312 this.resourceDictionaryRes = [];
314 // Save Template to store
315 // if (this.templateFileContent) {
316 this.packageCreationStore.addTemplate('Templates/' + this.fileName + '-template' + this.getFileExtension(),
317 this.templateFileContent);
318 this.templateFileContent = '';
321 this.toastr.success('File is created', 'success');
322 this.closeCreationForm();
324 console.log('this file already exist');
325 this.toastr.error('File name already exist', 'Error');
328 this.toastr.error('Add the file name', 'Error');
333 selectSource(dict, e) {
334 const source = e.target.value;
335 let keyDepend = null;
337 keyDepend = dict.definition.sources[source].properties['key-dependencies'] || null;
342 this.dependancies.set(dict.name, keyDepend);
344 // this.dependancies.delete(dict.name);
345 // this.dependanciesSource.delete(dict.name);
347 this.dependanciesSource.set(dict.name, source);
348 console.log(this.dependancies);
349 console.log(this.dependanciesSource);
352 getKeys(map: Map<string, any>) {
353 return Array.from(map.keys());
357 return this.dependancies.get(key);
361 this.dtTrigger.next();
363 // if (this.dtElement.dtInstance) {
364 // console.log('rerender');
365 // this.dtElement.dtInstance.then((dtInstance: DataTables.Api) => {
366 // dtInstance.destroy();
367 // this.dtElement.dtOptions = this.dtOptions;
368 // this.dtElement.dtTrigger.next();
369 // dtInstance.draw();
372 // this.dtTrigger.next();
376 ngOnDestroy(): void {
377 // Do not forget to unsubscribe the event
378 this.dtTrigger.unsubscribe();
379 this.resTableDtTrigger.unsubscribe();
383 class DependancyVal {
390 this.source = source;
391 this.keyDepend = keyDepend;