Beispiel #1
0
        public Model.RepresentationAttribute Transform(RepresentationAttribute attr)
        {
            var record = new Model.RepresentationAttribute
            {
                Id       = Guid.NewGuid().ToString(),
                Children = new List <Model.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);
            }

            record.Value = attr.GetSerializedValue();
            return(record);
        }
Beispiel #2
0
        public RepresentationAttribute Transform(Model.RepresentationAttribute attr)
        {
            if (attr == null || (attr.Children == null && string.IsNullOrWhiteSpace(attr.Value)))
            {
                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 record = JsonConvert.DeserializeObject <IEnumerable <string> >(attr.Value);
                    return(new SingularRepresentationAttribute <IEnumerable <string> >(schemaAttr, record));
                }

                var str = JsonConvert.DeserializeObject <string>(attr.Value);
                return(new SingularRepresentationAttribute <string>(schemaAttr, str));

            case Common.Constants.SchemaAttributeTypes.Boolean:
                if (isArr)
                {
                    var record = JsonConvert.DeserializeObject <IEnumerable <bool> >(attr.Value);
                    return(new SingularRepresentationAttribute <IEnumerable <bool> >(schemaAttr, record));
                }

                var b = JsonConvert.DeserializeObject <bool>(attr.Value);
                return(new SingularRepresentationAttribute <bool>(schemaAttr, b));

            case Common.Constants.SchemaAttributeTypes.DateTime:
                if (isArr)
                {
                    var record = JsonConvert.DeserializeObject <IEnumerable <DateTime> >(attr.Value);
                    return(new SingularRepresentationAttribute <IEnumerable <DateTime> >(schemaAttr, record));
                }

                var datetime = JsonConvert.DeserializeObject <DateTime>(attr.Value);
                return(new SingularRepresentationAttribute <DateTime>(schemaAttr, datetime));

            case Common.Constants.SchemaAttributeTypes.Decimal:
                if (isArr)
                {
                    var record = JsonConvert.DeserializeObject <IEnumerable <decimal> >(attr.Value);
                    return(new SingularRepresentationAttribute <IEnumerable <decimal> >(schemaAttr, record));
                }

                var dec = JsonConvert.DeserializeObject <decimal>(attr.Value);
                return(new SingularRepresentationAttribute <decimal>(schemaAttr, dec));

            case Common.Constants.SchemaAttributeTypes.Integer:
                if (isArr)
                {
                    var record = JsonConvert.DeserializeObject <IEnumerable <int> >(attr.Value);
                    return(new SingularRepresentationAttribute <IEnumerable <int> >(schemaAttr, record));
                }

                var i = JsonConvert.DeserializeObject <int>(attr.Value);
                return(new SingularRepresentationAttribute <int>(schemaAttr, i));
            }

            return(null);
        }
Beispiel #3
0
        public RepresentationAttribute Transform(Model.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);
        }
Beispiel #4
0
        public Model.RepresentationAttribute Transform(RepresentationAttribute attr)
        {
            var record = new Model.RepresentationAttribute
            {
                Id       = Guid.NewGuid().ToString(),
                Children = new List <Model.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 <Model.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 Model.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 Model.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);
        }