public static async void SaveOpenableObject(OpenableObject openableObject) { if (openableObject is LogView) { await SettingsService.AddOrUpdateLogView((LogView)openableObject, true); } else if (openableObject is Database) { await SettingsService.AddOrUpdateDatabase((Database)openableObject, true); } else if (openableObject is LogFile) { await SettingsService.AddOrUpdateLogFile((LogFile)openableObject, true); } }
public static async Task AddDatabaseRoutine(IDialogCoordinator dialogCoordinator, object dialogContext, SourceCache <Database, string> databasesSourceCache) { var maxCharLength = 100; var keepPrompting = true; var dialogSettings1 = new MetroDialogSettings() { AffirmativeButtonText = "Add", }; while (keepPrompting) { var newDatabaseName = dialogCoordinator.ShowModalInputExternal(dialogContext, "Add Database", $"Enter the database name in the format {_databaseNameFormat}.{Environment.NewLine + _databaseNameFormatNote}", dialogSettings1); dialogSettings1.DefaultText = newDatabaseName; if (newDatabaseName == null) { keepPrompting = false; } else if (string.IsNullOrWhiteSpace(newDatabaseName)) { await dialogCoordinator.ShowMessageAsync(dialogContext, "Invalid Database Name", "The new database name cannot be empty or whitespace."); } else if (newDatabaseName.Length > maxCharLength) { await dialogCoordinator.ShowMessageAsync(dialogContext, "Invalid Database Name", $"The new database name cannot be greater than {maxCharLength} characters."); dialogSettings1.DefaultText = newDatabaseName.Substring(0, maxCharLength); } else if (!newDatabaseName.Contains(AddDatabaseInfo.DatabaseInfoSplitter) || newDatabaseName.Contains("{") || newDatabaseName.Contains("}")) { await dialogCoordinator.ShowMessageAsync(dialogContext, "Invalid Database Name", $"The new database name must be in the format {_databaseNameFormat}.{Environment.NewLine + _databaseNameFormatNote}" + Environment.NewLine + $"Example: 198.163.22.10\\MSSQLSERVER,1433{AddDatabaseInfo.DatabaseInfoSplitter}CustomerInfo*username*password"); } else { var addDatabaseInfo = new AddDatabaseInfo(newDatabaseName); if (SettingsService.SettingsContainsDatabaseName(addDatabaseInfo.ToFormattedString())) { await dialogCoordinator.ShowMessageAsync(dialogContext, "Database Already Added", $"Database \"{newDatabaseName}\" has already been added."); keepPrompting = false; } else { //Show progress dialog while searching for new database var progressController = await dialogCoordinator.ShowProgressAsync(dialogContext, "Looking for Database", $"Trying to find database \"{newDatabaseName}\"..."); progressController.SetIndeterminate(); var databaseExists = false; await Task.Run(() => { databaseExists = TestDatabaseConnection(addDatabaseInfo.ToConnectionString()); }); await progressController.CloseAsync(); //If database can't be found, ask user if they want to continue adding database var dialogSettings = new MetroDialogSettings() { AffirmativeButtonText = "Yes", NegativeButtonText = "No", DefaultButtonFocus = MessageDialogResult.Affirmative, }; if (!databaseExists && await dialogCoordinator.ShowMessageAsync(dialogContext, "Database Not Found", $"The database \"{newDatabaseName}\" could not be found. Would you still like to add it?", MessageDialogStyle.AffirmativeAndNegative, dialogSettings) != MessageDialogResult.Affirmative) { return; } var newDatabase = new Database(addDatabaseInfo.ToFormattedString()); var serviceOperationHelper = new ServiceOperationHelper(typeof(Database), Plurality.Single, ServiceOperation.Add, "databases source cache", addDatabaseInfo.ToFormattedString()); await Task.Run(() => { try { serviceOperationHelper.LogServiceOperation(ServiceOperationStatus.Attempting); databasesSourceCache.AddOrUpdate(newDatabase); serviceOperationHelper.LogServiceOperation(ServiceOperationStatus.Succeeded); } catch (Exception ex) { serviceOperationHelper.LogServiceOperation(ex.Message); } }); keepPrompting = false; if (serviceOperationHelper.ServiceOperationResult.OperationSuceeded) { serviceOperationHelper.ServiceOperationResult = await SettingsService.AddOrUpdateDatabase(newDatabase, true); } if (serviceOperationHelper.ServiceOperationResult.OperationFailed) { await serviceOperationHelper.ServiceOperationResult.ShowUserErrorMessage(dialogCoordinator, dialogContext); } } } } }