private static ValueElementIdReferenceAPI FindValueElementIdReferenceForValueElementId(ValueElementIdAPI valueElementId, List <ValueElementIdReferenceAPI> valueElementIdReferences)
        {
            ValueElementIdReferenceAPI valueElementIdReference = null;

            if (valueElementId != null)
            {
                if (valueElementIdReferences != null &&
                    valueElementIdReferences.Count > 0)
                {
                    foreach (ValueElementIdReferenceAPI valueElementIdReferenceEntry in valueElementIdReferences)
                    {
                        // For the value element reference to be a match, both the identifier and the type element property identifier must match
                        if (valueElementIdReferenceEntry.id.Equals(valueElementId.id, StringComparison.OrdinalIgnoreCase) == true &&
                            ((string.IsNullOrWhiteSpace(valueElementId.typeElementPropertyId) == true &&
                              string.IsNullOrWhiteSpace(valueElementIdReferenceEntry.typeElementPropertyId) == true) ||
                             (string.IsNullOrWhiteSpace(valueElementId.typeElementPropertyId) == false &&
                              string.IsNullOrWhiteSpace(valueElementIdReferenceEntry.typeElementPropertyId) == false &&
                              valueElementId.typeElementPropertyId.Equals(valueElementIdReferenceEntry.typeElementPropertyId, StringComparison.OrdinalIgnoreCase) == true)))
                        {
                            valueElementIdReference = valueElementIdReferenceEntry;
                            break;
                        }
                    }
                }

                if (valueElementIdReference == null)
                {
                    // TODO: Warn the author that a reference has gone missing via a notification
                }
            }

            return(valueElementIdReference);
        }
        private static ObjectAPI Convert(Type type, String externalId, object source, List <ValueElementIdReferenceAPI> valueElementIdReferences)
        {
            ObjectAPI objectAPI = null;

            if (source != null)
            {
                // If we have a value element identifier, we need to translate it over
                if (type.Name.Equals("ValueElementIdAPI", StringComparison.OrdinalIgnoreCase) == true)
                {
                    type = new ValueElementIdReferenceAPI().GetType();
                }

                objectAPI = new ObjectAPI();
                objectAPI.developerName = GetCleanObjectName(type.Name);
                objectAPI.externalId    = externalId;
                objectAPI.properties    = new List <PropertyAPI>();

                foreach (PropertyInfo propertyInfo in type.GetRuntimeProperties())
                {
                    PropertyAPI propertyAPI = Convert(source, propertyInfo, valueElementIdReferences);
                    if (propertyAPI != null)
                    {
                        objectAPI.properties.Add(propertyAPI);
                    }
                }
            }

            return(objectAPI);
        }
Example #3
0
        private static ValueElementIdReferenceAPI FindValueElementIdReferenceForValueElementId(ValueElementIdAPI valueElementId, List <ValueElementIdReferenceAPI> valueElementIdReferences)
        {
            ValueElementIdReferenceAPI valueElementIdReference = null;

            if (valueElementId != null)
            {
                if (valueElementIdReferences != null &&
                    valueElementIdReferences.Count > 0)
                {
                    // If we're not given a property ID, then we're looking for a scalar value
                    if (string.IsNullOrWhiteSpace(valueElementId.typeElementPropertyId))
                    {
                        valueElementIdReference = valueElementIdReferences
                                                  .Where(valueElementIdReferenceEntry => valueElementIdReferenceEntry.id == valueElementId.id)
                                                  .FirstOrDefault();
                    }
                    else
                    {
                        // For the value element reference to be a match, both the identifier and the type element property identifier must match
                        valueElementIdReference = valueElementIdReferences
                                                  .Where(valueElementIdReferenceEntry => valueElementIdReferenceEntry.id == valueElementId.id)
                                                  .Where(valueElementIdReferenceEntry => valueElementIdReferenceEntry.typeElementPropertyId == valueElementId.typeElementPropertyId)
                                                  .FirstOrDefault();
                    }
                }

                if (valueElementIdReference == null)
                {
                    // TODO: Warn the author that a reference has gone missing via a notification
                }
            }

            return(valueElementIdReference);
        }
        public static List <object> Convert(Type type, List <ObjectAPI> objectAPIs)
        {
            List <object> list = null;

            if (objectAPIs != null &&
                objectAPIs.Count > 0)
            {
                String typeName = GetCleanObjectName(type.Name);

                // Create the list to hold the translated objects
                list = new List <object>();

                foreach (ObjectAPI objectAPI in objectAPIs)
                {
                    // If we have a value element identifier, we need to translate it over
                    if (type.Name.Equals("ValueElementIdAPI", StringComparison.OrdinalIgnoreCase) == true)
                    {
                        type = new ValueElementIdReferenceAPI().GetType();
                    }
                    else if (objectAPI.developerName.Equals(typeName, StringComparison.OrdinalIgnoreCase) == false)
                    {
                        throw new ArgumentNullException("ObjectAPI", String.Format("The provided list contains inconsistent objects. The Draw API expected {0} and it got {1}", typeName, objectAPI.developerName));
                    }

                    if (objectAPI.properties != null &&
                        objectAPI.properties.Count > 0)
                    {
                        // Create an instance of the typed object so we can fill it up with data
                        object typedObject = Activator.CreateInstance(type);

                        // Grab the properties from the type so we can assign them from the incoming object api properties
                        IEnumerable <PropertyInfo> propertyInfosFromType = type.GetRuntimeProperties();

                        // Go through the properties in the incoming data first as this may be a sub-set
                        foreach (PropertyAPI propertyAPI in objectAPI.properties)
                        {
                            // Now go through the properties in the object to see if we have a matching one
                            for (int i = 0; i < propertyInfosFromType.Count(); i++)
                            {
                                PropertyInfo propertyInfoFromType = propertyInfosFromType.ElementAt(i);

                                // Check to see if this is the matching property in the type
                                if (GetCleanPropertyName(propertyInfoFromType.Name).Equals(propertyAPI.developerName, StringComparison.OrdinalIgnoreCase) == true)
                                {
                                    // The the property from the object, but only if it's public
                                    PropertyInfo propertyInfo = typedObject.GetType().GetRuntimeProperty(propertyInfoFromType.Name);

                                    // Check to make sure we found one and that we can write to it
                                    if (propertyInfo != null &&
                                        propertyInfo.CanWrite == true)
                                    {
                                        // Convert the property over correctly for the object property
                                        if (propertyInfo.PropertyType.Name.Equals(typeof(String).Name, StringComparison.OrdinalIgnoreCase) == true)
                                        {
                                            propertyInfo.SetValue(typedObject, propertyAPI.contentValue, null);
                                        }
                                        else if (propertyInfo.PropertyType.Name.Equals(typeof(Guid).Name, StringComparison.OrdinalIgnoreCase) == true)
                                        {
                                            if (string.IsNullOrWhiteSpace(propertyAPI.contentValue) == false)
                                            {
                                                Guid guid = Guid.Empty;

                                                if (Guid.TryParse(propertyAPI.contentValue, out guid) == true)
                                                {
                                                    propertyInfo.SetValue(typedObject, guid, null);
                                                }
                                                else
                                                {
                                                    throw new ArgumentNullException("ObjectAPI.PropertyAPI", string.Format("The property value provided is not a valid Guid. The property being assigned is: '{0}'. The value provided is: '{1}'", propertyAPI.developerName, propertyAPI.contentValue));
                                                }
                                            }
                                        }
                                        else if (propertyInfo.PropertyType.Name.Equals(typeof(Int32).Name, StringComparison.OrdinalIgnoreCase) == true)
                                        {
                                            if (string.IsNullOrWhiteSpace(propertyAPI.contentValue) == false)
                                            {
                                                Int32 int32 = 0;

                                                if (Int32.TryParse(propertyAPI.contentValue, out int32) == true)
                                                {
                                                    propertyInfo.SetValue(typedObject, int32, null);
                                                }
                                                else
                                                {
                                                    throw new ArgumentNullException("ObjectAPI.PropertyAPI", string.Format("The property value provided is not a valid Number. The property being assigned is: '{0}'. The value provided is: '{1}'", propertyAPI.developerName, propertyAPI.contentValue));
                                                }
                                            }
                                        }
                                        else if (propertyInfo.PropertyType.Name.Equals(typeof(DateTime).Name, StringComparison.OrdinalIgnoreCase) == true)
                                        {
                                            if (string.IsNullOrWhiteSpace(propertyAPI.contentValue) == false)
                                            {
                                                DateTime dateTime = DateTime.Now;

                                                if (DateTime.TryParse(propertyAPI.contentValue, out dateTime) == true)
                                                {
                                                    propertyInfo.SetValue(typedObject, dateTime, null);
                                                }
                                                else
                                                {
                                                    throw new ArgumentNullException("ObjectAPI.PropertyAPI", string.Format("The property value provided is not a valid DateTime. The property being assigned is: '{0}'. The value provided is: '{1}'", propertyAPI.developerName, propertyAPI.contentValue));
                                                }
                                            }
                                        }
                                        else if (propertyInfo.PropertyType.Name.Equals(typeof(Boolean).Name, StringComparison.OrdinalIgnoreCase) == true)
                                        {
                                            if (string.IsNullOrWhiteSpace(propertyAPI.contentValue) == false)
                                            {
                                                Boolean boolean = false;

                                                if (Boolean.TryParse(propertyAPI.contentValue, out boolean) == true)
                                                {
                                                    propertyInfo.SetValue(typedObject, boolean, null);
                                                }
                                                else
                                                {
                                                    throw new ArgumentNullException("ObjectAPI.PropertyAPI", string.Format("The property value provided is not a valid Boolean. The property being assigned is: '{0}'. The value provided is: '{1}'", propertyAPI.developerName, propertyAPI.contentValue));
                                                }
                                            }
                                        }
                                        else
                                        {
                                            // We have some form of object type, so we need to do some additional testing
                                            if (typeof(Dictionary <String, String>).GetTypeInfo().IsAssignableFrom(propertyInfo.PropertyType.GetTypeInfo()))
                                            {
                                                // Check if we're looking at the Attributes field and whether we have any
                                                if (propertyInfo.Name.Equals("attributes") && propertyAPI.objectData != null && propertyAPI.objectData.Count > 0)
                                                {
                                                    // If there are any attributes then map all of them into a Dictionary<string, string>
                                                    var attributes = propertyAPI.objectData.Where(o => o.developerName.Equals("KeyPair"))
                                                                     .ToDictionary(
                                                        o =>
                                                    {
                                                        return(o.properties.First(p => p.developerName.Equals("Key")).contentValue);
                                                    },
                                                        o =>
                                                    {
                                                        return(o.properties.First(p => p.developerName.Equals("Value")).contentValue);
                                                    });

                                                    propertyInfo.SetValue(typedObject, attributes);
                                                }
                                            }
                                            else if (typeof(Enum).GetTypeInfo().IsAssignableFrom(propertyInfo.PropertyType.GetTypeInfo()))
                                            {
                                                propertyInfo.SetValue(typedObject, Enum.Parse(propertyInfo.PropertyType, propertyAPI.contentValue));
                                            }
                                            // string inherits from IEnumerable so add a check for "not string"
                                            else if (typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(propertyInfo.PropertyType.GetTypeInfo()) &&
                                                     propertyInfo.PropertyType != typeof(string))
                                            {
                                                Type itemType = null;

                                                // Check to make sure it is a full on list
                                                if (propertyInfo.PropertyType.GetTypeInfo().IsGenericType&& propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(List <>))
                                                {
                                                    itemType = propertyInfo.PropertyType.GenericTypeArguments.ElementAt(0);
                                                }

                                                // As an extra check, we make sure the name of the type ends with API or we may have a non-converted primitive property
                                                if (itemType.Name.EndsWith("API", StringComparison.OrdinalIgnoreCase) == false)
                                                {
                                                    throw new NotImplementedException(propertyInfo.PropertyType.Name);
                                                }

                                                List <object> objectList = Convert(itemType, propertyAPI.objectData);
                                                IList         typedList  = null;

                                                if (objectList != null &&
                                                    objectList.Count > 0)
                                                {
                                                    var listType = typeof(List <>).MakeGenericType(itemType);
                                                    typedList = (IList)Activator.CreateInstance(listType);

                                                    foreach (object objectEntry in objectList)
                                                    {
                                                        typedList.Add(objectEntry);
                                                    }
                                                }

                                                propertyInfo.SetValue(typedObject, typedList, null);
                                            }
                                            else
                                            {
                                                // As an extra check, we make sure the name of the type ends with API or we may have a non-converted primitive property
                                                if (propertyInfo.PropertyType.Name.EndsWith("API", StringComparison.OrdinalIgnoreCase) == false)
                                                {
                                                    throw new NotImplementedException(propertyInfo.PropertyType.Name);
                                                }

                                                List <object> propertyList = Convert(propertyInfo.PropertyType, propertyAPI.objectData);

                                                // Check to make sure we converted something over
                                                if (propertyList != null &&
                                                    propertyList.Count > 0)
                                                {
                                                    foreach (var propertyObject in propertyList)
                                                    {
                                                        // Assign the first entry in the returned list
                                                        propertyInfo.SetValue(typedObject, propertyObject, null);
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        // Add the typed object to the list
                        list.Add(typedObject);
                    }
                }
            }

            return(list);
        }