-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin-csv-generic-imexport.js
723 lines (673 loc) · 35.1 KB
/
plugin-csv-generic-imexport.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
function CSVGenericImportExportPlugin () {
this.prefix = this.constructor.name.toLowerCase();
this.csvdata = {};
this.mapping = {};
this.importable_entities = [];
this.importable_indexes = [];
this.modal_html = '';
this.mode = '';
this.filename = '';
this.preselected_status = [];
this.allowed_mimetypes = []
}
CSVGenericImportExportPlugin.prototype.log = function (msg, type, data) {
var type_str = '';
if (type != undefined) {
type_str = ' (' + type + ')';
}
var entry = this.constructor.name + type_str + ': ' + msg;
if (data != undefined) {
console.log(entry, data);
} else {
console.log(entry);
}
return this;
};
CSVGenericImportExportPlugin.prototype.init = function () {
var plugin = this;
// Set initial values
plugin.allowed_mimetypes = [
'text/csv',
'application/vnd.ms-excel'
];
// Render buttons, forms, etc.
plugin.render();
// Register event listener
$('#modals').on('change', '#' + plugin.prefix + '-file', function (){
$('#' + plugin.prefix + '-file-label').html(this.files[0].name);
plugin.filename = this.files[0].name;
plugin.loadCSV();
});
$('#modals').on('change', '#' + plugin.prefix + '-modal select', function (){
var sel = $(this);
var prop = sel.attr('id').substring(('#' + plugin.prefix).length);
var val = sel.val();
// set mapping and show parsed example value
if (val != '---') {
plugin.mapping[prop] = val;
sel.parent().next().html('<small class="text-success">' + plugin.csvdata.data[0][val] + '</small>');
} else {
delete plugin.mapping[prop];
sel.parent().next().empty();
}
});
$('#modals').on('change', '#' + plugin.prefix + '-id, #' + plugin.prefix + '-' + config.v.titleElement + ', #' + plugin.prefix + '-' + config.v.identifierElement + '', function (){
// If all required attributes are set, let's display the importable entities
var map_id = $('#' + plugin.prefix + '-id').val();
var map_title = $('#' + plugin.prefix + '-' + config.v.titleElement).val();
//var map_reference = $('#' + plugin.prefix + '-' + config.v.identifierElement).val();
var required_values = [map_id];
if (plugin.mode == 'import') {
required_values.push(map_title);
}
if (!required_values.includes('---')) {
// Settings form has to be rendered first, because updateMergables refers to those settings
plugin.renderSettingsForm();
switch (plugin.mode) {
case 'import':
plugin.renderEntitiesForm();
break;
case 'merge':
plugin.updateMergables();
break;
}
} else {
$('#' + plugin.prefix + '-form').empty();
$('#' + plugin.prefix + '-settings-form').empty();
plugin.importable_entities = [];
plugin.importable_indexes = [];
}
// Activate buttons
plugin.updateImportables();
});
$('#modals').on('change', '#' + plugin.prefix + '-settings-form-status input', function (e){
// Make sure button is not disabled
if (!$(this).hasClass('disabled') && plugin.mode == 'merge') {
plugin.updateMergables();
}
});
$('#modals').on('click', '#' + plugin.prefix + '-btn-import', function (e){
// Make sure button is not disabled
if (!$(this).hasClass('disabled')) {
plugin.importEntities(e);
}
});
$('#modals').on('click', '#' + plugin.prefix + '-btn-add', function (e){
// Make sure button is not disabled
if (!$(this).hasClass('disabled')) {
plugin.addEntities(e);
}
});
$('#modals').on('click', '#' + plugin.prefix + '-btn-merge', function (){
// Make sure button is not disabled
if (!$(this).hasClass('disabled')) {
plugin.mergeEntities().downloadCSV();
}
});
$('#modals').on('hidden.bs.modal', '#' + plugin.prefix + '-modal', function (e){
// Remove modal
$(this).remove();
});
plugin.log('Initialized.');
return plugin;
}
CSVGenericImportExportPlugin.prototype.render = function () {
var plugin = this;
// Create and register CSV import button
var btn_csv = $('<button class="btn btn-outline-light" id="btn-upload-' + this.prefix + '" type="button">\
<span class="fas fa-file-csv"></span> Import <span class="text-muted">from CSV</span> <span class="badge badge-light">generic</span>\
</button>');
btn_csv.on('click', function(e){
// Hide plugins
$('#app-content-plugins-area').collapse('hide');
// Render import modal and open
var title = '<span class="fas fa-file-csv"></span> Generic CSV Import';
var doc = 'With the <em>Generic CSV Import</em>-Plugin you can load your local data, stored in a comma separated value file (*.csv), into the app. Each row of your table-like data structure contains one dataset (all data according to one entity). The first row has to contain your column labels. You\'ll need them to map your data model to the configured app data model.';
plugin.mode = 'import';
plugin.preselected_status = ['unchecked'];
plugin
.renderModal(title, doc)
.renderModalFooter();
})
basicPluginActions.registerButton(btn_csv);
// Create and register CSV merge button
var btn_csv_merge = $('<button class="btn btn-outline-light" id="btn-merge-' + this.prefix + '" type="button">\
<span class="fas fa-file-csv"></span> Merge <span class="text-muted">with CSV</span> <span class="badge badge-light">generic</span>\
</button>');
btn_csv_merge.on('click', function(e){
// Hide plugins
$('#app-content-plugins-area').collapse('hide');
// Render merge modal and open
var title = '<span class="fas fa-file-csv"></span> Generic CSV Merge';
var doc = 'With the <em>Generic CSV Merge</em>-Plugin you can merge the app data with your local data (based on IDs), stored in a comma separated value file (*.csv) and download the result as (new) CSV-file. Each row of your table-like data structure contains one dataset (all data according to one entity). The first row has to contain your column labels. You\'ll need them to map your data model to the configured app data model.';
plugin.mode = 'merge';
plugin.preselected_status = ['safe'];
plugin
.renderModal(title, doc)
.renderModalFooter();
})
basicPluginActions.registerButton(btn_csv_merge);
return this;
}
CSVGenericImportExportPlugin.prototype.renderModal = function (title, documentation) {
this.modal_html = '<div class="modal fade" id="' + this.prefix + '-modal" tabindex="-1" aria-hidden="true" role="dialog">\
<div class="modal-dialog modal-lg" role="document">\
<div class="modal-content">\
<div class="modal-header">\
<h5 class="modal-title">' + title + '</h5>\
<button type="button" class="close" data-dismiss="modal" aria-label="Close">\
<span aria-hidden="true">✖</span>\
</button>\
</div>\
<div class="modal-body">\
<p>' + documentation + '</p>\
<form id="' + this.prefix + '-file-form">\
<div class="custom-file mb-2">\
<input type="file" class="custom-file-input" id="' + this.prefix + '-file">\
<label for="' + this.prefix + '-file" class="custom-file-label" id="' + this.prefix + '-file-label">CSV-file to load data from</label>\
</div>\
</form>\
<form id="' + this.prefix + '-mapping-form"></form>\
<form id="' + this.prefix + '-form"></form>\
<form id="' + this.prefix + '-settings-form"></form>\
</div>\
<div class="modal-footer">\
</div>\
</div>\
</div>\
</div>';
$(this.modal_html)
.appendTo('#modals')
.modal('show');
return this;
}
CSVGenericImportExportPlugin.prototype.renderModalFooter = function () {
switch (this.mode) {
case 'import':
var buttons = '<button type="button" class="btn btn-primary disabled" id="' + this.prefix + '-btn-import">Import <span class="badge badge-light found-csv-objects">0</span> objects</button>\
<button type="button" class="btn btn-primary disabled" id="' + this.prefix + '-btn-add">Add <span class="badge badge-light found-csv-objects">0</span> objects</button>\
';
$('#' + this.prefix + '-modal .modal-footer').append(buttons);
break;
case 'merge':
var buttons = '<button type="button" class="btn btn-primary disabled" id="' + this.prefix + '-btn-merge">Merge <span class="badge badge-light found-csv-objects">0</span> objects</button>\
';
$('#' + this.prefix + '-modal .modal-footer').append(buttons);
break;
}
return this;
}
CSVGenericImportExportPlugin.prototype.loadCSV = function () {
var plugin = this;
var btn_import = $('#' + plugin.prefix + '-btn-import');
var btn_add = $('#' + plugin.prefix + '-btn-add');
var btn_merge = $('#' + plugin.prefix + '-btn-merge');
var count_span = $('#' + plugin.prefix + '-modal .found-csv-objects');
var file_form = $('#' + this.prefix + '-file-form');
var file_input = $('#' + this.prefix + '-file');
var mapping_form = $('#' + this.prefix + '-mapping-form');
var entities_form = $('#' + this.prefix + '-form');
var settings_form = $('#' + this.prefix + '-settings-form');
var file = document.querySelector('#' + this.prefix + '-file').files[0];
// Clear possible further validation results
file_form.find('.is-valid, .is-invalid').removeClass('is-valid is-invalid');
file_form.find('.invalid-feedback, .valid-feedback').remove();
mapping_form.empty();
entities_form.empty();
settings_form.empty();
plugin.mapping = {};
plugin.importable_entities = [];
plugin.importable_indexes = [];
count_span.html('0');
btn_import.addClass('disabled');
btn_add.addClass('disabled');
btn_merge.addClass('disabled');
if (file) {
if (plugin.allowed_mimetypes.includes(file.type)) {
Papa.parse(file, {
header: true,
skipEmptyLines: true,
complete: function(results) {
plugin.log('File ' + file.name + ' loaded.');
if (results.errors.length == 0) {
if (results.data.length > 0) {
// Save parsed data
plugin.csvdata = results;
// At least one data row was detected
var msg = 'Parsed ' + results.data.length + ' data row(s) from CSV file.';
plugin.log(msg);
// Bootstrap form validation
file_input
.after('<div class="valid-feedback">' + msg + '</div>')
.addClass('is-valid');
// Build mapping interface
plugin.renderMappingForm();
} else {
// There is no data to process. User should choose another file.
var msg = 'The file contains no processable data. Please choose a file, which at least has one data row.';
plugin.log(msg, 'INFO');
// Bootstrap form validation
file_input
.after('<div class="invalid-feedback">' + msg + '</div>')
.addClass('is-invalid');
}
} else {
// There were parsing errors.
var msg = 'There are parsing errors. View JavaScript console for more information.';
plugin.log(msg, 'ERROR', results.errors);
// Bootstrap form validation
file_input
.after('<div class="invalid-feedback">' + msg + '</div>')
.addClass('is-invalid');
}
}
});
} else {
// Wrong filetype: abort
var msg = 'Wrong file type "' + file.type + '" detected. Please choose a CSV file (text/csv).';
plugin.log(msg, 'ERROR');
// Bootstrap form validation
file_input
.after('<div class="invalid-feedback">' + msg + '</div>')
.addClass('is-invalid');
}
}
return plugin;
}
CSVGenericImportExportPlugin.prototype.renderMappingForm = function () {
var plugin = this;
var mapping_form = $('#' + plugin.prefix + '-mapping-form');
var map_form_description_html ='<small class="form-text">Configure value mapping: <span class="text-muted">Please choose your local corresponding label for each attribute you want to import/export (at least the required ones). To give you a little help, the mapping form will show you an example value next to the choosen label, which is extracted from your first data row.</span></small>';
var required_attributes = ['id'];
if (plugin.mode == 'import') {
required_attributes.push(config.v.titleElement);
}
mapping_form.append(map_form_description_html);
required_attributes.forEach(function (a) {
mapping_form.append('<div class="form-group row">\
<label for="' + plugin.prefix + '-' + a + '" class="col-sm-4 col-form-label text-right"><small>' + a + '*</small></label>\
<div class="col-sm-4">\
<select id="' + plugin.prefix + '-' + a + '" class="form-control form-control-sm">\
</select>\
</div>\
<div class="col-sm-4 mapping-example-value text-truncate"></div>\
</div>');
});
mapping_form.append('<hr/>');
// As identifier elements are not part of the configurable modal and no required attributes
// we need to add it manually here
var id_conf = [{
displayName: 'Identifier', // Maybe this could be configurable?
localJSONPath: config.v.identifierElement
}];
// Create further mappable selects from config
var attributes = id_conf.concat(config.m);
attributes.forEach(function (e) {
if (e.localJSONPath && !required_attributes.includes(e.localJSONPath)) {
var select_html = '<div class="form-group row">\
<label for="' + plugin.prefix + '-' + e.localJSONPath + '" class="col-sm-4 col-form-label text-right"><small>' + e.displayName + '</small></label>\
<div class="col-sm-4">\
<select id="' + plugin.prefix + '-' + e.localJSONPath + '" class="form-control form-control-sm">\
</select>\
</div>\
<div class="col-sm-4 mapping-example-value text-truncate"></div>\
</div>\
';
mapping_form.append(select_html);
}
});
// Add select options
var select_elements = mapping_form.find('select')
select_elements.append('<option selected>---</option>')
this.csvdata.meta.fields.forEach(function (field) {
select_elements.append('<option>' + field + '</option>')
});
return this;
}
CSVGenericImportExportPlugin.prototype.renderEntitiesForm = function () {
var plugin = this;
var entities_form = $('#' + this.prefix + '-form');
var btn_import = $('#' + plugin.prefix + '-btn-import');
var btn_add = $('#' + plugin.prefix + '-btn-add');
entities_form.empty();
plugin.importable_indexes = [];
// Add importable entities to import form with filter buttons
var chk_button_filter = '<div class="btn-group form-group" role="group">\
<button class="btn btn-sm btn-secondary" id="' + plugin.prefix + '-import-csv-btn-chk-all" type="button">Select All</button>\
<button class="btn btn-sm btn-secondary" id="' + plugin.prefix + '-import-csv-btn-chk-none" type="button">Deselect All</button>\
</div>';
$(chk_button_filter).appendTo(entities_form);
var entities_btn_group = $('<div class="form-group"></div>').appendTo(entities_form);
// Import rows with no empty id and title column only. Filter data accordingly
plugin.importable_entities = plugin.csvdata.data.filter(function (r) {
return r[plugin.mapping['id']].trim() != '' && r[plugin.mapping[config.v.titleElement]].trim() != ''
});
plugin.importable_entities.forEach(function (e, i) {
// Add importable index to importable_indexes
plugin.importable_indexes.push(e[plugin.mapping['id']]);
// Build entity selection
var ref_html = '';
if (e[plugin.mapping['id']].trim()) {
ref_html += ' <span class="badge badge-warning">' + e[plugin.mapping['id']].trim() + '</span>';
}
if (e[plugin.mapping[config.v.identifierElement]]) {
var id_plain = e[plugin.mapping[config.v.identifierElement]];
const id_plain_processed = getPlainIdFromUrl(id_plain);
if (id_plain_processed !== null) {
console.log(id_plain, e[plugin.mapping[config.v.identifierElement]])
id_plain = id_plain_processed
}
ref_html += ' <span class="badge badge-dark">' + config.v.identifierAbbreviation + ': ' + id_plain + '</span>';
}
var chk_html = '<div class="form-check form-check-inline">\
<input class="form-check-input" type="checkbox" value="' + e[plugin.mapping['id']] + '" id="' + plugin.prefix + '-import-entitiy-' + i + '" checked>\
<label class="form-check-label" for="' + plugin.prefix + '-import-entitiy-' + i + '">\
' + e[plugin.mapping[config.v.titleElement]] + ref_html +'\
</label>\
</div>';
$(chk_html).appendTo(entities_btn_group);
})
// Register event listener
$('#modals').on('click', '#' + plugin.prefix + '-import-csv-btn-chk-all', function(){
$('#' + plugin.prefix + '-form input[type="checkbox"]').prop('checked', true).trigger('change');
});
$('#modals').on('click', '#' + plugin.prefix + '-import-csv-btn-chk-none', function(){
$('#' + plugin.prefix + '-form input[type="checkbox"]').prop('checked', false).trigger('change');
});
$('#modals').on('change', '#' + plugin.prefix + '-form input[type="checkbox"]', function (e) {plugin.updateImportables(e)} );
return plugin;
}
CSVGenericImportExportPlugin.prototype.renderSettingsForm = function (event) {
var plugin = this;
var settings_form = $('#' + this.prefix + '-settings-form');
settings_form.empty();
// METHOD
var method_description = $('<small class="form-text">Select merging method. <span class="text-muted">Hard mode will replace existing values in CSV with the values of the object. The soft mode only adds values where there is no one and leave existing values in CSV unchanged.</span></small>');
var method_buttons = $('<div class="btn-group btn-group-sm btn-group-toggle mb-2" data-toggle="buttons"></div>')
.append('<label class="btn btn-secondary active">\
<input type="radio" name="csv-merge-method" value="hard" autocomplete="off" checked> Hard\
</label>')
.append('<label class="btn btn-secondary">\
<input type="radio" name="csv-merge-method" value="soft" autocomplete="off"> Soft\
</label>')
// STATUS
var status_description_import = $('<small class="form-text">Select status. <span class="text-muted">The selected status will be set on all imported/added entities.</span></small>');
var status_description_merge = $('<small class="form-text">Select status to merge. <span class="text-muted">Only objects with the selected status will be merged. Multi-selection is possible.</span></small>');
var status_buttons = $('<div id="' + plugin.prefix + '-settings-form-status" class="btn-group btn-group-sm btn-group-toggle mb-2" data-toggle="buttons"></div>')
var status_buttons_type = 'radio';
if (plugin.mode == 'merge') {
status_buttons_type = 'checkbox';
}
config.status.available.forEach(function (status) {
if (plugin.preselected_status.includes(status)) {
status_buttons.append('<label class="btn btn-secondary active">\
<input type="' + status_buttons_type + '" name="csv-status" value="' + status + '" autocomplete="off" checked>\
' + status + '\
</label>')
} else {
status_buttons.append('<label class="btn btn-secondary">\
<input type="' + status_buttons_type + '" name="csv-status" value="' + status + '" autocomplete="off">\
' + status + '\
</label>')
}
});
// DELIMITER
var delimiter_description = ('<small class="form-text">Select delimiter. <span class="text-muted">This character will be used to seperate multiple values in one cell, e.g. variant names. Ensure it\'s not the same character used for cell seperation in your CSV-file.</span></small>');
var delimiter_buttons = $('<div class="btn-group btn-group-sm btn-group-toggle mb-2" data-toggle="buttons"></div>')
.append('<label class="btn btn-secondary active">\
<input type="radio" name="csv-delimiter" value="|" autocomplete="off" checked> |\
</label>')
.append('<label class="btn btn-secondary">\
<input type="radio" name="csv-delimiter" value="@" autocomplete="off"> @\
</label>')
// MODE
var mode_description = $('<small class="form-text">Select mode of identifier format. <span class="text-muted">Plain mode will save the IDs and URI-mode will save URIs.</span></small>');
var mode_buttons = $('<div class="btn-group btn-group-sm btn-group-toggle mb-2" data-toggle="buttons"></div>')
.append('<label class="btn btn-secondary active">\
<input type="radio" name="csv-merge-mode" value="plain" autocomplete="off" checked> Plain\
</label>')
.append('<label class="btn btn-secondary">\
<input type="radio" name="csv-merge-mode" value="uri" autocomplete="off"> URI\
</label>')
// Render settings according to the current mode
switch (plugin.mode) {
case 'import':
settings_form
.append(status_description_import)
.append(status_buttons)
.append(delimiter_description)
.append(delimiter_buttons);
break;
case 'merge':
settings_form
.append(method_description)
.append(method_buttons)
.append(status_description_merge)
.append(status_buttons)
.append(delimiter_description)
.append(delimiter_buttons)
.append(mode_description)
.append(mode_buttons);
break;
}
return plugin;
}
CSVGenericImportExportPlugin.prototype.updateImportables = function (event) {
var indexes = this.importable_indexes;
if (event != undefined) {
var target_chk = event.target;
if(target_chk.checked){
if(!indexes.includes(target_chk.value)){
indexes.push(target_chk.value);
}
} else {
if(indexes.includes(target_chk.value)){
var idx_ie = indexes.indexOf(target_chk.value);
indexes.splice(idx_ie, 1);
}
}
}
// update counter on import and add button
$('#' + this.prefix + '-modal .found-csv-objects').html(indexes.length);
// enable/disable import and add button according to amount of importables
if (indexes.length > 0) {
$('#' + this.prefix + '-btn-import').removeClass('disabled');
$('#' + this.prefix + '-btn-add').removeClass('disabled');
$('#' + this.prefix + '-btn-merge').removeClass('disabled');
} else {
$('#' + this.prefix + '-btn-import').addClass('disabled');
$('#' + this.prefix + '-btn-add').addClass('disabled');
$('#' + this.prefix + '-btn-merge').addClass('disabled');
}
return this;
}
CSVGenericImportExportPlugin.prototype.updateMergables = function () {
// Get selected status and count according objects
var settings_form = $('#' + this.prefix + '-settings-form');
var settings_array = settings_form.serializeArray();
var status = [];
settings_array.forEach(function (e) {
if (e.name == 'csv-status') {
status.push(e.value);
}
});
this.importable_indexes = asArray(data_objects[config.a.JSONContainer])
.filter(function (e) {
return status.includes(e[config.v.statusElement])
})
.map(function (e, i) {
return i;
});
this.updateImportables();
return this;
}
/*
importEntities: Delete existing entities and add new ones
*/
CSVGenericImportExportPlugin.prototype.importEntities = function(event) {
// Delete existing objects
var ids_to_delete = asArray(data_objects[config.a.JSONContainer]).map(obj => obj.id);
this.log('Deleting ' + ids_to_delete.length + ' data objects.');
ids_to_delete.forEach(function (id) {
deleteObject(event.target, id);
});
this.log('Existing data objects deleted.');
// Add all new entities
this.addEntities(event);
return this;
}
CSVGenericImportExportPlugin.prototype.addEntities = function(event) {
var plugin = this;
var ii = plugin.importable_indexes;
var settings_form = $('#' + plugin.prefix + '-settings-form');
var status = settings_form.serializeArray().find(ipt => ipt.name == 'csv-status').value;
var delimiter = settings_form.serializeArray().find(ipt => ipt.name == 'csv-delimiter').value;
plugin.log('Imported data is set to the status: "' + status + '".');
plugin.log('Adding ' + ii.length + ' objects ...');
plugin.importable_entities.forEach(function (e) {
if (ii.includes(e[plugin.mapping['id']])) {
// Set params for new local object
var params = {};
// Set required mapped params: id, title, reference and status
params['id'] = e[plugin.mapping['id']];
params[config.v.titleElement] = e[plugin.mapping[config.v.titleElement]];
// Check if we already have references set, which we can import.
var id = e[plugin.mapping[config.v.identifierElement]];
if (id !== undefined && id.trim() !== '' && (!id.toLowerCase().startsWith('http') || getPlainIdFromUrl(id) !== null)) {
// It should be possible to import plain IDs. So if there is an ID, not starting with the configured
// base URL, add them to the importable value. If there is another URI (starting with http), ignore the value
if (!id.toLowerCase().startsWith('http')) {
id = getUrlFromPlainId(id.trim());
}
// TODO: this structure must be configurable and should not be fixed in the code, because this is
// specific to the exist-db JSON export.
params[config.v.identifierElement] = {
'#text': id,
'preferred': 'YES'
};
}
params[config.v.statusElement] = status;
// Add all other mapped values
Object.keys(plugin.mapping).forEach(function (key) {
if (!['id', config.v.titleElement, config.v.identifierElement, config.v.statusElement].includes(key) && e[plugin.mapping[key]].trim()) {
// Check if attribute is configured as multiple. If so, use choosen delimiter.
var app_mapping_config = config.m.find(function (o) {
return o.localJSONPath == key;
});
if (app_mapping_config != undefined && app_mapping_config.multiple == true) {
// Split values with delimiter and add array
params[key] = e[plugin.mapping[key]].trim().split(delimiter);
} else {
params[key] = e[plugin.mapping[key]].trim();
}
}
});
addObject(event.target, params)
}
});
plugin.log('Import finished.');
// Hide the modal dialog, it will be reseted automatically by event hidden.bs.modal
$('#' + plugin.prefix + '-modal').modal('hide');
return plugin;
}
CSVGenericImportExportPlugin.prototype.mergeEntities = function() {
var plugin = this;
// Get settings from form
var updated_rows = updated_cells = 0;
var settings_form = $('#' + plugin.prefix + '-settings-form');
var settings_array = settings_form.serializeArray();
var method = '';
var status = [];
var delimiter = '';
var mode = '';
settings_array.forEach(function (e) {
if (e.name == 'csv-merge-method') {
method = e.value;
} else if (e.name == 'csv-status') {
status.push(e.value);
} else if (e.name == 'csv-delimiter') {
delimiter = e.value;
} else if (e.name == 'csv-merge-mode') {
mode = e.value;
}
});
plugin.log('Merging data ' + method + 'ly ...');
// Merging local data into CSV data based on IDs
// Loop through parsed data row
plugin.csvdata.data.forEach(function (row, index) {
// Get ID from mapped attribute
var id = row[plugin.mapping['id']].trim();
if (id != '') {
// Get local object with ID
var obj = getLocalObjectById(id);
// Check if object is in correct state
if (obj !== undefined && status.includes(obj[config.v.statusElement])) {
var updated = [];
// Loop through all other mapped values
Object.keys(plugin.mapping).forEach(function (key) {
switch (key) {
case 'id':
// Nothing to do here
break;
case config.v.identifierElement:
// Get preferred ID from local object
var preferred_id = getPreferredIdentifierFromObject(obj);
// if plain mode is choosen, remove base URL from preferred ID
if (mode == 'plain' && preferred_id) { preferred_id = getPlainIdFromUrl(preferred_id) }
// React according to choosen method
if (preferred_id != null && (method == 'hard' || (method == 'soft' && row[plugin.mapping[key]].trim() == ''))) {
plugin.csvdata.data[index][plugin.mapping[key]] = preferred_id;
updated.push(plugin.mapping[key]);
}
break;
default:
// React according to choosen method
if (obj[key] != undefined && (method == 'hard' || (method == 'soft' && row[plugin.mapping[key]].trim() == ''))) {
// Check if attribute is configured as multiple. If so, use choosen delimiter.
var app_mapping_config = config.m.find(function (o) {
return o.localJSONPath == key;
});
if (app_mapping_config != undefined && app_mapping_config.multiple == true) {
// Join values with delimiter and update CSV data
plugin.csvdata.data[index][plugin.mapping[key]] = asArray(obj[key]).join(delimiter);
} else {
plugin.csvdata.data[index][plugin.mapping[key]] = obj[key];
}
updated.push(plugin.mapping[key]);
}
}
});
if (updated.length > 0) {
updated_rows += 1;
updated_cells += updated.length;
plugin.log('Updated column(s) ' + updated.join(', ') + ' at CSV row with ID (' + plugin.mapping['id'] + '): ' + id);
}
}
}
});
// CSV data is updated. Time to parse and download
plugin.log('Merged ' +updated_cells + ' attribute(s) of ' + updated_rows + ' object(s).');
plugin.log('Merge finished.');
// Hide the modal dialog, it will be reseted automatically by event hidden.bs.modal
$('#' + plugin.prefix + '-modal').modal('hide');
return plugin;
}
CSVGenericImportExportPlugin.prototype.downloadCSV = function() {
var exportObj = this.csvdata.data;
var parse_config = this.csvdata.meta;
var exportStr = Papa.unparse(exportObj);
var dataStr = "data:text/csv;charset=utf-8," + encodeURIComponent(exportStr);
var downloadAnchorNode = document.createElement('a');
downloadAnchorNode.setAttribute("href", dataStr);
downloadAnchorNode.setAttribute("download", this.filename);
document.body.appendChild(downloadAnchorNode); // required for firefox
downloadAnchorNode.click();
downloadAnchorNode.remove();
return this;
}
var csvgiep = new CSVGenericImportExportPlugin();
// Initialize plugin if config is loaded
$('body').on('basicAppConfigLoaded', function () {
/* Initialize plugin */
csvgiep.init();
})