private static List <ResourceInstanceProperty> CloneObjectToResourceInstanceProperties(IEnumerable <ResourceProperty> properties, object o)
        {
            List <ResourceInstanceProperty> instanceProperties = new List <ResourceInstanceProperty>();

            foreach (ResourceProperty resProperty in properties)
            {
                if (resProperty.IsNavigation)
                {
                    continue;
                }
                object propertyObject = o.GetType().InvokeMember(resProperty.Name, System.Reflection.BindingFlags.GetProperty, null, o, new object[] { });
                ResourceInstanceProperty resourceInstanceProperty = null;
                if (resProperty.Type is ComplexType)
                {
                    ComplexResourceInstance complexInstance = CloneObjectToComplexResourceInstance(resProperty.Type as ComplexType, propertyObject);
                    resourceInstanceProperty = new ResourceInstanceComplexProperty(resProperty.Type.Name, resProperty.Name, complexInstance);
                }
                else
                {
                    resourceInstanceProperty = new ResourceInstanceSimpleProperty(resProperty.Name, new NodeValue(propertyObject, resProperty.Type));
                }
                instanceProperties.Add(resourceInstanceProperty);
            }
            return(instanceProperties);
        }
        private static ResourceInstanceComplexProperty ConvertComplexPayloadObjectToComplexProperty(ResourceProperty rp, PayloadComplexProperty payloadComplexProperty)
        {
            List <ResourceInstanceProperty> properties = new List <ResourceInstanceProperty>();

            foreach (ResourceProperty childProperty in (rp.Type as ComplexType).Properties.OfType <ResourceProperty>())
            {
                if (childProperty.IsComplexType)
                {
                    PayloadComplexProperty fromPayload = payloadComplexProperty.PayloadProperties[childProperty.Name] as PayloadComplexProperty;
                    properties.Add(ConvertComplexPayloadObjectToComplexProperty(childProperty, fromPayload));
                }
                else
                {
                    PayloadSimpleProperty fromPayload = payloadComplexProperty.PayloadProperties[childProperty.Name] as PayloadSimpleProperty;
                    if (fromPayload != null)
                    {
                        ResourceInstanceProperty newProperty = null;
                        object val = CommonPayload.DeserializeStringToObject(fromPayload.Value, childProperty.Type.ClrType, false, fromPayload.ParentObject.Format);
                        newProperty = new ResourceInstanceSimpleProperty(childProperty.Name, new NodeValue(val, childProperty.Type));
                        properties.Add(newProperty);
                    }
                }
            }
            ComplexResourceInstance complexResourceInstance = new ComplexResourceInstance(rp.Type.Name, properties.ToArray());

            return(new ResourceInstanceComplexProperty(rp.Type.Name, rp.Name, complexResourceInstance));
        }
Beispiel #3
0
        public KeyedResourceInstance CreateRandomResource(ResourceContainer container, KeyExpression specifiedKey)
        {
            List <ResourceInstanceProperty> properties = new List <ResourceInstanceProperty>();

            foreach (ResourceProperty p in this.Properties.OfType <ResourceProperty>().Where(rp => rp.IsNavigation == false && rp.PrimaryKey == null))
            {
                ResourceInstanceProperty property = p.CreateRandomResourceInstanceProperty();
                properties.Add(property);
            }

            return(ResourceInstanceUtil.CreateKeyedResourceInstance(specifiedKey, container, properties.ToArray()));
        }
        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()));
        }
        private static List <ResourceInstanceProperty> CloneRequiredRelationships(ResourceContainer container, ResourceType resourceType, KeyExpression keyExpression)
        {
            List <ResourceInstanceProperty> properties = new List <ResourceInstanceProperty>();
            //Foreach Navigation Property in dataObject create a bind
            Dictionary <ResourceProperty, KeyExpressions> navigationProperties = GetAllAssociatedKeys(container, resourceType, keyExpression);

            foreach (ResourceProperty navProperty in navigationProperties.Keys)
            {
                //ResourceAssociationEnd otherEnd = navProperty.ResourceAssociation.GetOtherEnd(navProperty);
                ResourceType      navPropertyResourceType = (navProperty.Type is CollectionType ? (navProperty.Type as CollectionType).SubType : navProperty.Type) as ResourceType;
                ResourceContainer otherContainer          = container.FindDefaultRelatedContainer(navProperty);

                bool foreignKeyViolation = false;
                foreach (ResourceProperty otherProperty in otherContainer.BaseType.Key.Properties)
                {
                    if (otherProperty.ForeignKeys.Count() > 0)
                    {
                        ResourceType otherType = otherProperty.ForeignKeys.First().PrimaryKey.Properties.OfType <ResourceProperty>().First().ResourceType;
                        if (otherType == container.BaseType)
                        {
                            foreignKeyViolation = true;
                            break;
                        }
                    }
                }
                if (foreignKeyViolation)
                {
                    continue;
                }

                KeyExpressions keyExpressions = navigationProperties[navProperty];
                if (navProperty.Type is ResourceType)
                {
                    if (keyExpressions.Count > 0)
                    {
                        properties.Add(CreateRefInstanceProperty(keyExpressions[0], otherContainer, navProperty));
                    }
                }
                else
                {
                    ResourceInstanceProperty property = CreateCollectionInstanceProperty(keyExpressions, otherContainer, navProperty);
                    if (property != null)
                    {
                        properties.Add(property);
                    }
                }
            }
            return(properties);
        }
        public static ComplexResourceInstance CreateComplexResourceInstance(ComplexType type)
        {
            List <ResourceInstanceProperty> instanceProperties = new List <ResourceInstanceProperty>();

            foreach (ResourceProperty childProperty in type.Properties)
            {
                ResourceInstanceProperty resourceInstanceProperty = null;
                if (childProperty.IsComplexType)
                {
                    resourceInstanceProperty = new ResourceInstanceComplexProperty(childProperty.Type.Name, childProperty.Name, CreateComplexResourceInstance((ComplexType)childProperty.Type));
                }
                else
                {
                    NodeValue nodeValue = Resource.CreateValue(childProperty);
                    resourceInstanceProperty = new ResourceInstanceSimpleProperty(childProperty.Name, nodeValue);
                }
                instanceProperties.Add(resourceInstanceProperty);
            }
            ComplexResourceInstance complexInstance = new ComplexResourceInstance(type.Name, instanceProperties.ToArray());

            return(complexInstance);
        }
Beispiel #7
0
        internal KeyedResourceInstance CreateRandomResource(ResourceContainer container, KeyExpressions existingKeys, bool populateNavProps)
        {
            KeyExpressions                  relatedForeignKeys = new KeyExpressions();
            ResourceInstanceKey             key        = ResourceInstanceUtil.CreateUniqueKey(container, this, relatedForeignKeys, existingKeys);
            List <ResourceInstanceProperty> properties = new List <ResourceInstanceProperty>();

            // populate the non-key, non-navigation properties
            //
            foreach (ResourceProperty p in this.Properties.OfType <ResourceProperty>().Where(rp => rp.IsNavigation == false && rp.PrimaryKey == null))
            {
                if (p.Facets.IsIdentity)
                {
                    continue;
                }

                ResourceInstanceProperty property = p.CreateRandomResourceInstanceProperty();
                properties.Add(property);
            }

            if (populateNavProps)
            {
                // populate the navigation properties, but don't go by key, as some foreign keys MAY NOT HAVE ASSOCIATED NAVIGATION PROPERTIES
                //
                foreach (ResourceProperty p in this.Properties.OfType <ResourceProperty>().Where(rp => rp.IsNavigation))
                {
                    // find a key for this navigation property
                    KeyExpression navPropKey = null;
                    foreach (KeyExpression keyExp in relatedForeignKeys)
                    {
                        //if (p.Type.Equals(keyExp.ResourceType)
                        //    || p.Type is ResourceType && (p.Type as ResourceType).BaseTypes.Contains(keyExp.ResourceType))
                        if (p.OtherAssociationEnd.ResourceType.Equals(keyExp.ResourceType))
                        {
                            navPropKey = keyExp;
                            break;
                        }
                    }

                    ResourceContainer associatedContainer = container.FindDefaultRelatedContainer(p);
                    if (navPropKey == null)
                    {
                        if (p.OtherAssociationEnd.ResourceType.Key.Properties.OfType <ResourceProperty>()
                            .Where(rp => rp.ForeignKeys.Any())
                            .Any(rp => rp.ForeignKeys.First().PrimaryKey.Properties.OfType <ResourceProperty>().First().ResourceType.Equals(this)))
                        {
                            // this association has a fk back to the current type, so it cannot be based on any existing entity
                            AstoriaTestLog.WriteLineIgnore("Skipping nav prop '{0}.{1}' due to foreign key constraint on entity being created", this.Name, p.Name);
                            continue;
                        }
                        else
                        {
                            navPropKey = associatedContainer.Workspace.GetRandomExistingKey(associatedContainer, p.OtherAssociationEnd.ResourceType);
                        }
                    }

                    if (navPropKey != null)
                    {
                        if (p.OtherAssociationEnd.Multiplicity == Multiplicity.Many)
                        {
                            properties.Add(ResourceInstanceUtil.CreateCollectionInstanceProperty(new KeyExpressions(navPropKey), navPropKey.ResourceContainer, p));
                        }
                        else
                        {
                            properties.Add(ResourceInstanceUtil.CreateRefInstanceProperty(navPropKey, navPropKey.ResourceContainer, p));
                        }
                    }
                }
            }
            return(ResourceInstanceUtil.CreateKeyedResourceInstance(key.CreateKeyExpression(container, this), container, properties.ToArray()));
        }