private bool RemoveSSDLTableToComplexProperty(ComplexProperty complexProperty, MappingBase mapping, SSDL.EntityType.EntityType ssdlTable)
        {
            bool deleteAll = true;

            foreach (var prop in mapping.Mapping.Keys.ToList())
            {
                var propMapping = mapping.Mapping[prop];
                if (propMapping.Count == 1)
                {
                    mapping.RemoveMapping(prop);
                }
                else
                {
                    propMapping.Remove(ssdlTable);
                    deleteAll = false;
                }
            }
            foreach (var complexProp in mapping.ComplexMapping.Keys.ToList())
            {
                if (RemoveSSDLTableToComplexProperty(complexProp, mapping.ComplexMapping[complexProp], ssdlTable))
                {
                    mapping.ComplexMapping.Remove(complexProp);
                }
                else
                {
                    deleteAll = false;
                }
            }
            return(deleteAll);
        }
        /// <summary>
        /// Converts a LinqNewExpression to a Complex Instance
        /// </summary>
        /// <param name="expression">Expression to convert to a ComplexInstance</param>
        /// <returns>Complex Instance</returns>
        internal static ComplexInstance ConvertToComplexInstance(this QueryExpression expression)
        {
            var nullConstantExpression = expression as QueryNullExpression;

            var queryComplexType = (QueryComplexType)expression.ExpressionType;

            if (nullConstantExpression != null)
            {
                return(new ComplexInstance(queryComplexType.ComplexType.FullName, true));
            }

            var structuralExpression = expression as LinqNewInstanceExpression;
            var newComplexInstance   = new ComplexInstance(queryComplexType.ComplexType.FullName, false);

            ExceptionUtilities.Assert(structuralExpression.MemberNames.Count == structuralExpression.Members.Count, "MemberNames and Members count are not equal");

            for (int i = 0; i < structuralExpression.MemberNames.Count; i++)
            {
                string memberName       = structuralExpression.MemberNames[i];
                var    memberExpression = structuralExpression.Members[i];
                var    memberProperty   = queryComplexType.ComplexType.Properties.Single(p => p.Name == memberName);

                var complexDataType    = memberProperty.PropertyType as ComplexDataType;
                var collectionDataType = memberProperty.PropertyType as CollectionDataType;

                if (complexDataType != null)
                {
                    var childComplexInstance = memberExpression.ConvertToComplexInstance();
                    var complexProperty      = new ComplexProperty()
                    {
                        Name = memberName, Value = childComplexInstance
                    };
                    newComplexInstance.Add(complexProperty);
                }
                else if (collectionDataType != null)
                {
                    var collectionPropertyType = memberProperty.PropertyType as CollectionDataType;
                    var convertedValue         = memberExpression.ConvertToMultiValue(collectionPropertyType.ElementDataType);
                    if (collectionPropertyType.ElementDataType is ComplexDataType)
                    {
                        newComplexInstance.Add(new ComplexMultiValueProperty(memberName, convertedValue as ComplexMultiValue));
                    }
                    else
                    {
                        var primitiveDataType = collectionPropertyType.ElementDataType as PrimitiveDataType;
                        ExceptionUtilities.CheckObjectNotNull(primitiveDataType, "Not a primitiveDataType '{0}'", collectionPropertyType.ElementDataType);
                        newComplexInstance.Add(new PrimitiveMultiValueProperty(memberName, convertedValue as PrimitiveMultiValue));
                    }
                }
                else
                {
                    var primitiveDataType = memberProperty.PropertyType as PrimitiveDataType;
                    ExceptionUtilities.CheckObjectNotNull(primitiveDataType, "Expected a PrimitiveDataType");
                    var primitiveValue = memberExpression.ConvertToPrimitiveValue(primitiveDataType);
                    newComplexInstance.Add(new PrimitiveProperty(memberName, primitiveDataType.GetEdmTypeName(), primitiveValue.ClrValue));
                }
            }

            return(newComplexInstance);
        }
        private static ModelElement CreateModelElementForEFObjectType(EFObject obj, Partition partition)
        {
            ModelElement modelElement = null;
            var t = obj.GetType();
            if (t == typeof(ConceptualEntityModel))
            {
                modelElement = new EntityDesignerViewModel(partition);
            }
            else if (t == typeof(ConceptualEntityType))
            {
                modelElement = new EntityType(partition);
            }
            else if (t == typeof(ConceptualProperty))
            {
                modelElement = new ScalarProperty(partition);
            }
            else if (t == typeof(ComplexConceptualProperty))
            {
                modelElement = new ComplexProperty(partition);
            }
            else if (t == typeof(Association))
            {
                modelElement = new ViewModel.Association(partition);
            }
            else if (t == typeof(EntityTypeBaseType))
            {
                modelElement = new Inheritance(partition);
            }
            else if (t == typeof(NavigationProperty))
            {
                modelElement = new ViewModel.NavigationProperty(partition);
            }

            return modelElement;
        }
        private bool TryToAddSSDLColumnToComplexProperty(ComplexProperty complexProperty, Func <MappingBase> mapping, SSDL.Property.Property column)
        {
            var prop = complexProperty.ComplexType.ScalarProperties.FirstOrDefault(p => p.Name == column.Name);

            if (prop != null)
            {
                mapping().AddMapping(prop, column);
                return(true);
            }
            foreach (var complexProp in complexProperty.ComplexType.ComplexProperties)
            {
                if (TryToAddSSDLColumnToComplexProperty(complexProp, () =>
                {
                    var complexMapping = mapping().ComplexMapping;
                    if (complexMapping.ContainsKey(complexProp))
                    {
                        return(complexMapping[complexProp]);
                    }
                    var complexPropMapping = new ComplexPropertyMapping(EntityType, complexProp);
                    complexMapping.Add(complexProp, complexPropMapping);
                    return(complexPropMapping);
                }, column))
                {
                    return(true);
                }
            }
            return(false);
        }
Example #5
0
        /// <summary>
        /// Normalizes complex properties, potentially replacing them with collections if the metadata indicates the payload is from a service operation
        /// </summary>
        /// <param name="payloadElement">The payload element to potentially replace</param>
        /// <returns>The original element or a copy to replace it with</returns>
        public override ODataPayloadElement Visit(ComplexProperty payloadElement)
        {
            var replaced = base.Visit(payloadElement);

            if (replaced.ElementType == ODataPayloadElementType.ComplexProperty)
            {
                payloadElement = (ComplexProperty)replaced;

                // if the payload looks like
                //   <Foo>
                //     <element m:type="Edm.Int32">3</element>
                //   </Foo>
                // or
                //   <Foo>
                //     <element m:type="Complex">
                //       <Bar>3</Bar>
                //     </element>
                //   </Foo>
                // then it may be deserialized as a complex instance with exactly 1 property, when it should be a collection of size 1
                //
                if (this.ShouldReplaceWithCollection(payloadElement, payloadElement.Value.IsNull, payloadElement.Value.FullTypeName))
                {
                    // only replace if there is exactly 1 property
                    if (payloadElement.Value.Properties.Count() == 1)
                    {
                        // get the single property and check to see if its name is 'element'
                        var property = payloadElement.Value.Properties.Single();
                        if (property.Name == ODataConstants.CollectionItemElementName)
                        {
                            // determine whether it is a primitive or complex value based on the kind of property
                            ODataPayloadElementCollection collection = null;
                            if (property.ElementType == ODataPayloadElementType.PrimitiveProperty)
                            {
                                var primitiveProperty = (PrimitiveProperty)property;
                                collection = new PrimitiveCollection(primitiveProperty.Value);
                            }
                            else if (property.ElementType == ODataPayloadElementType.ComplexProperty)
                            {
                                var complexProperty = (ComplexProperty)property;
                                collection = new ComplexInstanceCollection(complexProperty.Value);
                            }

                            // if it was primitive or complex, replace it
                            if (collection != null)
                            {
                                return(payloadElement
                                       .ReplaceWith(collection)
                                       .WithAnnotations(new CollectionNameAnnotation()
                                {
                                    Name = payloadElement.Name
                                }));
                            }
                        }
                    }
                }
            }

            return(replaced);
        }
Example #6
0
 public virtual ComplexPropertyMapping GetSpecificMapping(ComplexProperty complexProperty)
 {
     if (ComplexMapping.ContainsKey(complexProperty))
     {
         return(ComplexMapping[complexProperty]);
     }
     return(null);
 }
        private static ComplexProperty GetComplexProperty(IRandomNumberGenerator random, EdmModel model = null, ODataVersion version = ODataVersion.V4)
        {
            var instance = GetComplexInstance(random, model, version);
            var property = new ComplexProperty(instance.FullTypeName, instance);

            property.WithTypeAnnotation(model.FindDeclaredType(instance.FullTypeName));
            return(property);
        }
Example #8
0
        /// <summary>
        /// Visits the payload element
        /// </summary>
        /// <param name="payloadElement">The payload element to visit</param>
        public override void Visit(ComplexProperty payloadElement)
        {
            ExceptionUtilities.CheckArgumentNotNull(payloadElement, "payloadElement");
            var annotation = payloadElement.Annotations.Where(a => a is MemberPropertyAnnotation).SingleOrDefault();

            payloadElement.Annotations.Remove(annotation);

            this.VisitProperty(payloadElement, payloadElement.Value);
        }
Example #9
0
        /// <summary>
        /// Creates a new complex property; does not set a value that will happen in Visit(ComplexInstance)
        /// </summary>
        /// <param name="payloadElement">The primitive collection property to process.</param>
        public override void Visit(ComplexProperty payloadElement)
        {
            var value = new ODataComplexValue()
            {
                TypeName   = payloadElement.Value.FullTypeName,
                Properties = new List <ODataProperty>()
            };

            this.items.Push(value);
            base.Visit(payloadElement);

            var odataProperty = new ODataProperty()
            {
                Name  = payloadElement.Name,
                Value = value
            };

            //remove ComplexValue from items as it will be added to parent or added as property
            this.items.Pop();

            object parent = null;

            if (this.items.Count > 0)
            {
                parent = this.items.Peek();
            }

            if (parent != null)
            {
                var entry = parent as ODataResource;
                if (entry != null)
                {
                    var properties = (List <ODataProperty>)entry.Properties;
                    properties.Add(odataProperty);
                }

                var complexValue = parent as ODataComplexValue;
                if (complexValue != null)
                {
                    var properties = (List <ODataProperty>)complexValue.Properties;
                    properties.Add(odataProperty);
                }

                var collection = parent as ODataCollectionValue;
                if (collection != null)
                {
                    var items = (List <object>)collection.Items;
                    items.Add(odataProperty);
                }
            }
            else
            {
                //since there is no parent add property to items
                this.items.Push(odataProperty);
            }
        }
        internal override ComplexPropertyMapping GetMapping(ComplexProperty complexProperty)
        {
            var value = GetSpecificMapping(complexProperty);

            if (value != null)
            {
                return(value);
            }
            return(EntityType.BaseType == null ? null : EntityType.BaseType.Mapping.GetMapping(complexProperty));
        }
 private void parseComplexProperty(ComplexProperty property)
 {
     foreach (string subElement in _reader.ReadSubElements())
     {
         if (subElement == SubElements.Properties)
         {
             readProperties(property.Properties, property.Type);
         }
     }
 }
        public UIRelatedProperty AddComplexProperty(string propertyName, ComplexType complexType)
        {
            var complexProperty = new ComplexProperty(complexType)
            {
                Name = propertyName
            };

            BusinessInstance.ComplexProperties.Add(complexProperty);
            return(Properties[complexProperty] as UIRelatedProperty);
        }
Example #13
0
        internal ListViewItem GetListViewItem(ComplexProperty complexProperty, out int index)
        {
            var value = (from lvi in VisualTreeHelperUtil.GetControlsDecendant <ListViewItem>(propertiesListView)
                         let uiRelatedProperty = lvi.Content as UIRelatedProperty
                                                 where uiRelatedProperty != null
                                                 select new { ListViewItem = lvi, UIRelatedProperty = uiRelatedProperty }).Select((lvi, i) => new { ListViewItem = lvi, Index = i }).First(lvi => lvi.ListViewItem.UIRelatedProperty.BusinessInstance == complexProperty);

            index = value.Index;
            return(value.ListViewItem.ListViewItem);
        }
        /// <summary>
        /// Normalizes complex properties, potentially replacing them with collections if the metadata indicates the payload is from a service operation
        /// </summary>
        /// <param name="payloadElement">The payload element to potentially replace</param>
        /// <returns>The original element or a copy to replace it with</returns>
        public override ODataPayloadElement Visit(ComplexProperty payloadElement)
        {
            var replaced = base.Visit(payloadElement);
            if (replaced.ElementType == ODataPayloadElementType.ComplexProperty)
            {
                payloadElement = (ComplexProperty)replaced;

                // if the payload looks like
                //   <Foo>
                //     <element m:type="Edm.Int32">3</element>
                //   </Foo>
                // or
                //   <Foo>
                //     <element m:type="Complex">
                //       <Bar>3</Bar>
                //     </element>
                //   </Foo>
                // then it may be deserialized as a complex instance with exactly 1 property, when it should be a collection of size 1
                //
                if (this.ShouldReplaceWithCollection(payloadElement, payloadElement.Value.IsNull, payloadElement.Value.FullTypeName))
                {
                    // only replace if there is exactly 1 property
                    if (payloadElement.Value.Properties.Count() == 1)
                    {
                        // get the single property and check to see if its name is 'element'
                        var property = payloadElement.Value.Properties.Single();
                        if (property.Name == ODataConstants.CollectionItemElementName)
                        {
                            // determine whether it is a primitive or complex value based on the kind of property
                            ODataPayloadElementCollection collection = null;
                            if (property.ElementType == ODataPayloadElementType.PrimitiveProperty)
                            {
                                var primitiveProperty = (PrimitiveProperty)property;
                                collection = new PrimitiveCollection(primitiveProperty.Value);
                            }
                            else if (property.ElementType == ODataPayloadElementType.ComplexProperty)
                            {
                                var complexProperty = (ComplexProperty)property;
                                collection = new ComplexInstanceCollection(complexProperty.Value);
                            }

                            // if it was primitive or complex, replace it
                            if (collection != null)
                            {
                                return payloadElement
                                    .ReplaceWith(collection)
                                    .WithAnnotations(new CollectionNameAnnotation() { Name = payloadElement.Name });
                            }
                        }
                    }
                }
            }

            return replaced;
        }
        private static void GatherScalarsFromComplexProperty(EntityInfo info, ComplexProperty complexProperty)
        {
            foreach (var sp in complexProperty.ScalarProperties())
            {
                info.NonKeyScalars.Add(sp);
            }

            foreach (var cp in complexProperty.ComplexProperties())
            {
                GatherScalarsFromComplexProperty(info, cp);
            }
        }
Example #16
0
        private bool FillComplexProperty(ComplexProperty property, InternalTypeInfo typeInfo, object value)
        {
            if (property == null)
            {
                return(false);
            }

            // Parsing properties
            ParseProperties(property, typeInfo, value);

            return(true);
        }
Example #17
0
        private bool fillComplexProperty(ComplexProperty property, TypeInfo typeInfo, object value)
        {
            if (property == null)
            {
                return(false);
            }

            // Parsing properties
            this.parseProperties(property, typeInfo, value);

            return(true);
        }
        /// <summary>
        /// Visits the payload element
        /// </summary>
        /// <param name="payloadElement">The payload element to visit</param>
        public override void Visit(ComplexProperty payloadElement)
        {
            ExceptionUtilities.CheckArgumentNotNull(payloadElement, "payloadElement");
            ExceptionUtilities.CheckObjectNotNull(this.currentXElement, "Current XElement is not defined");

            // when name is null, it means that it is in the top level. We should use <m:value
            XElement complexXElement = payloadElement.Name == null?CreateMetadataElement(this.currentXElement, AtomValueElement) : CreateDataServicesElement(this.currentXElement, payloadElement.Name);

            this.VisitPayloadElement(payloadElement.Value, complexXElement);

            PostProcessXElement(payloadElement, complexXElement);
        }
            /// <summary>
            /// Visits a payload element whose root is a ComplexProperty.
            /// </summary>
            /// <param name="expected">The root node of payload element being visited.</param>
            public void Visit(ComplexProperty expected)
            {
                ExceptionUtilities.CheckArgumentNotNull(expected, "expected");
                var observed = this.GetNextObservedElement <ComplexProperty>();

                using (this.Assert.WithMessage("Complex property '{0}' did not match expectation", expected.Name))
                {
                    this.Assert.AreEqual(expected.Name, observed.Name, "Property name did not match expectation");
                    this.CompareAnnotations(expected.Annotations, observed.Annotations);
                    this.WrapAccept(expected.Value, observed.Value);
                }
            }
Example #20
0
        private void parseProperties(ComplexProperty property, TypeInfo typeInfo, object value)
        {
            IList <PropertyInfo> propertyInfos = _propertyProvider.GetProperties(typeInfo);

            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                object subValue = propertyInfo.GetValue(value, _emptyObjectArray);

                Property subProperty = CreateProperty(propertyInfo.Name, subValue);

                property.Properties.Add(subProperty);
            }
        }
Example #21
0
        public virtual ComplexPropertyMapping GetSpecificMappingCreateIfNull(ComplexProperty complexProperty)
        {
            var value = GetSpecificMapping(complexProperty);

            if (value != null)
            {
                return(value);
            }
            value = new ComplexPropertyMapping(EntityType, complexProperty);
            ComplexMapping.Add(complexProperty, value);
            EntityType.Mapping.OnPropertyChanged("IsCompletlyMapped");
            return(value);
        }
        private static ComplexProperty CreateComplexPropertyUsingComplexProperty(
            ComplexProperty parentComplexProperty, ComplexConceptualProperty property)
        {
            // make sure that we don't already have one
            var cp = parentComplexProperty.FindComplexProperty(property);

            if (cp == null)
            {
                cp = CreateNewComplexProperty(parentComplexProperty, property);
                parentComplexProperty.AddComplexProperty(cp);
            }
            return(cp);
        }
        private static ScalarProperty CreateScalarPropertyUsingComplexProperty(
            ComplexProperty complexProperty, Property entityProperty, Property tableColumn)
        {
            // make sure that we don't already have one
            var sp = complexProperty.FindScalarProperty(entityProperty, tableColumn);

            if (sp == null)
            {
                sp = CreateNewScalarProperty(complexProperty, entityProperty, tableColumn);
                complexProperty.AddScalarProperty(sp);
            }
            return(sp);
        }
Example #24
0
        /// <summary>
        /// Visits the children of the given payload element and replaces it with a copy if any child changes
        /// </summary>
        /// <param name="payloadElement">The payload element to potentially replace</param>
        /// <returns>The original element or a copy to replace it with</returns>
        public virtual ODataPayloadElement Visit(ComplexProperty payloadElement)
        {
            ExceptionUtilities.CheckArgumentNotNull(payloadElement, "payloadElement");
            var replacedValue = this.Recurse(payloadElement.Value) as ComplexInstance;

            ExceptionUtilities.CheckObjectNotNull(replacedValue, "Replaced value was null or wrong type");
            if (!this.ShouldReplace(payloadElement.Value, replacedValue))
            {
                return(payloadElement);
            }

            return(payloadElement.ReplaceWith(new ComplexProperty(payloadElement.Name, replacedValue)));
        }
 public DynamicProperty<object> Duplicate(object obj)
 {
     DynamicProperty<object> complexProperty = new ComplexProperty<object>();
     SimpleProperty<object> simpleProperty;
     foreach (System.Reflection.PropertyInfo property in obj.GetType().GetProperties())
     {
         object value = property.GetValue(obj, null);
         simpleProperty = new SimpleProperty<object>(value);
         
         complexProperty.AddProperty(property.Name, simpleProperty); 
     }
     return complexProperty;
 }
 private void PreserveComplexPropertyMapping(
     CommandProcessorContext cpc, ComplexProperty complexPropertyMapping, ComplexConceptualProperty createdComplexTypeProperty)
 {
     // walk the Properties tree
     foreach (var sp in complexPropertyMapping.ScalarProperties())
     {
         PreserveScalarPropertyMapping(cpc, sp, createdComplexTypeProperty);
     }
     foreach (var cp in complexPropertyMapping.ComplexProperties())
     {
         PreserveComplexPropertyMapping(cpc, cp, createdComplexTypeProperty);
     }
 }
Example #27
0
        private object CreateObjectFromComplexProperty(ComplexProperty property)
        {
            var obj = Tools.CreateInstance(property.Type);

            if (property.Reference != null)
            {
                _objectCache.Add(property.Reference.Id, obj);
            }

            FillProperties(obj, property.Properties);

            return(obj);
        }
Example #28
0
        private void ParseProperties(ComplexProperty property, InternalTypeInfo typeInfo, object value)
        {
            var propertyInfos = _propertyProvider.GetProperties(typeInfo);

            foreach (var propertyInfo in propertyInfos)
            {
                var subValue = propertyInfo.GetValue(value, _emptyObjectArray);

                var subProperty = CreateProperty(propertyInfo.Name, subValue);

                property.Properties.Add(subProperty);
            }
        }
        /// <summary>
        /// Converts the complex property into a navigation property based on if the base returned an entity instance
        /// </summary>
        /// <param name="payloadElement">The payload element to potentially replace</param>
        /// <returns>The original element or a copy to replace it with</returns>
        public override ODataPayloadElement Visit(ComplexProperty payloadElement)
        {
            ExceptionUtilities.CheckArgumentNotNull(payloadElement, "payloadElement");
            var replaced = (ComplexProperty)base.Visit(payloadElement);

            ExceptionUtilities.CheckObjectNotNull(replaced, "ComplexProperty Expected");
            if (replaced.Value.ElementType == ODataPayloadElementType.EntityInstance)
            {
                var navigation = new NavigationPropertyInstance(replaced.Name, replaced.Value);
                return(replaced.ReplaceWith(navigation));
            }

            return(replaced);
        }
Example #30
0
 public ComplexPropertyMapping this[ComplexProperty complexProperty]
 {
     get
     {
         var value = GetMapping(complexProperty);
         if (value != null)
         {
             return(value);
         }
         value = new ComplexPropertyMapping(EntityType, complexProperty);
         ComplexMapping.Add(complexProperty, value);
         EntityType.Mapping.OnPropertyChanged("IsCompletlyMapped");
         return(value);
     }
 }
Example #31
0
        /// <summary>
        /// Visits the entity instance and removes any complex with no properties as this will not be written.
        /// </summary>
        /// <param name="payloadElement">The payload element to visit</param>
        public override void Visit(EntityInstance payloadElement)
        {
            ExceptionUtilities.CheckArgumentNotNull(payloadElement, "payloadElement");

            foreach (var property in payloadElement.Properties.ToList())
            {
                ComplexProperty complex = property as ComplexProperty;
                if (complex != null && complex.Value.Properties.Count() == 0)
                {
                    payloadElement.Remove(complex);
                }
            }

            base.Visit(payloadElement);
        }
Example #32
0
        /// <summary>
        /// Creates a new complex property; does not set a value that will happen in Visit(ComplexInstance)
        /// </summary>
        /// <param name="payloadElement">The primitive collection property to process.</param>
        public override void Visit(ComplexProperty payloadElement)
        {
            var odataProperty = new ODataProperty()
            {
                Name  = payloadElement.Name,
                Value = new ODataComplexValue()
                {
                    TypeName = payloadElement.Value.FullTypeName
                }
            };

            this.currentProperties.Add(odataProperty);

            base.Visit(payloadElement);
        }
Example #33
0
        /// <summary>
        /// Build QueryValue from action response payload
        /// </summary>
        /// <param name="payload">response payload element</param>
        /// <param name="queryType">query type to build</param>
        /// <returns>query value that represents the payload</returns>
        private QueryValue BuildQueryValueForActionResponse(ODataPayloadElement payload, QueryType queryType)
        {
            EntitySetInstance           entitySetInstance           = payload as EntitySetInstance;
            PrimitiveProperty           primitiveProperty           = payload as PrimitiveProperty;
            ComplexProperty             complexProperty             = payload as ComplexProperty;
            PrimitiveMultiValueProperty primitiveMultiValueProperty = payload as PrimitiveMultiValueProperty;
            ComplexMultiValueProperty   complexMultiValueProperty   = payload as ComplexMultiValueProperty;
            PrimitiveCollection         primitiveCollection         = payload as PrimitiveCollection;
            ComplexInstanceCollection   complexInstanceCollection   = payload as ComplexInstanceCollection;

            if (entitySetInstance != null)
            {
                var xmlBaseAnnotations = payload.Annotations.OfType <XmlBaseAnnotation>();
                var collectionType     = this.currentExpression.ExpressionType as QueryCollectionType;
                ExceptionUtilities.CheckObjectNotNull(collectionType, "Cannot cast expression type to QueryCollectionType.");
                var elementType = collectionType.ElementType as QueryEntityType;
                return(this.BuildFromEntitySetInstance(entitySetInstance, elementType, xmlBaseAnnotations));
            }
            else if (primitiveProperty != null)
            {
                return(this.PayloadElementToQueryValueConverter.Convert(primitiveProperty.Value, queryType));
            }
            else if (complexProperty != null)
            {
                return(this.PayloadElementToQueryValueConverter.Convert(complexProperty.Value, queryType));
            }
            else if (primitiveMultiValueProperty != null)
            {
                return(this.PayloadElementToQueryValueConverter.Convert(primitiveMultiValueProperty.Value, queryType));
            }
            else if (complexMultiValueProperty != null)
            {
                return(this.PayloadElementToQueryValueConverter.Convert(complexMultiValueProperty.Value, queryType));
            }
            else if (primitiveCollection != null)
            {
                return(this.PayloadElementToQueryValueConverter.Convert(primitiveCollection, queryType));
            }
            else if (complexInstanceCollection != null)
            {
                return(this.PayloadElementToQueryValueConverter.Convert(complexInstanceCollection, queryType));
            }
            else
            {
                ExceptionUtilities.CheckArgumentNotNull(payload as EntityInstance, "Unexpected response payload type: " + payload.ElementType + ".");
                return(this.PayloadElementToQueryValueConverter.Convert(payload, queryType));
            }
        }
        /// <summary>
        /// Visits a payload element whose root is a ComplexProperty.
        /// </summary>
        /// <param name="payloadElement">The root node of the payload element being visited.</param>
        public override void Visit(ComplexProperty payloadElement)
        {
            base.Visit(payloadElement);

            if (this.CurrentElementIsRoot())
            {
                Func<MemberProperty, bool> matchesProperty =
                    (p) =>
                    {
                        if (p.Name == payloadElement.Name && p.PropertyType is ComplexDataType)
                        {
                            var complexType = ((ComplexDataType)p.PropertyType).Definition;
                            return complexType.FullName == payloadElement.Value.FullTypeName;
                        }

                        return false;
                    };

                Func<IEdmProperty, bool> EdmMatchesProperty =
                        (p) =>
                        {
                            if (p.Name == payloadElement.Name && p.DeclaringType as IEdmComplexType != null)
                            {
                                var complexType = (IEdmComplexType)p.DeclaringType;
                                return complexType.FullName() == payloadElement.Value.FullTypeName;
                            }

                            return false;
                        };

                var valueTypeAnnotation = payloadElement.Value.Annotations.OfType<EntityModelTypeAnnotation>().SingleOrDefault();
                if (valueTypeAnnotation != null)
                {
                    if (valueTypeAnnotation.EdmModelType != null)
                    {
                        var edmEntityType = valueTypeAnnotation.EdmModelType;
                        this.AddExpectedTypeToProperty(payloadElement, edmEntityType, EdmMatchesProperty);
                    }
                }
                else
                {
                    var edmEntityType = this.ResolvePropertyEdmDataType(payloadElement.Value.FullTypeName);
                    this.AddExpectedTypeToProperty(payloadElement, edmEntityType, EdmMatchesProperty);
                }
            }

            this.AnnotateIfOpenProperty(payloadElement, payloadElement.Value);
        }
        /// <summary>
        ///     Creates a ComplexProperty in the given ComplexProperty.
        /// </summary>
        /// <param name="complexProperty">The parent ComplexProperty to place this ComplexProperty; cannot be null.</param>
        /// <param name="property">This must be a valid ComplexTypeProperty.</param>
        /// <param name="isPartial"></param>
        internal CreateFragmentComplexPropertyCommand(ComplexProperty complexProperty, ComplexConceptualProperty property)
            : base(PrereqId)
        {
            CommandValidation.ValidateComplexProperty(complexProperty);
            CommandValidation.ValidateConceptualProperty(property);

            _parentComplexProperty = complexProperty;
            var mappingFragment = complexProperty.MappingFragment;
            if (mappingFragment != null
                && mappingFragment.EntityTypeMapping != null)
            {
                _conceptualEntityType = mappingFragment.EntityTypeMapping.FirstBoundConceptualEntityType;
            }
            _property = property;
            _mode = Mode.ComplexProperty;
        }
        private static ComplexProperty CreateNewComplexProperty(EFElement parent, ComplexConceptualProperty property)
        {
            // actually create it in the XLinq tree
            var cp = new ComplexProperty(parent, null);
            cp.Name.SetRefName(property);

            XmlModelHelper.NormalizeAndResolve(cp);

            if (cp == null)
            {
                throw new ItemCreationFailureException();
            }

            Debug.Assert(cp.Name.Target != null && cp.Name.Target.LocalName.Value == cp.Name.RefName, "Broken property resolution");

            return cp;
        }
        /// <summary>
        /// Converts the complex property into a navigation property based on if the base returned an entity instance
        /// </summary>
        /// <param name="payloadElement">The payload element to potentially replace</param>
        /// <returns>The original element or a copy to replace it with</returns>
        public override ODataPayloadElement Visit(ComplexProperty payloadElement)
        {
            ExceptionUtilities.CheckArgumentNotNull(payloadElement, "payloadElement");
            var replaced = (ComplexProperty)base.Visit(payloadElement);
            ExceptionUtilities.CheckObjectNotNull(replaced, "ComplexProperty Expected");
            if (replaced.Value.ElementType == ODataPayloadElementType.EntityInstance)
            {
                var navigation = new NavigationPropertyInstance(replaced.Name, replaced.Value);
                return replaced.ReplaceWith(navigation);
            }

            return replaced;
        }
 private void addAttributes(ComplexProperty prop)
 {
     //loop through .NET properties
     foreach (var child in prop.Type.GetProperties())
     {
         //loop through all xml attribute attributes assigned to it
         foreach (var attrib in
             from c in child.GetCustomAttributes(true)
             where c is XMLAttributeAttribute
             select c as XMLAttributeAttribute)
         {
             //try reading
             try
             {
                 SimpleProperty sp = Property.CreateInstance(PropertyArt.Simple, child.Name, child.PropertyType) as SimpleProperty;
                 if (sp != null)
                 {
                     sp.Value = _reader.GetAttributeAsObject(attrib.AttributeName, child.PropertyType);
                 }
                 prop.Properties.Add(sp);
             }
             catch
             {
                 //don't fail, continue
             }
         }
     }
 }
 private static ComplexProperty CreateComplexPropertyUsingComplexProperty(
     ComplexProperty parentComplexProperty, ComplexConceptualProperty property)
 {
     // make sure that we don't already have one
     var cp = parentComplexProperty.FindComplexProperty(property);
     if (cp == null)
     {
         cp = CreateNewComplexProperty(parentComplexProperty, property);
         parentComplexProperty.AddComplexProperty(cp);
     }
     return cp;
 }
Example #40
0
 private void ParseComplexProperty(ComplexProperty property)
 {
     foreach (var subElement in _reader.ReadSubElements()) {
         if (subElement == SubElements.Properties) {
             ReadProperties(property.Properties, property.Type);
         }
     }
 }
Example #41
0
        private void ParseProperties(ComplexProperty property, TypeInfo typeInfo, object value)
        {
            var propertyInfos = _propertyProvider.GetProperties(typeInfo);
            foreach (var propertyInfo in propertyInfos) {
                var subValue = propertyInfo.GetValue(value, _emptyObjectArray);

                var subProperty = CreateProperty(propertyInfo.Name, subValue);

                property.Properties.Add(subProperty);
            }
        }
 /// <summary>
 /// Property changed.
 /// </summary>
 /// <param name="complexProperty">The complex property.</param>
 private void PropertyChanged(ComplexProperty complexProperty)
 {
     this.Changed();
 }
Example #43
0
        private object createObjectFromComplexProperty(ComplexProperty property)
        {
            object obj = Tools.CreateInstance(property.Type);

            if (property.Reference != null)
            {
                // property has Reference, only objects referenced multiple times
                // have properties with references. Object must be cached to
                // resolve its references in the future.
                _objectCache.Add(property.Reference.Id, obj);
            }

            fillProperties(obj, property.Properties);

            return obj;
        }
        private void parseComplexProperty(ComplexProperty property)
        {
            foreach (string subElement in _reader.ReadSubElements())
            {
                if (subElement == SubElements.Properties)
                {
                    readProperties(property.Properties, property.Type);
                }
            }

            //parse XML attributes
            addAttributes(property);
        }
        /// <summary>
        ///     Creates a ScalarProperty in the given ComplexProperty.
        /// </summary>
        /// <param name="complexProperty">The ComplexProperty to place this ScalarProperty; cannot be null.</param>
        /// <param name="property">This must be a valid Property from the C-Model.</param>
        /// <param name="tableColumn">This must be a valid Property from the S-Model.</param>
        internal CreateFragmentScalarPropertyCommand(ComplexProperty complexProperty, Property property, Property tableColumn)
        {
            CommandValidation.ValidateComplexProperty(complexProperty);
            CommandValidation.ValidateConceptualProperty(property);
            CommandValidation.ValidateTableColumn(tableColumn);

            ComplexProperty = complexProperty;
            var mappingFragment = complexProperty.MappingFragment;
            if (mappingFragment != null
                && mappingFragment.EntityTypeMapping != null)
            {
                ConceptualEntityType = mappingFragment.EntityTypeMapping.FirstBoundConceptualEntityType;
            }
            Property = property;
            TableColumn = tableColumn;

            ModeValue = Mode.ComplexProperty;
        }
 private static ScalarProperty CreateScalarPropertyUsingComplexProperty(
     ComplexProperty complexProperty, Property entityProperty, Property tableColumn)
 {
     // make sure that we don't already have one
     var sp = complexProperty.FindScalarProperty(entityProperty, tableColumn);
     if (sp == null)
     {
         sp = CreateNewScalarProperty(complexProperty, entityProperty, tableColumn);
         complexProperty.AddScalarProperty(sp);
     }
     return sp;
 }
        /// <summary>
        /// Creates a new complex property; does not set a value that will happen in Visit(ComplexInstance)
        /// </summary>
        /// <param name="payloadElement">The primitive collection property to process.</param>
        public override void Visit(ComplexProperty payloadElement)
        {
            var odataProperty = new ODataProperty()
            {
                Name = payloadElement.Name,
                Value = new ODataComplexValue()
                {
                    TypeName = payloadElement.Value.FullTypeName
                }
            };
            this.currentProperties.Add(odataProperty);

            base.Visit(payloadElement);
        }
 /// <summary>
 /// Change event handler.
 /// </summary>
 /// <param name="complexProperty">The complex property.</param>
 private void DaysOfTheWeekChanged(ComplexProperty complexProperty)
 {
     this.Changed();
 }
 /// <summary>
 /// A search filter has changed.
 /// </summary>
 /// <param name="complexProperty">The complex property.</param>
 private void SearchFilterChanged(ComplexProperty complexProperty)
 {
     this.Changed();
 }
            /// <summary>
            /// Visits a payload element whose root is a ComplexProperty.
            /// </summary>
            /// <param name="payloadElement">The root node of payload element being visited.</param>
            public void Visit(ComplexProperty payloadElement)
            {
                ExceptionUtilities.CheckArgumentNotNull(payloadElement, "payloadElement");
                bool needsWrapping = this.isRootElement;
                this.isRootElement = false;
                if (needsWrapping)
                {
                    this.writer.StartObjectScope();                    
                }

                if (!string.IsNullOrEmpty(payloadElement.Name))
                {
                    this.writer.WriteName(payloadElement.Name);
                }

                this.Recurse(payloadElement.Value);

                if (needsWrapping)
                {
                    this.writer.EndScope();
                }
            }
 private void parseComplexProperty(ComplexProperty property)
 {
     // There are properties
     readProperties(property.Properties, property.Type);
 }
        /// <summary>
        /// Visits a complex property.  A copy of the current properties to be written is taken so that
        /// the complex property can use the global list to track its properties as its children are visited.
        /// Adds the complex property to the list of properties to be written.
        /// </summary>
        /// <param name="payloadElement">The complex property to visit and add</param>
        public override void Visit(ComplexProperty payloadElement)
        {
            //Copy global property list to local variable and clear it so it can be used
            //for the complex properties children
            var arr = this.odataProperties;
            this.odataProperties = new List<ODataProperty>();

            base.Visit(payloadElement);
            ExceptionUtilities.CheckObjectNotNull(this.odataProperties, "ODataProperties cannot be null");

            //create a new complex property to add to the list from the properties children
            var complexValue = new ODataComplexValue() { TypeName = payloadElement.Value.FullTypeName };
            complexValue.Properties = this.odataProperties.ToList();//.AsEnumerable();

            //Return the global property list to its initial state with the new complex property added
            this.odataProperties.Clear();
            this.odataProperties.AddRange(arr);
            this.odataProperties.Add(CreateProperty(payloadElement.Name, complexValue));
        }
Example #53
0
        private object createObjectFromComplexProperty(ComplexProperty property)
        {
            var obj = Tools.CreateInstance(property.Type);

            fillProperties(obj, property.Properties);

            return obj;
        }
        protected override void ProcessPreReqCommands()
        {
            if (_mode == Mode.ComplexProperty
                && _parentComplexProperty == null)
            {
                var prereq = GetPreReqCommand(PrereqId) as CreateFragmentComplexPropertyCommand;
                if (prereq != null)
                {
                    _parentComplexProperty = prereq.ComplexProperty;
                    CommandValidation.ValidateComplexProperty(_parentComplexProperty);

                    var mappingFragment = _parentComplexProperty.MappingFragment;
                    if (mappingFragment != null
                        && mappingFragment.EntityTypeMapping != null)
                    {
                        _conceptualEntityType = mappingFragment.EntityTypeMapping.FirstBoundConceptualEntityType;
                    }
                }

                Debug.Assert(_parentComplexProperty != null, "We didn't get a good ComplexProperty out of the Command");
            }
        }
Example #55
0
        private bool FillComplexProperty(ComplexProperty property, TypeInfo typeInfo, object value)
        {
            if (property == null) {
                return false;
            }

            // Parsing properties
            ParseProperties(property, typeInfo, value);

            return true;
        }
        protected override void InvokeInternal(CommandProcessorContext cpc)
        {
            Debug.Assert(
                _mode == Mode.EntityType || _mode == Mode.MappingFragment || _mode == Mode.ComplexProperty,
                "Unknown mode set in CreateFragmentComplexPropertyCommand");

            if (_mode == Mode.EntityType)
            {
                // safety check, this should never be hit
                if (_conceptualEntityType == null
                    || _property == null
                    || _tableColumn == null)
                {
                    throw new ArgumentNullException();
                }

                _createdProperty = CreateComplexPropertyUsingEntity(
                    cpc,
                    _conceptualEntityType, _property, _tableColumn);
            }
            else if (_mode == Mode.ComplexProperty)
            {
                // safety check, this should never be hit
                if (_parentComplexProperty == null
                    || _property == null)
                {
                    throw new ArgumentNullException();
                }

                _createdProperty = CreateComplexPropertyUsingComplexProperty(_parentComplexProperty, _property);
            }
            else
            {
                // safety check, this should never be hit
                if (_mappingFragment == null
                    || _property == null)
                {
                    throw new ArgumentNullException();
                }

                _createdProperty = CreateComplexPropertyUsingFragment(_mappingFragment, _property);
            }
        }
 private void PreserveComplexPropertyMapping(
     CommandProcessorContext cpc, ComplexProperty complexPropertyMapping, ComplexConceptualProperty createdComplexTypeProperty)
 {
     // walk the Properties tree
     foreach (var sp in complexPropertyMapping.ScalarProperties())
     {
         PreserveScalarPropertyMapping(cpc, sp, createdComplexTypeProperty);
     }
     foreach (var cp in complexPropertyMapping.ComplexProperties())
     {
         PreserveComplexPropertyMapping(cpc, cp, createdComplexTypeProperty);
     }
 }
 public override void Visit(ComplexProperty payloadElement)
 {
     base.Visit(payloadElement);
     this.ReplaceExpectedTypeAnnotationIfRootElement(payloadElement);
 }
        private static void GatherScalarsFromComplexProperty(EntityInfo info, ComplexProperty complexProperty)
        {
            foreach (var sp in complexProperty.ScalarProperties())
            {
                info.NonKeyScalars.Add(sp);
            }

            foreach (var cp in complexProperty.ComplexProperties())
            {
                GatherScalarsFromComplexProperty(info, cp);
            }
        }
        /// <summary>
        /// Deserializes the element as either a complex, a primitive, or a null property, based on the content
        /// </summary>
        /// <param name="property">The xml to deserialize</param>
        /// <param name="typeNameFallback">TypeName to use instead of the one from the XElement[type] attribute</param>
        /// <returns>A property representing the given xml</returns>
        private PropertyInstance DeserializeProperty(XElement property, string typeNameFallback)
        {
            string propertyName = property.Name.LocalName;

            // get the type name
            string typeNameFromPayload = null;
            XAttribute typeAttribute = property.Attribute(MetadataType);
            if (typeAttribute != null)
            {
                typeNameFromPayload = typeAttribute.Value;
            }

            // set type to be fallback when typeattribute does not exist
            var typeNameForClrTypeLookup = typeNameFromPayload;
            if (typeNameForClrTypeLookup == null && !string.IsNullOrEmpty(typeNameFallback))
            {
                typeNameForClrTypeLookup = typeNameFallback;
            }

            // try to infer the clr type
            Type clrType = null;
            if (!string.IsNullOrEmpty(typeNameForClrTypeLookup))
            {
                ExceptionUtilities.CheckObjectNotNull(this.PrimitiveDataTypeConverter, "Cannot infer clr type from edm type without converter");
                clrType = this.PrimitiveDataTypeConverter.ToClrType(typeNameForClrTypeLookup);
            }

            PropertyInstance result;
            if (property.HasElements)
            {
                // must be complex, a multivalue, or spatial
                ExceptionUtilities.CheckObjectNotNull(this.SpatialFormatter, "Cannot safely deserialize element with children without spatial formatter.");

                // try to infer which spatial type hierarchy it is from the type name in the payload
                SpatialTypeKind? kind = null;
                if (clrType != null)
                {
                    SpatialUtilities.TryInferSpatialTypeKind(clrType, out kind);
                }

                object spatialInstance;
                if (this.SpatialFormatter.TryParse(property.Elements().First(), kind, out spatialInstance))
                {
                    ExceptionUtilities.Assert(property.Elements().Count() == 1, "Spatial property had more than 1 sub-element");
                    result = new PrimitiveProperty(propertyName, typeNameFromPayload, spatialInstance);
                }
                else if (property.Elements().All(e => e.Name == DataServicesElement))
                {
                    result = this.DeserializeCollectionProperty(property);
                }
                else
                {
                    result = new ComplexProperty(propertyName, this.DeserializeComplexInstance(property));
                }
            }
            else
            {
                // check for the null attribute
                bool isNull = false;
                XAttribute isNullAttribute = property.Attribute(MetadataNull);
                if (isNullAttribute != null)
                {
                    isNull = bool.Parse(isNullAttribute.Value);
                }

                // If its null and we can't tell whether it is primitive or complex, then return a null marker
                if (isNull && clrType == null)
                {
                    result = new NullPropertyInstance(propertyName, typeNameFromPayload);
                }
                else if (typeNameFromPayload != null && typeNameFromPayload.StartsWith(ODataConstants.BeginMultiValueTypeIdentifier, StringComparison.Ordinal))
                {
                    ExceptionUtilities.CheckObjectNotNull(this.PrimitiveDataTypeConverter, "Cannot infer clr type from edm type without converter");

                    string elementTypeName = ParseBagElementTypeName(typeNameFromPayload);
                    if (this.PrimitiveDataTypeConverter.ToClrType(elementTypeName) != null)
                    {
                        result = new PrimitiveMultiValueProperty(propertyName, new PrimitiveMultiValue(typeNameFromPayload, isNull));
                    }
                    else
                    {
                        result = new ComplexMultiValueProperty(propertyName, new ComplexMultiValue(typeNameFromPayload, isNull));
                    }
                }
                else
                {
                    object value;
                    if (isNull)
                    {
                        value = null;
                    }
                    else if (clrType != null)
                    {
                        ExceptionUtilities.CheckObjectNotNull(this.PrimitiveConverter, "PrimitiveConverter has not been set.");
                        value = this.PrimitiveConverter.DeserializePrimitive(property.Value, clrType);
                    }
                    else
                    {
                        value = property.Value;
                    }

                    result = new PrimitiveProperty(propertyName, typeNameFromPayload, value);
                }
            }

            AddXmlBaseAnnotation(result, property);

            return result;
        }