Beispiel #1
0
        /// <summary>
        /// Initializes the manager.
        /// NOTE: This method should be called before the usage of the manager,
        ///       usually at the entry point of the application!
        /// </summary>
        /// <returns></returns>
        public async Task InitializeAsync()
        {
            // If there isn't an options file...
            if (!File.Exists(OptionsFileName))
            {
                // There is nothing to initialize from, so return
                return;
            }

            // Get the options
            var options = XMLHelpers.FromXml <CeidDiplomatikiOptionsDataModel>(OptionsFileName);

            // For every database...
            foreach (var database in options.Databases)
            {
                // Add it
                Register(database);
            }

            // For every database options...
            foreach (var databaseOptions in Databases)
            {
                // Get the connection string
                databaseOptions.TryGetConnectionString(out var connectionString);

                // Get the analyzer
                var analyzer = CeidDiplomatikiDI.GetDatabaseAnalyzer(databaseOptions.Provider);

                // Get the database
                var database = analyzer.GetDatabases().First(x => x.DatabaseName == databaseOptions.DatabaseName);

                // Get the tables
                var tables = analyzer.GetTables(databaseOptions.DatabaseName);

                // Get the columns
                var columns = analyzer.GetColumns(database.DatabaseName, null);

                // For every query map related to that database...
                foreach (var queryMapDataModel in options.QueryMaps.Where(x => x.DatabaseId == databaseOptions.Id))
                {
                    // The joins collection
                    var joins = new List <JoinMap>();

                    // For every pair...
                    foreach (var joinDataModel in queryMapDataModel.Joins)
                    {
                        // Get the principle column
                        var principleColumn = columns.First(x => x.ColumnName == joinDataModel.PrincipleKeyColumnName);

                        // Get the foreign key column
                        var referencedColumn = columns.First(x => x.ColumnName == joinDataModel.ForeignKeyColumnName);

                        // Create the join map
                        var joinMap = new JoinMap(tables.First(x => x.TableName == joinDataModel.TableName), principleColumn, tables.First(x => x.TableName == joinDataModel.ReferencedTableName), referencedColumn, joinDataModel.Index, joinDataModel.IsInverted);

                        // Add it to the joins
                        joins.Add(joinMap);
                    }

                    // Create the map
                    var queryMap = QueryMap.FromDataModel(databaseOptions, database, tables.Where(x => queryMapDataModel.TableNames.Contains(x.TableName)).ToList(), columns.Where(x => queryMapDataModel.TableNames.Contains(x.TableName)).ToList(), joins, queryMapDataModel);

                    // Register it
                    await RegisterAsync(queryMap);
                }
            }

            // For every page...
            foreach (var page in options.PageMaps)
            {
                // Add it
                Register(PageMap.FromDataModel(page, null));
            }

            // Await a task
            await Task.CompletedTask;
        }
        /// <summary>
        /// Creates and adds the required GUI elements
        /// </summary>
        private void CreateGUI()
        {
            // Create the add button
            AddButton = ControlsFactory.CreateStandardAddCircularButton();

            AddButton.Command = new RelayCommand(async() =>
            {
                // Disable the button
                AddButton.IsEnabled = false;

                // Get the analyzer
                var analyzer = CeidDiplomatikiDI.GetDatabaseAnalyzer(DatabaseOptions.Provider);

                // Get the connection string
                DatabaseOptions.TryGetConnectionString(out var connectionString);

                // Get all the tables
                var tables = analyzer.GetTables(Database.DatabaseName);

                // Get all the foreign key columns
                var foreignKeyColumns = analyzer.GetForeignKeyColumns(Database.DatabaseName, null);

                // Create a steps presenter
                var stepsPresenter = new StepsPresenter()
                {
                    AllowArbitraryNavigation = false
                };

                // Create a node component
                var nodeComponent = new NodeComponent <IDbProviderTable>(tables, (table) =>
                {
                    // Get the foreign keys of the table
                    var foreignKeys = foreignKeyColumns.Where(x => x.TableName == table.TableName).ToList();

                    // Get the tables whose primary key is related to one of the foreign keys
                    var foreignToPrimaryKeyTables = tables.Where(x => foreignKeys.Any(y => y.ReferencedTableName == x.TableName)).ToList();

                    // Get the foreign keys that are related to the primary key of the table
                    var relatedForeignKeys = foreignKeyColumns.Where(x => x.ReferencedTableName == table.TableName).ToList();

                    // Get the tables
                    var primaryToForeignKeyTables = tables.Where(x => relatedForeignKeys.Any(y => y.TableName == x.TableName)).ToList();

                    return(foreignToPrimaryKeyTables.Concat <IDbProviderTable, IDbProviderTable>(primaryToForeignKeyTables).Distinct());
                }, table => table.TableName);

                var nodeComponentScrollViewer = new ScrollViewer()
                {
                    Content = nodeComponent,
                    HorizontalScrollBarVisibility = ScrollBarVisibility.Visible
                };

                // Add it to the steps presenter
                stepsPresenter.Add("Query", nodeComponentScrollViewer, element => nodeComponent.NodePath.Model != null);

                // Create the form
                var form = new DataForm <QueryMap>()
                           .ShowInput(x => x.Name, settings => { settings.Name = CeidDiplomatikiDataModelHelpers.QueryMapMapper.Value.GetTitle(x => x.Name); settings.IsRequired = true; })
                           .ShowInput(x => x.Description, settings => settings.Name      = CeidDiplomatikiDataModelHelpers.QueryMapMapper.Value.GetTitle(x => x.Description))
                           .ShowStringColorInput(x => x.Color, settings => settings.Name = CeidDiplomatikiDataModelHelpers.QueryMapMapper.Value.GetTitle(x => x.Color));

                // Add it to the steps presenter
                stepsPresenter.Add("Info", form, (element) => element.Validate());

                // Show a dialog
                var dialogResult = await DialogHelpers.ShowStepsDialogAsync(this, "Query map creation", null, stepsPresenter, IconPaths.TablePath);

                // If we didn't get positive feedback...
                if (!dialogResult.Feedback)
                {
                    // Re enable the button
                    AddButton.IsEnabled = true;

                    // Return
                    return;
                }

                // Get the selected tables
                tables = nodeComponent.NodePath.DistinctModels;

                // Get the pairs
                var pairs = nodeComponent.NodePath.Pairs;

                // Get the node path
                var nodePath = nodeComponent.NodePath;

                // Get the columns of the table
                var columns = analyzer.GetColumns(DatabaseOptions.DatabaseName, null).Where(x => tables.Any(y => y.TableName == x.TableName)).ToList();

                // The joins collection
                var joins = new List <JoinMap>();

                // For every pair...
                foreach (var pair in pairs)
                {
                    JoinMap joinMap;

                    if (foreignKeyColumns.Any(x => x.TableName == pair.Value.Model.TableName && x.ReferencedTableName == pair.Key.Model.TableName))
                    {
                        // Get the foreign key column
                        var foreignKeyColumn = foreignKeyColumns.First(x => x.TableName == pair.Value.Model.TableName && x.ReferencedTableName == pair.Key.Model.TableName);

                        // Get the principle column
                        var principleColumn = columns.First(x => x.ColumnName == foreignKeyColumn.ReferencedColumnName);

                        // Get the foreign key column
                        var referencedColumn = columns.First(x => x.ColumnName == foreignKeyColumn.ColumnName);

                        // Create the join map
                        joinMap = new JoinMap(pair.Key.Model, principleColumn, pair.Value.Model, referencedColumn, pair.Key.Index, false);
                    }
                    else
                    {
                        // Get the foreign key column
                        var foreignKeyColumn = foreignKeyColumns.First(x => x.ReferencedTableName == pair.Value.Model.TableName && x.TableName == pair.Key.Model.TableName);

                        // Get the principle column
                        var principleColumn = columns.First(x => x.ColumnName == foreignKeyColumn.ColumnName);

                        // Get the foreign key column
                        var referencedColumn = columns.First(x => x.ColumnName == foreignKeyColumn.ReferencedColumnName);

                        // Create the join map
                        joinMap = new JoinMap(pair.Key.Model, principleColumn, pair.Value.Model, referencedColumn, pair.Key.Index, true);
                    }

                    // Add it to the joins
                    joins.Add(joinMap);
                }

                // Create the model
                var model = QueryMap.FromDataModel(DatabaseOptions, Database, tables, columns, joins);

                // Set it to the form
                form.Model = model;

                // Update its values
                form.UpdateModelValues();

                // Get the manager
                var manager = CeidDiplomatikiDI.GetCeidDiplomatikiManager;

                // Register the model
                await manager.RegisterAsync(model);

                // Save the changes
                var result = await manager.SaveChangesAsync();

                // If there was an error...
                if (!result.Successful)
                {
                    // Show the error
                    await result.ShowDialogAsync(this);

                    // Re enable the button
                    AddButton.IsEnabled = true;

                    // Return
                    return;
                }

                // Add it to the presenter
                DataPresenter.Add(model);

                // Re enable the button
                AddButton.IsEnabled = true;
            });

            // Add it to the content grid
            ContentGrid.Children.Add(AddButton);
        }
        /// <summary>
        /// Creates and returns the GUI in a form of a <see cref="FrameworkElement"/>
        /// </summary>
        /// <returns></returns>
        protected override FrameworkElement CreateBaseGUIElement()
        {
            // Create the content stack panel
            ContentStackPanel = new StackPanel()
            {
                Orientation = Orientation.Vertical
            };

            #region SQLite

            // Create the SQLite stack panel collapsible vertical menu
            SQLiteStackPanelCollapsibleVerticalMenu = new StackPanelCollapsibleVerticalMenu <UIElement>()
            {
                IsOpen = true,
                Text   = "SQLite"
            };

            // Create the SQLite options data grid
            SQLiteOptionsDataGrid = CeidDiplomatikiDataModelHelpers.CreateDefaultSQLiteOptionsDataModelDataGrid();

            SQLiteOptionsDataGrid.ConfigureOptions((container, grid, row, model) =>
            {
                container.AddDeleteOption(async(button) => await RemoveOptionAsync(model));
            });


            SQLiteOptionsDataGrid.Margin = new Thickness(NormalUniformMargin);

            // Add it to the collapsible menu
            SQLiteStackPanelCollapsibleVerticalMenu.Add(SQLiteOptionsDataGrid);

            // Add the collapsible menu to the stack panel
            ContentStackPanel.Children.Add(SQLiteStackPanelCollapsibleVerticalMenu);

            #endregion

            #region MySQL

            // Create the MySQL stack panel collapsible vertical menu
            MySQLStackPanelCollapsibleVerticalMenu = new StackPanelCollapsibleVerticalMenu <UIElement>()
            {
                IsOpen = true,
                Text   = "MySQL"
            };

            // Create the MySQL options data grid
            MySQLOptionsDataGrid = CeidDiplomatikiDataModelHelpers.CreateDefaultMySQLOptionsDataModelDataGrid();

            MySQLOptionsDataGrid.ConfigureOptions((container, grid, row, model) =>
            {
                container.AddOpenOption(async(button) =>
                {
                    var provider = SQLDatabaseProvider.MySQL;

                    // Get the analyzer
                    var analyzer = CeidDiplomatikiDI.GetDatabaseAnalyzer(provider);

                    // Get the connection string
                    model.TryGetConnectionString(out var connectionString);

                    // Get the database
                    var database = analyzer.GetDatabases().First(x => x.DatabaseName == model.DatabaseName);

                    // Show the page
                    await WindowsControlsDI.GetWindowsDialogManager.OpenAsync(model.DatabaseName, IconPaths.DatabasePath, () =>
                    {
                        return(new QueryMapsPage(database, model));
                    }, connectionString);
                });
                container.AddDeleteOption(async(button) => await RemoveOptionAsync(model));
            });



            MySQLOptionsDataGrid.Margin = new Thickness(NormalUniformMargin);

            // Add it to the collapsible menu
            MySQLStackPanelCollapsibleVerticalMenu.Add(MySQLOptionsDataGrid);

            // Add the collapsible menu to the stack panel
            ContentStackPanel.Children.Add(MySQLStackPanelCollapsibleVerticalMenu);

            #endregion

            #region SQLServer

            // Create the SQLServer stack panel collapsible vertical menu
            SQLServerStackPanelCollapsibleVerticalMenu = new StackPanelCollapsibleVerticalMenu <UIElement>()
            {
                IsOpen = true,
                Text   = "SQLServer"
            };

            // Create the SQLServer options data grid
            SQLServerOptionsDataGrid = CeidDiplomatikiDataModelHelpers.CreateDefaultSQLServerOptionsDataModelDataGrid();

            SQLServerOptionsDataGrid.ConfigureOptions((container, grid, row, model) =>
            {
                container.AddDeleteOption(async(button) => await RemoveOptionAsync(model));
            });


            SQLServerOptionsDataGrid.Margin = new Thickness(NormalUniformMargin);

            // Add it to the collapsible menu
            SQLServerStackPanelCollapsibleVerticalMenu.Add(SQLServerOptionsDataGrid);

            // Add the collapsible menu to the stack panel
            ContentStackPanel.Children.Add(SQLServerStackPanelCollapsibleVerticalMenu);

            #endregion

            #region PostgreSQL

            // Create the PostgreSQL stack panel collapsible vertical menu
            PostgreSQLStackPanelCollapsibleVerticalMenu = new StackPanelCollapsibleVerticalMenu <UIElement>()
            {
                IsOpen = true,
                Text   = "PostgreSQL"
            };

            // Create the PostgreSQL options data grid
            PostgreSQLOptionsDataGrid = CeidDiplomatikiDataModelHelpers.CreateDefaultPostgreSQLOptionsDataModelDataGrid();

            PostgreSQLOptionsDataGrid.ConfigureOptions((container, grid, row, model) =>
            {
                container.AddDeleteOption(async(button) => await RemoveOptionAsync(model));
            });

            PostgreSQLOptionsDataGrid.Margin = new Thickness(NormalUniformMargin);

            // Add it to the collapsible menu
            PostgreSQLStackPanelCollapsibleVerticalMenu.Add(PostgreSQLOptionsDataGrid);

            // Add the collapsible menu to the stack panel
            ContentStackPanel.Children.Add(PostgreSQLStackPanelCollapsibleVerticalMenu);

            #endregion

            // Return the content stack panel
            return(ContentStackPanel);
        }