aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Modules/Tango.FSE.MachineConfiguration/ViewModels/DataStoreViewVM.cs
blob: a90d5aa26de5c06d79d2c177ae9f17b6a028314b (plain)
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using Tango.Core.Commands;
using Tango.Core.DI;
using Tango.CSV;
using Tango.DataStore;
using Tango.DataStore.Editing;
using Tango.FSE.Common;
using Tango.FSE.Common.Connection;
using Tango.FSE.Common.DataStore;
using Tango.FSE.Common.Notifications;
using Tango.FSE.MachineConfiguration.Dialogs;
using Tango.FSE.MachineConfiguration.Messages;
using Tango.FSE.MachineConfiguration.Models;
using Tango.PMR.DataStore;
using static Tango.FSE.BL.Services.MachineConfigurationService;
using static Tango.SharedUI.Controls.NavigationControl;

namespace Tango.FSE.MachineConfiguration.ViewModels
{
    public class DataStoreViewVM : FSEViewModel, INavigationViewModel
    {
        private String _machineGuid;
        private MachineEditingComposition _editingComposition;
        private ICollectionView _selectedCollectionView;
        private ICollectionView _collectionsView;

        [TangoInject]
        public IDataStoreProvider DataStoreProvider { get; set; }

        private DataStoreModel _dataStore;
        public DataStoreModel DataStore
        {
            get { return _dataStore; }
            set { _dataStore = value; RaisePropertyChangedAuto(); }
        }

        private DataStoreCollectionModel _selectedCollection;
        public DataStoreCollectionModel SelectedCollection
        {
            get { return _selectedCollection; }
            set { _selectedCollection = value; RaisePropertyChangedAuto(); OnSelectedCollectionChanged(); }
        }

        private DataStoreItemModel _selectedItem;
        public DataStoreItemModel SelectedItem
        {
            get { return _selectedItem; }
            set { _selectedItem = value; RaisePropertyChangedAuto(); }
        }

        private String _filter;
        public String Filter
        {
            get { return _filter; }
            set { _filter = value; RaisePropertyChangedAuto(); _selectedCollectionView?.Refresh(); }
        }

        public bool CanSync
        {
            get { return MachineProvider.IsPPCAvailable && MachineProvider.Machine != null && _machineGuid == MachineProvider.Machine.Guid; }
        }

        public RelayCommand<DataStoreItemModel> RemoveItemCommand { get; set; }
        public RelayCommand<DataStoreCollectionModel> RemoveCollectionCommand { get; set; }
        public RelayCommand AddItemCommand { get; set; }
        public RelayCommand AddCollectionCommand { get; set; }
        public RelayCommand<DataStoreItemModel> EditItemCommand { get; set; }
        public RelayCommand SaveCommand { get; set; }
        public RelayCommand ReloadCommand { get; set; }
        public RelayCommand ImportCommand { get; set; }
        public RelayCommand ExportCommand { get; set; }

        public DataStoreViewVM()
        {
            RegisterForMessage<EditingCompositionLoadedMessage>(OnEditingCompositionLoaded);
            RemoveItemCommand = new RelayCommand<DataStoreItemModel>(RemoveItem);
            RemoveCollectionCommand = new RelayCommand<DataStoreCollectionModel>(RemoveCollection);
            AddItemCommand = new RelayCommand(AddItem);
            AddCollectionCommand = new RelayCommand(AddCollection);
            EditItemCommand = new RelayCommand<DataStoreItemModel>(EditItem);
            SaveCommand = new RelayCommand(SaveModel);
            ReloadCommand = new RelayCommand(ReloadDataStore);
            ExportCommand = new RelayCommand(ExportDataStore);
            ImportCommand = new RelayCommand(ImportDataStore);
        }

        [TangoInject]
        public DataStoreViewVM(IMachineProvider machineProvider) : this()
        {
            machineProvider.MachineConnected += (_, __) => RaisePropertyChanged(nameof(CanSync));
            machineProvider.MachineDisconnected += (_, __) => RaisePropertyChanged(nameof(CanSync));
        }

        private async void ReloadDataStore()
        {
            if (DataStore != null)
            {
                if (DataStore.Collections.SelectMany(x => x.Items).Any(x => x.HasDifference))
                {
                    if (!await NotificationProvider.ShowWarningQuestion("Reloading the data store will discard all the current changes. Are you sure?", "RELOAD"))
                    {
                        return;
                    }
                }
            }

            await LoadDataStore();
        }

        private async void EditItem(DataStoreItemModel item)
        {
            if (!CurrentUser.HasPermission(Tango.BL.Enumerations.Permissions.DataStoreWrite))
            {
                await NotificationProvider.ShowError("The current user profile does not allow editing of the data store.\nPlease contact your administrator.");
                return;
            }

            var vm = await NotificationProvider.ShowDialog<DataStoreItemEditDialogViewVM>(new DataStoreItemEditDialogViewVM() { Item = item, EnableTypeChange = !item.IsGlobal && item.GlobalItem == null });

            if (vm.DialogResult)
            {
                item.IsDeleted = false;
                item.IsGlobal = false;
                item.Type = vm.Type;
                item.Value = vm.Value;
            }
        }

        private async void AddCollection()
        {
            if (!CurrentUser.HasPermission(Tango.BL.Enumerations.Permissions.DataStoreCreate))
            {
                await NotificationProvider.ShowError("The current user profile does not allow creating new data store collections.\nPlease contact your administrator.");
                return;
            }

            var result = await NotificationProvider.ShowInputBox("Add Collection", "Please enter the collection name", MaterialDesignThemes.Wpf.PackIconKind.Collection, null, null, 20, null, null, (input) =>
               {
                   if (DataStore.Collections.ToList().Exists(x => x.Name.ToLower() == input.ToLower()))
                   {
                       return "The specified collection already exists.";
                   }

                   if (!DataStoreHelper.ValidateCollectionOrKeyName(input))
                   {
                       return "Collection name contains invalid characters.";
                   }

                   return null;
               });

            if (result.Confirmed)
            {
                if (DataStore.Collections.ToList().Exists(x => x.Name.ToLower() == result.Input.ToLower()))
                {
                    await NotificationProvider.ShowError("The specified collection already exists.");
                    AddCollection();
                    return;
                }

                DataStore.Collections.Add(new DataStoreCollectionModel()
                {
                    Name = result.Input
                });

                SelectedCollection = DataStore.Collections.Last();
            }
        }

        private async void AddItem()
        {
            if (!CurrentUser.HasPermission(Tango.BL.Enumerations.Permissions.DataStoreCreate))
            {
                await NotificationProvider.ShowError("The current user profile does not allow creating new data store items.\nPlease contact your administrator.");
                return;
            }

            var result = await NotificationProvider.ShowInputBox("Add Item", "Please enter the item key", MaterialDesignThemes.Wpf.PackIconKind.KeyAdd, null, null, 20, null, null, (input) =>
            {
                if (SelectedCollection.Items.ToList().Exists(x => x.Key.ToLower() == input.ToLower()))
                {
                    return "The specified key already exists in the collection.";
                }

                if (!DataStoreHelper.ValidateCollectionOrKeyName(input))
                {
                    return "Key name contains invalid characters.";
                }

                return null;
            });

            if (result.Confirmed)
            {
                if (SelectedCollection.Items.ToList().Exists(x => x.Key == result.Input))
                {
                    await NotificationProvider.ShowError("The specified key already exists in the collection.");
                    AddItem();
                    return;
                }

                SelectedCollection.Items.Add(new DataStoreItemModel()
                {
                    Date = DateTime.UtcNow,
                    Guid = Guid.NewGuid().ToString(),
                    Key = result.Input,
                    Type = Tango.DataStore.DataType.Int32,
                    Value = 10
                });
            }
        }

        private async void RemoveCollection(DataStoreCollectionModel collection)
        {
            if (!CurrentUser.HasPermission(Tango.BL.Enumerations.Permissions.DataStoreCreate))
            {
                await NotificationProvider.ShowError("The current user profile does not allow deleting data store collections.\nPlease contact your administrator.");
                return;
            }

            collection.IsDeleted = true;
            _collectionsView?.Refresh();
        }

        private async void RemoveItem(DataStoreItemModel item)
        {
            if (!CurrentUser.HasPermission(Tango.BL.Enumerations.Permissions.DataStoreCreate))
            {
                await NotificationProvider.ShowError("The current user profile does not allow deleting data store items.\nPlease contact your administrator.");
                return;
            }

            item.IsDeleted = true;
            _selectedCollectionView.Refresh();
        }

        private void OnEditingCompositionLoaded(EditingCompositionLoadedMessage obj)
        {
            DataStore = null;
            _editingComposition = obj.EditingComposition;
            _machineGuid = obj.EditingComposition.Machine.Guid;
        }

        private void OnSelectedCollectionChanged()
        {
            if (SelectedCollection != null)
            {
                _selectedCollectionView = CollectionViewSource.GetDefaultView(SelectedCollection.Items);
                _selectedCollectionView.SortDescriptions.Add(new SortDescription(nameof(DataStoreItemModel.Key), ListSortDirection.Ascending));
                _selectedCollectionView.Filter = (x) => FilterItems(x as DataStoreItemModel);
                _selectedCollectionView.Refresh();
            }
        }

        private bool FilterItems(DataStoreItemModel itemModel)
        {
            return (!itemModel.IsDeleted || itemModel.GlobalItem != null) && (String.IsNullOrWhiteSpace(Filter) || itemModel.Key.ToLower().Contains(Filter.ToLower()));
        }

        private async void SaveModel()
        {
            if (!CurrentUser.HasPermission(Tango.BL.Enumerations.Permissions.DataStoreWrite))
            {
                await NotificationProvider.ShowError("The current user profile does not allow editing of the data store.\nPlease contact your administrator.");
                return;
            }

            try
            {
                using (NotificationProvider.PushTaskItem("Updating data store..."))
                {
                    await DataStoreProvider.UpdateDataStoreModel(DataStore, _machineGuid);
                }

                if (MachineProvider.IsPPCAvailable)
                {
                    await NotificationProvider.ShowSuccess("The connected machine is now synchronized with the data store.");
                }
                else
                {
                    await NotificationProvider.ShowSuccess("Data store saved successfully.\nThe machine will be updated on the next synchronization pass.");
                }
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Error saving data store.");
                await NotificationProvider.ShowError($"Error occurred while trying to save the data store.\n{ex.FlattenMessage()}");
                return;
            }

            var selectedCollection = SelectedCollection;
            var selectedItem = SelectedItem;

            await LoadDataStore();

            if (selectedCollection != null)
            {
                SelectedCollection = DataStore.Collections.FirstOrDefault(x => x.Name == selectedCollection.Name);
            }

            if (SelectedCollection != null && selectedItem != null)
            {
                SelectedItem = SelectedCollection.Items.FirstOrDefault(x => x.Key == selectedItem.Key);
            }
        }

        private async Task LoadDataStore()
        {
            try
            {
                using (NotificationProvider.PushTaskItem("Loading data store..."))
                {
                    IsFree = false;
                    RaisePropertyChanged(nameof(CanSync));

                    var dataStore = await DataStoreProvider.GetDataStoreModel(_machineGuid);

                    _collectionsView = CollectionViewSource.GetDefaultView(dataStore.Collections);
                    _collectionsView.Filter = (x) => { return !(x as DataStoreCollectionModel).IsDeleted; };
                    _collectionsView.Refresh();

                    DataStore = dataStore;
                    SelectedCollection = DataStore.Collections.FirstOrDefault();

                    IsFree = true;
                }
            }
            catch (Exception ex)
            {
                IsFree = true;
                DataStore = null;
                LogManager.Log(ex, "Error loading data store.");
                if (await NotificationProvider.ShowWarningQuestion($"Error occurred while trying to load the data store.\n{ex.FlattenMessage()}", "RETRY", "CANCEL"))
                {
                    await LoadDataStore();
                    return;
                }
            }
        }

        public async override void OnNavigatedTo()
        {
            base.OnNavigatedTo();

            if (!CurrentUser.HasPermission(Tango.BL.Enumerations.Permissions.DataStoreRead))
            {
                return;
            }

            if (DataStore == null)
            {
                await LoadDataStore();
            }
        }

        private async void ImportDataStore()
        {
            var result = await StorageProvider.OpenFile("Import data store", "CSV Files|*.csv");

            if (result)
            {
                try
                {
                    DataStoreModel model = new DataStoreModel();

                    using (NotificationProvider.PushTaskItem("Analyzing file..."))
                    {
                        await Task.Delay(1000);

                        await Task.Factory.StartNew(() =>
                        {
                            var items = CsvFile.Read<CsvModel>(new CsvSource(result.SelectedItem)).ToList();

                            foreach (var collection in items.GroupBy(x => x.Collection))
                            {
                                DataStoreCollectionModel collectionModel = new DataStoreCollectionModel();

                                collectionModel = new DataStoreCollectionModel();
                                collectionModel.Name = collection.First().Collection;
                                collectionModel.IsSelected = true;
                                model.Collections.Add(collectionModel);

                                foreach (var item in collection)
                                {
                                    DataStoreItemModel itemModel = new DataStoreItemModel();
                                    itemModel.Guid = Guid.NewGuid().ToString();
                                    itemModel.Key = item.Key;
                                    itemModel.Date = DateTime.Parse(item.Date);
                                    itemModel.IsSelected = true;

                                    DataStore.DataType type = (DataStore.DataType)Enum.Parse(typeof(DataStore.DataType), item.Type);
                                    itemModel.OriginalType = type;
                                    itemModel.Type = type;

                                    DataStoreMessageType? messageType = null;
                                    if (type == Tango.DataStore.DataType.Proto && item.MessageType.IsNotNullOrEmpty())
                                    {
                                        messageType = (DataStoreMessageType)Enum.Parse(typeof(DataStoreMessageType), item.MessageType);
                                    }

                                    itemModel.OriginalValue = DataStoreHelper.ParseDataStoreValue(type, item.Value, messageType);
                                    itemModel.Value = itemModel.OriginalValue;

                                    collectionModel.Items.Add(itemModel);
                                }
                            }
                        });
                    }

                    var vm = await NotificationProvider.ShowDialog<ImportDialogViewVM>(new ImportDialogViewVM() { DataStore = model });

                    if (vm.DialogResult)
                    {
                        if (!vm.DataStore.Collections.SelectMany(x => x.Items).Any(x => x.IsSelected))
                        {
                            await NotificationProvider.ShowInfo("No items selected to import.");
                            return;
                        }

                        foreach (var collection in vm.DataStore.Collections.Where(x => x.Items.Any(y => y.IsSelected)))
                        {
                            var collectionModel = DataStore.Collections.FirstOrDefault(x => x.Name == collection.Name);

                            if (collectionModel == null)
                            {
                                DataStore.Collections.Add(collection);
                            }
                            else
                            {
                                foreach (var item in collection.Items.Where(x => x.IsSelected))
                                {
                                    var itemModel = collectionModel.Items.FirstOrDefault(x => x.Key == item.Key);

                                    if (itemModel == null)
                                    {
                                        itemModel = new DataStoreItemModel();
                                        itemModel.Guid = item.Guid;
                                        itemModel.Key = item.Key;
                                        collectionModel.Items.Add(itemModel);
                                    }

                                    itemModel.Date = DateTime.UtcNow;
                                    itemModel.IsDeleted = false;
                                    itemModel.IsGlobal = false;
                                    itemModel.Type = item.Type;
                                    itemModel.Value = item.Value;
                                }
                            }
                        }

                        await NotificationProvider.ShowSuccess("Data store imported successfully.");
                    }
                }
                catch (Exception ex)
                {
                    LogManager.Log(ex, "Error importing data store.");
                    await NotificationProvider.ShowError($"Error importing the data store.\n{ex.FlattenMessage()}");
                }
            }
        }

        private async void ExportDataStore()
        {
            var vm = await NotificationProvider.ShowDialog<ExportDialogViewVM>(new ExportDialogViewVM() { DataStore = DataStore });

            if (vm.DialogResult)
            {
                if (!vm.DataStore.Collections.SelectMany(x => x.Items).Any(x => x.IsSelected))
                {
                    await NotificationProvider.ShowInfo("No items selected to export.");
                    return;
                }

                var result = await StorageProvider.SaveFile("Export data store", "CSV Files|*.csv", $"{_editingComposition.Machine.SerialNumber}_datastore_{DateTime.Now.ToFileName()}.csv", ".csv");

                if (result)
                {
                    try
                    {
                        using (NotificationProvider.PushTaskItem("Exporting data store..."))
                        {
                            await Task.Delay(1000);

                            await Task.Factory.StartNew(() =>
                            {
                                using (DynamicCsvFile csvFile = new DynamicCsvFile(result.SelectedItem))
                                {
                                    csvFile.Columns.Add(new DynamicCsvFileColumn() { Name = "Date" });
                                    csvFile.Columns.Add(new DynamicCsvFileColumn() { Name = "Collection" });
                                    csvFile.Columns.Add(new DynamicCsvFileColumn() { Name = "Key" });
                                    csvFile.Columns.Add(new DynamicCsvFileColumn() { Name = "Type" });
                                    csvFile.Columns.Add(new DynamicCsvFileColumn() { Name = "MessageType" });
                                    csvFile.Columns.Add(new DynamicCsvFileColumn() { Name = "Value" });

                                    foreach (var collection in DataStore.Collections)
                                    {
                                        foreach (var item in collection.Items.Where(x => x.IsSelected && !x.IsGlobal && x.Value != null))
                                        {
                                            DataStoreMessageType? messageType = null;

                                            if (item.Type == Tango.DataStore.DataType.Proto)
                                            {
                                                messageType = (item.Value as DataStoreProtoObject).MessageType;
                                            }

                                            csvFile.Append(item.Date, $"\"{collection.Name}\"", $"\"{item.Key}\"", item.Type, messageType.ToStringSafe(), $"\"{item.FormattedValue.Replace("\"", "\"\"")}\"");
                                        }
                                    }
                                }
                            });
                        }

                        NotificationProvider.PushSnackbarItem(MessageType.Success, "Export Data Store", true, "Data store exported successfully.", TimeSpan.FromSeconds(8), null, () =>
                        {
                            StorageProvider.ShowInExplorer(result.SelectedItem);
                        });
                    }
                    catch (Exception ex)
                    {
                        LogManager.Log(ex, "Error exporting data store.");
                        await NotificationProvider.ShowError($"Error exporting the data store.\n{ex.FlattenMessage()}");
                    }
                }
            }
        }
    }
}