/// <summary>
        /// Generate string can be used as Goolge Chart Data
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string ToGoogleChartJsonTable(this TabularTable data)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("{\"cols\" : [");
            sb.Append(string.Join(",", data.cols.Select(c => c.ToGoogleChartJsonCol())));
            sb.Append("]"); //end of cols
            sb.Append(", \"rows\":[");
            if (data.rows.Any())
            {
                foreach (var row in data.rows)
                {
                    sb.Append("{ \"c\":[");
                    //add each cells
                    //for (int i = 0; i < row.Count(); i++ )
                    int cellIndex = 0;
                    foreach (var cell in row)
                    {
                        var col = data.cols[cellIndex++];
                        col.ToGoogleChartJsonCell(cell, sb).Append(",");
                    }
                    sb.Remove(sb.Length - 1, 1); //remove last extra , brought by last cell
                    sb.Append("]},");            //end of each row
                }
                sb.Remove(sb.Length - 1, 1);     //remove last extra brought by last row ,
            }
            sb.Append("]}");                     //end of rows and table
            return(sb.ToString());
        }
        public void GivenColumnWithMultiExpressionInSamePath_WhenResolveDefinitionNode_ResolveDefinitionNodesShouldBeReturn()
        {
            Dictionary <string, PropertiesGroup> propertiesGroups = new Dictionary <string, PropertiesGroup>();

            var properties = new List <Property>();

            properties.Add(new Property()
            {
                Name           = "TestName1",
                Path           = "",
                FhirExpression = "testExpression1"
            });
            properties.Add(new Property()
            {
                Name           = "TestName2",
                Path           = "",
                FhirExpression = "testExpression2"
            });
            TabularTable table = new TabularTable();

            table.Properties = properties;

            TabularMappingDefinition defination = BaseMappingDefinitionLoader.BuildMappingDefinition(table, propertiesGroups, 5);

            Assert.AreEqual("TestName1", defination.Root.ColumnDefinitions[0].Name);
            Assert.AreEqual("testExpression1", defination.Root.ColumnDefinitions[0].FhirExpression);
            Assert.AreEqual("TestName2", defination.Root.ColumnDefinitions[1].Name);
            Assert.AreEqual("testExpression2", defination.Root.ColumnDefinitions[1].FhirExpression);
        }
        public void Given2ColumnWithSamePath_WhenResolveDefinitionNode_2ResolveDefinitionNodesShouldBeReturn()
        {
            var properties = new List <Property>();

            properties.Add(new Property()
            {
                Name = "TestName1",
                Path = "test1.test2",
                Type = "string"
            });

            properties.Add(new Property()
            {
                Name = "TestName2",
                Path = "test1.test2",
                Type = "string"
            });
            TabularTable table = new TabularTable();

            table.Properties = properties;

            TabularMappingDefinition defination = BaseMappingDefinitionLoader.BuildMappingDefinition(table, new Dictionary <string, PropertiesGroup>(), 5);

            Assert.AreEqual(2, defination.Root.Children["test1"].Children["test2"].ColumnDefinitions.Count());
            Assert.AreEqual("Test1TestName1", defination.Root.Children["test1"].Children["test2"].ColumnDefinitions[0].Name);
            Assert.AreEqual("Test1TestName2", defination.Root.Children["test1"].Children["test2"].ColumnDefinitions[1].Name);
        }
        public void GivenColumnWith2LongPath_WhenResolveDefinitionNode_MergedResolveDefinitionNodesShouldBeReturn()
        {
            Dictionary <string, PropertiesGroup> propertiesGroups = new Dictionary <string, PropertiesGroup>();

            var properties = new List <Property>();

            properties.Add(new Property()
            {
                Name = "TestName1",
                Path = "test1.test2"
            });
            properties.Add(new Property()
            {
                Name = "TestName2",
                Path = "test1.test3"
            });
            TabularTable table = new TabularTable();

            table.Properties = properties;

            TabularMappingDefinition defination = BaseMappingDefinitionLoader.BuildMappingDefinition(table, propertiesGroups, 5);

            Assert.AreEqual("Test1TestName1", defination.Root.Children["test1"].Children["test2"].ColumnDefinitions[0].Name);
            Assert.AreEqual("Test1TestName2", defination.Root.Children["test1"].Children["test3"].ColumnDefinitions[0].Name);
        }
        public void GivenColumnWithLongPathGroup_WhenResolveDefinitionNode_ResolveDefinitionNodesShouldBeReturn()
        {
            Dictionary <string, PropertiesGroup> propertiesGroups = new Dictionary <string, PropertiesGroup>();

            propertiesGroups["test"] = new PropertiesGroup()
            {
                PropertiesGroupName = "test",
                Properties          = new List <Property>()
                {
                    new Property()
                    {
                        Name = "p1", Type = "string", Path = "abc"
                    }
                }
            };
            var properties = new List <Property>();

            properties.Add(new Property()
            {
                Name            = "TestName",
                Path            = "test1.test2",
                PropertiesGroup = "test"
            });
            TabularTable table = new TabularTable();

            table.Properties = properties;

            TabularMappingDefinition defination = BaseMappingDefinitionLoader.BuildMappingDefinition(table, propertiesGroups, 5);

            Assert.AreEqual("Test1Test2p1", defination.Root.Children["test1"].Children["test2"].Children["abc"].ColumnDefinitions[0].Name);
        }
Beispiel #6
0
        public static TabularMappingDefinition BuildMappingDefinition(TabularTable tabularTable, Dictionary <string, PropertiesGroup> propertiesGroups, int maxDepth)
        {
            TabularMappingDefinition result = new TabularMappingDefinition(tabularTable.Name)
            {
                ResourceType = tabularTable.ResourceType,
                Unrollpath   = tabularTable.UnrollPath
            };

            ResolveDefinitionNode(result.Root, tabularTable.Properties, propertiesGroups, new LinkedList <string>(), new HashSet <string>(), maxDepth);

            return(result);
        }
        public static TabularTable FromEnumerableObjects <T>(IEnumerable <T> objects, IList <TabularColumn> cols)
        {
            var          rows  = new List <List <object> >();
            TabularTable table = new TabularTable(cols, rows);

            foreach (var obj in objects)
            {
                var row = new List <object>(cols.Count);
                foreach (var col in cols)
                {
                    row.Add(GetValue(obj, col.id));
                }
                rows.Add(row);
            }
            return(table);
        }
        public static TabularTable FromDataTable(DataTable dataTable, IList <TabularColumn> cols)
        {
            //to do: input parameter check
            var          rows  = new List <List <object> >();
            TabularTable table = new TabularTable(cols, rows);

            foreach (DataRow dr in dataTable.Rows)
            {
                var row = new List <object>(cols.Count);
                foreach (var col in cols)
                {
                    row.Add(dr[col.id]);
                }
                rows.Add(row);
            }
            return(table);
        }
        //to do: make cols optional
        public static TabularTable FromDataReader(IDataReader dataReader, IList <TabularColumn> cols)
        {
            //to do: input parameter check
            var          rows  = new List <List <object> >();
            TabularTable table = new TabularTable(cols, rows);

            while (dataReader.Read())
            {
                var row = new List <object>(cols.Count);
                foreach (var col in cols)
                {
                    row.Add(dataReader[col.id]);
                }
                rows.Add(row);
            }
            return(table);
        }
        public void GivenDefinitionMoreThanMaxLevel_WhenResolveDefinitionNode_ResolveDefinitionNodesShouldBeReturn()
        {
            Dictionary <string, PropertiesGroup> propertiesGroups = new Dictionary <string, PropertiesGroup>();

            propertiesGroups["test1"] = new PropertiesGroup()
            {
                PropertiesGroupName = "test1",
                Properties          = new List <Property>()
                {
                    new Property()
                    {
                        Name = "p11", Type = "string", Path = "abc1"
                    },
                    new Property()
                    {
                        PropertiesGroup = "test1", Path = "abc2"
                    }
                }
            };

            var properties = new List <Property>();

            properties.Add(new Property()
            {
                Name            = "TestName",
                Path            = "test1",
                PropertiesGroup = "test1"
            });
            properties.Add(new Property()
            {
                Name = "p1",
                Path = "test",
                Type = "string"
            });
            TabularTable table = new TabularTable();

            table.Properties = properties;

            TabularMappingDefinition defination = BaseMappingDefinitionLoader.BuildMappingDefinition(table, propertiesGroups, 2);

            Assert.AreEqual(3, defination.Columns.Count());
        }
        public static string ToDataTableJSON(this TabularTable data)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(@"{""aaData"": [");
            if (data.rows.Any())
            {
                foreach (var row in data.rows)
                {
                    sb.AppendFormat("[");
                    foreach (var cell in row)
                    {
                        sb.AppendFormat("\"{0}\",", cell == null ? string.Empty : cell.ToString().EscapeJSON());
                    }
                    sb.Remove(sb.Length - 1, 1); //remove last extra , brought by last cell
                    sb.AppendFormat("],");
                }
                sb.Remove(sb.Length - 1, 1); //remove last extra , brought by last row
            }
            sb.AppendFormat("], \"iTotalDisplayRecords\" : {0}, \"iTotalRecords\": {1} }}", data.TotalRows, data.TotalRows);
            return(sb.ToString());
        }
        public static Stream ToCsvStream(this TabularTable data, Encoding encoding = null, string delimiter = ",")
        {
            if (encoding == null)
            {
                encoding = Encoding.Unicode;
            }

            MemoryStream stream = new MemoryStream();
            var          writer = new StreamWriter(stream, encoding);

            //write header
            writer.WriteLine(string.Join(delimiter, data.cols.Select(c => c.lable)));
            //write rows
            foreach (var row in data.rows)
            {
                writer.WriteLine(string.Join(delimiter, row));
            }
            writer.Flush();

            //imporve later
            stream.Seek(0, SeekOrigin.Begin);
            return(stream);
        }