Пример #1
0
        /// <summary>
        /// Translate an ExpandedReferenceSelectItem
        /// </summary>
        /// <param name="item">the item to Translate</param>
        /// <returns>Defined by the implementer</returns>
        public override string Translate(ExpandedReferenceSelectItem item)
        {
            NodeToStringBuilder nodeToStringBuilder = new NodeToStringBuilder();
            string currentExpandClause = String.Join("/", item.PathToNavigationProperty.WalkWith(PathSegmentToStringTranslator.Instance).ToArray());
            string res = string.Empty;

            if (item.FilterOption != null)
            {
                res += "$filter=" + nodeToStringBuilder.TranslateFilterClause(item.FilterOption);
            }

            if (item.OrderByOption != null)
            {
                res += string.IsNullOrEmpty(res) ? null : ";";
                res += "$orderby=" + nodeToStringBuilder.TranslateOrderByClause(item.OrderByOption);
            }

            if (item.TopOption != null)
            {
                res += string.IsNullOrEmpty(res) ? null : ";";
                res += "$top=" + item.TopOption.ToString();
            }

            if (item.SkipOption != null)
            {
                res += string.IsNullOrEmpty(res) ? null : ";";
                res += "$skip=" + item.SkipOption.ToString();
            }

            if (item.CountOption != null)
            {
                res += string.IsNullOrEmpty(res) ? null : ";";
                res += "$count";
                res += ExpressionConstants.SymbolEqual;
                if (item.CountOption == true)
                {
                    res += ExpressionConstants.KeywordTrue;
                }
                else
                {
                    res += ExpressionConstants.KeywordFalse;
                }
            }

            if (item.SearchOption != null)
            {
                res += string.IsNullOrEmpty(res) ? null : ";";
                res += "$search";
                res += ExpressionConstants.SymbolEqual;
                res += nodeToStringBuilder.TranslateSearchClause(item.SearchOption);
            }

            // nested $compute=
            if (item.ComputeOption != null)
            {
                res += string.IsNullOrEmpty(res) ? null : ";";
                res += "$compute";
                res += ExpressionConstants.SymbolEqual;
                res += nodeToStringBuilder.TranslateComputeClause(item.ComputeOption);
            }

            // nested $apply=
            if (item.ApplyOption != null)
            {
                res += string.IsNullOrEmpty(res) ? null : ";";
                var applyClauseToStringBuilder = new ApplyClauseToStringBuilder();
                res += applyClauseToStringBuilder.TranslateApplyClause(item.ApplyOption);
            }

            return(string.Concat(currentExpandClause, string.IsNullOrEmpty(res) ? null : string.Concat(ExpressionConstants.SymbolOpenParen, res, ExpressionConstants.SymbolClosedParen)));
        }
Пример #2
0
        /// <summary>
        /// Build ODataUri into a Uri, the result URIs query options are URL encoded.
        /// </summary>
        /// <param name="odataUri">ODataUri which will be build to relative url</param>
        /// <param name="urlKeyDelimiter">Value from ODataUrlKeyDelimiter</param>
        /// <returns>Uri of the semantic tree</returns>
        public static Uri BuildUri(this ODataUri odataUri, ODataUrlKeyDelimiter urlKeyDelimiter)
        {
            NodeToStringBuilder nodeToStringBuilder = new NodeToStringBuilder();
            SelectExpandClauseToStringBuilder selectExpandClauseToStringBuilder = new SelectExpandClauseToStringBuilder();

            String queryOptions     = String.Empty;
            bool   writeQueryPrefix = true;

            if (odataUri.Filter != null)
            {
                queryOptions     = WriteQueryPrefixOrSeparator(writeQueryPrefix, queryOptions);
                writeQueryPrefix = false;
                queryOptions     = string.Concat(queryOptions, "$filter", ExpressionConstants.SymbolEqual, Uri.EscapeDataString(nodeToStringBuilder.TranslateFilterClause(odataUri.Filter)));
            }

            if (odataUri.SelectAndExpand != null)
            {
                string result = selectExpandClauseToStringBuilder.TranslateSelectExpandClause(odataUri.SelectAndExpand, true);
                if (!string.IsNullOrEmpty(result))
                {
                    queryOptions     = WriteQueryPrefixOrSeparator(writeQueryPrefix, queryOptions);
                    writeQueryPrefix = false;
                    queryOptions     = string.Concat(queryOptions, result);
                }
            }

            if (odataUri.Apply != null)
            {
                var    applyClauseToStringBuilder = new ApplyClauseToStringBuilder();
                string result = applyClauseToStringBuilder.TranslateApplyClause(odataUri.Apply);
                if (!string.IsNullOrEmpty(result))
                {
                    queryOptions     = WriteQueryPrefixOrSeparator(writeQueryPrefix, queryOptions);
                    writeQueryPrefix = false;
                    queryOptions     = string.Concat(queryOptions, result);
                }
            }

            if (odataUri.OrderBy != null)
            {
                queryOptions     = WriteQueryPrefixOrSeparator(writeQueryPrefix, queryOptions);
                writeQueryPrefix = false;
                queryOptions     = string.Concat(queryOptions, "$orderby", ExpressionConstants.SymbolEqual, Uri.EscapeDataString(nodeToStringBuilder.TranslateOrderByClause(odataUri.OrderBy)));
            }

            if (odataUri.Top != null)
            {
                queryOptions     = WriteQueryPrefixOrSeparator(writeQueryPrefix, queryOptions);
                writeQueryPrefix = false;
                queryOptions     = string.Concat(queryOptions, "$top", ExpressionConstants.SymbolEqual, Uri.EscapeDataString(odataUri.Top.ToString()));
            }

            if (odataUri.Skip != null)
            {
                queryOptions     = WriteQueryPrefixOrSeparator(writeQueryPrefix, queryOptions);
                writeQueryPrefix = false;
                queryOptions     = string.Concat(queryOptions, "$skip", ExpressionConstants.SymbolEqual, Uri.EscapeDataString(odataUri.Skip.ToString()));
            }

            if (odataUri.Index != null)
            {
                queryOptions     = WriteQueryPrefixOrSeparator(writeQueryPrefix, queryOptions);
                writeQueryPrefix = false;
                queryOptions     = string.Concat(queryOptions, "$index", ExpressionConstants.SymbolEqual, Uri.EscapeDataString(odataUri.Index.ToString()));
            }

            if (odataUri.QueryCount != null)
            {
                queryOptions     = WriteQueryPrefixOrSeparator(writeQueryPrefix, queryOptions);
                writeQueryPrefix = false;
                queryOptions     = string.Concat(queryOptions, "$count", ExpressionConstants.SymbolEqual, Uri.EscapeDataString(odataUri.QueryCount == true ? ExpressionConstants.KeywordTrue : ExpressionConstants.KeywordFalse));
            }

            if (odataUri.Search != null)
            {
                queryOptions     = WriteQueryPrefixOrSeparator(writeQueryPrefix, queryOptions);
                writeQueryPrefix = false;
                queryOptions     = string.Concat(queryOptions, "$search", ExpressionConstants.SymbolEqual, Uri.EscapeDataString(nodeToStringBuilder.TranslateSearchClause(odataUri.Search)));
            }

            if (odataUri.SkipToken != null)
            {
                queryOptions     = WriteQueryPrefixOrSeparator(writeQueryPrefix, queryOptions);
                writeQueryPrefix = false;
                queryOptions     = string.Concat(queryOptions, "$skiptoken", ExpressionConstants.SymbolEqual, Uri.EscapeDataString(odataUri.SkipToken));
            }

            if (odataUri.DeltaToken != null)
            {
                queryOptions     = WriteQueryPrefixOrSeparator(writeQueryPrefix, queryOptions);
                writeQueryPrefix = false;
                queryOptions     = string.Concat(queryOptions, "$deltatoken", ExpressionConstants.SymbolEqual, Uri.EscapeDataString(odataUri.DeltaToken));
            }

            if (odataUri.ParameterAliasNodes != null && odataUri.ParameterAliasNodes.Count > 0)
            {
                string aliasNode = nodeToStringBuilder.TranslateParameterAliasNodes(odataUri.ParameterAliasNodes);
                queryOptions     = String.IsNullOrEmpty(aliasNode) ? queryOptions : String.Concat(WriteQueryPrefixOrSeparator(writeQueryPrefix, queryOptions), aliasNode);
                writeQueryPrefix = false;
            }

            string res = String.Concat(odataUri.Path.ToResourcePathString(urlKeyDelimiter), queryOptions);

            return(odataUri.ServiceRoot == null ? new Uri(res, UriKind.Relative) : new Uri(odataUri.ServiceRoot, new Uri(res, UriKind.Relative)));
        }