示例#1
0
        private string GetInsertString(Workspace workspace, ResourceType resourceType, fxLanguage language, Random random)
        {
            string commandText = "INSERT INTO {0} ({1}) VALUES ({2});";
            string tableName   = resourceType.Name;
            string columns     = "";
            string values      = "";

            ResourceInstanceKey rik = ResourceInstanceUtil.CreateUniqueKey(workspace.ServiceContainer.ResourceContainers[resourceType.Name], resourceType);

            foreach (ResourceInstanceProperty rip in rik.KeyProperties)
            {
                columns += rip.Name + ",";
                string propertyValue = AstoriaUnitTests.Data.TypeData.XmlValueFromObject(((ResourceInstanceSimpleProperty)rip).PropertyValue);
                values += propertyValue + ",";
            }

            foreach (ResourceProperty prop in resourceType.Properties)
            {
                if (!prop.IsComplexType && !prop.IsNavigation && !(prop.Type is CollectionType) && prop.PrimaryKey == null)
                {
                    columns += prop.Name + ",";

                    if (prop.Type is ClrString)
                    {
                        int maxSize = (int)(prop.Facets["MaxSize"].Value.ClrValue);
                        if (maxSize > 1000)
                        {
                            maxSize = 1000;
                        }

                        values += "'" + language.CreateData(random, maxSize).Replace("'", "''") + "',";
                    }
                    else
                    {
                        values += prop.Type.CreateValue().ToString() + ",";
                    }
                }
            }

            columns = columns.TrimEnd(',');
            values  = values.TrimEnd(',');

            string fullTableName = String.Format("[{0}].[{1}].[{2}]", this.DatabaseName, "dbo", tableName);

            return(string.Format(commandText, fullTableName, columns, values));
        }
        protected string CreateCanonicalUri(ResourceInstanceKey key)
        {
            UriQueryBuilder builder = new UriQueryBuilder(Workspace, Workspace.ServiceUri);
            builder.UseBinaryFormatForDates = false;
            builder.CleanUpSpecialCharacters = false;

            KeyExpression keyExpression = ResourceInstanceUtil.ConvertToKeyExpression(key, Workspace);
            if (keyExpression != null)
            {
                QueryNode query = ContainmentUtil.BuildCanonicalQuery(keyExpression);
                return builder.Build(query);
            }
             
            IEnumerable<ResourceInstanceSimpleProperty> properties = key.KeyProperties.OfType<ResourceInstanceSimpleProperty>();

            string keyString = builder.CreateKeyString(properties.Select(p => p.Name).ToArray(), properties.Select(p => p.PropertyValue).ToArray());
            string uri = Workspace.ServiceUri + "/" + key.ResourceSetName + "(" + keyString + ")";

            return uri;
        }
示例#3
0
            private ResourceBodyTree CanonicalResourcePayload(Workspace workspace, ResourceContainer resourceContainer)
            {
                ResourceType type = resourceContainer.BaseType;
                
                // TODO: support PUT by adding key support.

                ResourceInstanceProperty[] keyProperties = new ResourceInstanceProperty[0];
                ResourceInstanceKey keyExpression = new ResourceInstanceKey(resourceContainer,type, keyProperties);

                List<ResourceInstanceProperty> properties = new List<ResourceInstanceProperty>();
                foreach (NodeProperty p in type.Properties)
                {
                    // TODO: support to implement binding.
                    if (p.Type is ResourceCollection)
                    {
                        continue;
                    }
                    if (p.Type is ComplexType)
                    {
                        continue;
                    }

                    properties.Add(new ResourceInstanceSimpleProperty(p.Name, p.GetSampleValue()));
                }

                KeyedResourceInstance instance = new KeyedResourceInstance(keyExpression, properties.ToArray());
                return instance;
            }
 public static KeyExpression ConvertToKeyExpression(ResourceInstanceKey key, Workspace workspace)
 {
     ResourceContainer container = workspace.ServiceContainer.ResourceContainers[key.ResourceSetName];
     if (container == null)
         return null;
     ResourceType type = container.ResourceTypes.FirstOrDefault(rt => rt.Name == key.ResourceTypeName);
     if (type == null)
         return null;
     return key.CreateKeyExpression(container, type);
 }
        internal static KeyedResourceInstance CreateKeyedResourceInstanceFromPayloadObject(ResourceContainer container, ResourceType resourceType, PayloadObject payloadObject)
        {
            List<ResourceInstanceProperty> properties = new List<ResourceInstanceProperty>();
            List<ResourceInstanceProperty> keyProperties = new List<ResourceInstanceProperty>();

            foreach (ResourceProperty property in resourceType.Properties.OfType<ResourceProperty>())
            {
                if (property.IsNavigation)
                    continue;

                if (property.IsComplexType)
                {
                    PayloadComplexProperty fromPayload = payloadObject[property.Name] as PayloadComplexProperty;
                    properties.Add(ConvertComplexPayloadObjectToComplexProperty(property, fromPayload));
                }
                else
                {
                    string stringValue;
                    if (payloadObject.PayloadProperties.Any(p => p.Name == property.Name))
                    {
                        PayloadSimpleProperty fromPayload = payloadObject[property.Name] as PayloadSimpleProperty;
                        stringValue = fromPayload.Value;
                    }
                    else
                    {
                        if (!payloadObject.CustomEpmMappedProperties.TryGetValue(property.Name, out stringValue))
                            stringValue = null;
                    }

                    ResourceInstanceProperty newProperty = null;
                    object val = CommonPayload.DeserializeStringToObject(stringValue, property.Type.ClrType, false, payloadObject.Format);
                    newProperty = new ResourceInstanceSimpleProperty(property.Name, new NodeValue(val, property.Type));

                    if (property.PrimaryKey != null)
                        keyProperties.Add(newProperty);
                    else
                        properties.Add(newProperty);
                }
            }

            ResourceInstanceKey key = new ResourceInstanceKey(container, resourceType, keyProperties.ToArray());
            return new KeyedResourceInstance(key, properties.ToArray());

        }
        internal static KeyedResourceInstance CreateKeyedResourceInstanceByExactClone(ResourceContainer container, ResourceType resourceType, object dataObject)
        {
            List<ResourceInstanceProperty> properties = new List<ResourceInstanceProperty>();
            List<ResourceInstanceProperty> keyProperties = new List<ResourceInstanceProperty>();
            properties.AddRange(CloneObjectToResourceInstanceProperties(resourceType.Properties.OfType<ResourceProperty>().Where(rp => rp.PrimaryKey == null && rp.IsNavigation == false), dataObject));
            keyProperties.AddRange(CloneObjectToResourceInstanceProperties(resourceType.Properties.OfType<ResourceProperty>().Where(rp => rp.PrimaryKey != null && rp.IsNavigation == false), dataObject));

            ResourceInstanceKey key = new ResourceInstanceKey(container, resourceType, keyProperties.ToArray());
            KeyedResourceInstance keyResourceInstance = new KeyedResourceInstance(key, properties.ToArray());
            return keyResourceInstance;

        }
        private static ResourceInstanceKey TryCreateUniqueResourceInstanceKey(ResourceContainer container, ResourceType resType, KeyExpressions relatedForeignKeys)
        {
            Workspace workspace = container.Workspace;
            List<ResourceInstanceSimpleProperty> keyProperties = new List<ResourceInstanceSimpleProperty>();

            Dictionary<ResourceProperty, ResourceProperty> foreignKeyMap
                = resType.Key.Properties.OfType<ResourceProperty>()
                .Where(p => p.ForeignKeys.Any())
                .ToDictionary(p => p, p => p.ForeignKeys.First().PrimaryKey.Properties.OfType<ResourceProperty>().First());
            Dictionary<string, ResourceProperty> reverseForeignKeyMap = new Dictionary<string, ResourceProperty>();
            foreach (KeyValuePair<ResourceProperty, ResourceProperty> pair in foreignKeyMap)
                reverseForeignKeyMap[pair.Value.Name] = pair.Key;

            Dictionary<ResourceProperty, object> propertyValues = new Dictionary<ResourceProperty, object>();

            List<ResourceProperty> constrainedProperties = new List<ResourceProperty>();
            foreach(ResourceProperty property in resType.Key.Properties.OfType<ResourceProperty>())
            {
                if(foreignKeyMap.ContainsKey(property))
                    constrainedProperties.Add(property);
                else
                {
                    NodeValue obj = (property.Type as PrimitiveType).CreateRandomValueForFacets(property.Facets);
                    propertyValues[property] = obj.ClrValue;
                }
            }

            foreach (ResourceProperty currentProperty in constrainedProperties)
            {
                if (propertyValues.ContainsKey(currentProperty))
                    continue;

                ResourceProperty foreignProperty = foreignKeyMap[currentProperty];

                ResourceContainer foreignContainer = container.FindDefaultRelatedContainer(foreignProperty.ResourceType);
                KeyExpression foreignKey = relatedForeignKeys.Where(k => k.ResourceContainer == foreignContainer).FirstOrDefault();
                if (foreignKey == null)
                {
                    KeyExpressions foreignKeys = workspace.GetAllExistingKeysOfType(foreignContainer, foreignProperty.ResourceType);
                    while (foreignKey == null && foreignKeys.Any())
                    {
                        foreignKey = foreignKeys.Choose();

                        // ensure that for every property in the key, it matches any local values
                        for (int i = 0; i < foreignKey.Properties.Length; i++)
                        {
                            string keyPropertyName = foreignKey.Properties[i].Name;
                            ResourceProperty localProperty;
                            if (reverseForeignKeyMap.TryGetValue(keyPropertyName, out localProperty))
                            {
                                object keyValue = foreignKey.Values[i].ClrValue;
                                object localValue;
                                if (propertyValues.TryGetValue(localProperty, out localValue))
                                {
                                    if (localValue != keyValue)
                                    {
                                        foreignKeys.Remove(foreignKey);
                                        foreignKey = null;
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    if (foreignKey == null)
                        AstoriaTestLog.FailAndThrow("Could not find an appropriate foreign key");
                    relatedForeignKeys.Add(foreignKey);
                }

                for (int i = 0; i < foreignKey.Properties.Length; i++)
                {
                    NodeProperty p = foreignKey.Properties[i];

                    if (p.Name == foreignProperty.Name)
                        propertyValues[currentProperty] = foreignKey.Values[i].ClrValue;
                    else if (p.ForeignKeys.Count() > 0)
                    {
                        string foreign = p.ForeignKeys.First().PrimaryKey.Properties.OfType<ResourceProperty>().First().Name;

                        ResourceProperty localProperty;
                        if (reverseForeignKeyMap.TryGetValue(foreign, out localProperty))
                            propertyValues[localProperty] = foreignKey.Values[i].ClrValue;
                    }
                }
            }

            foreach(ResourceProperty property in resType.Key.Properties.OfType<ResourceProperty>())
                if(!propertyValues.ContainsKey(property))
                    propertyValues[property] = (object)null;

            foreach (KeyValuePair<ResourceProperty, object> pair in propertyValues)
            {
                keyProperties.Add(new ResourceInstanceSimpleProperty(pair.Key.Facets, pair.Key.Name, pair.Value));
            }

            ResourceInstanceKey resourceInstanceKey = new ResourceInstanceKey(container, resType, keyProperties.ToArray());
            return resourceInstanceKey;
        }