Ejemplo n.º 1
0
        private Dtobase Filter(Dtobase item, Type type, byte level)
        {
            try
            {
                if (null == item)
                {
                    return(null);
                }

                Dtobase        dto        = (Dtobase)Activator.CreateInstance(type);
                PropertyMeta[] properties = this.context.GetPropertyDefs(type);
                for (int i = 0; i < properties.Length; i++)
                {
                    PropertyMeta property = properties[i];
                    if (!property.Valid || !property.HasLevel(level))
                    {
                        continue;
                    }

                    Object value = property.Read(item);
                    if (PropertyMeta.ClassType.Value == property.PropertyClassType ||
                        PropertyMeta.ClassType.CustomType == property.PropertyClassType ||
                        PropertyMeta.ClassType.CustomTypeCollection == property.PropertyClassType)
                    {
                        property.Write(dto, value);
                    }
                    else if (PropertyMeta.ClassType.Class == property.PropertyClassType)
                    {
                        byte nestedLevel = property.DtoNestedLevel;
                        if (level != property.DtoLevel)
                        {
                            nestedLevel = (byte)(nestedLevel + level - property.DtoLevel);
                        }

                        property.Write(dto,
                                       this.Filter((Dtobase)value, property.PropertyClass, nestedLevel));
                    }
                    else if (PropertyMeta.ClassType.Collection == property.PropertyClassType)
                    {
                        byte nestedLevel = property.DtoNestedLevel;
                        if (level != property.DtoLevel)
                        {
                            nestedLevel = (byte)(nestedLevel + level - property.DtoLevel);
                        }

                        property.Write(dto,
                                       this.FilterList((IEnumerable)value, property.InnerGenericClass, nestedLevel));
                    }
                }

                return(dto);
            }
            catch (Exception ex)
            {
                throw new Exception(
                          "Filtering failed. Type:'" + item.GetType().Name +
                          "'. Level:'" + level + "'", ex);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Validates if current field value is correctly parsed
        /// </summary>
        /// <param name="msg">failure message</param>
        public void MustBeValidFormat(string msg)
        {
            Dtobase target = this.pathHelper.GetTopNodeTarget(this.path);

            if (!this.checker.IsValidFormat(this.fieldName, target))
            {
                this.Fail(msg);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Validates if current field value is not assigned by JSON deserializer
        /// </summary>
        /// <param name="msg">failure message</param>
        public void MustBeNotAssigned(string msg)
        {
            Dtobase target = this.pathHelper.GetTopNodeTarget(this.path);

            if (this.checker.IsAssigned(this.fieldName, target))
            {
                this.Fail(msg);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Validates if the number of assigned fields in the current target object is greater or equal to min number
        /// </summary>
        /// <param name="count">min number of assigned fields</param>
        /// <param name="msg">failure message</param>
        public void MustHaveMinAssignedFieldsCount(int count, string msg)
        {
            Dtobase target = this.pathHelper.GetTopNodeTarget(this.path);

            if (null == target.GetAssignedFields() ||
                count > target.GetAssignedFields().Count)
            {
                this.FailRoot(msg);
            }
        }
Ejemplo n.º 5
0
        private void MapTo(Dtobase source, Dtobase dest, Type type)
        {
            if (null == source)
            {
                return;
            }

            try
            {
                PropertyMeta[] properties = this.context.GetPropertyDefs(type);
                for (int i = 0; i < properties.Length; i++)
                {
                    PropertyMeta property = properties[i];
                    if (!property.Valid || !property.IsAssigned(source))
                    {
                        continue;
                    }

                    Object sourceValue = property.Read(source);
                    Object destValue   = property.Read(dest);

                    if (null == sourceValue ||
                        null == destValue ||
                        PropertyMeta.ClassType.Value == property.PropertyClassType ||
                        PropertyMeta.ClassType.CustomType == property.PropertyClassType ||
                        PropertyMeta.ClassType.CustomTypeCollection == property.PropertyClassType)
                    {
                        property.Write(dest, sourceValue);
                    }
                    else if (PropertyMeta.ClassType.Class == property.PropertyClassType)
                    {
                        if (object.Equals(sourceValue, destValue))
                        {
                            this.MapTo((Dtobase)sourceValue, (Dtobase)destValue, sourceValue.GetType());
                        }
                        else
                        {
                            property.Write(dest, sourceValue);
                        }
                    }
                    else if (PropertyMeta.ClassType.Collection == property.PropertyClassType)
                    {
                        IEnumerable destList   = (IEnumerable)destValue;
                        IEnumerable sourceList = (IEnumerable)sourceValue;
                        this.MapToCollection(sourceList, destList, source);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(
                          "Mapping failed from '" + source.GetType().Name +
                          "' to '" + dest.GetType().Name + "'", ex);
            }
        }
Ejemplo n.º 6
0
        private IList FilterList(IEnumerable items, Type type, byte level)
        {
            if (null == items)
            {
                return(null);
            }

            IList list = (IList)this.context.BuildList(type);

            foreach (Dtobase item in items)
            {
                Dtobase dto = this.Filter(item, type, level);
                list.Add(dto);
            }

            return(list);
        }
Ejemplo n.º 7
0
        private void Map(Dtobase source, Type type)
        {
            PropertyMeta property = null;

            try
            {
                PropertyMeta[] properties = this.context.GetPropertyDefs(type);
                for (int i = 0; i < properties.Length; i++)
                {
                    property = properties[i];
                    if (!property.Valid)
                    {
                        continue;
                    }

                    object sourceValue = property.Read(source);
                    if (null != sourceValue)
                    {
                        if (PropertyMeta.ClassType.Class == property.PropertyClassType)
                        {
                            this.Map((Dtobase)sourceValue, property.PropertyClass);
                        }
                        else if (PropertyMeta.ClassType.Collection == property.PropertyClassType)
                        {
                            IEnumerable sourceSet = (IEnumerable)sourceValue;
                            foreach (object item in sourceSet)
                            {
                                this.Map((Dtobase)item, property.InnerGenericClass);
                                if (item is IChild)
                                {
                                    (item as IChild).ConnectToParent(source);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Mapping failed. Type:'" + source.GetType().Name + "'. Name:'" + property.Name + "'", ex);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// True if current field is correctly parsed, otherwise false
        /// </summary>
        /// <returns>true if valid</returns>
        public bool IsValidFormat()
        {
            Dtobase target = this.pathHelper.GetTopNodeTarget(this.path);

            return(this.checker.IsValidFormat(this.fieldName, target));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// True if current field is assigned, otherwise false
        /// </summary>
        /// <returns>true is assigned</returns>
        public bool IsAssigned()
        {
            Dtobase target = this.pathHelper.GetTopNodeTarget(this.path);

            return(this.checker.IsAssigned(this.fieldName, target));
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Sets top node target
 /// </summary>
 /// <param name="path">existing path</param>
 /// <param name="target">target object</param>
 public void SetTopNodeTarget(LinkedList <PathNode> path, Dtobase target)
 {
     path.Last.Value.Target = target;
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Assignes value to a property instance
 /// </summary>
 /// <param name="target">object instance</param>
 /// <param name="value">value</param>
 public void Write(Dtobase target, object value)
 {
     this.descriptor.SetValue(target, value);
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Checks if field value is assigned
 /// </summary>
 /// <returns></returns>
 public bool IsAssigned(string fieldName, Dtobase target)
 {
     return(target.IsFieldAssigned(fieldName));
 }
Ejemplo n.º 13
0
        private void MapToCollection(IEnumerable sourceCollection, IEnumerable destCollection, Dtobase parent)
        {
            List <Dtobase> destItems      = new List <Dtobase>();
            bool           firstIteration = true;
            bool           child          = false;

            foreach (Dtobase item in destCollection)
            {
                if (firstIteration)
                {
                    firstIteration = false;
                    child          = item is IChild;
                }

                destItems.Add(item);
            }

            IList       destList           = null;
            IEnumerable destHashSet        = null;
            MethodInfo  hashSetAdd         = null;
            MethodInfo  hashSetClear       = null;
            Type        destCollectionType = destCollection.GetType();

            if (LIST_TYPE.IsAssignableFrom(destCollectionType))
            {
                destList = (IList)destCollection;
                destList.Clear();
            }
            else // Treat as ISet<?>
            {
                destHashSet = (IEnumerable)destCollection;
                Type hashSetType = destCollection.GetType();
                hashSetAdd   = hashSetType.GetMethod("Add");
                hashSetClear = hashSetType.GetMethod("Clear");
                hashSetClear.Invoke(destHashSet, null);
            }

            List <Dtobase> sourceItems = new List <Dtobase>();

            foreach (Dtobase item in sourceCollection)
            {
                sourceItems.Add(item);
                Dtobase matchedDestItem = null;
                foreach (Dtobase destItem in destItems)
                {
                    if (object.Equals(destItem, item))
                    {
                        matchedDestItem = destItem;
                        break;
                    }
                }

                if (null != matchedDestItem)
                {
                    this.MapTo(item, matchedDestItem, item.GetType());
                    if (null != destList)
                    {
                        destList.Add(matchedDestItem);
                    }
                    else
                    {
                        hashSetAdd.Invoke(destHashSet, new object[] { matchedDestItem });
                    }
                }
                else
                {
                    if (null != destList)
                    {
                        destList.Add(item);
                    }
                    else
                    {
                        hashSetAdd.Invoke(destHashSet, new object[] { item });
                    }

                    if (child)
                    {
                        ((IChild)item).ConnectToParent(parent);
                    }
                }
            }

            if (sourceItems.Count < destItems.Count)
            {
                foreach (Dtobase original in destItems)
                {
                    bool itemDeleted = true;
                    foreach (Dtobase updated in sourceItems)
                    {
                        if (object.Equals(original, updated))
                        {
                            itemDeleted = false;
                        }
                    }
                }
            }
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Reads value of the property instance
 /// </summary>
 /// <param name="target">object instance</param>
 /// <returns>value</returns>
 public object Read(Dtobase target)
 {
     return(this.descriptor.GetValue(target));
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Sets current target object
 /// </summary>
 /// <param name="value">target object</param>
 public void SetTarget(Dtobase value)
 {
     this.pathHelper.SetTopNodeTarget(this.path, value);
     this.isTargetValid = true;
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Checks if field value was correctly parsed
 /// </summary>
 /// <returns></returns>
 public bool IsValidFormat(string fieldName, Dtobase target)
 {
     return(!target.IsFieldWrong(fieldName));
 }
Ejemplo n.º 17
0
        /// <summary>
        /// Parses JSON data into domain object
        /// </summary>
        /// <param name="node">JSON tree</param>
        /// <param name="type">domain object type</param>
        /// <returns>parsed domain object instance</returns>
        public object Parse(JToken node, Type type)
        {
            try
            {
                Dtobase dto = (Dtobase)Activator.CreateInstance(type);
                if (!node.HasValues)
                {
                    return(dto);
                }

                PropertyMeta[] properties = this.context.GetPropertyDefs(type);
                for (int i = 0; i < properties.Length; i++)
                {
                    PropertyMeta propertyDef = properties[i];
                    if (!propertyDef.Valid)
                    {
                        continue;
                    }

                    JToken propertyValue = node[propertyDef.Name];
                    if (null != propertyValue)
                    {
                        try
                        {
                            if (PropertyMeta.ClassType.Value == propertyDef.PropertyClassType)
                            {
                                dto.AddAssignedField(propertyDef.Name);
                                propertyDef.Write(dto, this.ParseValue(propertyValue, propertyDef));
                            }
                            else if (PropertyMeta.ClassType.Class == propertyDef.PropertyClassType ||
                                     PropertyMeta.ClassType.CustomType == propertyDef.PropertyClassType)
                            {
                                dto.AddAssignedField(propertyDef.Name);
                                propertyDef.Write(dto, this.Parse(propertyValue, propertyDef.PropertyClass));
                            }
                            else if (PropertyMeta.ClassType.Collection == propertyDef.PropertyClassType ||
                                     PropertyMeta.ClassType.CustomTypeCollection == propertyDef.PropertyClassType)
                            {
                                IList list = (IList)this.context.BuildList(propertyDef.InnerGenericClass);
                                foreach (JToken item in propertyValue)
                                {
                                    list.Add(this.Parse(item, propertyDef.InnerGenericClass));
                                }

                                propertyDef.Write(dto, list);
                                dto.AddAssignedField(propertyDef.Name);
                            }
                        }
                        catch
                        {
                            dto.AddWrongField(propertyDef.Name);
                        }
                    }
                }

                return(dto);
            }
            catch (Exception ex)
            {
                throw new Exception("JSON parsing failed", ex);
            }
        }
Ejemplo n.º 18
0
 /// <summary>
 /// Determines if the property instance is assigned by JSON deserializer
 /// </summary>
 /// <param name="target">object instance</param>
 /// <returns>true if assigned</returns>
 public bool IsAssigned(Dtobase target)
 {
     return(target.IsFieldAssigned(this.name));
 }