/// <summary> /// Load boilerplate data only when no data is configured locally /// </summary> private void LoadBoilerPlate() { try { using (DataContext context = new DataContext()) { if (!DisplayHosts.Any()) { DisplayHosts.Add(new VibesHost { Name = "No Hosts Loaded", Id = 0 }); SelectedHost = DisplayHosts.OrderByDescending(h => h.Id).FirstOrDefault(); CanEditHost = false; } if (!DisplayCms.Any()) { DisplayCms.Add(new VibesCm { CmResourceName = "No CMs loaded", Id = 0 }); SelectedCm = DisplayCms.OrderByDescending(c => c.Id).FirstOrDefault(); CanEditCm = false; } } } catch (Exception ex) { Log.Error($"Error loading boilerplate data: {ex.Message}"); } }
/// <summary> /// Loads data from database /// Calls LoadBoilerplate if no data is loaded /// </summary> /// <param name="type">The GuiObjectType to load</param> /// <param name="objectId">If an object is selected prior to refreshed, it's ID can be passed so it remains selected</param> private void LoadData(GuiObjectTypes type, int objectId = 0) { try { switch (type) { case GuiObjectTypes.VibesHost: using (DataContext context = new DataContext()) { DisplayHosts.Clear(); // Nothing found, load boilerplate and terminate if (!context.EnvironmentHosts.Any()) { LoadBoilerPlate(); return; } // Load Hosts from DB foreach (VibesHost host in context.EnvironmentHosts.OrderBy(h => h.Name)) { VibesHost newHost = host.DeepCopy(); newHost.PropertyChanged += new PropertyChangedEventHandler(PersistTargetChanges); DisplayHosts.Add(newHost); } // Set GUI particulars and reload CM's SelectedHost = objectId == 0 ? DisplayHosts.FirstOrDefault() : DisplayHosts.SingleOrDefault(h => h.Id == objectId); CanEditHost = true; LoadData(GuiObjectTypes.VibesCm); } break; case GuiObjectTypes.VibesCm: using (DataContext context = new DataContext()) { DisplayCms.Clear(); // Nothing found, load boilerplate and terminate if (!context.HostCms.Any(c => c.VibesHostId == SelectedHost.Id)) { LoadBoilerPlate(); return; } // Load CMs from DB DisplayCms.Clear(); foreach (VibesCm cm in context.HostCms.Where(c => c.VibesHostId == SelectedHost.Id).OrderBy(c => c.CmResourceName)) { VibesCm newCm = cm.DeepCopy(); newCm.PropertyChanged += new PropertyChangedEventHandler(PersistTargetChanges); DisplayCms.Add(newCm); } // Set GUI particulars SelectedCm = objectId == 0 ? DisplayCms.FirstOrDefault() : DisplayCms.SingleOrDefault(c => c.Id == objectId); CanEditCm = true; } break; } } catch (Exception ex) { MessageBox.Show($"Error loading data to GUI: {ex.Message}"); Log.Error($"Error loading data to GUI: {ex.Message}"); } }