using System; using System.Collections.Generic; using DTS.Common.Classes.Viewer.TestMetadata; using System.ComponentModel; using System.IO; using System.Linq; using System.Windows; using System.Windows.Threading; using DTS.Common.Base; using DTS.Common.Events; using DTS.Common.Interface; using DTS.Viewer.TestSummaryList.ViewModel; using Prism.Events; // ReSharper disable InconsistentNaming // ReSharper disable UnassignedGetOnlyAutoProperty namespace DTS.Viewer.TestSummaryList.Model { public class TestSummaryModel : IBaseModel { public ITestSummaryListViewModel Parent { get; set; } public IEventAggregator _eventAggregator { get; set; } /// /// Returns list of Test Definition from DTS/Data folder /// /// Data folder /// .dts file /// whether to set file as selected/included /// public void GetTestSummary(string path, string file, bool Include = false, bool selectAll = false) { Parent.IsBusy = true; _eventAggregator.GetEvent().Publish(true); _eventAggregator.GetEvent().Publish(new AppStatusExArg(AppStatusArg.Busy, "GetTestSummary")); Dispatcher.CurrentDispatcher.InvokeAsync(async () => { try { var tmd = new TestMetadataList(); if (!string.IsNullOrEmpty(path) && !Directory.Exists(path)) { Directory.CreateDirectory(path); } var list = await tmd.GetTestSummaryListAsync(Parent, path, file); if (Parent.TestSummaryList.Any()) { foreach (var l in list) { //http://manuscript.dts.local/f/cases/28164/Shift-T0-with-derive-ROI-from-ALL-does-not-shift-T0-for-ROI //we need to check more than just id and setup name //here I added the data type too (ROI/ALL) but ideally we would just use the filename //the existing structure did not seem to have file name exposed so I just made use of one additional //piece of data if (Parent.TestSummaryList.Any(summ => summ.Id == l.Id && summ.SetupName == l.SetupName && summ.DataType == l.DataType)) { var testToReplace = Parent.TestSummaryList.First(summ => summ.Id == l.Id && summ.SetupName == l.SetupName && summ.DataType == l.DataType); var testIndex = Parent.TestSummaryList.IndexOf(testToReplace); var isTestSelected = testToReplace.IsSelected; Parent.SelectedTestSummaryList.Remove(testToReplace); Parent.TestSummaryList.Remove(testToReplace); Parent.TestSummaryList.Insert(testIndex, l); Parent.TestSummaryList[testIndex].IsSelected = isTestSelected; } else { Parent.TestSummaryList.Add(l); } } } else { list.CollectionChanged += Parent.TestSummaryList_CollectionChanged; Parent.TestSummaryList = list; } DetermineTestsSelected(selectAll); _eventAggregator.GetEvent().Publish(new TestLoadedCountNotificationArg { LoadedCount = Parent.TestSummaryList.Count, ParentVM = ((TestSummaryViewListModel)Parent).Parent }); Parent.IsBusy = false; _eventAggregator.GetEvent().Publish(false); try { //put this in a try/catch because I'm putting it in as a new feature into a regression build and wanted to cut down //the chances a new feature adding a crash //16158 Browse button on View Data tab not functional //this will select the test //we don't need to select it if there's only one in the list as the code above will select it automatically if there's //only one in the parent summary list if (list.Any() && Include && Parent.TestSummaryList.Count != 1) { list[0].IsSelected = true; } } catch (Exception) { } } catch (Exception ex) { MessageBox.Show("Error", ex.Message, MessageBoxButton.OK); Parent.IsBusy = false; _eventAggregator.GetEvent().Publish(false); } finally { _eventAggregator.GetEvent().Publish(new AppStatusExArg(AppStatusArg.Available, "GetTestSummary")); } }, DispatcherPriority.Background); } /// /// 43387 - If called by Viewer, only select the first test, if called by Export, select all. /// /// private void DetermineTestsSelected(bool selectAll) { if (selectAll) { foreach (var testSummaryList in Parent.TestSummaryList) { testSummaryList.IsSelected = true; } } else { if (Parent.TestSummaryList.Count == 1) { Parent.TestSummaryList[0].IsSelected = true; } else if (Parent.TestSummaryList.Count > 1) { var names = Parent.TestSummaryList.Select(p => p.TestMetadata.TestSetup.SetupName).Distinct(); //http://manuscript.dts.local/f/cases/35546/Viewer-always-selects-first-dataset-after-making-modification-to-other-dataset //don't select the all test if there are multiple tests involved, we are not in the view all step ... if (1 == names.Count() && Parent.TestSummaryList.Any(p => p.DataType == "ALL")) { Parent.TestSummaryList[0].IsSelected = true; } } } } /// ///Occurs when a property value changes. /// public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } public bool IsSaved { get; } } }