/// <summary> /// Loops through the cube metadata and calls the passed functions for each cell. /// </summary> /// <param name="updateDimensionCell">Action to perform on a dimension cell.</param> /// <param name="updateMeasureCell">Action to perform on a measure cell.</param> private void EnumerateCubeMetadata( Action<ExcelRangeBase, CubeDimension, MeasureGroup> updateDimensionCell, Action<ExcelRangeBase, Measure, MeasureGroup> updateMeasureCell) { using (var server = new Server()) { server.Connect(this.connectionBuilder.ConnectionString); var database = server.FindDatabases() .Where(x => x.Name == this.connectionBuilder.InitialCatalog) .First(); this.doc = new ExcelPackage(new FileInfo(this.outputFile)); foreach (var cube in database.FindCubes()) { Console.WriteLine("{0} cube", cube.Name); List<CubeDimension> dimensions = new List<CubeDimension>(cube.FindDimensions()); if (this.doc.Workbook.Worksheets[cube.Name] != null) { this.doc.Workbook.Worksheets.Delete(cube.Name); } var sheet = this.doc.Workbook.Worksheets.Add(cube.Name); ExcelRangeBase dimensionCell = sheet.Cells["A2"]; foreach (var d in dimensions) { Console.WriteLine("\t{0}", d.Name); dimensionCell.Value = d.Dimension.Name; var roleCell = dimensionCell.Offset(0, 1); roleCell.Value = d.Name; roleCell.AddComment(FormatAttributesComment(d), "James Snape"); roleCell.Comment.AutoFit = true; dimensionCell = dimensionCell.Offset(1, 0); } ExcelRangeBase measuregroupCell = sheet.Cells["C1"]; foreach (var mg in cube.FindMeasureGroups().OrderBy(x => x.Name)) { Console.WriteLine("\t{0}", mg.Name); measuregroupCell.Value = mg.Name; ExcelRangeBase tickCell = measuregroupCell.Offset(1, 0); List<CubeDimension> mgdims = new List<CubeDimension>(mg.FindDimensions()); foreach (var d in dimensions) { if (mgdims.Contains(d)) { updateDimensionCell(tickCell, d, mg); } tickCell = tickCell.Offset(1, 0); } foreach (var m in mg.Measures.Cast<Measure>()) { updateMeasureCell(tickCell, m, mg); tickCell = tickCell.Offset(1, 0); } measuregroupCell = measuregroupCell.Offset(0, 1); } } } this.doc.Save(); this.doc.Dispose(); if (this.showGeneratedFile) { Process.Start(this.outputFile); } }
public void GenerateTestSuite( [DefaultValue("..\\..\\Templates")] string templatePath, [DefaultValue("..\\..\\RegressionTests")] string outputPath) { string measureGroupTemplate = File.ReadAllText(Path.Combine(templatePath, "MeasureGroup.template")); string dimensionTemplate = File.ReadAllText(Path.Combine(templatePath, "Dimension.template")); string measureTemplate = File.ReadAllText(Path.Combine(templatePath, "Measure.template")); using (var server = new Server()) { server.Connect(this.connectionBuilder.ConnectionString); var database = server.FindDatabases() .Where(x => x.Name == this.connectionBuilder.InitialCatalog) .First(); foreach (var cube in database.FindCubes()) { Console.WriteLine("{0} cube", cube.Name); var cubePath = Path.Combine(outputPath, FormatFileName(cube.Name)); if (!Directory.Exists(cubePath)) { Directory.CreateDirectory(cubePath); } List<CubeDimension> dimensions = new List<CubeDimension>(cube.FindDimensions()); foreach (var mg in cube.FindMeasureGroups().OrderBy(x => x.Name)) { Console.WriteLine("\t{0}", mg.Name); var mgPath = Path.Combine(cubePath, FormatFileName(mg.Name) + ".cs"); List<CubeDimension> mgdims = new List<CubeDimension>(mg.FindDimensions()); var dimensionClasses = new List<string>(dimensions.Count + 1); foreach (var d in dimensions) { if (mgdims.Contains(d)) { Console.WriteLine("\t\t{0}", d.Name); var dimensionMeasureTests = new List<string>(mg.Measures.Count); foreach (var m in mg.Measures.Cast<Measure>()) { StringBuilder measureBuilder = new StringBuilder(measureTemplate); measureBuilder.Replace("$(AggregationFunction)", FormatCodeName(m.AggregateFunction.ToString())); measureBuilder.Replace("$(MeasureName)", FormatCodeName(m.Name)); measureBuilder.Replace("$(Query)", GenerateMdxQueryFor(m, d, cube)); dimensionMeasureTests.Add(measureBuilder.ToString()); } StringBuilder dimensionBuilder = new StringBuilder(dimensionTemplate); dimensionBuilder.Replace("$(MeasureGroupName)", FormatCodeName(mg.Name)); dimensionBuilder.Replace("$(DimensionName)", FormatCodeName(d.Name)); dimensionBuilder.Replace("$(Measure)", string.Join("\n", dimensionMeasureTests)); dimensionClasses.Add(dimensionBuilder.ToString()); } } var measureTests = new List<string>(mg.Measures.Count); foreach (var m in mg.Measures.Cast<Measure>()) { Console.WriteLine("\t\t{0}", m.Name); StringBuilder measureBuilder = new StringBuilder(measureTemplate); measureBuilder.Replace("$(AggregationFunction)", FormatCodeName(m.AggregateFunction.ToString())); measureBuilder.Replace("$(MeasureName)", FormatCodeName(m.Name)); measureTests.Add(measureBuilder.ToString()); } StringBuilder allDimensionBuilder = new StringBuilder(dimensionTemplate); allDimensionBuilder.Replace("$(MeasureGroupName)", FormatCodeName(mg.Name)); allDimensionBuilder.Replace("$(DimensionName)", "All"); allDimensionBuilder.Replace("$(Measure)", string.Join("\n", measureTests)); dimensionClasses.Add(allDimensionBuilder.ToString()); StringBuilder measureGroupBuilder = new StringBuilder(measureGroupTemplate); measureGroupBuilder.Replace("$(MeasureGroupName)", FormatCodeName(mg.Name)); measureGroupBuilder.Replace("$(CubeName)", FormatCodeName(cube.Name)); measureGroupBuilder.Replace("$(Dimension)", string.Join("\n", dimensionClasses)); File.WriteAllText(mgPath, measureGroupBuilder.ToString()); } } } }