private void UpdateRepresentationAttribute(Models.RepresentationAttribute representationAttribute, SchemaAttributeResponse schemaAttr) { representationAttribute.SchemaAttribute = schemaAttr.ToModel(); if (schemaAttr.Type != "complex") { return; } var complex = schemaAttr as ComplexSchemaAttributeResponse; foreach (var child in representationAttribute.Children) { if (schemaAttr.MultiValued) { foreach (var c in child.Children) { var sra = complex.SubAttributes.First(s => s.Id == c.SchemaAttributeId); UpdateRepresentationAttribute(c, sra); } } else { var subAttr = complex.SubAttributes.First(s => s.Id == child.SchemaAttributeId); UpdateRepresentationAttribute(child, subAttr); } } }
public RepresentationAttribute Transform(Models.RepresentationAttribute attr) { if (attr == null || (attr.Children == null && string.IsNullOrWhiteSpace(attr.Value)) && attr.ValueNumber == default(double)) { return(null); } SchemaAttributeResponse schemaAttr = null; if (attr.SchemaAttribute != null) { schemaAttr = Transform(attr.SchemaAttribute); } if (attr.SchemaAttribute != null && attr.SchemaAttribute.Type == Common.Constants.SchemaAttributeTypes.Complex || attr.SchemaAttribute == null && attr.Children != null && attr.Children.Any()) { ComplexRepresentationAttribute result = new ComplexRepresentationAttribute(schemaAttr); result.Values = new List <RepresentationAttribute>(); foreach (var child in attr.Children) { var transformed = Transform(child); if (transformed == null) { continue; } transformed.Parent = result; result.Values = result.Values.Concat(new[] { transformed }); } return(result); } var isArr = attr.SchemaAttribute.MultiValued; switch (attr.SchemaAttribute.Type) { case Common.Constants.SchemaAttributeTypes.String: if (isArr) { var values = new List <string>(); if (attr.Values != null) { foreach (var value in attr.Values) { values.Add(value.Value); } } return(new SingularRepresentationAttribute <IEnumerable <string> >(schemaAttr, values)); } return(new SingularRepresentationAttribute <string>(schemaAttr, attr.Value)); case Common.Constants.SchemaAttributeTypes.Boolean: bool r = false; if (isArr) { var values = new List <bool>(); if (attr.Values != null) { foreach (var value in attr.Values) { if (bool.TryParse(value.Value, out r)) { values.Add(r); } } } return(new SingularRepresentationAttribute <IEnumerable <bool> >(schemaAttr, values)); } bool.TryParse(attr.Value, out r); return(new SingularRepresentationAttribute <bool>(schemaAttr, r)); case Common.Constants.SchemaAttributeTypes.DateTime: DateTime dateTime = DateTime.Now; double d; if (isArr) { var values = new List <DateTime>(); if (attr.Values != null) { foreach (var value in attr.Values) { if (double.TryParse(value.Value, out d)) { values.Add(d.ToDateTime()); } } } return(new SingularRepresentationAttribute <IEnumerable <DateTime> >(schemaAttr, values)); } if (attr.ValueNumber != default(double)) { dateTime = attr.ValueNumber.ToDateTime(); } return(new SingularRepresentationAttribute <DateTime>(schemaAttr, dateTime)); case Common.Constants.SchemaAttributeTypes.Decimal: decimal dec; if (isArr) { var values = new List <decimal>(); if (attr.Values != null) { foreach (var value in attr.Values) { if (decimal.TryParse(value.Value, out dec)) { values.Add(dec); } } } return(new SingularRepresentationAttribute <IEnumerable <decimal> >(schemaAttr, values)); } return(new SingularRepresentationAttribute <decimal>(schemaAttr, (decimal)attr.ValueNumber)); case Common.Constants.SchemaAttributeTypes.Integer: int i; if (isArr) { var values = new List <int>(); if (attr.Values != null) { foreach (var value in attr.Values) { if (int.TryParse(value.Value, out i)) { values.Add(i); } } } return(new SingularRepresentationAttribute <IEnumerable <int> >(schemaAttr, values)); } return(new SingularRepresentationAttribute <int>(schemaAttr, (int)attr.ValueNumber)); } return(null); }
public Models.RepresentationAttribute Transform(RepresentationAttribute attr) { var record = new Models.RepresentationAttribute { Id = Guid.NewGuid().ToString(), Children = new List <Models.RepresentationAttribute>() }; if (attr.SchemaAttribute != null) { record.SchemaAttributeId = attr.SchemaAttribute.Id; } var complexAttr = attr as ComplexRepresentationAttribute; if (complexAttr != null) { if (complexAttr.Values != null) { foreach (var child in complexAttr.Values) { var transformed = Transform(child); if (transformed == null) { continue; } transformed.Parent = record; record.Children.Add(transformed); } } return(record); } if (attr.SchemaAttribute.MultiValued) { var singular = attr as SingularRepresentationAttribute <IEnumerable <string> >; if (singular != null) { var representationAttributeValues = new List <RepresentationAttributeValue>(); switch (attr.SchemaAttribute.Type) { case Common.Constants.SchemaAttributeTypes.Boolean: case Common.Constants.SchemaAttributeTypes.String: case Common.Constants.SchemaAttributeTypes.Integer: case Common.Constants.SchemaAttributeTypes.Decimal: foreach (var value in singular.Value) { representationAttributeValues.Add(new RepresentationAttributeValue { Id = Guid.NewGuid().ToString(), Value = value.ToString() }); } break; case Common.Constants.SchemaAttributeTypes.DateTime: foreach (var value in singular.Value) { DateTime dt; if (DateTime.TryParse(value, out dt)) { representationAttributeValues.Add(new RepresentationAttributeValue { Id = Guid.NewGuid().ToString(), Value = dt.ToUnix().ToString() }); } } break; } record.Values = representationAttributeValues; } } else { var value = attr.GetValue(); switch (attr.SchemaAttribute.Type) { case Common.Constants.SchemaAttributeTypes.Boolean: case Common.Constants.SchemaAttributeTypes.String: record.Value = value == null ? string.Empty : value.ToString(); break; case Common.Constants.SchemaAttributeTypes.Decimal: var dec = (decimal)value; record.ValueNumber = (double)dec; break; case Common.Constants.SchemaAttributeTypes.Integer: var i = (int)value; record.ValueNumber = i; break; case Common.Constants.SchemaAttributeTypes.DateTime: var d = (DateTime)value; record.ValueNumber = d.ToUnix(); break; } } return(record); }