private static ObservableCollection <TableModifierModel> LoadTableTemplates(List <TableModifier> userConfigTemplates, SchemaMetadata schemaMetadata) { var returnVM = new ObservableCollection <TableModifierModel>(); foreach (var tableMetadata in schemaMetadata) { var tableVM = new TableModifierModel(); tableVM._name = tableMetadata.Name; List <DataBuilder> userConfigDataBuilders = null; List <DerivativeSubTable> userConfigDerivativeSubTable = null; ForeignKeys userConfigFk = null; var userConfigTemplate = userConfigTemplates.FirstOrDefault(t => t.Name.ToLower() == tableMetadata.Name); if (userConfigTemplate != null) { userConfigDataBuilders = userConfigTemplate.DataBuilders; if (userConfigTemplate.DerativeTables != null) { tableVM._derativeTablesGlobalAccess = userConfigTemplate.DerativeTables.GlobalAccess; tableVM._derativeTablesGlobalCascade = userConfigTemplate.DerativeTables.GlobalCascade; userConfigDerivativeSubTable = userConfigTemplate.DerativeTables.DerativeSubTables; } if (userConfigTemplate.ForeignKeys != null) { userConfigFk = userConfigTemplate.ForeignKeys; } tableVM._isStatic = userConfigTemplate.IsStatic; } tableVM._dataBuilders = LoadDataBuildersTemplate(userConfigDataBuilders, tableMetadata.ColumnsDefinition); tableVM._derivativeTables = LoadDerivativeTableTemplate(userConfigDerivativeSubTable, tableMetadata.DerivativeTables); tableVM._foreignKeys = LoadForeingKeyTemplate(userConfigFk, tableMetadata.ForeignKeys); returnVM.Add(tableVM); } return(returnVM); }
private static bool MergeTable(TableModifierModel mergedTable, List <TableModifier> userConfigTables, TableMetadata defaultTable) { var hasChange = false; var userConfigTable = userConfigTables.FirstOrDefault(t => t.Name == mergedTable.Name); if (defaultTable == null) { hasChange = true; } //Add new if (userConfigTable == null) { userConfigTable = new TableModifier { Name = mergedTable.Name }; userConfigTables.Add(userConfigTable); } userConfigTable.IsStatic = mergedTable.IsStatic; userConfigTable.DerativeTables.GlobalAccess = mergedTable.DerativeTablesGlobalAccess; userConfigTable.DerativeTables.GlobalCascade = mergedTable.DerativeTablesGlobalCascade; if (mergedTable.IsStatic || mergedTable.DerativeTablesGlobalAccess != DerivativeTableAccess.NotSet || mergedTable.DerativeTablesGlobalCascade == true) { hasChange = true; } //Clear userConfigTable.ForeignKeys.ForeignKeyAdd = new List <ForeignKeyAdd>(); userConfigTable.ForeignKeys.ForeignKeyRemove = new ForeignKeyRemove(); userConfigTable.DerativeTables.DerativeSubTables = new List <DerivativeSubTable>(); userConfigTable.DataBuilders = new List <DataBuilder>(); //Merge FK for (int i = mergedTable.ForeignKeys.Count - 1; i >= 0; i--) { var mergedFk = mergedTable.ForeignKeys[i]; var defaultFk = defaultTable.ForeignKeys.FirstOrDefault(f => f.ServerIdTo.ToString() == mergedFk.ServerIdTo && f.DatabaseTo == mergedFk.DatabaseTo && f.SchemaTo == mergedFk.SchemaTo && f.TableTo == mergedFk.TableTo); if (MergeForeignKey(mergedFk, mergedTable.ForeignKeys, userConfigTable.ForeignKeys, defaultFk, defaultTable.ColumnsDefinition)) { hasChange = true; } } //Merge derivative table for (int i = mergedTable.DerivativeTables.Count - 1; i >= 0; i--) { var mergedDerivativeTable = mergedTable.DerivativeTables[i]; var defaultDt = defaultTable.DerivativeTables.FirstOrDefault(d => d.ServerId.ToString() == mergedDerivativeTable.ServerId && d.Database == mergedDerivativeTable.Database && d.Schema == mergedDerivativeTable.Schema && d.Table == mergedDerivativeTable.Table); if (MergedDerivativeTable(mergedDerivativeTable, mergedTable.DerivativeTables, userConfigTable.DerativeTables, defaultDt)) { hasChange = true; } } //Merge data builders foreach (var mergedDataBuilder in mergedTable.DataBuilders) { if (MergeDataBuilders(mergedDataBuilder, userConfigTable.DataBuilders)) { hasChange = true; } } //Cleaning if (!userConfigTable.DataBuilders.Any()) { userConfigTable.DataBuilders = null; } if (!userConfigTable.ForeignKeys.ForeignKeyAdd.Any()) { userConfigTable.ForeignKeys.ForeignKeyAdd = null; } if (!userConfigTable.ForeignKeys.ForeignKeyRemove.Columns.Any()) { userConfigTable.ForeignKeys.ForeignKeyRemove = null; } if (userConfigTable.ForeignKeys.ForeignKeyAdd == null && userConfigTable.ForeignKeys.ForeignKeyRemove == null) { userConfigTable.ForeignKeys = null; } if (mergedTable.DerativeTablesGlobalAccess == DerivativeTableAccess.NotSet && mergedTable.DerativeTablesGlobalCascade == false && !userConfigTable.DerativeTables.DerativeSubTables.Any()) { userConfigTable.DerativeTables = null; } //If no change has been detected with the default config if (!hasChange) { userConfigTables.Remove(userConfigTable); } return(hasChange); }