예제 #1
0
        private static EdmEntityObjectCollection GetCustomers()
        {
            if (_untypedSimpleOpenCustormers != null)
            {
                return(_untypedSimpleOpenCustormers);
            }

            IEdmModel       edmModel     = OpenEntityTypeTests.GetUntypedEdmModel();
            IEdmEntityType  customerType = edmModel.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "UntypedSimpleOpenCustomer");
            EdmEntityObject customer     = new EdmEntityObject(customerType);

            customer.TrySetPropertyValue("CustomerId", 1);

            //Add Numbers primitive collection property
            customer.TrySetPropertyValue("DeclaredNumbers", new[] { 1, 2 });

            //Add Color, Colors enum(collection) property
            IEdmEnumType  colorType = edmModel.SchemaElements.OfType <IEdmEnumType>().First(c => c.Name == "Color");
            EdmEnumObject color     = new EdmEnumObject(colorType, "Red");
            EdmEnumObject color2    = new EdmEnumObject(colorType, "0");
            EdmEnumObject color3    = new EdmEnumObject(colorType, "Red");

            customer.TrySetPropertyValue("Color", color);

            List <IEdmEnumObject> colorList = new List <IEdmEnumObject>();

            colorList.Add(color);
            colorList.Add(color2);
            colorList.Add(color3);
            IEdmCollectionTypeReference enumCollectionType = new EdmCollectionTypeReference(new EdmCollectionType(colorType.ToEdmTypeReference(false)));
            EdmEnumObjectCollection     colors             = new EdmEnumObjectCollection(enumCollectionType, colorList);

            customer.TrySetPropertyValue("Colors", colors);
            customer.TrySetPropertyValue("DeclaredColors", colors);

            //Add Addresses complex(collection) property
            EdmComplexType addressType =
                edmModel.SchemaElements.OfType <IEdmComplexType>().First(c => c.Name == "Address") as EdmComplexType;
            EdmComplexObject address = new EdmComplexObject(addressType);

            address.TrySetPropertyValue("Street", "No1");
            EdmComplexObject address2 = new EdmComplexObject(addressType);

            address2.TrySetPropertyValue("Street", "No2");

            List <IEdmComplexObject> addressList = new List <IEdmComplexObject>();

            addressList.Add(address);
            addressList.Add(address2);
            IEdmCollectionTypeReference complexCollectionType = new EdmCollectionTypeReference(new EdmCollectionType(addressType.ToEdmTypeReference(false)));
            EdmComplexObjectCollection  addresses             = new EdmComplexObjectCollection(complexCollectionType, addressList);

            customer.TrySetPropertyValue("DeclaredAddresses", addresses);

            EdmEntityObjectCollection customers = new EdmEntityObjectCollection(new EdmCollectionTypeReference(new EdmCollectionType(customerType.ToEdmTypeReference(false))));

            customers.Add(customer);
            _untypedSimpleOpenCustormers = customers;
            return(_untypedSimpleOpenCustormers);
        }
예제 #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
            });
        }
        public ODataDeltaResourceSetSerializerTests()
        {
            _model       = SerializationTestsHelpers.SimpleCustomerOrderModel();
            _customerSet = _model.EntityContainer.FindEntitySet("Customers");
            _model.SetAnnotationValue(_customerSet.EntityType(), new ClrTypeAnnotation(typeof(Customer)));
            _path      = new ODataPath(new EntitySetSegment(_customerSet));
            _customers = new[] {
                new Customer()
                {
                    FirstName   = "Foo",
                    LastName    = "Bar",
                    ID          = 10,
                    HomeAddress = new Address()
                    {
                        Street  = "Street",
                        ZipCode = null,
                    }
                },
                new Customer()
                {
                    FirstName = "Foo",
                    LastName  = "Bar",
                    ID        = 42,
                }
            };

            _deltaResourceSetCustomers = new EdmChangedObjectCollection(_customerSet.EntityType());
            EdmDeltaResourceObject newCustomer = new EdmDeltaResourceObject(_customerSet.EntityType());

            newCustomer.TrySetPropertyValue("ID", 10);
            newCustomer.TrySetPropertyValue("FirstName", "Foo");
            EdmComplexObject newCustomerAddress = new EdmComplexObject(_model.FindType("Default.Address") as IEdmComplexType);

            newCustomerAddress.TrySetPropertyValue("Street", "Street");
            newCustomerAddress.TrySetPropertyValue("ZipCode", null);
            newCustomer.TrySetPropertyValue("HomeAddress", newCustomerAddress);
            _deltaResourceSetCustomers.Add(newCustomer);

            _customersType = _model.GetEdmTypeReference(typeof(Customer[])).AsCollection();
            _writeContext  = new ODataSerializerContext()
            {
                NavigationSource = _customerSet, Model = _model, Path = _path
            };
        }
예제 #4
0
        private EdmEntityObjectCollection GetCustomers()
        {
            IEdmModel edmModel = Request.ODataProperties().Model;

            IEdmEntityType  customerType = edmModel.SchemaElements.OfType <IEdmEntityType>().Single(e => e.Name == "Customer");
            IEdmEnumType    colorType    = edmModel.SchemaElements.OfType <IEdmEnumType>().Single(e => e.Name == "Color");
            IEdmComplexType addressType  = edmModel.SchemaElements.OfType <IEdmComplexType>().Single(e => e.Name == "Address");

            // an enum object
            EdmEnumObject color = new EdmEnumObject(colorType, "Red");

            // a complex object
            EdmComplexObject address1 = new EdmComplexObject(addressType);

            address1.TrySetPropertyValue("Street", "ZiXing Rd");                                            // Declared property
            address1.TrySetPropertyValue("StringProperty", "CN");                                           // a string dynamic property
            address1.TrySetPropertyValue("GuidProperty", new Guid("181D3A20-B41A-489F-9F15-F91F0F6C9ECA")); // a guid dynamic property

            // another complex object with complex dynamic property
            EdmComplexObject address2 = new EdmComplexObject(addressType);

            address2.TrySetPropertyValue("Street", "ZiXing Rd");       // Declared property
            address2.TrySetPropertyValue("AddressProperty", address1); // a complex dynamic property

            // an entity object
            EdmEntityObject customer = new EdmEntityObject(customerType);

            customer.TrySetPropertyValue("CustomerId", 1);      // Declared property
            customer.TrySetPropertyValue("Name", "Mike");       // Declared property
            customer.TrySetPropertyValue("Color", color);       // an enum dynamic property
            customer.TrySetPropertyValue("Address1", address1); // a complex dynamic property
            customer.TrySetPropertyValue("Address2", address2); // another complex dynamic property

            // a collection with one object
            EdmEntityObjectCollection collection =
                new EdmEntityObjectCollection(
                    new EdmCollectionTypeReference(
                        new EdmCollectionType(new EdmEntityTypeReference(customerType, isNullable: true))));

            collection.Add(customer);

            return(collection);
        }
예제 #5
0
        IEdmComplexObject Map(IEdmModel model, ComplexPoly p)
        {
            var type = (IEdmComplexType)GetType(model, p.GetType());

            var co = new EdmComplexObject(type);

            co.TrySetPropertyValue("Name", p.Name);

            var b = p as ComplexPolyB;

            if (b != null)
            {
                co.TrySetPropertyValue("Prop1", b.Prop1);
                co.TrySetPropertyValue("Prop2", b.Prop2);
                co.TrySetPropertyValue("Prop3", b.Prop3);
                co.TrySetPropertyValue("Prop4", b.Prop4);
            }

            var c = p as ComplexPolyC;

            if (c != null)
            {
                co.TrySetPropertyValue("Taste", c.Taste);
            }

            return(co);
        }
        protected virtual IEdmEntityObject BuildEdmEntityObject(Entity record, SavedQueryView view, IEnumerable <SavedQueryView.ViewColumn> viewColumns, EdmEntityTypeReference dataEntityTypeReference, EdmComplexTypeReference entityReferenceComplexTypeReference, EdmComplexTypeReference optionSetComplexTypeReference, string entityListIdString, string viewIdString)
        {
            if (record == null)
            {
                return(null);
            }

            var entityObject = new EdmEntityObject(dataEntityTypeReference);

            entityObject.TrySetPropertyValue(view.PrimaryKeyLogicalName, record.Id);

            foreach (var column in viewColumns)
            {
                var value = record.Attributes.Contains(column.LogicalName) ? record.Attributes[column.LogicalName] : null;

                if (value is AliasedValue)
                {
                    var aliasedValue = value as AliasedValue;
                    value = aliasedValue.Value;
                }

                if (column.Metadata == null)
                {
                    continue;
                }

                var propertyName = column.LogicalName;

                if (propertyName.Contains('.'))
                {
                    propertyName = string.Format("{0}-{1}", column.Metadata.EntityLogicalName, column.Metadata.LogicalName);
                }

                switch (column.Metadata.AttributeType)
                {
                case AttributeTypeCode.Money:
                    var     money      = value as Money;
                    decimal moneyValue = 0;
                    if (money != null)
                    {
                        moneyValue = money.Value;
                    }
                    entityObject.TrySetPropertyValue(propertyName, moneyValue);
                    break;

                case AttributeTypeCode.Customer:
                case AttributeTypeCode.Lookup:
                case AttributeTypeCode.Owner:
                    var entityReference = value as EntityReference;
                    if (entityReference == null)
                    {
                        continue;
                    }
                    var entityReferenceObject = new EdmComplexObject(entityReferenceComplexTypeReference);
                    entityReferenceObject.TrySetPropertyValue("Name", entityReference.Name);
                    entityReferenceObject.TrySetPropertyValue("Id", entityReference.Id);
                    entityObject.TrySetPropertyValue(propertyName, entityReferenceObject);
                    break;

                case AttributeTypeCode.State:
                    var stateOptionSet = value as OptionSetValue;
                    if (stateOptionSet == null)
                    {
                        continue;
                    }
                    var stateAttributeMetadata = column.Metadata as StateAttributeMetadata;
                    if (stateAttributeMetadata == null)
                    {
                        continue;
                    }
                    var stateOption = stateAttributeMetadata.OptionSet.Options.FirstOrDefault(o => o != null && o.Value != null && o.Value.Value == stateOptionSet.Value);
                    if (stateOption == null)
                    {
                        continue;
                    }
                    var stateLabel      = stateOption.Label.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == LanguageCode) ?? stateOption.Label.GetLocalizedLabel();
                    var stateOptionName = stateLabel == null?stateOption.Label.GetLocalizedLabelString() : stateLabel.Label;

                    var stateOptionSetObject = new EdmComplexObject(optionSetComplexTypeReference);
                    stateOptionSetObject.TrySetPropertyValue("Name", stateOptionName);
                    stateOptionSetObject.TrySetPropertyValue("Value", stateOptionSet.Value);
                    entityObject.TrySetPropertyValue(propertyName, stateOptionSetObject);
                    break;

                case AttributeTypeCode.Picklist:
                    var optionSet = value as OptionSetValue;
                    if (optionSet == null)
                    {
                        continue;
                    }
                    var picklistAttributeMetadata = column.Metadata as PicklistAttributeMetadata;
                    if (picklistAttributeMetadata == null)
                    {
                        continue;
                    }
                    var option = picklistAttributeMetadata.OptionSet.Options.FirstOrDefault(o => o != null && o.Value != null && o.Value.Value == optionSet.Value);
                    if (option == null)
                    {
                        continue;
                    }
                    var label = option.Label.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == LanguageCode) ?? option.Label.GetLocalizedLabel();
                    var name  = label == null?option.Label.GetLocalizedLabelString() : label.Label;

                    var optionSetObject = new EdmComplexObject(optionSetComplexTypeReference);
                    optionSetObject.TrySetPropertyValue("Name", name);
                    optionSetObject.TrySetPropertyValue("Value", optionSet.Value);
                    entityObject.TrySetPropertyValue(propertyName, optionSetObject);
                    break;

                case AttributeTypeCode.Status:
                    var statusOptionSet = value as OptionSetValue;
                    if (statusOptionSet == null)
                    {
                        continue;
                    }
                    var statusAttributeMetadata = column.Metadata as StatusAttributeMetadata;
                    if (statusAttributeMetadata == null)
                    {
                        continue;
                    }
                    var statusOption = statusAttributeMetadata.OptionSet.Options.FirstOrDefault(o => o != null && o.Value != null && o.Value.Value == statusOptionSet.Value);
                    if (statusOption == null)
                    {
                        continue;
                    }
                    var statusLabel      = statusOption.Label.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == LanguageCode) ?? statusOption.Label.GetLocalizedLabel();
                    var statusOptionName = statusLabel == null?statusOption.Label.GetLocalizedLabelString() : statusLabel.Label;

                    var statusOptionSetObject = new EdmComplexObject(optionSetComplexTypeReference);
                    statusOptionSetObject.TrySetPropertyValue("Name", statusOptionName);
                    statusOptionSetObject.TrySetPropertyValue("Value", statusOptionSet.Value);
                    entityObject.TrySetPropertyValue(propertyName, statusOptionSetObject);
                    break;

                default:
                    entityObject.TrySetPropertyValue(propertyName, value);
                    break;
                }
                entityObject.TrySetPropertyValue("list-id", entityListIdString);
                entityObject.TrySetPropertyValue("view-id", viewIdString);
            }
            return(entityObject);
        }