Example #1
0
        private static PropertyAPI GetPropertyAPIFromType(object source, PropertyInfo propertyInfo, List <ValueElementIdReferenceAPI> valueElementIdReferences)
        {
            List <ObjectAPI> objectData    = null;
            string           value         = null;
            object           propertyValue = null;

            // Check to see if the source is a value element identifier, if so we map that over to the full reference
            if (source is ValueElementIdAPI)
            {
                source = FindValueElementIdReferenceForValueElementId((ValueElementIdAPI)source, valueElementIdReferences);
            }

            if (source != null)
            {
                propertyValue = propertyInfo.GetValue(source, null);
            }

            // All of the ValueElementIds are translated to ValueElementIdReferences, so we need to do a little switch here
            if (propertyInfo.PropertyType.Name.Equals("ValueElementIdAPI", StringComparison.OrdinalIgnoreCase))
            {
                // We switch the source to the matching ValueElementIdReference rather than the actual ValueElementId
                source = FindValueElementIdReferenceForValueElementId((ValueElementIdAPI)propertyValue, valueElementIdReferences);
            }

            if (propertyValue != null)
            {
                if (propertyValue is DateTime)
                {
                    value = (new DateTimeOffset((DateTime)propertyValue)).ToString("o");
                }
                else if (propertyValue is DateTimeOffset)
                {
                    value = ((DateTimeOffset)propertyValue).ToString("o");
                }
                else if (propertyValue is int ||
                         propertyValue is bool ||
                         propertyValue is string ||
                         propertyValue is Guid)
                {
                    value = propertyValue.ToString();
                }
                else
                {
                    objectData = new List <ObjectAPI>();
                    objectData.Add(Convert(propertyInfo.PropertyType, Fuid.NewGuid().ToString(), propertyValue, valueElementIdReferences));
                }
            }

            return(new PropertyAPI
            {
                developerName = GetCleanPropertyName(propertyInfo.Name),
                contentValue = value,
                objectData = objectData,
                typeElementPropertyId = null
            });
        }
Example #2
0
        private static PropertyAPI GetPropertyAPIFromDictionary(object source, PropertyInfo propertyInfo, List <ValueElementIdReferenceAPI> valueElementIdReferences)
        {
            PropertyAPI propertyAPI = null;
            IDictionary value       = (IDictionary)propertyInfo.GetValue(source, null);

            if (value != null)
            {
                var values = value as IDictionary <string, string>;
                if (values != null && values.Count > 0)
                {
                    // Add a new list of "Object: String" type objects
                    propertyAPI = new PropertyAPI();

                    if (propertyInfo.Name.EqualsIgnoreCase("properties"))
                    {
                        propertyAPI.developerName = "Properties";
                    }
                    else
                    {
                        propertyAPI.developerName = "Attributes";
                    }

                    // For each keypair, create a new "Object: String" object
                    propertyAPI.objectData = new List <ObjectAPI>();

                    foreach (var attribute in values)
                    {
                        propertyAPI.objectData.Add(new ObjectAPI
                        {
                            developerName = "KeyPair",
                            externalId    = Fuid.NewGuid().ToString(),
                            properties    = new List <PropertyAPI>
                            {
                                new PropertyAPI {
                                    developerName = "Key", contentValue = attribute.Key
                                },
                                new PropertyAPI {
                                    developerName = "Value", contentValue = attribute.Value
                                }
                            }
                        });
                    }
                }
            }

            return(propertyAPI);
        }
Example #3
0
        private static PropertyAPI GetPropertyAPIValueFromCollection(IEnumerable values, PropertyInfo propertyInfo, List <ValueElementIdReferenceAPI> valueElementIdReferences)
        {
            List <ObjectAPI> objectAPIs = null;
            Type             type       = propertyInfo.PropertyType;

            // Check to make sure it is a full on list and get the type from the entries
            if (type.GetTypeInfo().IsGenericType&& type.GetGenericTypeDefinition() == typeof(List <>))
            {
                type = type.GetTypeInfo().GenericTypeArguments.ElementAt(0);
            }

            var idProperty = type.GetRuntimeProperties().FirstOrDefault(property => property.Name.EqualsIgnoreCase("id"));

            if (values != null)
            {
                objectAPIs = new List <ObjectAPI>();

                foreach (object objectEntry in values)
                {
                    string id;

                    if (objectEntry is ElementAPI)
                    {
                        // Assign the identifier property from the object
                        id = ((ElementAPI)objectEntry).id;
                    }
                    else if (idProperty != null)
                    {
                        id = System.Convert.ToString(idProperty.GetValue(objectEntry));
                    }
                    else
                    {
                        // Assign a random external identifier
                        id = Fuid.NewGuid().ToString();
                    }

                    objectAPIs.Add(Convert(type, id, objectEntry, valueElementIdReferences));
                }
            }

            return(new PropertyAPI
            {
                developerName = GetCleanPropertyName(propertyInfo.Name),
                objectData = objectAPIs,
                typeElementPropertyId = null
            });
        }
Example #4
0
        public static string SerializeObjectData(ObjectAPI objectAPI, TypeElementResponseAPI typeElementResponse)
        {
            string xml        = null;
            string internalId = null;
            string externalId = null;

            if (objectAPI.internalId != null &&
                objectAPI.internalId.Trim().Length > 0)
            {
                internalId = objectAPI.internalId;
            }
            else
            {
                internalId = Fuid.NewGuid().ToString();
            }

            if (objectAPI.externalId != null &&
                objectAPI.externalId.Trim().Length > 0)
            {
                externalId = objectAPI.externalId;
            }
            else
            {
                externalId = Fuid.NewGuid().ToString();
            }

            xml  = "";
            xml += "<complextype internalid=\"" + internalId + "\" externalid=\"" + externalId + "\" typeelementid=\"" + typeElementResponse.id + "\">";

            if (objectAPI.properties != null &&
                objectAPI.properties.Count > 0)
            {
                foreach (PropertyAPI propertyAPI in objectAPI.properties)
                {
                    bool   typeElementEntryFound = false;
                    string typeElementEntryId    = null;
                    string contentType           = null;

                    if (typeElementResponse.properties != null &&
                        typeElementResponse.properties.Count > 0)
                    {
                        foreach (TypeElementPropertyAPI typeElementEntryAPI in typeElementResponse.properties)
                        {
                            if (typeElementEntryAPI.developerName.Equals(propertyAPI.developerName, StringComparison.OrdinalIgnoreCase))
                            {
                                typeElementEntryId = typeElementEntryAPI.id;
                                contentType        = typeElementEntryAPI.contentType;

                                typeElementEntryFound = true;
                                break;
                            }
                        }
                    }

                    Validation.Instance.IsTrue(typeElementEntryFound, "TypeElementEntry", "Type element entry could not be found.");

                    xml += "<complextypeentry typeelemententryid=\"" + typeElementEntryId + "\" contenttype=\"" + contentType + "\">";

                    if (contentType.Equals(ManyWhoConstants.CONTENT_TYPE_OBJECT, StringComparison.OrdinalIgnoreCase))
                    {
                        throw new ArgumentException("contentType", "Object properties not supported yet.");
                    }

                    if (contentType.Equals(ManyWhoConstants.CONTENT_TYPE_LIST, StringComparison.OrdinalIgnoreCase))
                    {
                        throw new ArgumentException("contentType", "List properties not supported yet.");
                    }

                    // Wrap primitive values in cdata so we don't screw up the xml document with invalid markup
                    xml += "<![CDATA[" + propertyAPI.contentValue + "]]>";

                    xml += "</complextypeentry>";
                }
            }

            xml += "</complextype>";

            return(xml);
        }