Ejemplo n.º 1
0
        public string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup <string, HttpActionDescriptor> actionMap)
        {
            if (odataPath == null || controllerContext == null || actionMap == null)
            {
                return(null);
            }

            if (controllerContext.Request.Method != HttpMethod.Get)
            {
                return(null);
            }

            if (odataPath.PathTemplate != "~/unboundfunction")
            {
                return(null);
            }

            string actionName = null;
            UnboundFunctionPathSegment function = null;

            function = odataPath.Segments.First() as UnboundFunctionPathSegment;
            if (function == null)
            {
                return(null);
            }

            actionName = function.FunctionName;
            AddKeyValueToRouteData(controllerContext, odataPath);

            // how to add the parameter values;

            return(actionName);
        }
        public string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup <string, HttpActionDescriptor> actionMap)
        {
            //var es = odataPath.Segments.FirstOrDefault(s => s.SegmentKind == "entityset") as EntitySetPathSegment;
            var nav = odataPath.Segments.FirstOrDefault(s => s.SegmentKind == "navigation") as NavigationPathSegment;

            if (odataPath.PathTemplate == "~/unboundaction")
            {
                UnboundActionPathSegment actionSegment = odataPath.Segments.First() as UnboundActionPathSegment;
                if (actionSegment != null)
                {
                    IEdmActionImport action = actionSegment.Action;

                    if (actionMap.Contains(action.Name))
                    {
                        return(action.Name);
                    }
                }
            }
            else if (odataPath.PathTemplate == "~/unboundfunction")
            {
                UnboundFunctionPathSegment functionSegment = odataPath.Segments.First() as UnboundFunctionPathSegment;
                if (functionSegment != null)
                {
                    IEdmFunctionImport function = functionSegment.Function;

                    if (actionMap.Contains(function.Name))
                    {
                        return(function.Name);
                    }
                }
            }

            if (odataPath.PathTemplate == "~/entityset/key/navigation" && nav != null)
            {
                controllerContext.RouteData.Values["key"] = (odataPath.Segments[1] as KeyValuePathSegment).Value;
                return("Get" + nav.NavigationPropertyName);
            }
            else if (odataPath.PathTemplate == "~/entityset/key/navigation/key" && nav != null)
            {
                controllerContext.RouteData.Values["key1"] = (odataPath.Segments[1] as KeyValuePathSegment).Value;
                controllerContext.RouteData.Values["key2"] = (odataPath.Segments[3] as KeyValuePathSegment).Value;
                return("Get" + nav.NavigationProperty.ToEntityType().Name);
            }

            return(null);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Выполняет пользовательскую функцию.
        /// </summary>
        /// <param name="queryParameters">Параметры запроса.</param>
        /// <returns>Результат выполнения пользовательской функции, преобразованный к типам сущностей EDM-модели или к примитивным типам.</returns>
        internal IHttpActionResult ExecuteUserFunction(QueryParameters queryParameters)
        {
            queryParameters.Count   = null;
            queryParameters.Request = Request;
            ODataPath odataPath = Request.ODataProperties().Path;
            UnboundFunctionPathSegment segment = odataPath.Segments[odataPath.Segments.Count - 1] as UnboundFunctionPathSegment;

            if (segment == null || !_functions.IsRegistered(segment.FunctionName))
            {
                return(SetResult("Function not found"));
            }

            Function function = _functions.GetFunction(segment.FunctionName);
            Dictionary <string, object> parameters = new Dictionary <string, object>();

            foreach (var parameterName in function.ParametersTypes.Keys)
            {
                var parameterValue = segment.GetParameterValue(parameterName);
                if (parameterValue is ODataEnumValue)
                {
                    parameterValue = Enum.Parse(function.ParametersTypes[parameterName], (parameterValue as ODataEnumValue).Value);
                }

                parameters.Add(parameterName, parameterValue);
            }

            var result = function.Handler(queryParameters, parameters);

            if (result == null)
            {
                return(SetResult("Result is null."));
            }

            if (!(result is string) && result is IEnumerable)
            {
                Type type = null;
                if (result.GetType().IsGenericType)
                {
                    Type[] args = result.GetType().GetGenericArguments();
                    if (args.Length == 1)
                    {
                        type = args[0];
                    }
                }

                if (result.GetType().IsArray)
                {
                    type = result.GetType().GetElementType();
                }

                if (type != null && (type.IsSubclassOf(typeof(DataObject)) || type == typeof(DataObject)))
                {
                    var queryOpt = CreateODataQueryOptions(type);

                    QueryOptions = queryOpt;
                    if (QueryOptions.SelectExpand != null && QueryOptions.SelectExpand.SelectExpandClause != null)
                    {
                        Request.ODataProperties().SelectExpandClause = QueryOptions.SelectExpand.SelectExpandClause;
                    }

                    this.type = type;
                    CreateDynamicView();
                    IncludeCount = false;
                    if (queryOpt.Count != null && queryOpt.Count.Value)
                    {
                        IncludeCount = true;
                        if (queryParameters.Count != null)
                        {
                            Count = (int)queryParameters.Count;
                        }
                        else
                        {
                            Count = GetObjectsCount(type, queryOpt);
                        }
                    }

                    NameValueCollection queryParams = Request.RequestUri.ParseQueryString();

                    if ((_model.ExportService != null || _model.ODataExportService != null) && (Request.Properties.ContainsKey(PostPatchHandler.AcceptApplicationMsExcel) || Convert.ToBoolean(queryParams.Get("exportExcel"))))
                    {
                        _objs = (result as IEnumerable).Cast <DataObject>().ToArray();
                        return(ResponseMessage(CreateExcel(queryParams)));
                    }

                    var coll = GetEdmCollection((IEnumerable)result, type, 1, null, _dynamicView);
                    return(SetResult(coll));
                }

                return(SetResult(result));
            }

            if (result is DataObject)
            {
                QueryOptions = CreateODataQueryOptions(result.GetType());
                if (QueryOptions.SelectExpand != null && QueryOptions.SelectExpand.SelectExpandClause != null)
                {
                    Request.ODataProperties().SelectExpandClause = QueryOptions.SelectExpand.SelectExpandClause;
                }

                this.type = result.GetType();
                CreateDynamicView();
                var entityType = _model.GetEdmEntityType(this.type);
                return(SetResult(GetEdmObject(entityType, result, 1, null, _dynamicView)));
            }

            return(SetResultPrimitive(result.GetType(), result));
        }