Source

main / Source / Excel / Excel.AddIn / Excel.AddIn / ViewModels / MetadataListViewModel.cs

Naethra D ba1cb57 











































Naethra D db09f1b 

Naethra D ba1cb57 










































Naethra D db09f1b 





Naethra D ba1cb57 






















































































































































Naethra D db09f1b 



Naethra D ba1cb57 




Naethra D db09f1b 
Naethra D ba1cb57 




Naethra D db09f1b 




Naethra D ba1cb57 











































































Naethra D db09f1b 
Naethra D ba1cb57 











































































































































Naethra D db09f1b 
Naethra D ba1cb57 
Naethra D db09f1b 






Naethra D ba1cb57 
































  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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows.Input;
using System.Windows.Threading;
using DataUp.Excel.Common;

namespace DataUp.Excel.Addin
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class MetadataListViewModel : ViewModelBase
    {
        private ObservableCollection<MetadataViewModel> metadataList;
        private ObservableCollection<ParameterMetadataViewModel> parameterMetadataList;
        private string citation;

        private ICommand generateMetadataCommand;
        private ICommand generateCitationCommand;
        private ICommand loadDefaultPropertiesCommand;
        private ICommand clearAllCommand;
        private ICommand resetCommand;
        private ICommand generateMetadata;
        private ICommand generateParameterMetadata;
        private ICommand addNewParameterCommand;

        private ICommand deleteParameterCommand;

        #region CustomEvent

        /// <summary>
        /// Capture View window close request
        /// </summary>
        public event EventHandler RequestClose;

        #endregion

        public ObservableCollection<string> TypeList { get; set; }

        public ObservableCollection<string> StandardUnitsList { get; set; }

        public ObservableCollection<string> EntityNameList { get; set; }

        public ObservableCollection<MetadataViewModel> MetadataList
        {
            get
            {
                return this.metadataList;
            }
            set
            {
                this.metadataList = value;
                OnNotifyPropertyChanged("MetadataList");
            }
        }

        public ObservableCollection<ParameterMetadataViewModel> ParameterMetadataList
        {
            get
            {
                return this.parameterMetadataList;
            }
            set
            {
                this.parameterMetadataList = value;
                OnNotifyPropertyChanged("ParameterMetadataList");
            }
        }

        public string Citation
        {
            get
            {
                return this.citation;
            }
            set
            {
                this.citation = value;
                OnNotifyPropertyChanged("Citation");
            }
        }

        public bool IsLoaded { get; set; }

        public bool IsParameterInputBoxOpen { get; set; }

        public int SelectedTabIndex
        {
            get;
            set;
        }

        public ICommand GenerateMetadataCommand
        {
            get
            {
                return generateMetadataCommand;
            }
            set
            {
                if (generateMetadataCommand != value)
                {
                    generateMetadataCommand = value;
                    OnNotifyPropertyChanged("GenerateMetadataCommand");
                }
            }
        }

        public ICommand GenerateMetadata
        {
            get
            {
                return generateMetadata;
            }
            set
            {
                if (generateMetadata != value)
                {
                    generateMetadata = value;
                    OnNotifyPropertyChanged("GenerateMetadata");
                }
            }
        }

        public ICommand GenerateCitationCommand
        {
            get
            {
                return generateCitationCommand;
            }
            set
            {
                if (generateCitationCommand != value)
                {
                    generateCitationCommand = value;
                    OnNotifyPropertyChanged("GenerateCitationCommand");
                }
            }
        }

        public ICommand LoadDefaultPropertiesCommand
        {
            get
            {
                return loadDefaultPropertiesCommand;
            }
            set
            {
                if (loadDefaultPropertiesCommand != value)
                {
                    loadDefaultPropertiesCommand = value;
                    OnNotifyPropertyChanged("LoadDefaultPropertiesCommand");
                }
            }
        }

        public ICommand ClearAllCommand
        {
            get
            {
                return clearAllCommand;
            }
            set
            {
                if (clearAllCommand != value)
                {
                    clearAllCommand = value;
                    OnNotifyPropertyChanged("ClearAllCommand");
                }
            }
        }

        public ICommand ResetCommand
        {
            get
            {
                return resetCommand;
            }
            set
            {
                if (resetCommand != value)
                {
                    resetCommand = value;
                    OnNotifyPropertyChanged("ResetCommand");
                }
            }
        }

        public ICommand GenerateParameterMetadata
        {
            get
            {
                return generateParameterMetadata;
            }
            set
            {
                if (generateParameterMetadata != value)
                {
                    generateParameterMetadata = value;
                    OnNotifyPropertyChanged("GenerateParameterMetadata");
                }
            }
        }

        public ICommand AddNewParameterCommand
        {
            get
            {
                return addNewParameterCommand;
            }
            set
            {
                if (addNewParameterCommand != value)
                {
                    addNewParameterCommand = value;
                    OnNotifyPropertyChanged("AddNewParameterCommand");
                }
            }
        }

        public ICommand DeleteParameterCommand
        {
            get
            {
                return deleteParameterCommand;
            }
            set
            {
                if (deleteParameterCommand != value)
                {
                    deleteParameterCommand = value;
                    OnNotifyPropertyChanged("DeleteParameterCommand");
                }
            }
        }

        public MetadataListViewModel()
        {
            this.MetadataList = new ObservableCollection<MetadataViewModel>();
            this.ParameterMetadataList = new ObservableCollection<ParameterMetadataViewModel>();

            this.TypeList = new ObservableCollection<string>();
            this.StandardUnitsList = new ObservableCollection<string>();
            this.EntityNameList = new ObservableCollection<string>();

            this.TypeList.Add(" ");
            this.StandardUnitsList.Add("");

            foreach (var item in Common.Constants.ParameterMetadataType.Split(','))
            {
                this.TypeList.Add(item.Trim());
            }
            
            foreach (var item in Helper.GetStandardUnits())
            {
                this.StandardUnitsList.Add(item.Trim());
            }

            foreach (var item in Controller.Instance.GetSheetNames())
            {
                this.EntityNameList.Add(item.Trim());
            }
            SelectedTabIndex = 0;
            InitCommands();
        }

        private void InitCommands()
        {
            this.GenerateMetadataCommand = new DelegateCommand<MetadataListViewModel>(viewModel =>
            {
                this.OnRequestClose();
            }
            , CanSave
            );

            this.LoadDefaultPropertiesCommand = new DelegateCommand<MetadataListViewModel>(viewModel =>
            {
                var documentProperties = Controller.Instance.GetDocumentProperties();

                UpdateExcelProperties(documentProperties);
            });

            this.ClearAllCommand = new DelegateCommand<MetadataListViewModel>(viewModel =>
            {
                this.ClearAll();
            });

            this.ResetCommand = new DelegateCommand<MetadataListViewModel>(viewModel =>
            {
                // Get Value from Existing Metadata Sheet.
                this.LoadMetadata();
            });

            this.GenerateCitationCommand = new DelegateCommand<MetadataListViewModel>(viewModel =>
            {
                this.OnRequestClose();
                this.UpdateCitation();
            });

            this.GenerateMetadata = new DelegateCommand<MetadataListViewModel>(viewModel =>
            {
                this.OnRequestClose();
            }
            , CanSave
            );

            this.GenerateParameterMetadata = new DelegateCommand(GenerateParameterMetadataExecute, () => { return !IsParameterInputBoxOpen; });
            this.AddNewParameterCommand = new DelegateCommand(AddNewParameterCommandExecute);

            this.DeleteParameterCommand = new DelegateCommand<ParameterMetadataViewModel>(DeleteParameterCommandExecute);
        }

        private void GenerateParameterMetadataExecute()
        {
            IsParameterInputBoxOpen = true;

            var newParameterList = Controller.Instance.GetNewParameterList();
            foreach (var item in newParameterList)
            {
                var existingParameter = this.ParameterMetadataList.Where(p => !string.IsNullOrWhiteSpace(p.Name) && (string.Compare(p.Name.Trim(), item.Name.Trim(), true) == 0)).FirstOrDefault();
                if (existingParameter == null)
                {
                    ParameterMetadataViewModel model = new ParameterMetadataViewModel();
                    model.SetValuesFrom(item);
                    model.EnableUnits = false;
                    this.ParameterMetadataList.Add(model);
                }
            }

            IsParameterInputBoxOpen = false;
        }

        private void AddNewParameterCommandExecute()
        {
            if (this.ParameterMetadataList == null)
            {
                this.ParameterMetadataList = new ObservableCollection<ParameterMetadataViewModel>();
            }

            this.ParameterMetadataList.Add(new ParameterMetadataViewModel() { EntityName = this.EntityNameList[0] });
        }

        private void DeleteParameterCommandExecute(ParameterMetadataViewModel item)
        {
            if (item != null)
            {
                this.ParameterMetadataList.Remove(item);
            }
        }

        /// <summary>
        /// Raise window close event
        /// </summary>
        public void OnRequestClose()
        {
            RequestClose.OnFire(this, EventArgs.Empty);
        }

        public void ClearAll()
        {
            foreach (var item in this.MetadataList)
            {
                item.Value = string.Empty;
            }
        }

        public void UpdateExcelProperties(Dictionary<string, string> documentProperties)
        {
            // author
            //var authorNode = this.MetadataList.Where(m => m.Name.EndsWith("First name", StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
            //if (authorNode != null)
            //{
            //    authorNode.Value = documentProperties.ContainsKey("Author") ? documentProperties["Author"] : string.Empty;
            //}

            // beginDate
            var beginDateNode = this.MetadataList.Where(m => m.Name.EndsWith("Today's date", StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
            if (beginDateNode != null)
            {
                DateTime beginDate = DateTime.UtcNow;
                string begin = documentProperties.ContainsKey("Creation date") ? documentProperties["Creation date"] : string.Empty;

                if (!string.IsNullOrWhiteSpace(begin))
                {
                    DateTime.TryParse(begin, out beginDate);
                }

                beginDateNode.Value = beginDate.ToString(Common.Constants.DefaultDateFormat);
            }

            // title
            var titleNode = this.MetadataList.Where(m => m.Name.EndsWith("Title of dataset", StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
            if (titleNode != null)
            {
                titleNode.Value = documentProperties.ContainsKey("Title") ? documentProperties["Title"] : string.Empty;
            }
        }

        public void UpdateCitation()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            string author = GetValue("Creator: First name");
            string pubDate = GetValue("Today's date");
            string title = GetValue("Title of dataset");
            string edition = GetValue("Keyword thesaurus used");
            string pulisher = GetValue("Repository name and contact information");
            string identifier = GetValue(Common.Constants.IdentifierKeyName);

            this.Citation = string.Format("{0} ({1}) : {2}. {3}. {4}. {5}", author, pubDate, title, edition, pulisher, identifier);
        }

        public void LoadMetadata()
        {
            try
            {
                var metaData = Controller.Instance.GetMetadata();

                this.MetadataList.Clear();
                foreach (var item in metaData.MetadataList)
                {
                    this.MetadataList.Add(item);
                }
                this.ParameterMetadataList.Clear();
                foreach (var item in metaData.ParameterMetadataList)
                {
                    this.ParameterMetadataList.Add(item);
                }
            }
            catch (Exception)
            {
            }
        }

        public void LoadMetadataAsync()
        {
            using (BackgroundWorker worker = new BackgroundWorker())
            {
                Dispatcher UiDispatcher = Dispatcher.CurrentDispatcher;
                worker.DoWork += (o, ea) =>
                {
                    var metaData = Controller.Instance.GetMetadata();

                    UiDispatcher.Invoke((Action)(() =>
                    {
                        this.MetadataList.Clear();
                        foreach (var item in metaData.MetadataList)
                        {
                            this.MetadataList.Add(item);
                        }

                        this.ParameterMetadataList.Clear();
                        foreach (var item in metaData.ParameterMetadataList)
                        {
                            this.ParameterMetadataList.Add(item);
                        }
                    }));
                };

                worker.RunWorkerCompleted += (o, ea) =>
                {
                    this.RaiseRunWorkerCompleted(o, ea);
                };

                this.RaiseRunWorkerStarted(this);
                worker.RunWorkerAsync();
            }
        }

        public bool IsValid()
        {
            bool isValid = true;

            foreach (var item in this.MetadataList)
            {
                if (!string.IsNullOrWhiteSpace(item.Validate()))
                {
                    isValid = false;
                    break;
                }
            }

            foreach (var item in this.ParameterMetadataList)
            {
                if (!item.Validate())
                {
                    isValid = false;
                    break;
                }
            }
            return isValid;
        }

        private bool CanSave(MetadataListViewModel viewModel)
        {
            bool isValid = true;

            foreach (var item in this.MetadataList)
            {
                if (!string.IsNullOrWhiteSpace(item.Validate()))
                {
                    isValid = false;
                    break;
                }

            }
            return isValid;
        }

        private string GetValue(string keyName)
        {
            string value = string.Empty;

            var authorNode = this.MetadataList.Where(m => m.Name.EndsWith(keyName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
            if (authorNode != null)
            {
                value = authorNode.Value;
            }

            return value;
        }
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.