/// <summary> /// Creates a function that takes in an object and generates nextlink uri. /// </summary> /// <param name="resourceSet">The resource set describing a collection of structured objects.</param> /// <param name="resourceSetInstance">The instance representing the resourceSet being written.</param> /// <param name="writeContext">The serializer context.</param> /// <returns>The function that generates the NextLink from an object.</returns> internal static Func <object, Uri> GetNextLinkGenerator(ODataResourceSetBase resourceSet, IEnumerable resourceSetInstance, ODataSerializerContext writeContext) { if (resourceSet != null && resourceSet.NextPageLink != null) { Uri defaultUri = resourceSet.NextPageLink; return((obj) => { return defaultUri; }); } if (writeContext.ExpandedResource == null) { if (writeContext.Request != null && writeContext.QueryContext != null) { SkipTokenHandler handler = writeContext.QueryContext.GetSkipTokenHandler(); return((obj) => { return handler.GenerateNextPageLink(new System.Uri(writeContext.Request.GetEncodedUrl()), (writeContext.Request.ODataFeature() as ODataFeature).PageSize, obj, writeContext); }); } } else { // nested resourceSet ITruncatedCollection truncatedCollection = resourceSetInstance as ITruncatedCollection; if (truncatedCollection != null && truncatedCollection.IsTruncated) { return((obj) => { return GetNestedNextPageLink(writeContext, truncatedCollection.PageSize, obj); }); } } return((obj) => { return null; }); }
private static ODataCollectionStart GetCollectionStart(ODataSerializerContext writeContext) { ODataCollectionStart collectionStart = new ODataCollectionStart { Name = writeContext.RootElementName }; if (writeContext.Request != null) { if (writeContext.InternalRequest.Context.NextLink != null) { collectionStart.NextPageLink = writeContext.InternalRequest.Context.NextLink; } else if (writeContext.InternalRequest.Context.QueryOptions != null) { // Collection serializer is called only for collection of primitive values - A null object will be supplied since it is a non-entity value SkipTokenHandler skipTokenHandler = writeContext.QueryOptions.Context.GetSkipTokenHandler(); collectionStart.NextPageLink = skipTokenHandler.GenerateNextPageLink(writeContext.InternalRequest.RequestUri, writeContext.InternalRequest.Context.PageSize, null, writeContext); } if (writeContext.InternalRequest.Context.TotalCount != null) { collectionStart.Count = writeContext.InternalRequest.Context.TotalCount; } } return(collectionStart); }
private static Uri GetNestedNextPageLink(ODataSerializerContext writeContext, int pageSize, object obj) { Contract.Assert(writeContext.ExpandedResource != null); IEdmNavigationSource sourceNavigationSource = writeContext.ExpandedResource.NavigationSource; NavigationSourceLinkBuilderAnnotation linkBuilder = writeContext.Model.GetNavigationSourceLinkBuilder(sourceNavigationSource); Uri navigationLink = linkBuilder.BuildNavigationLink(writeContext.ExpandedResource, writeContext.NavigationProperty); Uri nestedNextLink = GenerateQueryFromExpandedItem(writeContext, navigationLink); SkipTokenHandler nextLinkGenerator = null; if (writeContext.QueryContext != null) { nextLinkGenerator = writeContext.QueryContext.GetSkipTokenHandler(); } if (nestedNextLink != null) { if (nextLinkGenerator != null) { return(nextLinkGenerator.GenerateNextPageLink(nestedNextLink, pageSize, obj, writeContext)); } return(GetNextPageHelper.GetNextPageLink(nestedNextLink, pageSize)); } return(null); }
/// <summary> /// Writes the given <paramref name="graph"/> using the given <paramref name="writer"/>. /// </summary> /// <param name="writer">The <see cref="ODataCollectionWriter"/> to use.</param> /// <param name="graph">The collection to write.</param> /// <param name="collectionType">The EDM type of the collection.</param> /// <param name="writeContext">The serializer context.</param> public virtual void WriteCollection(ODataCollectionWriter writer, object graph, IEdmTypeReference collectionType, ODataSerializerContext writeContext) { if (writer == null) { throw Error.ArgumentNull(nameof(writer)); } if (writeContext == null) { throw Error.ArgumentNull(nameof(writeContext)); } ODataCollectionStart collectionStart = new ODataCollectionStart { Name = writeContext.RootElementName }; if (writeContext.Request != null) { ODataFeature odataFeature = writeContext.Request.ODataFeature() as ODataFeature; if (odataFeature.NextLink != null) { collectionStart.NextPageLink = odataFeature.NextLink; } else if (odataFeature.QueryOptions != null) { // Collection serializer is called only for collection of primitive values - A null object will be supplied since it is a non-entity value SkipTokenHandler skipTokenHandler = writeContext.QueryOptions.Context.GetSkipTokenHandler(); collectionStart.NextPageLink = skipTokenHandler.GenerateNextPageLink(new Uri(writeContext.Request.GetEncodedUrl()), odataFeature.PageSize, null, writeContext); } if (odataFeature.TotalCount != null) { collectionStart.Count = odataFeature.TotalCount; } } writer.WriteStart(collectionStart); if (graph != null) { ODataCollectionValue collectionValue = CreateODataValue(graph, collectionType, writeContext) as ODataCollectionValue; if (collectionValue != null) { foreach (object item in collectionValue.Items) { writer.WriteItem(item); } } } writer.WriteEnd(); }
private static Uri GetNestedNextPageLink(ODataSerializerContext writeContext, int pageSize, object obj) { Contract.Assert(writeContext.ExpandedResource != null); Uri navigationLink; IEdmNavigationSource sourceNavigationSource = writeContext.ExpandedResource.NavigationSource; NavigationSourceLinkBuilderAnnotation linkBuilder = writeContext.Model.GetNavigationSourceLinkBuilder(sourceNavigationSource); // In Contained Navigation, we don't have navigation property binding, // Hence we cannot get the NavigationLink from the NavigationLinkBuilder if (writeContext.NavigationSource.NavigationSourceKind() == EdmNavigationSourceKind.ContainedEntitySet) { // Contained navigation. Uri idlink = linkBuilder.BuildIdLink(writeContext.ExpandedResource); var link = idlink.ToString() + "/" + writeContext.NavigationProperty.Name; navigationLink = new Uri(link); } else { // Non-Contained navigation. navigationLink = linkBuilder.BuildNavigationLink(writeContext.ExpandedResource, writeContext.NavigationProperty); } Uri nestedNextLink = GenerateQueryFromExpandedItem(writeContext, navigationLink); SkipTokenHandler nextLinkGenerator = null; if (writeContext.QueryContext != null) { nextLinkGenerator = writeContext.QueryContext.GetSkipTokenHandler(); } if (nestedNextLink != null) { if (nextLinkGenerator != null) { return(nextLinkGenerator.GenerateNextPageLink(nestedNextLink, pageSize, obj, writeContext)); } return(GetNextPageHelper.GetNextPageLink(nestedNextLink, pageSize)); } return(null); }
protected override Task GetNextBatchFromServerAsync(SkipTokenHandler skipHandler, CancellationToken cancellationToken) { IEnumerable <int> chars; if (!skipHandler.AtLeastOneCallMade) { chars = Enumerable.Range((int)'A', this.pageSize); } else { chars = Enumerable.Range((int)(skipHandler.SkipToken[0]) + 1, this.pageSize); } var letters = chars.TakeWhile(c => c <= this.last) .Select(c => ((char)c).ToString()) .ToArray(); _currentBatch = letters; skipHandler.SkipToken = GetSkipToken(); return(Task.Delay(0)); }