/// <summary> /// Creates and adds the required GUI elements /// </summary> private void CreateGUI() { // Create the add button AddButton = ControlsFactory.CreateStandardAddCircularButton(); AddButton.Command = new RelayCommand(async() => { // Create the form var form = new DataForm <DataGridPresenterMap>(new DataGridPresenterMap(QueryMap)) { Mapper = CeidDiplomatikiDataModelHelpers.DataGridPresenterMapMapper.Value } .ShowInput(x => x.Name, settings => { settings.Name = CeidDiplomatikiDataModelHelpers.DataGridPresenterMapMapper.Value.GetTitle(x => x.Name); settings.IsRequired = true; }) .ShowInput(x => x.Description, settings => settings.Name = CeidDiplomatikiDataModelHelpers.DataGridPresenterMapMapper.Value.GetTitle(x => x.Description)) .ShowStringColorInput(x => x.Color, settings => settings.Name = CeidDiplomatikiDataModelHelpers.DataGridPresenterMapMapper.Value.GetTitle(x => x.Color)); // Show an add dialog var dialogResult = await DialogHelpers.ShowConventionalAddDialogAsync(this, "Data grid creation", null, form); // If we didn't get positive feedback... if (!dialogResult.Feedback) { // Return return; } // Get the manager var manager = CeidDiplomatikiDI.GetCeidDiplomatikiManager; // Register it QueryMap.Add(form.Model); // Save the changes var result = await manager.SaveChangesAsync(); // If there was an error... if (!result.Successful) { // Show the error await result.ShowDialogAsync(this); // Return return; } // Add the model DataPresenter.Add(form.Model); }); // Add it to the content grid ContentGrid.Children.Add(AddButton); }
/// <summary> /// Creates and returns a <see cref="QueryMap"/> from the specified <paramref name="dataModel"/>. /// NOTE: If the model is not set, then the default values are used! /// </summary> /// <param name="databaseOptions">The database options</param> /// <param name="database">The database that contains the mapped table</param> /// <param name="columns">The columns of the tables</param> /// <param name="tables">The tables</param> /// <param name="joins">The joins</param> /// <param name="dataModel">The data model</param> /// <returns></returns> public static QueryMap FromDataModel(BaseDatabaseOptionsDataModel databaseOptions, IDbProviderDatabase database, IEnumerable <IDbProviderTable> tables, IEnumerable <IDbProviderColumn> columns, IEnumerable <JoinMap> joins, QueryMapDataModel dataModel = null) { // Create the map var queryMap = new QueryMap(databaseOptions, database, tables, columns, joins); // If there is a data model... if (dataModel != null) { queryMap.Id = dataModel.Id; queryMap.Color = dataModel.Color; queryMap.Description = dataModel.Description; queryMap.Name = dataModel.Name; } var tableToTypeBuilderMapper = new Dictionary <IDbProviderTable, TypeBuilder>(); // For every table... foreach (var table in tables) { // Create the builder var typeBuilder = CeidDiplomatikiHelpers.GetTypeBuilder(queryMap.Id + "-" + table.TableName); // For every table column... foreach (var column in columns.Where(x => x.TableName == table.TableName)) { // Create a property TypeBuilderHelpers.CreateProperty(typeBuilder, column.ColumnName, column.DataType); } // Map it tableToTypeBuilderMapper.Add(table, typeBuilder); } // For every join... foreach (var join in joins) { // Get the principle model type builder var principleModelTypeBuilder = tableToTypeBuilderMapper.First(x => x.Key == join.Table).Value; // Get the referenced model type builder var referencedModelTypeBuilder = tableToTypeBuilderMapper.First(x => x.Key == join.ReferencedTable).Value; // Create the principle navigation property type var principleNavigationPropertyType = typeof(IEnumerable <>).MakeGenericType(referencedModelTypeBuilder); // Add it to the principle model TypeBuilderHelpers.CreateProperty(principleModelTypeBuilder, CeidDiplomatikiHelpers.GetPluralForm(join.ReferencedTable.TableName), principleNavigationPropertyType); // Add the foreign navigation property type TypeBuilderHelpers.CreateProperty(referencedModelTypeBuilder, join.Table.TableName, principleModelTypeBuilder); } var tableToTypeMapper = new Dictionary <IDbProviderTable, Type>(); // For every table to type builder map... foreach (var map in tableToTypeBuilderMapper) { // Build the type var type = map.Value.CreateType(); if (joins.Any(x => x.Index == 0 && x.Table.TableName == map.Key.TableName)) { queryMap.RootType = type; } else if (joins.Count() == 0) { queryMap.RootType = type; } // Map it tableToTypeMapper.Add(map.Key, type); } // Set the types to the query queryMap.DataModelTypes = tableToTypeMapper.Select(x => x.Value).ToList(); // Create the db context type builder var dbContextTypeBuilder = CeidDiplomatikiHelpers.GetTypeBuilder(Guid.NewGuid().ToString() + "-DbContext"); // Set the base type var baseType = typeof(PresenterDbContext); // Inherit from the PresenterDbContext dbContextTypeBuilder.SetParent(baseType); // For every data model type... foreach (var map in tableToTypeMapper) { // Create the DbSet type that will be set as a property to the DbContext var dbSetType = typeof(DbSet <>).MakeGenericType(map.Value); // Add it to the type TypeBuilderHelpers.CreateProperty(dbContextTypeBuilder, map.Key.TableName, dbSetType); } // Create the constructors TypeBuilderHelpers.CreatePassThroughConstructors(dbContextTypeBuilder, baseType); // Create the db context type queryMap.DbContextType = dbContextTypeBuilder.CreateType(); // If there is a data model... // NOTE: We create the column and the data presenter maps at the end of the initialization // because some properties of the query map are required to get set! if (dataModel != null) { dataModel.PropertyMaps.ForEach(model => queryMap.Add(PropertyMap.FromDataModel(queryMap, queryMap.DataModelTypes.First(x => queryMap.GetTableName(x) == model.TableName), dataModel.PropertyMaps.First(x => x.TableName == model.TableName && x.ColumnName == model.ColumnName)))); dataModel.DataGridPresenterMaps.ForEach(model => queryMap.Add(DataGridPresenterMap.FromDataModel(queryMap, model))); } // Return the map return(queryMap); }
/// <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; // Create a steps presenter var stepsPresenter = new StepsPresenter() { AllowArbitraryNavigation = false }; // Create a columns container var propertiesContainer = new UniformGridCollapsibleVerticalMenuCategorizingItemsControl <PropertyInfo, InformationalButton>() { Columns = 3, CategoryNameGetter = (prop) => prop.DeclaringType.Name.Split("-").Last() }; propertiesContainer.Linker = (prop) => new InformationalButton() { MinWidth = 0, Text = prop.Name, Command = new RelayCommand(() => { foreach (var element in propertiesContainer.Elements) { element.Selected = false; } propertiesContainer.GetElement(prop).Selected = true; }) }; // Add the tables propertiesContainer.SetItemsSource(QueryMap.DataModelTypes.SelectMany(x => x.GetProperties().Where(y => !DataPresenter.Items.Any(z => z.PropertyInfo == y))).OrderBy(x => x.Name)); // Add it to the steps presenter stepsPresenter.Add("Property", propertiesContainer, (element) => element.Elements.Any(x => x.Selected)); // Create the form var form = new DataForm <PropertyMap>() { Mapper = CeidDiplomatikiDataModelHelpers.PropertyMapMapper.Value } .ShowInput(x => x.Name, settings => settings.IsRequired = true) .ShowInput(x => x.Description) .ShowStringColorInput(x => x.Color) .ShowSelectMultipleOptionsInput(x => x.Attributes, (form, propertyInfo) => new DropDownMenuOptionsFormInput <ColumnAttribute>(form, propertyInfo, ColumnAttributes.Data.Value, x => x.Name), null, settings => settings.IsRequired = true) .ShowNumericInput(x => x.Order) .ShowInput(x => x.DefaultValue) .ShowInput(x => x.IsEditable) .ShowInput(x => x.IsRequired) .ShowInput(x => x.IsPreview); // Add it to the steps presenter stepsPresenter.Add("Info", form, (element) => element.Validate()); // Show a dialog var dialogResult = await DialogHelpers.ShowStepsDialogAsync(this, "Property map creation", null, stepsPresenter, IconPaths.TableColumnPath); // If we didn't get positive feedback... if (!dialogResult.Feedback) { // Re enable the button AddButton.IsEnabled = true; // Return return; } // Get the selected property var property = propertiesContainer.Get(propertiesContainer.Elements.First(x => x.Selected)); // Create the model var model = new PropertyMap(QueryMap, property.DeclaringType, property, QueryMap.GetColumnOrNull(property)); // Set it to the form form.Model = model; // Update its values form.UpdateModelValues(); // Get the manager var manager = CeidDiplomatikiDI.GetCeidDiplomatikiManager; // Register the model QueryMap.Add(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); }