/// <inheritdoc />
        public override object Read(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext)
        {
            if (messageReader == null)
            {
                throw Error.ArgumentNull("messageReader");
            }

            IEdmTypeReference edmType = readContext.GetEdmType(type);
            Contract.Assert(edmType != null);

            if (!edmType.IsCollection())
            {
                throw Error.Argument("type", SRResources.ArgumentMustBeOfType, EdmTypeKind.Collection);
            }

            IEdmCollectionTypeReference collectionType = edmType.AsCollection();
            IEdmTypeReference elementType = collectionType.ElementType();

            IEnumerable result = ReadInline(ReadCollection(messageReader, elementType), edmType, readContext) as IEnumerable;
            if (result != null && readContext.IsUntyped && elementType.IsComplex())
            {
                EdmComplexObjectCollection complexCollection = new EdmComplexObjectCollection(collectionType);
                foreach (EdmComplexObject complexObject in result)
                {
                    complexCollection.Add(complexObject);
                }
                return complexCollection;
            }

            return result;
        }
Example #2
0
        /// <summary>
        /// Gets a List for all entities of a group.
        /// </summary>
        /// <param name="group"></param>
        /// <param name="edmComplexType"></param>
        /// <returns></returns>
        protected SubCollectionContext GetList(IEnumerable <Dictionary <string, object> > group, EdmComplexType edmComplexType, int limit = 0)
        {
            var queryable = new List <Dictionary <string, object> >();
            var collectionTypeReference = new EdmCollectionTypeReference(new EdmCollectionType(new EdmComplexTypeReference(edmComplexType, true)));
            var subCollection           = new EdmComplexObjectCollection(collectionTypeReference);
            int count = 0;

            foreach (var entity in group)
            {
                var dict             = new Dictionary <string, object>();
                var edmComplexObject = new EdmComplexObject(edmComplexType);
                foreach (var propertyKvp in entity)
                {
                    edmComplexObject.TrySetPropertyValue(propertyKvp.Key, propertyKvp.Value);
                    dict.Add(propertyKvp.Key, propertyKvp.Value);
                }
                subCollection.Add(edmComplexObject);
                queryable.Add(dict);
                if (limit >= 1 && ++count == limit)
                {
                    break;
                }
            }
            return(new SubCollectionContext {
                Result = subCollection, QueryAbleResult = queryable
            });
        }
        /// <inheritdoc />
        public override object Read(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext)
        {
            if (messageReader == null)
            {
                throw Error.ArgumentNull("messageReader");
            }

            IEdmTypeReference edmType = readContext.GetEdmType(type);

            Contract.Assert(edmType != null);

            if (!edmType.IsCollection())
            {
                throw Error.Argument("type", SRResources.ArgumentMustBeOfType, EdmTypeKind.Collection);
            }

            IEdmCollectionTypeReference collectionType = edmType.AsCollection();
            IEdmTypeReference           elementType    = collectionType.ElementType();

            IEnumerable result = ReadInline(ReadCollection(messageReader, elementType), edmType, readContext) as IEnumerable;

            if (result != null && readContext.IsUntyped && elementType.IsComplex())
            {
                EdmComplexObjectCollection complexCollection = new EdmComplexObjectCollection(collectionType);
                foreach (EdmComplexObject complexObject in result)
                {
                    complexCollection.Add(complexObject);
                }
                return(complexCollection);
            }

            return(result);
        }
Example #4
0
        /// <inheritdoc />
        public sealed override object ReadInline(object item, IEdmTypeReference edmType, ODataDeserializerContext readContext)
        {
            if (item == null)
            {
                return(null);
            }
            if (edmType == null)
            {
                throw Error.ArgumentNull("edmType");
            }

            if (!edmType.IsCollection())
            {
                throw new SerializationException(
                          Error.Format(SRResources.TypeCannotBeDeserialized, edmType.ToTraceString(), typeof(ODataMediaTypeFormatter)));
            }

            IEdmCollectionTypeReference collectionType = edmType.AsCollection();
            IEdmTypeReference           elementType    = collectionType.ElementType();

            ODataCollectionValue collection = item as ODataCollectionValue;

            if (collection == null)
            {
                throw Error.Argument("item", SRResources.ArgumentMustBeOfType, typeof(ODataCollectionValue).Name);
            }
            // Recursion guard to avoid stack overflows
            RuntimeHelpers.EnsureSufficientExecutionStack();

            IEnumerable result = ReadCollectionValue(collection, elementType, readContext);

            if (result != null)
            {
                if (readContext.IsUntyped && elementType.IsComplex())
                {
                    EdmComplexObjectCollection complexCollection = new EdmComplexObjectCollection(collectionType);
                    foreach (EdmComplexObject complexObject in result)
                    {
                        complexCollection.Add(complexObject);
                    }
                    return(complexCollection);
                }
                else if (readContext.IsUntyped && elementType.IsEnum())
                {
                    EdmEnumObjectCollection enumCollection = new EdmEnumObjectCollection(collectionType);
                    foreach (EdmEnumObject enumObject in result)
                    {
                        enumCollection.Add(enumObject);
                    }
                    return(enumCollection);
                }
                else
                {
                    Type        elementClrType = EdmLibHelpers.GetClrType(elementType, readContext.Model);
                    IEnumerable castedResult   = _castMethodInfo.MakeGenericMethod(elementClrType).Invoke(null, new object[] { result }) as IEnumerable;
                    return(castedResult);
                }
            }
            return(null);
        }
        /// <inheritdoc />
        public sealed override object ReadInline(object item, IEdmTypeReference edmType, ODataDeserializerContext readContext)
        {
            if (item == null)
            {
                return(null);
            }

            if (edmType == null)
            {
                throw Error.ArgumentNull("edmType");
            }

            if (!edmType.IsCollection() || !edmType.AsCollection().ElementType().IsStructured())
            {
                throw Error.Argument("edmType", SRResources.TypeMustBeResourceSet, edmType.ToTraceString());
            }

            ODataResourceSetWrapper resourceSet = item as ODataResourceSetWrapper;

            if (resourceSet == null)
            {
                throw Error.Argument("item", SRResources.ArgumentMustBeOfType, typeof(ODataResourceSetWrapper).Name);
            }

            // Recursion guard to avoid stack overflows
            RuntimeHelpers.EnsureSufficientExecutionStack();

            IEdmStructuredTypeReference elementType = edmType.AsCollection().ElementType().AsStructured();

            IEnumerable result = ReadResourceSet(resourceSet, elementType, readContext);

            if (result != null && elementType.IsComplex())
            {
                if (readContext.IsUntyped)
                {
                    EdmComplexObjectCollection complexCollection = new EdmComplexObjectCollection(edmType.AsCollection());
                    foreach (EdmComplexObject complexObject in result)
                    {
                        complexCollection.Add(complexObject);
                    }
                    return(complexCollection);
                }
                else
                {
                    Type        elementClrType = EdmLibHelpers.GetClrType(elementType, readContext.Model);
                    IEnumerable castedResult   =
                        CastMethodInfo.MakeGenericMethod(elementClrType).Invoke(null, new object[] { result }) as
                        IEnumerable;
                    return(castedResult);
                }
            }
            else
            {
                return(result);
            }
        }
Example #6
0
        public static IEdmObject ConvertToEdmObject(this IEnumerable enumerable, IEdmCollectionTypeReference collectionType)
        {
            Contract.Assert(enumerable != null);
            Contract.Assert(collectionType != null);

            IEdmTypeReference elementType = collectionType.ElementType();

            if (elementType.IsEntity())
            {
                EdmEntityObjectCollection entityCollection =
                    new EdmEntityObjectCollection(collectionType);

                foreach (EdmEntityObject entityObject in enumerable)
                {
                    entityCollection.Add(entityObject);
                }

                return(entityCollection);
            }
            else if (elementType.IsComplex())
            {
                EdmComplexObjectCollection complexCollection =
                    new EdmComplexObjectCollection(collectionType);

                foreach (EdmComplexObject complexObject in enumerable)
                {
                    complexCollection.Add(complexObject);
                }

                return(complexCollection);
            }
            else if (elementType.IsEnum())
            {
                EdmEnumObjectCollection enumCollection =
                    new EdmEnumObjectCollection(collectionType);

                foreach (EdmEnumObject enumObject in enumerable)
                {
                    enumCollection.Add(enumObject);
                }

                return(enumCollection);
            }

            return(null);
        }
        /// <inheritdoc />
        public override object Read(ODataMessageReader messageReader, ODataDeserializerContext readContext)
        {
            if (messageReader == null)
            {
                throw Error.ArgumentNull("messageReader");
            }

            IEnumerable result = ReadInline(ReadCollection(messageReader), readContext) as IEnumerable;
            if (result != null && readContext.IsUntyped && ElementType.IsComplex())
            {
                EdmComplexObjectCollection complexCollection = new EdmComplexObjectCollection(CollectionType);
                foreach (EdmComplexObject complexObject in result)
                {
                    complexCollection.Add(complexObject);
                }
                return complexCollection;
            }

            return result;
        }
Example #8
0
        /// <inheritdoc />
        public override object Read(ODataMessageReader messageReader, ODataDeserializerContext readContext)
        {
            if (messageReader == null)
            {
                throw Error.ArgumentNull("messageReader");
            }

            IEnumerable result = ReadInline(ReadCollection(messageReader), readContext) as IEnumerable;

            if (result != null && readContext.IsUntyped && ElementType.IsComplex())
            {
                EdmComplexObjectCollection complexCollection = new EdmComplexObjectCollection(CollectionType);
                foreach (EdmComplexObject complexObject in result)
                {
                    complexCollection.Add(complexObject);
                }
                return(complexCollection);
            }

            return(result);
        }
Example #9
0
        public IHttpActionResult Get()
        {
            IEdmEntityType             customerType            = Request.GetModel().FindDeclaredType("WebStack.QA.Test.OData.TestCustomer") as IEdmEntityType;
            IEdmEntityType             customerWithAddressType = Request.GetModel().FindDeclaredType("WebStack.QA.Test.OData.TestCustomerWithAddress") as IEdmEntityType;
            IEdmComplexType            addressType             = Request.GetModel().FindDeclaredType("WebStack.QA.Test.OData.TestAddress") as IEdmComplexType;
            IEdmEntityType             orderType      = Request.GetModel().FindDeclaredType("WebStack.QA.Test.OData.TestOrder") as IEdmEntityType;
            IEdmEntitySet              ordersSet      = Request.GetModel().FindDeclaredEntitySet("TestOrders") as IEdmEntitySet;
            EdmChangedObjectCollection changedObjects = new EdmChangedObjectCollection(customerType);

            EdmDeltaComplexObject a = new EdmDeltaComplexObject(addressType);

            a.TrySetPropertyValue("State", "State");
            a.TrySetPropertyValue("ZipCode", null);

            EdmDeltaEntityObject changedEntity = new EdmDeltaEntityObject(customerWithAddressType);

            changedEntity.TrySetPropertyValue("Id", 1);
            changedEntity.TrySetPropertyValue("Name", "Name");
            changedEntity.TrySetPropertyValue("Address", a);
            changedEntity.TrySetPropertyValue("PhoneNumbers", new List <String> {
                "123-4567", "765-4321"
            });
            changedEntity.TrySetPropertyValue("OpenProperty", 10);
            changedEntity.TrySetPropertyValue("NullOpenProperty", null);
            changedObjects.Add(changedEntity);

            EdmComplexObjectCollection places = new EdmComplexObjectCollection(new EdmCollectionTypeReference(new EdmCollectionType(new EdmComplexTypeReference(addressType, true))));
            EdmDeltaComplexObject      b      = new EdmDeltaComplexObject(addressType);

            b.TrySetPropertyValue("City", "City2");
            b.TrySetPropertyValue("State", "State2");
            b.TrySetPropertyValue("ZipCode", 12345);
            b.TrySetPropertyValue("OpenProperty", 10);
            b.TrySetPropertyValue("NullOpenProperty", null);
            places.Add(a);
            places.Add(b);

            var newCustomer = new EdmDeltaEntityObject(customerType);

            newCustomer.TrySetPropertyValue("Id", 10);
            newCustomer.TrySetPropertyValue("Name", "NewCustomer");
            newCustomer.TrySetPropertyValue("FavoritePlaces", places);
            changedObjects.Add(newCustomer);

            var newOrder = new EdmDeltaEntityObject(orderType);

            newOrder.NavigationSource = ordersSet;
            newOrder.TrySetPropertyValue("Id", 27);
            newOrder.TrySetPropertyValue("Amount", 100);
            changedObjects.Add(newOrder);

            var deletedCustomer = new EdmDeltaDeletedEntityObject(customerType);

            deletedCustomer.Id     = "7";
            deletedCustomer.Reason = DeltaDeletedEntryReason.Changed;
            changedObjects.Add(deletedCustomer);

            var deletedOrder = new EdmDeltaDeletedEntityObject(orderType);

            deletedOrder.NavigationSource = ordersSet;
            deletedOrder.Id     = "12";
            deletedOrder.Reason = DeltaDeletedEntryReason.Deleted;
            changedObjects.Add(deletedOrder);

            var deletedLink = new EdmDeltaDeletedLink(customerType);

            deletedLink.Source       = new Uri("http://localhost/odata/DeltaCustomers(1)");
            deletedLink.Target       = new Uri("http://localhost/odata/DeltaOrders(12)");
            deletedLink.Relationship = "Orders";
            changedObjects.Add(deletedLink);

            var addedLink = new EdmDeltaLink(customerType);

            addedLink.Source       = new Uri("http://localhost/odata/DeltaCustomers(10)");
            addedLink.Target       = new Uri("http://localhost/odata/DeltaOrders(27)");
            addedLink.Relationship = "Orders";
            changedObjects.Add(addedLink);

            return(Ok(changedObjects));
        }
Example #10
0
 IEdmObject InvokeTVF(IEdmFunction func, JObject parameterValues, ODataQueryOptions queryOptions = null)
 {
     IEdmType edmType = func.ReturnType.Definition;
     IEdmType elementType = (edmType as IEdmCollectionType).ElementType.Definition;
     EdmComplexObjectCollection collection = new EdmComplexObjectCollection(new EdmCollectionTypeReference(edmType as IEdmCollectionType));
     var target = BuildTVFTarget(func, parameterValues);
     var cmd = BuildSqlQueryCmd(queryOptions, target);
     using (DbAccess db = new DbAccess(this.ConnectionString))
     {
         db.ExecuteReader(cmd, (reader) =>
         {
             EdmComplexObject entity = new EdmComplexObject(elementType as IEdmComplexType);
             for (int i = 0; i < reader.FieldCount; i++)
             {
                 reader.SetEntityPropertyValue(i, entity);
             }
             collection.Add(entity);
         }, null, CommandType.Text);
     }
     return collection;
 }
Example #11
0
 IEdmObject InvokeFuncCollection(IEdmFunction func, JObject parameterValues, ODataQueryOptions queryOptions = null)
 {
     IEdmType edmType = func.ReturnType.Definition;
     IEdmType elementType = (edmType as IEdmCollectionType).ElementType.Definition;
     EdmComplexObjectCollection collection = new EdmComplexObjectCollection(new EdmCollectionTypeReference(edmType as IEdmCollectionType));
     using (DbAccess db = new DbAccess(this.ConnectionString))
     {
         db.ExecuteReader(func.Name, (reader) =>
         {
             EdmComplexObject entity = new EdmComplexObject(elementType as IEdmComplexType);
             for (int i = 0; i < reader.FieldCount; i++)
             {
                 reader.SetEntityPropertyValue(i, entity);
             }
             collection.Add(entity);
         }, (pars) =>
         {
             SetParameter(func, parameterValues, edmType, pars);
         });
     }
     return collection;
 }
        /// <inheritdoc />
        public sealed override object ReadInline(object item, IEdmTypeReference edmType, ODataDeserializerContext readContext)
        {
            if (item == null)
            {
                return null;
            }
            if (edmType == null)
            {
                throw Error.ArgumentNull("edmType");
            }

            if (!edmType.IsCollection())
            {
                throw new SerializationException(
                    Error.Format(SRResources.TypeCannotBeDeserialized, edmType.ToTraceString(), typeof(ODataMediaTypeFormatter)));
            }

            IEdmCollectionTypeReference collectionType = edmType.AsCollection();
            IEdmTypeReference elementType = collectionType.ElementType();

            ODataCollectionValue collection = item as ODataCollectionValue;

            if (collection == null)
            {
                throw Error.Argument("item", SRResources.ArgumentMustBeOfType, typeof(ODataCollectionValue).Name);
            }
            // Recursion guard to avoid stack overflows
            RuntimeHelpers.EnsureSufficientExecutionStack();

            IEnumerable result = ReadCollectionValue(collection, elementType, readContext);
            if (result != null)
            {
                if (readContext.IsUntyped && elementType.IsComplex())
                {
                    EdmComplexObjectCollection complexCollection = new EdmComplexObjectCollection(collectionType);
                    foreach (EdmComplexObject complexObject in result)
                    {
                        complexCollection.Add(complexObject);
                    }
                    return complexCollection;
                }
                else if (readContext.IsUntyped && elementType.IsEnum())
                {
                    EdmEnumObjectCollection enumCollection = new EdmEnumObjectCollection(collectionType);
                    foreach (EdmEnumObject enumObject in result)
                    {
                        enumCollection.Add(enumObject);
                    }
                    return enumCollection;
                } 
                else
                {
                    Type elementClrType = EdmLibHelpers.GetClrType(elementType, readContext.Model);
                    IEnumerable castedResult = _castMethodInfo.MakeGenericMethod(elementClrType).Invoke(null, new object[] { result }) as IEnumerable;
                    return castedResult;
                }
            }
            return null;
        }