/// <summary> /// Initializes a new instance of the Model class using multiple parameters. /// </summary> /// <param name="parentTabularModel">TabularModel object that the Model object belongs to.</param> /// <param name="tomModel">Tabular Object Model Model object abtstracted by the Model class.</param> public Model(TabularModel parentTabularModel, Tom.Model tomModel) : base(tomModel) { _parentTabularModel = parentTabularModel; _tomModel = tomModel; PopulateProperties(); }
public static bool CheckModel( TabModel.Model model, out List <TabModel.Table> unsupportedTables, out List <TabModel.Measure> unsupportedMeasures, out List <TabModel.Relationship> unsupportedRelationships) { // Check tables unsupportedTables = model.Tables.Where(t => t.Name == "Date").ToList(); // TODO: remove the filter of Date table if/when Microsoft fixes the bug on the service bool checkedTables = (unsupportedTables.Count == 0); // Check measures List <TabModel.Measure> allMeasures = model.Tables.SelectMany(t => t.Measures).Select(m => m).ToList(); bool checkedMeasures = CheckUnsupportedMeasures(allMeasures, out unsupportedMeasures, out List <TabModel.Measure> supportedMeasures); // Check relationships unsupportedRelationships = model.Relationships.Where(r => { // Inactive relationships are not supported if (!r.IsActive) { return(true); } // 1:1 and M:M cardinality relationships are not supported TabModel.SingleColumnRelationship sr = r as TabModel.SingleColumnRelationship; return(sr?.FromCardinality == sr?.ToCardinality); }).ToList(); bool checkedRelationships = (unsupportedRelationships.Count == 0); return(checkedMeasures && checkedRelationships); }
internal static Model CreateFromMetadata(TOM.Model metadataObject) { var obj = new Model(metadataObject); obj.Init(); return(obj); }
private static void ReplaceRolesFromDestination(JObject sourceModel, TOM.Model destinationModel) { var roles = new JArray(); sourceModel["roles"] = roles; foreach (var role in destinationModel.Roles) { roles.Add(JObject.Parse(TOM.JsonSerializer.SerializeObject(role))); } }
private static Microsoft.AnalysisServices.Tabular.Model CreateModel(string modelName, string modelDescription) { var model = new Microsoft.AnalysisServices.Tabular.Model() { Name = modelName, Description = modelDescription }; return(model); }
public static ReferenceModel Create(TOM.Model model) { return(new ReferenceModel { Name = model.Name, Description = model.Description, Tables = model.Tables.Select(t => ReferenceTable.Create(t)).ToArray(), Perspectives = model.Perspectives.Select(p => ReferencePerspective.Create(p)).ToArray(), Roles = model.Roles.Select(r => ReferenceRole.Create(r)).ToArray() }); }
private TomExtractor(Tom.Model model, string extractorApp, string extractorVersion) { tomModel = model; AssemblyName tomExtractorAssemblyName = this.GetType().Assembly.GetName(); Version version = tomExtractorAssemblyName.Version; DaxModel = new Dax.Metadata.Model(tomExtractorAssemblyName.Name, tomExtractorAssemblyName.Version.ToString(), extractorApp, extractorVersion); if (tomModel != null) { PopulateModel(); } }
static void AddSalesRegionMeasures() { // DAX query to be submitted totabuar database engine String query = "EVALUATE( VALUES(Customers[Sales Region]) )"; AdomdConnection adomdConnection = new AdomdConnection(connectString); adomdConnection.Open(); AdomdCommand adomdCommand = new AdomdCommand(query, adomdConnection); AdomdDataReader reader = adomdCommand.ExecuteReader(); // open connection use TOM to create new measures TOM.Server server = new TOM.Server(); server.Connect(connectString); TOM.Model model = server.Databases[0].Model; TOM.Table salesTable = model.Tables["Sales"]; String measureDescription = "Auto Measures"; // delete any previously created "Auto" measures foreach (TOM.Measure m in salesTable.Measures) { if (m.Description == measureDescription) { salesTable.Measures.Remove(m); model.SaveChanges(); } } // Create the new measures while (reader.Read()) { String SalesRegion = reader.GetValue(0).ToString(); String measureName = $"{SalesRegion} Sales"; TOM.Measure measure = new TOM.Measure() { Name = measureName, Description = measureDescription, DisplayFolder = "Auto Measures", FormatString = "$#,##0", Expression = $@"CALCULATE( SUM(Sales[SalesAmount]), Customers[Sales Region] = ""{SalesRegion}"" )" }; salesTable.Measures.Add(measure); } model.SaveChanges(); reader.Dispose(); adomdConnection.Close(); }
public static CreateDatasetRequest GetDatasetRequest( string datasetName, TabModel.Model model, Action <TabModel.Measure> unsupportedMeasureAction, Action <TabModel.Relationship> unsupportedRelationshipAction ) { return(GetDataset( datasetName, model.Tables, model.Relationships, unsupportedMeasureAction, unsupportedRelationshipAction )); }
private void ReplaceDataSourcesFromDestination(JObject sourceModel, TOM.Model destinationModel) { // Replace existing data sources with those in the target DB: if (sourceModel["dataSources"] is JArray dataSources) { foreach (var destinationDataSource in destinationModel.DataSources) { dataSources.FirstOrDefault(d => d.Value <string>("name").EqualsI(destinationDataSource.Name))?.Remove(); dataSources.Add(JObject.Parse(TOM.JsonSerializer.SerializeObject(destinationDataSource, new TOM.SerializeOptions { IncludeRestrictedInformation = true }))); } } }
private static void ProcessTabularRelationships(Microsoft.AnalysisServices.Tabular.Model model, DataSet result) { var rels = new List <ModelAnalyzerRelationship>(); foreach (SingleColumnRelationship scanRel in model.Relationships) { // Check whether the relationship is a known type, otherwise skips it SingleColumnRelationship rel = scanRel as SingleColumnRelationship; if (rel != null) { rels.Add(new ModelAnalyzerRelationship(rel)); } } var relationshipsTable = rels.ToDataTable(); relationshipsTable.TableName = "Relationships"; result.Tables.Add(relationshipsTable); }
/// <summary> /// Returns a collection of Table objects that do not have a DataSource associated with them. These can be calculated tables or tables with M partitions that do not refer to a DataSource. /// </summary> /// <returns></returns> public TableCollection WithoutDataSource(Microsoft.AnalysisServices.Tabular.Model model) { TableCollection tablesWithDataSource = new TableCollection(); foreach (Microsoft.AnalysisServices.Tabular.DataSource dataSource in model.DataSources) { tablesWithDataSource.AddRange(this.FilterByDataSource(dataSource)); } TableCollection tablesWithoutDataSource = new TableCollection(); foreach (Table table in this) { if (!tablesWithDataSource.ContainsName(table.Name)) { tablesWithoutDataSource.Add(table); } } return(tablesWithoutDataSource); }
private static void ReplaceRoleMembersFromDestination(JObject sourceModel, TOM.Model destinationModel) { if (sourceModel["roles"] is JArray roles && roles.Count > 0) { foreach (var role in roles) { var members = new JArray(); role["members"] = members; // Remove members if present and add original: var roleName = role["name"].Value <string>(); if (destinationModel.Roles.Contains(roleName)) { foreach (var member in destinationModel.Roles[roleName].Members) { members.Add(JObject.Parse(TOM.JsonSerializer.SerializeObject(member))); } } } } }
public static Dax.Metadata.Model GetDaxModel(string serverName, string databaseName, string applicationName, string applicationVersion, bool readStatisticsFromData = true) { Microsoft.AnalysisServices.Database db = GetDatabase(serverName, databaseName); Microsoft.AnalysisServices.Tabular.Model tomModel = db.Model; var daxModel = Dax.Metadata.Extractor.TomExtractor.GetDaxModel(tomModel, applicationName, applicationVersion); var connectionString = GetConnectionString(serverName, databaseName); using (var connection = new OleDbConnection(connectionString)) { // Populate statistics from DMV Dax.Metadata.Extractor.DmvExtractor.PopulateFromDmv(daxModel, connection, serverName, databaseName, applicationName, applicationVersion); // Populate statistics by querying the data model if (readStatisticsFromData) { Dax.Metadata.Extractor.StatExtractor.UpdateStatisticsModel(daxModel, connection); } } return(daxModel); }
public static Dax.Metadata.Model GetDaxModel(Tom.Model model, string extractorApp, string extractorVersion) { TomExtractor extractor = new TomExtractor(model, extractorApp, extractorVersion); return(extractor.DaxModel); }
private TomExtractor(Tom.Model model) : this(model, null, null) { }
//TODO: Add direct query support /* Complete feature list, to be noted when complete support added * - Database * - Direct Query * - Datasources (Done) * - Tables (Done) * - Translation of table (Done) * - Attributes (Done) * - Translation of Attribute (Done) * - Hierarchies (Done) * - Translation of Hierarchies (Done) * - Levels (Done) * - Translation of Levels (Done) * - Measures (Done) * - Translation of Measures (Done) * - KPI's (Done) * - Perspectives (Done) * - Roles (Done) * - Row Level Security (Done) * - Members (Done) * - Relationships (Done) */ /// <summary> /// Given a 1200 or 1400 model, produces an equivelant 1103 model. Incompatible features are not added. /// </summary> /// <param name="TOMDatabase"></param> /// <returns></returns> public static AMO.Database ToAMODatabase(TOM.Database TOMDatabase) { /* The Database representing a Tabular model rarely has a 1 to 1 mapping between * a TOM object and an AMO object. * * Some objects which DO have 1:1 mappings (at least, in a logical sense): * - DataSources * - Hierarchies * - Levels * - Perspectives * - Partitions * - Translations (however, they are structured differently) * - Annotations (These can also accept XML nodes, not just strings) * * The following do NOT: * - Tables * - Columns * - Measures * - Relationships * - Roles * * The following is, surprisingly, supported: * - Tabular Actions * - HideMemberIf * - Display Folders * - Custom Format strings * - Translations * - Similar to 1200, more than one column with the same source column * * Please note this does NOT produce a file able to be opened by Visual Studio. * Unlike 1200 models, Visual Studio cannot open all valid 1103 models. * It relies heavily on custom annotations, understood mainly through trial and error. * * In addition to these custom annotations, some features have no alternative (custom format strings), and * are completely unsupported by both Visual Studio and BIDS. * * Display Folders, Tabular Actions, and Translations are all supported using BIDS. * * These drastically increase the database/file size, and as such are optionally added in a seperate function. */ TOM.Model TOMModel = TOMDatabase.Model; //Create Database AMO.Database AMODatabase = new AMO.Database(TOMDatabase.Name, TOMDatabase.Name); //Initialise with default values. AMODatabase.StorageEngineUsed = AMO.StorageEngineUsed.InMemory; AMODatabase.CompatibilityLevel = SQL2012SP1; //DataSource has 1:1 mapping with AMO object #region DataSources foreach (TOM.ProviderDataSource TOMDataSource in TOMModel.DataSources) { AMO.DataSource AMODataSource = new AMO.RelationalDataSource(TOMDataSource.Name, TOMDataSource.Name); AMODataSource.Description = TOMDataSource.Description; AMODataSource.ConnectionString = TOMDataSource.ConnectionString; AMODataSource.ImpersonationInfo = new AMO.ImpersonationInfo(); switch (TOMDataSource.ImpersonationMode) { case TOM.ImpersonationMode.Default: AMODataSource.ImpersonationInfo.ImpersonationMode = AMO.ImpersonationMode.Default; break; case TOM.ImpersonationMode.ImpersonateAccount: AMODataSource.ImpersonationInfo.ImpersonationMode = AMO.ImpersonationMode.ImpersonateAccount; break; case TOM.ImpersonationMode.ImpersonateAnonymous: AMODataSource.ImpersonationInfo.ImpersonationMode = AMO.ImpersonationMode.ImpersonateAnonymous; break; case TOM.ImpersonationMode.ImpersonateCurrentUser: AMODataSource.ImpersonationInfo.ImpersonationMode = AMO.ImpersonationMode.ImpersonateCurrentUser; break; case TOM.ImpersonationMode.ImpersonateServiceAccount: AMODataSource.ImpersonationInfo.ImpersonationMode = AMO.ImpersonationMode.ImpersonateServiceAccount; break; case TOM.ImpersonationMode.ImpersonateUnattendedAccount: AMODataSource.ImpersonationInfo.ImpersonationMode = AMO.ImpersonationMode.ImpersonateUnattendedAccount; break; } AMODataSource.ImpersonationInfo.Account = TOMDataSource.Account; AMODataSource.ImpersonationInfo.Password = TOMDataSource.Password; switch (TOMDataSource.Isolation) { case TOM.DatasourceIsolation.ReadCommitted: AMODataSource.Isolation = AMO.DataSourceIsolation.ReadCommitted; break; case TOM.DatasourceIsolation.Snapshot: AMODataSource.Isolation = AMO.DataSourceIsolation.Snapshot; break; } //1 "tick" = 100 nanoseconds = 1*10^-7 seconds. AMODataSource.Timeout = new TimeSpan(TOMDataSource.Timeout * 10000000); AMODatabase.DataSources.Add(AMODataSource); } #endregion /* The DSV is surprisingly simple. * For each physical table (but NOT partition), a DataTable needs to be added to the DSV DataSet, * with the same name as the table. * * Similarly, for each distinct source column, a DataColumn needs to be added to the corresponding DataTable */ #region DataSourceView using (AMO.DataSourceView dsv = new AMO.DataSourceView(AMODatabase.DataSources[0].Name)) { System.Data.DataSet Schema = new System.Data.DataSet(AMODatabase.DataSources[0].Name); dsv.Schema = Schema; dsv.DataSourceID = AMODatabase.DataSources[0].Name; AMODatabase.DataSourceViews.Add(dsv); } #endregion #region Create Cube AMO.Cube Cube = new AMO.Cube(TOMModel.Name, TOMModel.Name); Cube.Source = new AMO.DataSourceViewBinding(AMODatabase.DataSourceViews[0].ID); Cube.StorageMode = AMO.StorageMode.InMemory; //Create the MdxScript for holding DAX commands. using (AMO.MdxScript Script = Cube.MdxScripts.Add(MDXScriptName, MDXScriptName)) { //You MUST have a "default" MDX measure for some reason. //We make sure to hide it - it serves no real purpose, besides its own existence System.Text.StringBuilder InitialisationCommand = new System.Text.StringBuilder(); InitialisationCommand.AppendLine("CALCULATE;"); InitialisationCommand.AppendLine("CREATE MEMBER CURRENTCUBE.Measures.[_No measures defined] AS 1, VISIBLE = 0;"); InitialisationCommand.AppendLine("ALTER CUBE CURRENTCUBE UPDATE DIMENSION Measures, Default_Member = [_No measures defined];"); Script.Commands.Add(new AMO.Command(InitialisationCommand.ToString())); } AMODatabase.Cubes.Add(Cube); #endregion #region Add Tables foreach (TOM.Table TOMTable in TOMModel.Tables) { //Three "parts" to a Table: //1. A System.Data.DataTable, with the same Name as the TOMTable //2. A dimension with the same name as the TOMTable //3. A measure group with the same name as the TOMTable, System.Data.DataTable SchemaTable = new System.Data.DataTable(TOMTable.Name); AMODatabase.DataSourceViews[0].Schema.Tables.Add(SchemaTable); string RowNumberColumnName = string.Format(System.Globalization.CultureInfo.InvariantCulture, "RowNumber_{0}", System.Guid.NewGuid()); #region Add Table Dimension try { using (AMO.Dimension Dimension = AMODatabase.Dimensions.Add(TOMTable.Name, TOMTable.Name)) { Dimension.Source = new AMO.DataSourceViewBinding(AMODatabase.DataSourceViews[0].ID); Dimension.StorageMode = AMO.DimensionStorageMode.InMemory; Dimension.UnknownMember = AMO.UnknownMemberBehavior.AutomaticNull; Dimension.UnknownMemberName = "Unknown"; using (Dimension.ErrorConfiguration = new AMO.ErrorConfiguration()) { Dimension.ErrorConfiguration.KeyNotFound = AMO.ErrorOption.IgnoreError; Dimension.ErrorConfiguration.KeyDuplicate = AMO.ErrorOption.ReportAndStop; Dimension.ErrorConfiguration.NullKeyNotAllowed = AMO.ErrorOption.ReportAndStop; } Dimension.ProactiveCaching = new AMO.ProactiveCaching(); System.TimeSpan DefaultProactiveCachingTimeSpan = new System.TimeSpan(0, 0, -1); Dimension.ProactiveCaching.SilenceInterval = DefaultProactiveCachingTimeSpan; Dimension.ProactiveCaching.Latency = DefaultProactiveCachingTimeSpan; Dimension.ProactiveCaching.SilenceOverrideInterval = DefaultProactiveCachingTimeSpan; Dimension.ProactiveCaching.ForceRebuildInterval = DefaultProactiveCachingTimeSpan; Dimension.ProactiveCaching.Source = new AMO.ProactiveCachingInheritedBinding(); Dimension.Description = TOMTable.Description; // Define RowNumber using (AMO.DimensionAttribute RowNumberDimAttribute = Dimension.Attributes.Add(RowNumberColumnName, RowNumberColumnName)) { RowNumberDimAttribute.Type = AMO.AttributeType.RowNumber; RowNumberDimAttribute.KeyUniquenessGuarantee = true; RowNumberDimAttribute.Usage = AMO.AttributeUsage.Key; RowNumberDimAttribute.KeyColumns.Add(new AMO.DataItem()); RowNumberDimAttribute.KeyColumns[0].DataType = System.Data.OleDb.OleDbType.Integer; RowNumberDimAttribute.KeyColumns[0].DataSize = 4; RowNumberDimAttribute.KeyColumns[0].NullProcessing = AMO.NullProcessing.Error; RowNumberDimAttribute.KeyColumns[0].Source = new AMO.RowNumberBinding(); RowNumberDimAttribute.NameColumn = new AMO.DataItem(); RowNumberDimAttribute.NameColumn.DataType = System.Data.OleDb.OleDbType.WChar; RowNumberDimAttribute.NameColumn.DataSize = 4; RowNumberDimAttribute.NameColumn.NullProcessing = AMO.NullProcessing.ZeroOrBlank; RowNumberDimAttribute.NameColumn.Source = new AMO.RowNumberBinding(); RowNumberDimAttribute.OrderBy = AMO.OrderBy.Key; RowNumberDimAttribute.AttributeHierarchyVisible = false; } // Add Translations foreach (TOM.Culture TOMCulture in TOMModel.Cultures) { AMO.Translation AMOTranslation = TranslationHelper.GetTranslation(TOMCulture, TOMTable); if (AMOTranslation != null) { Dimension.Translations.Add(AMOTranslation); } } } } catch (Exception e) { throw new Exception(string.Format("The following error occurred creating the DImension for table {0}: {1}", TOMTable.Name, e.Message), e); } #endregion #region Add Table MeasureGroup try { using (AMO.MeasureGroup TableMeasureGroup = Cube.MeasureGroups.Add(TOMTable.Name, TOMTable.Name)) { TableMeasureGroup.StorageMode = AMO.StorageMode.InMemory; TableMeasureGroup.ProcessingMode = AMO.ProcessingMode.Regular; // Add Default Measure string DefaultMeasureID = string.Concat("_Count", TOMTable.Name); using (AMO.Measure DefaultMeasure = TableMeasureGroup.Measures.Add(DefaultMeasureID, DefaultMeasureID)) using (AMO.RowBinding DefaultMeasureRowBinding = new AMO.RowBinding(TOMTable.Name)) using (AMO.DataItem DefaultMeasureSource = new AMO.DataItem(DefaultMeasureRowBinding)) { DefaultMeasure.AggregateFunction = AMO.AggregationFunction.Count; DefaultMeasure.DataType = AMO.MeasureDataType.BigInt; DefaultMeasure.Visible = false; DefaultMeasureSource.DataType = System.Data.OleDb.OleDbType.BigInt; DefaultMeasure.Source = DefaultMeasureSource; } // Add Dimension to Measure Group using (AMO.DegenerateMeasureGroupDimension DefaultMeasureGroupDimension = new AMO.DegenerateMeasureGroupDimension(TOMTable.Name)) using (AMO.MeasureGroupAttribute MeasureGroupAttribute = new AMO.MeasureGroupAttribute(RowNumberColumnName)) using (AMO.ColumnBinding RowNumberColumnBinding = new AMO.ColumnBinding(TOMTable.Name, RowNumberColumnName)) using (AMO.DataItem RowNumberKeyColumn = new AMO.DataItem(RowNumberColumnBinding)) { DefaultMeasureGroupDimension.ShareDimensionStorage = AMO.StorageSharingMode.Shared; DefaultMeasureGroupDimension.CubeDimensionID = TOMTable.Name; MeasureGroupAttribute.Type = AMO.MeasureGroupAttributeType.Granularity; RowNumberKeyColumn.DataType = System.Data.OleDb.OleDbType.Integer; MeasureGroupAttribute.KeyColumns.Add(RowNumberKeyColumn); DefaultMeasureGroupDimension.Attributes.Add(MeasureGroupAttribute); TableMeasureGroup.Dimensions.Add(DefaultMeasureGroupDimension); } //Partitions have a 1:1 mapping #region Partitions foreach (TOM.Partition TOMPartition in TOMTable.Partitions) { using (AMO.Partition AMOPartition = new AMO.Partition(TOMPartition.Name, TOMPartition.Name)) { AMOPartition.StorageMode = AMO.StorageMode.InMemory; AMOPartition.ProcessingMode = AMO.ProcessingMode.Regular; AMOPartition.Source = new AMO.QueryBinding( ((TOM.QueryPartitionSource)TOMPartition.Source).DataSource.Name, ((TOM.QueryPartitionSource)TOMPartition.Source).Query ); AMOPartition.Type = AMO.PartitionType.Data; TableMeasureGroup.Partitions.Add(AMOPartition); } } #endregion } } catch (Exception e) { throw new Exception(string.Format("The following error occurred creating the MeasureGroup for table {0}: {1}", TOMTable.Name, e.Message), e); } #endregion //Add the dimension to the cube Cube.Dimensions.Add(TOMTable.Name, TOMTable.Name, TOMTable.Name); Cube.Dimensions[TOMTable.Name].Visible = !TOMTable.IsHidden; /* No 1:1 mapping exists for columns. Each column consists of the following: * - A DataColumn with the same name as the column, * attached to the DataTable correlating to the Table, located in the DSV * - An attribute with the same name as the column, added to the Dimension * - An AttributeRelationship with the RowNumber column */ #region Add Columns foreach (TOM.Column TOMColumn in TOMTable.Columns) { switch (TOMColumn.Type) { case TOM.ColumnType.Data: //Add the DataColumn corresponding to the SourceColumn, if it does not already exist TOM.DataColumn TOMDataColumn = (TOM.DataColumn)TOMColumn; if (!SchemaTable.Columns.Contains(TOMDataColumn.SourceColumn)) { SchemaTable.Columns.Add(new System.Data.DataColumn(((TOM.DataColumn)TOMColumn).SourceColumn)); } System.Data.OleDb.OleDbType ColumnDataType = DataTypeHelper.ToOleDbType(TOMDataColumn.DataType); AMO.DimensionAttribute NormalAttribute = AMODatabase.Dimensions[TOMTable.Name].Attributes.Add(TOMDataColumn.Name, TOMDataColumn.Name); NormalAttribute.Usage = AMO.AttributeUsage.Regular; NormalAttribute.KeyUniquenessGuarantee = false; NormalAttribute.KeyColumns.Add(new AMO.DataItem(SchemaTable.TableName, SchemaTable.Columns[TOMDataColumn.SourceColumn].ColumnName, ColumnDataType)); NormalAttribute.KeyColumns[0].Source = new AMO.ColumnBinding(SchemaTable.TableName, SchemaTable.Columns[TOMDataColumn.SourceColumn].ColumnName); NormalAttribute.KeyColumns[0].NullProcessing = AMO.NullProcessing.Preserve; NormalAttribute.NameColumn = new AMO.DataItem(SchemaTable.TableName, SchemaTable.Columns[TOMDataColumn.SourceColumn].ColumnName, System.Data.OleDb.OleDbType.WChar); NormalAttribute.NameColumn.Source = new AMO.ColumnBinding(SchemaTable.TableName, SchemaTable.Columns[TOMDataColumn.SourceColumn].ColumnName); NormalAttribute.NameColumn.NullProcessing = AMO.NullProcessing.ZeroOrBlank; NormalAttribute.OrderBy = AMO.OrderBy.Key; AMO.AttributeRelationship NormalAttributeRelationship = AMODatabase.Dimensions[TOMTable.Name].Attributes[RowNumberColumnName].AttributeRelationships.Add(NormalAttribute.ID); NormalAttribute.AttributeHierarchyVisible = !TOMDataColumn.IsHidden; NormalAttribute.Description = TOMDataColumn.Description; NormalAttribute.AttributeHierarchyDisplayFolder = TOMDataColumn.DisplayFolder; //Add Translations to the CalculatedAttribute foreach (TOM.Culture TOMCulture in TOMModel.Cultures) { using (AMO.Translation AMOTranslation = TranslationHelper.GetTranslation(TOMCulture, TOMColumn)) if (AMOTranslation != null) { NormalAttribute.Translations.Add(new AMO.AttributeTranslation { Caption = AMOTranslation.Caption, Description = AMOTranslation.Description, DisplayFolder = AMOTranslation.DisplayFolder }); } } NormalAttributeRelationship.Cardinality = AMO.Cardinality.Many; NormalAttributeRelationship.OverrideBehavior = AMO.OverrideBehavior.None; break; case TOM.ColumnType.Calculated: TOM.CalculatedColumn TOMCalculatedColumn = (TOM.CalculatedColumn)TOMColumn; System.Data.OleDb.OleDbType CalculatedColumnDataType = DataTypeHelper.ToOleDbType(TOMColumn.DataType); //Add Attribute to the Dimension AMO.Dimension dim = AMODatabase.Dimensions[TOMTable.Name]; AMO.DimensionAttribute CalculatedAttribute = dim.Attributes.Add(TOMCalculatedColumn.Name, TOMCalculatedColumn.Name); CalculatedAttribute.Usage = AMO.AttributeUsage.Regular; CalculatedAttribute.KeyUniquenessGuarantee = false; CalculatedAttribute.KeyColumns.Add(new AMO.DataItem(TOMTable.Name, TOMCalculatedColumn.Name, CalculatedColumnDataType)); CalculatedAttribute.KeyColumns[0].Source = new AMO.ExpressionBinding(TOMCalculatedColumn.Expression); CalculatedAttribute.KeyColumns[0].NullProcessing = AMO.NullProcessing.Preserve; CalculatedAttribute.NameColumn = new AMO.DataItem(TOMTable.Name, TOMCalculatedColumn.Name, System.Data.OleDb.OleDbType.WChar); CalculatedAttribute.NameColumn.Source = new AMO.ExpressionBinding(TOMCalculatedColumn.Expression); CalculatedAttribute.NameColumn.NullProcessing = AMO.NullProcessing.ZeroOrBlank; CalculatedAttribute.OrderBy = AMO.OrderBy.Key; AMO.AttributeRelationship currentAttributeRelationship = dim.Attributes[RowNumberColumnName].AttributeRelationships.Add(CalculatedAttribute.ID); CalculatedAttribute.AttributeHierarchyVisible = !TOMCalculatedColumn.IsHidden; CalculatedAttribute.Description = TOMCalculatedColumn.Description; CalculatedAttribute.AttributeHierarchyDisplayFolder = TOMCalculatedColumn.DisplayFolder; //Add Translations to the CalculatedAttribute //Loop through each culture, and add the translation associated with that culture. foreach (TOM.Culture TOMCulture in TOMModel.Cultures) { using (AMO.Translation AMOTranslation = TranslationHelper.GetTranslation(TOMCulture, TOMColumn)) if (AMOTranslation != null) { CalculatedAttribute.Translations.Add(new AMO.AttributeTranslation { Caption = AMOTranslation.Caption, Description = AMOTranslation.Description, DisplayFolder = AMOTranslation.DisplayFolder }); } } currentAttributeRelationship.Cardinality = AMO.Cardinality.Many; currentAttributeRelationship.OverrideBehavior = AMO.OverrideBehavior.None; //Add CalculatedColumn as attribute to the MeasureGroup AMO.MeasureGroup mg = Cube.MeasureGroups[TOMTable.Name]; AMO.DegenerateMeasureGroupDimension currentMGDim = (AMO.DegenerateMeasureGroupDimension)mg.Dimensions[TOMTable.Name]; AMO.MeasureGroupAttribute mga = new AMO.MeasureGroupAttribute(TOMCalculatedColumn.Name); mga.KeyColumns.Add(new AMO.DataItem(TOMTable.Name, TOMCalculatedColumn.Name, System.Data.OleDb.OleDbType.Empty)); mga.KeyColumns[0].Source = new AMO.ExpressionBinding(TOMCalculatedColumn.Expression); currentMGDim.Attributes.Add(mga); break; default: throw new System.NotImplementedException(string.Format("Cannot deploy Column of type {0}", TOMColumn.Type.ToString())); } } #endregion //Add sort by columns foreach (TOM.Column TOMColumn in TOMTable.Columns) { if (TOMColumn.SortByColumn != null) { AMODatabase.Dimensions[TOMTable.Name].Attributes[TOMColumn.Name].OrderByAttributeID = TOMColumn.SortByColumn.Name; } } #region Add Hierarchies foreach (TOM.Hierarchy TOMHierarchy in TOMTable.Hierarchies) { //Create the Hierarchy, and add it AMO.Hierarchy AMOHierarchy = AMODatabase.Dimensions[TOMTable.Name].Hierarchies.Add(TOMHierarchy.Name, TOMHierarchy.Name); AMOHierarchy.Description = TOMHierarchy.Description; AMOHierarchy.DisplayFolder = TOMHierarchy.DisplayFolder; AMOHierarchy.AllMemberName = "All"; foreach (TOM.Level TOMLevel in TOMHierarchy.Levels) { AMO.Level AMOLevel = AMOHierarchy.Levels.Add(TOMLevel.Name); AMOLevel.SourceAttribute = AMODatabase.Dimensions[TOMTable.Name].Attributes[TOMLevel.Column.Name]; AMOLevel.Description = TOMLevel.Description; //Add Translations to the CalculatedAttribute //Loop through each culture, and add the translation associated with that culture. foreach (TOM.Culture TOMCulture in TOMModel.Cultures) { using (AMO.Translation AMOTranslation = TranslationHelper.GetTranslation(TOMCulture, TOMLevel)) if (AMOTranslation != null) { AMOLevel.Translations.Add(AMOTranslation); } } } } #endregion } #endregion #region Add Measures using (AMO.MdxScript MdxScript = Cube.DefaultMdxScript) { //Create a "default" measure, required for the cube to function. MdxScript.Commands.Add(new AMO.Command("CALCULATE;" + "CREATE MEMBER CURRENTCUBE.Measures.[_No measures defined] AS 1, VISIBLE = 0;" + "ALTER CUBE CURRENTCUBE UPDATE DIMENSION Measures, Default_Member = [_No measures defined];")); foreach (TOM.Table TOMTable in TOMModel.Tables) { foreach (TOM.Measure TOMMeasure in TOMTable.Measures) { //Create the Command, which contains the definition of the Measure AMO.Command AMOCommand = new AMO.Command(string.Format("CREATE MEASURE '{0}'[{1}]={2};", TOMTable.Name, TOMMeasure.Name.Replace("]", "]]"), TOMMeasure.Expression)); if (TOMMeasure.KPI != null) { #region Add KPI //Start building the final command - we add to it as we go along string FinalKPICreate = string.Format( "CREATE KPI CURRENTCUBE.[{1}] AS Measures.[{1}], ASSOCIATED_MEASURE_GROUP = '{0}'", TOMTable.Name, TOMMeasure.Name ); //Goal/Target if (!string.IsNullOrWhiteSpace(TOMMeasure.KPI.TargetExpression)) { AMOCommand.Text += Environment.NewLine + string.Format(string.Format("CREATE MEASURE '{0}'[_{1} Goal]={2};", TOMTable.Name, TOMMeasure.Name.Replace("]", "]]"), TOMMeasure.KPI.TargetExpression)); FinalKPICreate += string.Format(", GOAL = Measures.[_{0} Goal]", TOMMeasure.Name.Replace("]", "]]")); } //Status if (!string.IsNullOrWhiteSpace(TOMMeasure.KPI.StatusExpression)) { AMOCommand.Text += Environment.NewLine + string.Format(string.Format("CREATE MEASURE '{0}'[_{1} Status]={2};", TOMTable.Name, TOMMeasure.Name.Replace("]", "]]"), TOMMeasure.KPI.StatusExpression)); FinalKPICreate += string.Format(", STATUS = Measures.[_{0} Status]", TOMMeasure.Name.Replace("]", "]]")); if (!string.IsNullOrWhiteSpace(TOMMeasure.KPI.StatusGraphic)) { FinalKPICreate += string.Format(", STATUS_GRAPHIC = '{0}'", TOMMeasure.KPI.StatusGraphic); } } //Trend if (!string.IsNullOrWhiteSpace(TOMMeasure.KPI.TrendExpression)) { AMOCommand.Text += Environment.NewLine + string.Format(string.Format("CREATE MEASURE '{0}'[_{1} Trend]={2};", TOMTable.Name, TOMMeasure.Name.Replace("]", "]]"), TOMMeasure.KPI.TrendExpression)); FinalKPICreate += string.Format(", TREND = Measures.[_{0} Trend]", TOMMeasure.Name.Replace("]", "]]")); if (!string.IsNullOrWhiteSpace(TOMMeasure.KPI.TrendGraphic)) { FinalKPICreate += string.Format(", TREND_GRAPHIC = '{0}'", TOMMeasure.KPI.TrendGraphic); } } AMOCommand.Text += Environment.NewLine + FinalKPICreate + ";"; //Create calculation properties, hiding the "fake" measures (if they exist) //Target if (!string.IsNullOrWhiteSpace(TOMMeasure.KPI.TargetExpression)) { AMO.CalculationProperty TargetCalculationProperty = new AMO.CalculationProperty(string.Format("[_{0} Goal]", TOMMeasure.Name.Replace("]", "]]")), AMO.CalculationType.Member); TargetCalculationProperty.Description = TOMMeasure.KPI.TargetDescription; TargetCalculationProperty.CalculationType = AMO.CalculationType.Member; TargetCalculationProperty.Visible = false; if (!string.IsNullOrWhiteSpace(TOMMeasure.KPI.TargetFormatString)) { TargetCalculationProperty.FormatString = "'" + TOMMeasure.KPI.TargetFormatString + "'"; } MdxScript.CalculationProperties.Add(TargetCalculationProperty); } //Status if (!string.IsNullOrWhiteSpace(TOMMeasure.KPI.StatusExpression)) { AMO.CalculationProperty StatusCalculationProperty = new AMO.CalculationProperty(string.Format("[_{0} Status]", TOMMeasure.Name.Replace("]", "]]")), AMO.CalculationType.Member); StatusCalculationProperty.Description = TOMMeasure.KPI.StatusDescription; StatusCalculationProperty.CalculationType = AMO.CalculationType.Member; StatusCalculationProperty.Visible = false; MdxScript.CalculationProperties.Add(StatusCalculationProperty); } //Trend if (!string.IsNullOrWhiteSpace(TOMMeasure.KPI.StatusExpression)) { AMO.CalculationProperty TrendCalculationProperty = new AMO.CalculationProperty(string.Format("[_{0} Trend]", TOMMeasure.Name.Replace("]", "]]")), AMO.CalculationType.Member); TrendCalculationProperty.Description = TOMMeasure.KPI.TrendDescription; TrendCalculationProperty.CalculationType = AMO.CalculationType.Member; TrendCalculationProperty.Visible = false; MdxScript.CalculationProperties.Add(TrendCalculationProperty); } //Create the KPI calculation property AMO.CalculationProperty KPICalculationProperty = new AMO.CalculationProperty(string.Format("KPIs.[{0}]", TOMMeasure.Name.Replace("]", "]]")), AMO.CalculationType.Member); KPICalculationProperty.Description = TOMMeasure.KPI.Description; KPICalculationProperty.CalculationType = AMO.CalculationType.Member; MdxScript.CalculationProperties.Add(KPICalculationProperty); #endregion } //Add the Command to the MdxScript MdxScript.Commands.Add(AMOCommand); //Create the Calculation Property, which contains the various properties of the Measure AMO.CalculationProperty CalculationProperty = new AMO.CalculationProperty(string.Format("[{0}]", TOMMeasure.Name.Replace("]", "]]")), AMO.CalculationType.Member); CalculationProperty.Description = TOMMeasure.Description; CalculationProperty.DisplayFolder = TOMMeasure.DisplayFolder; CalculationProperty.CalculationType = AMO.CalculationType.Member; CalculationProperty.Visible = !TOMMeasure.IsHidden; if (!string.IsNullOrWhiteSpace(TOMMeasure.FormatString)) { CalculationProperty.FormatString = "'" + TOMMeasure.FormatString + "'"; } //Add Translations to the Calculation property foreach (TOM.Culture TOMCulture in TOMModel.Cultures) { using (AMO.Translation AMOTranslation = TranslationHelper.GetTranslation(TOMCulture, TOMMeasure)) if (AMOTranslation != null) { CalculationProperty.Translations.Add(AMOTranslation); } } //Finally, add the CalculationProperty to the MDX script MdxScript.CalculationProperties.Add(CalculationProperty); } } } #endregion #region Add Relationships //Relationships are just TOO awful. They are placed in their own helper function as a result. foreach (TOM.SingleColumnRelationship TOMRelationship in TOMModel.Relationships) { RelationshipHelper.CreateRelationship(TOMRelationship, AMODatabase); } #endregion #region Add Perspectives foreach (TOM.Perspective TOMPerspective in TOMModel.Perspectives) { AMO.Perspective AMOPerspective = new AMO.Perspective(TOMPerspective.Name); foreach (TOM.PerspectiveTable TOMTable in TOMPerspective.PerspectiveTables) { //Add Perspective Dimension AMO.PerspectiveDimension PerspectiveDimension = new AMO.PerspectiveDimension(TOMTable.Name); //Perspective Columns foreach (TOM.PerspectiveColumn TOMPerspectiveColumn in TOMTable.PerspectiveColumns) { PerspectiveDimension.Attributes.Add(TOMPerspectiveColumn.Name); } //Perspective Hierarchies foreach (TOM.PerspectiveHierarchy TOMPerspectiveHierarchy in TOMTable.PerspectiveHierarchies) { PerspectiveDimension.Hierarchies.Add(TOMPerspectiveHierarchy.Name); } //Add Perspective MeasureGroup AMO.PerspectiveMeasureGroup PerspectiveMeasureGroup = new AMO.PerspectiveMeasureGroup(TOMTable.Name); //Perspective Measures //In this case, ']' is NOT "double quoted", unlike the calculation references, s foreach (TOM.PerspectiveMeasure TOMPerspectiveMeasure in TOMTable.PerspectiveMeasures) { AMOPerspective.Calculations.Add('[' + TOMPerspectiveMeasure.Name + ']'); } //VS does not add KPI's to Perspectives, so I have no idea how to do this... //TODO: Add KPIs to perspectives } AMODatabase.Cubes[0].Perspectives.Add(AMOPerspective); } #endregion #region Roles foreach (TOM.ModelRole TOMRole in TOMDatabase.Model.Roles) { //Ceate database role AMO.Role DatabaseRole = AMODatabase.Roles.Add(TOMRole.Name); DatabaseRole.Description = TOMRole.Description; //Add Members to Database role foreach (TOM.ModelRoleMember TOMMember in TOMRole.Members) { DatabaseRole.Members.Add(new AMO.RoleMember { Name = TOMMember.MemberName, Sid = TOMMember.MemberID }); } //Add DatabasePermission AMO.DatabasePermission DatabasePermission = AMODatabase.DatabasePermissions.Add(TOMRole.Name, TOMRole.Name, TOMRole.Name); //Add CubePermission AMO.CubePermission CubePermission = AMODatabase.Cubes[0].CubePermissions.Add(TOMRole.Name, TOMRole.Name, TOMRole.Name); switch (TOMRole.ModelPermission) { case TOM.ModelPermission.Administrator: //Add ReadDefinition, Read, and Administer to DatabasePermission DatabasePermission.ReadDefinition = AMO.ReadDefinitionAccess.Allowed; DatabasePermission.Read = AMO.ReadAccess.Allowed; DatabasePermission.Administer = true; //Nothing extra to add to CubePermission, that is not added after this // switch statement break; case TOM.ModelPermission.None: //Add no permissions to DatabasePermission. break; case TOM.ModelPermission.Read: //Add only Read access to Database Permission DatabasePermission.Read = AMO.ReadAccess.Allowed; //CubePermission CubePermission.Read = AMO.ReadAccess.Allowed; break; case TOM.ModelPermission.ReadRefresh: DatabasePermission.Read = AMO.ReadAccess.Allowed; DatabasePermission.Process = true; //CubePermission CubePermission.Read = AMO.ReadAccess.Allowed; CubePermission.Process = true; break; case TOM.ModelPermission.Refresh: DatabasePermission.Process = true; //CubePermission CubePermission.Process = true; break; } CubePermission.ReadSourceData = AMO.ReadSourceDataAccess.None; //Finally, add the row filters foreach (TOM.TablePermission TablePermission in TOMRole.TablePermissions) { AMO.DimensionPermission DimensionPermission = new AMO.DimensionPermission(TOMRole.Name, TOMRole.Name, TOMRole.Name); DimensionPermission.AllowedRowsExpression = TablePermission.FilterExpression; AMODatabase.Dimensions.GetByName(TablePermission.Table.Name).DimensionPermissions.Add(DimensionPermission); } } #endregion return(AMODatabase); }
private void ReplacePartitionsFromDestination(JObject sourceModel, TOM.TableCollection sourceModelTables, TOM.Model destinationModel, DeploymentOptions options) { if (sourceModel["tables"] is JArray tables) { foreach (var table in tables) { var tableName = table["name"].Value <string>(); if (destinationModel.Tables.Contains(tableName)) { // Use destination partitions only if both source and destination tables are imported: if (!(sourceModelTables[tableName].IsImported() && destinationModel.Tables[tableName].IsImported())) { continue; } var t = destinationModel.Tables[tableName]; // If destination partition is not a policyrange if (!options.DeployPartitions || (options.SkipRefreshPolicyPartitions && t.GetSourceType() == TOM.PartitionSourceType.PolicyRange)) { // Retain existing partitions on destination: var partitions = new JArray(); table["partitions"] = partitions; foreach (var pt in t.Partitions) { partitions.Add(JObject.Parse(TOM.JsonSerializer.SerializeObject(pt))); } } } } } }