public void SyncToDsDwMap(DsDwMap_M aDsDwMap, bool aReload) { ReferenceToDsDwMap = aDsDwMap; if (aReload) { MeasureGroupList.AList.Clear(); DimensionList.AList.Clear(); } if (aDsDwMap != null) { foreach (DsDwTableMap_M aDsDwTableMap in aDsDwMap.DsDwTableMapList.Where(tm => tm != null)) { CbMeasureGroup_M aCbMeasureGroup = MeasureGroupList.AList.FirstOrDefault(mg => mg.ReferenceToDsDwTableMapGuid == aDsDwTableMap.MyGuid); if (aCbMeasureGroup != null) { aCbMeasureGroup.ReferenceToDsDwTableMap = aDsDwTableMap; } else { aCbMeasureGroup = new CbMeasureGroup_M { Name = aDsDwTableMap.DwSchemaTableName.Replace("dbo.", "").Replace(".", ""), ReferenceToDsDwTableMap = aDsDwTableMap }; MeasureGroupList.AList.Add(aCbMeasureGroup); } CbDimension_M aCbDimension = DimensionList.AList.FirstOrDefault(mg => mg.ReferenceToDsDwTableMapGuid == aDsDwTableMap.MyGuid); if (aCbDimension != null) { aCbDimension.ReferenceToDsDwTableMap = aDsDwTableMap; } else { aCbDimension = new CbDimension_M { Name = aDsDwTableMap.DwSchemaTableName.Replace("dbo.", "").Replace(".", ""), ReferenceToDsDwTableMap = aDsDwTableMap }; DimensionList.AList.Add(aCbDimension); } aCbMeasureGroup.FactDimension = aCbDimension; if (aDsDwTableMap.DsDwColumnMapList != null) { foreach (DsDwColumnMap_M aDsDwColumnMap in aDsDwTableMap.DsDwColumnMapList.Where(cm => cm != null && cm.Include)) { CbMeasure_M aCbMeasure = aCbMeasureGroup.MeasureList.FirstOrDefault(mg => mg.ReferenceToDsDwColumnMapGuid == aDsDwColumnMap.MyGuid); if (aCbMeasure != null) { aCbMeasure.ReferenceToDsDwColumnMap = aDsDwColumnMap; } else { aCbMeasure = new CbMeasure_M { Name = aDsDwColumnMap.DwColumn.Name, ReferenceToDsDwColumnMap = aDsDwColumnMap }; aCbMeasureGroup.MeasureList.Add(aCbMeasure); aCbMeasure.UpdateProperties(); } CbDimensionAttribute_M aCbDimensionAttribute = aCbDimension.AttributeList.FirstOrDefault(mg => mg.ReferenceToDsDwColumnMapGuid == aDsDwColumnMap.MyGuid); if (aCbDimensionAttribute != null) { aCbDimensionAttribute.ReferenceToDsDwColumnMap = aDsDwColumnMap; } else { aCbDimensionAttribute = new CbDimensionAttribute_M { Name = aDsDwColumnMap.DwColumn.Name, ReferenceToDsDwColumnMap = aDsDwColumnMap }; aCbDimension.AttributeList.Add(aCbDimensionAttribute); aCbDimensionAttribute.UpdateProperties(); } } aCbMeasureGroup.MeasureList.RemoveAll(m => m == null || m.ReferenceToDsDwColumnMap == null); aCbDimension.AttributeList.RemoveAll(a => a == null || a.ReferenceToDsDwColumnMap == null); } } MeasureGroupList.AList.RemoveAll(mg => mg == null || mg.ReferenceToDsDwTableMap == null); DimensionList.AList.RemoveAll(d => d == null || d.ReferenceToDsDwTableMap == null); //fix ReferenceToDimension links //SerializableObject.FixAllReferences(); foreach (CbMeasureGroup_M aCbMeasureGroup in MeasureGroupList.AList) { foreach (CbMeasure_M aCbMeasure in aCbMeasureGroup.MeasureList.Where(m => m != null && m.ReferenceToDsDwColumnMap != null)) { if (aCbMeasure.ReferenceToDsDwColumnMap.Transformation == DsDwColumnTransformation.ForeignKey) { aCbMeasure.ReferenceToDimension = DimensionList.AList.FirstOrDefault( dm => dm.ReferenceToDsDwTableMap == aCbMeasure.ReferenceToDsDwColumnMap.DwForeignKeyReferencedTableMap); } else { aCbMeasure.ReferenceToDimension = null; } } } } }
public void InitializeDsDwMap() { if (DwRebuildDsDwTableMap) { AppendLogLine("Rebuilding table map..."); DsDwMapChanged = true; DsDwMap = null; } if (DsDwMap == null) { DsDwMap = new DsDwMap_M(); } else { SycDwTableViewMapList(); } foreach (TableView_M aTableView in DwTableViewList) { if ((aTableView != null) && (!DsDwMap.DsDwTableMapList.Any(tm => tm.DsObjectId == aTableView.ObjectId))) { DsDwMapChanged = true; AppendLogLine(string.Format("Processing table/view: {0}...", aTableView.Name)); DsDwTableMap_M aDsDwTableMap = new DsDwTableMap_M { DsIsTable = aTableView.IsTable, DsTableName = aTableView.Name, DsSchemaName = aTableView.Schema, DwTableName = aTableView.Name, DwSchemaName = aTableView.Schema, DsRowCount = aTableView.RowCount, DsObjectId = aTableView.ObjectId, DsDatabaseName = DsDbName, DsServerName = DsDbServerName }; DsDwMap.DsDwTableMapList.Add(aDsDwTableMap); TableViewBase aTableViewBase; if (aTableView.IsTable) { aTableViewBase = _DsDb.Tables.Cast <Table>().FirstOrDefault(t => t.ID == aTableView.ObjectId); } else { aTableViewBase = _DsDb.Views.Cast <Microsoft.SqlServer.Management.Smo.View>() .FirstOrDefault(t => t.ID == aTableView.ObjectId); } if (aTableViewBase != null) { foreach (Column aColumn in aTableViewBase.Columns) { DsDwColumnMap_M aDsDwColumnMap = new DsDwColumnMap_M { DsColumn = new DsDwColumn_M(aColumn), DwColumn = new DsDwColumn_M(aColumn), Include = true }; if (aColumn.InPrimaryKey) { aDsDwColumnMap.Transformation = DsDwColumnTransformation.BusinesKey; aDsDwTableMap.DsDwColumnMapList.Insert(0, aDsDwColumnMap); } else { aDsDwColumnMap.Transformation = DsDwColumnTransformation.Scd1; aDsDwTableMap.DsDwColumnMapList.Add(aDsDwColumnMap); } } if (aTableView.IsTable) { AddExistingForeignKeysTable((Table)aTableViewBase, aDsDwTableMap); } if (!aTableView.IsTable) //this is a view { aTableViewBase = _DsDb.Views.Cast <Microsoft.SqlServer.Management.Smo.View>() .FirstOrDefault(t => t.ID == aTableView.ObjectId); ParseViewSql aParseViewSql = new ParseViewSql(); Microsoft.SqlServer.Management.Smo.View aView = (aTableViewBase as Microsoft.SqlServer.Management.Smo.View); if (aParseViewSql.ParseView(aView.TextHeader + aView.TextBody)) { FillColumnAndForeignKeyInfoView(aParseViewSql.ColumnList, aDsDwTableMap); } } AddDwSupportColumns(aDsDwTableMap); } } else { //Update DbName and ServerName DsDwTableMap_M aUpdateDsDwTableMap = DsDwMap.DsDwTableMapList.FirstOrDefault(tm => tm.DsObjectId == aTableView.ObjectId); if (aUpdateDsDwTableMap != null) { aUpdateDsDwTableMap.DsDatabaseName = DsDbName; aUpdateDsDwTableMap.DsServerName = DsDbServerName; } } } if (DsDwMapChanged || true) { AppendLogLine(string.Format("Always rebuilding foreign key links.")); DsDwMap.ConfigureForeignKeyTransformations(); } else { AppendLogLine(string.Format("Map not changed, not rebuilding foreign key links.")); } DsDwMapChanged = false; NotifyPropertyChanged("DsDwMap"); }