/// <summary> /// Performs the required includes on the specified <paramref name="queryable"/> /// </summary> /// <param name="queryable">The queryable</param> /// <param name="joinMap">The join map</param> /// <returns></returns> private IQueryable PerformThenInclude(IQueryable queryable, JoinMap joinMap) { // Get the ThenInclude<TEntity, TPreviousProperty, TProperty> method var thenIncludeMethod = typeof(EntityFrameworkQueryableExtensions).GetMethods().Where(x => x.Name == nameof(EntityFrameworkQueryableExtensions.ThenInclude)).ElementAt(0); // Get the type of the model var modelType = queryable.GetType().GetGenericArguments()[0]; // Get the previous property type var previousPropertyType = TypeHelpers.GetNonEnumerableType(queryable.GetType().GetGenericArguments()[1]); // Get the navigation property name var navigationPropertyName = CeidDiplomatikiHelpers.GetPluralForm(joinMap.ReferencedTable.TableName); // Get the navigation property var navigationProperty = previousPropertyType.GetProperty(navigationPropertyName); // Create the expression var expression = ExpressionHelpers.CreatePropertySelectorExpression(previousPropertyType, navigationProperty); // Set the generic arguments (TEntity, TPreviousProperty, TProperty) thenIncludeMethod = thenIncludeMethod.MakeGenericMethod(modelType, previousPropertyType, navigationProperty.PropertyType); // Call the ThenInclude method queryable = (IQueryable)thenIncludeMethod.Invoke(null, new object[] { queryable, expression }); // Return the queryable return(queryable); }
/// <summary> /// Traverses the join path till it reached the join that's the root of the chained joins /// that end with the specified <paramref name="joinMap"/> /// </summary> /// <param name="joinMap">The join map</param> /// <returns></returns> private IEnumerable <JoinMap> ComputePath(JoinMap joinMap) { // Declare the result var result = new List <JoinMap>(); // Declare the current map var current = joinMap; while (current != null) { result.Add(current); current = Joins.FirstOrDefault(x => x.ReferencedTable == current.Table); } // Invert the list result.Reverse(); // Return the result return(result); }
/// <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> /// 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; }