private static EdmModel CreateCustomerProductsModel()
        {
            var model = new EdmModel();

            var container = new EdmEntityContainer("defaultNamespace", "DefaultContainer");
            model.AddElement(container);

            var productType = new EdmEntityType("defaultNamespace", "Product");
            model.AddElement(productType);

            var customerType = new EdmEntityType("defaultNamespace", "Customer");
            customerType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "Products", Target = productType, TargetMultiplicity = EdmMultiplicity.Many });
            productType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "Customers", Target = customerType, TargetMultiplicity = EdmMultiplicity.Many });

            model.AddElement(customerType);

            var productSet = new EdmEntitySet(
                container,
                "Products",
                productType);

            container.AddElement(productSet);

            var customerSet = new EdmEntitySet(
                container,
                "Customers",
                customerType);

            var productsNavProp = customerType.NavigationProperties().Single(np => np.Name == "Products");
            customerSet.AddNavigationTarget(productsNavProp, productSet);
            container.AddElement(customerSet);

            return model;
        }
        private static void AddProcedures(this IEdmModel model, IEnumerable<ProcedureConfiguration> configurations, EdmEntityContainer container, Dictionary<Type, IEdmStructuredType> edmTypeMap, Dictionary<string, EdmEntitySet> edmEntitySetMap)
        {
            foreach (ProcedureConfiguration procedure in configurations)
            {
                switch (procedure.Kind)
                {
                    case ProcedureKind.Action:
                        ActionConfiguration action = procedure as ActionConfiguration;
                        IEdmTypeReference returnReference = GetEdmTypeReference(edmTypeMap, action.ReturnType, nullable: true);
                        IEdmExpression expression = GetEdmEntitySetExpression(edmEntitySetMap, action);

                        EdmFunctionImport functionImport = new EdmFunctionImport(container, action.Name, returnReference, expression, action.IsSideEffecting, action.IsComposable, action.IsBindable);
                        if (action.IsBindable)
                        {
                            model.SetIsAlwaysBindable(functionImport, action.IsAlwaysBindable);
                            if (action.BindingParameter.TypeConfiguration.Kind == EdmTypeKind.Entity)
                            {
                                Func<EntityInstanceContext, Uri> actionFactory = action.GetActionLink();
                                if (actionFactory != null)
                                {
                                    model.SetAnnotationValue<ActionLinkBuilder>(functionImport, new ActionLinkBuilder(actionFactory));
                                }
                            }
                        }

                        foreach (ParameterConfiguration parameter in action.Parameters)
                        {
                            // TODO: http://aspnetwebstack.codeplex.com/workitem/417
                            bool isParameterOptional = EdmLibHelpers.IsNullable(parameter.TypeConfiguration.ClrType);
                            IEdmTypeReference parameterTypeReference = GetEdmTypeReference(edmTypeMap, parameter.TypeConfiguration, nullable: isParameterOptional);
                            EdmFunctionParameter functionParameter = new EdmFunctionParameter(functionImport, parameter.Name, parameterTypeReference, EdmFunctionParameterMode.In);
                            functionImport.AddParameter(functionParameter);
                        }
                        container.AddElement(functionImport);
                        break;

                    case ProcedureKind.Function:
                        Contract.Assert(false, "Functions are not supported.");
                        break;

                    case ProcedureKind.ServiceOperation:
                        Contract.Assert(false, "ServiceOperations are not supported.");
                        break;
                }
            }
        }
Exemplo n.º 3
0
        private static IEdmModel BuildModel(JObject viewObject, out IList<string> fieldsToIgnore)
        {
            var model = new EdmModel();
            fieldsToIgnore = new List<string>();

            var name = viewObject.PrimitivePropertyValue<string>("name");
            name = name.Replace(' ', '_');

            var entityType = new EdmEntityType(false, false, null, "OData4Socrata", name,
                                               Enumerable.Empty<IEdmStructuralProperty>());
            var entitySet = new EdmEntitySet(name, entityType);

            EdmComplexType phoneComplex = null;
            EdmComplexType locationComplex = null;
            EdmComplexType urlComplex = null;

            foreach (var column in viewObject.ArrayPropertyValue<JObject>("columns"))
            {
                var fieldName = column.PrimitivePropertyValue<string>("name");

                var sodaType = column.PrimitivePropertyValue<string>("dataTypeName");
                IEdmTypeReference typeReference;
                switch (sodaType)
                {
                    case "meta_data":
                        fieldsToIgnore.Add(fieldName);
                        continue;

                    case "text":

                        typeReference = EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (string));
                        break;

                    case "url":
                        if (urlComplex == null)
                        {
                            urlComplex = new EdmComplexType(false, false, null, entityType.Namespace, "url");
                            new EdmStructuralProperty(urlComplex, "url", EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (string)),
                                                      null, EdmConcurrencyMode.None);
                            model.AddElement(urlComplex);
                        }

                        typeReference = new EdmComplexTypeReference(urlComplex, false);
                        break;

                    case "phone":
                        if (phoneComplex == null)
                        {
                            phoneComplex = new EdmComplexType(false, false, null, entityType.Namespace, "phone");
                            new EdmStructuralProperty(phoneComplex, "phone_number",
                                                      EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (string)), null,
                                                      EdmConcurrencyMode.None);
                            new EdmStructuralProperty(phoneComplex, "phone_type",
                                                      EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (string)), null,
                                                      EdmConcurrencyMode.None);
                            model.AddElement(phoneComplex);
                        }

                        typeReference = new EdmComplexTypeReference(phoneComplex, false);
                        break;

                    case "location":
                        if (locationComplex == null)
                        {
                            locationComplex = new EdmComplexType(false, false, null, entityType.Namespace, "location");
                            new EdmStructuralProperty(locationComplex, "human_address",
                                                      EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (string)), null,
                                                      EdmConcurrencyMode.None);
                            new EdmStructuralProperty(locationComplex, "latitude",
                                                      EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (string)), null,
                                                      EdmConcurrencyMode.None);
                            new EdmStructuralProperty(locationComplex, "longitude",
                                                      EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (string)), null,
                                                      EdmConcurrencyMode.None);
                            new EdmStructuralProperty(locationComplex, "machine_address",
                                                      EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (string)), null,
                                                      EdmConcurrencyMode.None);
                            new EdmStructuralProperty(locationComplex, "needs_recoding",
                                                      EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (bool)), null,
                                                      EdmConcurrencyMode.None);
                            model.AddElement(locationComplex);
                        }

                        typeReference = new EdmComplexTypeReference(locationComplex, false);
                        break;

                    default:
                        throw new Exception();
                }

                new EdmStructuralProperty(entityType, fieldName, typeReference, null, EdmConcurrencyMode.None);
            }

            model.AddElement(entityType);

            var entityContainer = new EdmEntityContainer();
            model.AddEntityContainer(entityContainer);
            entityContainer.AddElement(entitySet);

            return model;
        }