private static void Main(string[] args) { string json = @"{ 'condition': 'AND', 'rules': [ { 'id': 'price', 'field': 'price', 'typeEntity': 'mod', 'type': 'double', 'input': 'text', 'operator': 'less', 'value': '10.25' }, { 'condition': 'OR', 'rules': [ { 'id': 'category', 'field': 'category', 'typeEntity': 'opt', 'type': 'integer', 'input': 'select', 'operator': 'equal', 'value': '2' }, { 'id': 'category', 'field': 'category', 'type': 'integer', 'input': 'select', 'operator': 'equal', 'value': '1' } ] } ] } "; //json = "{\"condition\":\"AND\",\"rules\":[{\"id\":\"OPT\",\"field\":\"OPT\",\"type\":\"string\",\"input\":\"select\",\"operator\":\"equal\",\"value\":\"1\"},{\"id\":\"Modello\",\"field\":\"Modello\",\"type\":\"string\",\"input\":\"select\",\"operator\":\"equal\",\"value\":\"135\"}]}"; //json = @"{'condition':'AND','rules':[{'id':'CILINDRATA ','field':'CILINDRATA ','type':'string','input':'select','operator':'equal','value':'001743'}]}"; var fakeCdaMasterObj = new CdaMasterDto() { GlobalId = new Guid(), Id = 1, Version = 1, Active = true, Cancelled = false, Code = "XCGY", CreationTime = DateTime.UtcNow, Description = "test", Domain = "UTE", IdPlantCreator = 1, Name = "TestCDA", UpdatedTime = DateTime.UtcNow, XsdVersion = "1.0" }; XNode cda = Task.Run(async () => await Transformation.FromJsonToXml(fakeCdaMasterObj, json)).Result; System.Console.WriteLine(); System.Console.WriteLine(cda); System.Console.WriteLine(); string tagsQuery = Task.Run(async () => await Transformation.FromJsonToTagsSqlQuery(json, "TagText", "EntityCode")).Result; System.Console.WriteLine(); System.Console.WriteLine(tagsQuery); System.Console.WriteLine(); FromXmlTransformationsResult result = Task.Run(async () => await Transformation.FromXmlTransformations(fakeCdaMasterObj.Id, fakeCdaMasterObj.Version, cda, "CIS", "QueryforCdaManagerAnalysis")).Result; result.TagsTransformation.ResultValue.ForEach(tag => System.Console.WriteLine(tag.ToString())); System.Console.WriteLine(); System.Console.WriteLine(result.QueryTransformations.ResultValue); System.Console.ReadKey(); }
/// <summary> /// /// </summary> /// <param name="jsonCda"></param> /// <param name="tagTextColumnName"></param> /// <param name="entityCodeColumnName"></param> /// <returns></returns> public static async Task<string> FromJsonToTagsSqlQuery(string jsonCda, string tagTextColumnName, string entityCodeColumnName) { Debug.Assert(!string.IsNullOrWhiteSpace(jsonCda), "jsonCda is null or empty"); var fakeCdaMasterObj = new CdaMasterDto() { GlobalId = new Guid(), Id = -1, Version = 1, Name = string.Empty, Active = true, Cancelled = false, Code = string.Empty, CreationTime = DateTime.UtcNow, UpdatedTime = DateTime.UtcNow, Description = string.Empty, Domain = string.Empty, IdPlantCreator = 1, XsdVersion = string.Empty }; XNode cdaXml = await FromJsonToXml(fakeCdaMasterObj, jsonCda); XElement conditionsRoot = ((XElement)cdaXml).FirstNode as XElement; var queryBuilder = new StringBuilder(); char openGroupChar = '('; char closeGroupChar = ')'; var conditionSymbol = string.Empty; foreach (var group in conditionsRoot.Descendants("GROUP")) { conditionSymbol = group.Attribute("CONDITION").Value; queryBuilder.AppendFormat(@"{0}", openGroupChar); foreach (var ruleElement in group.Elements("RULE")) { var entityCode = ruleElement.Attribute("ENTITYCODE").Value; var entityType = ruleElement.Attribute("ENTITYTYPE").Value; var operatorSymbol = ruleElement.Attribute("OPERATOR").Value; var value = ruleElement.Attribute("VALUE").Value; if (!string.IsNullOrEmpty(value)) { queryBuilder.AppendFormat(@"{0} = ""{1} {2} {3}""", tagTextColumnName, entityCode, operatorSymbol.ToLower(), value); } else { queryBuilder.AppendFormat(@"{0} = ""{1}""", entityCodeColumnName, entityCode); } queryBuilder.AppendFormat(@" {0} ", conditionSymbol); } queryBuilder.RemoveLast($" {conditionSymbol} "); queryBuilder.AppendFormat(@"{0}", closeGroupChar); queryBuilder.AppendFormat(@" {0} ", conditionSymbol); } queryBuilder.RemoveLast($" {conditionSymbol} "); return await Task.FromResult(queryBuilder.ToString()); }