private void Read(Lazy<ODataReader> lazyReader)
        {
            foreach (var state in this.expectedStates)
            {
                lazyReader.Value.Read();
                ExceptionUtilities.Assert(lazyReader.Value.State == state, "Expected %1, Found %2", state, lazyReader.Value.State);
                switch (state)
                {
                    case ODataReaderState.FeedStart:
                        if (readItems.Count > 0)
                        {
                            ODataNavigationLink navLink = (ODataNavigationLink)readItems.Peek();
                            var annotation = navLink.GetAnnotation<ODataNavigationLinkExpandedItemObjectModelAnnotation>();
                            if (annotation == null)
                            {
                                annotation = new ODataNavigationLinkExpandedItemObjectModelAnnotation();
                                navLink.SetAnnotation(annotation);
                            }

                            annotation.ExpandedItem = lazyReader.Value.Item;
                        }

                        readItems.Push(lazyReader.Value.Item);
                        break;
                    case ODataReaderState.EntryStart:
                        var currentEntry = (ODataEntry)lazyReader.Value.Item;
                        if (readItems.Count > 0)
                        {
                            ODataFeed feed = readItems.Peek() as ODataFeed;
                            if (feed != null)
                            {
                                var annotation = feed.GetAnnotation<ODataFeedEntriesObjectModelAnnotation>();
                                if (annotation == null)
                                {
                                    annotation = new ODataFeedEntriesObjectModelAnnotation();
                                    feed.SetAnnotation(annotation);
                                }
                                annotation.Add((ODataEntry)lazyReader.Value.Item);
                            }
                            else
                            {
                                ODataNavigationLink navLink = (ODataNavigationLink)readItems.Peek();
                                var annotation = navLink.GetAnnotation<ODataNavigationLinkExpandedItemObjectModelAnnotation>();
                                if (annotation == null)
                                {
                                    annotation = new ODataNavigationLinkExpandedItemObjectModelAnnotation();
                                    navLink.SetAnnotation(annotation);
                                }

                                annotation.ExpandedItem = currentEntry;
                            }
                        }

                        readItems.Push(currentEntry);
                        break;
                    case ODataReaderState.NavigationLinkStart:
                        ODataEntry entry = (ODataEntry)readItems.Peek();
                        var navLinksAnnotation = entry.GetAnnotation<ODataEntryNavigationLinksObjectModelAnnotation>();
                        if (navLinksAnnotation == null)
                        {
                            navLinksAnnotation = new ODataEntryNavigationLinksObjectModelAnnotation();
                            entry.SetAnnotation(navLinksAnnotation);
                        }

                        navLinksAnnotation.Add((ODataNavigationLink)lazyReader.Value.Item, entry.Properties.Count() + navLinksAnnotation.Count);
                        readItems.Push(lazyReader.Value.Item);
                        break;

                    case ODataReaderState.EntryEnd:
                    case ODataReaderState.FeedEnd:
                    case ODataReaderState.NavigationLinkEnd:
                        if (readItems.Count() > 1)
                            readItems.Pop();
                        break;
                }
            }

            this.expectedStates.Clear();
        }
Пример #2
0
        private void Read(Lazy <ODataReader> lazyReader)
        {
            foreach (var state in this.expectedStates)
            {
                lazyReader.Value.Read();
                ExceptionUtilities.Assert(lazyReader.Value.State == state, "Expected %1, Found %2", state, lazyReader.Value.State);
                switch (state)
                {
                case ODataReaderState.FeedStart:
                    if (readItems.Count > 0)
                    {
                        ODataNavigationLink navLink = (ODataNavigationLink)readItems.Peek();
                        var annotation = navLink.GetAnnotation <ODataNavigationLinkExpandedItemObjectModelAnnotation>();
                        if (annotation == null)
                        {
                            annotation = new ODataNavigationLinkExpandedItemObjectModelAnnotation();
                            navLink.SetAnnotation(annotation);
                        }

                        annotation.ExpandedItem = lazyReader.Value.Item;
                    }

                    readItems.Push(lazyReader.Value.Item);
                    break;

                case ODataReaderState.EntryStart:
                    var currentEntry = (ODataEntry)lazyReader.Value.Item;
                    if (readItems.Count > 0)
                    {
                        ODataFeed feed = readItems.Peek() as ODataFeed;
                        if (feed != null)
                        {
                            var annotation = feed.GetAnnotation <ODataFeedEntriesObjectModelAnnotation>();
                            if (annotation == null)
                            {
                                annotation = new ODataFeedEntriesObjectModelAnnotation();
                                feed.SetAnnotation(annotation);
                            }
                            annotation.Add((ODataEntry)lazyReader.Value.Item);
                        }
                        else
                        {
                            ODataNavigationLink navLink = (ODataNavigationLink)readItems.Peek();
                            var annotation = navLink.GetAnnotation <ODataNavigationLinkExpandedItemObjectModelAnnotation>();
                            if (annotation == null)
                            {
                                annotation = new ODataNavigationLinkExpandedItemObjectModelAnnotation();
                                navLink.SetAnnotation(annotation);
                            }

                            annotation.ExpandedItem = currentEntry;
                        }
                    }

                    readItems.Push(currentEntry);
                    break;

                case ODataReaderState.NavigationLinkStart:
                    ODataEntry entry = (ODataEntry)readItems.Peek();
                    var        navLinksAnnotation = entry.GetAnnotation <ODataEntryNavigationLinksObjectModelAnnotation>();
                    if (navLinksAnnotation == null)
                    {
                        navLinksAnnotation = new ODataEntryNavigationLinksObjectModelAnnotation();
                        entry.SetAnnotation(navLinksAnnotation);
                    }

                    navLinksAnnotation.Add((ODataNavigationLink)lazyReader.Value.Item, entry.Properties.Count() + navLinksAnnotation.Count);
                    readItems.Push(lazyReader.Value.Item);
                    break;

                case ODataReaderState.EntryEnd:
                case ODataReaderState.FeedEnd:
                case ODataReaderState.NavigationLinkEnd:
                    if (readItems.Count() > 1)
                    {
                        readItems.Pop();
                    }
                    break;
                }
            }

            this.expectedStates.Clear();
        }
        /// <summary>
        /// Visits an entity instance: creates a new ODataEntry instance, collects and sets all the properties,
        /// calls ODataWriter.WriteStart(), then visits the navigation properties and calls ODataWriter.WriteEnd()
        /// </summary>
        /// <param name="payloadElement">The entity instance to write.</param>
        public override void Visit(EntityInstance payloadElement)
        {
            // create an ODataEntry and write it
            string editLinkString = payloadElement.GetEditLink();
            string selfLinkString = payloadElement.GetSelfLink();
            string entryId = payloadElement.Id;

            var entry = new ODataEntry()
            {
                Id = string.IsNullOrEmpty(entryId) ? null : new Uri(entryId),
                ETag = payloadElement.ETag,
                EditLink = string.IsNullOrEmpty(editLinkString) ? null : new Uri(editLinkString),
                ReadLink = string.IsNullOrEmpty(selfLinkString) ? null : new Uri(selfLinkString),
                TypeName = payloadElement.FullTypeName,
                Properties = new List<ODataProperty>(),
                SerializationInfo = new ODataFeedAndEntrySerializationInfo()
                {
                    NavigationSourceEntityTypeName = payloadElement.FullTypeName,
                    NavigationSourceName = "MySet",
                    ExpectedTypeName = payloadElement.FullTypeName
                }
            };

            if (payloadElement.IsMediaLinkEntry())
            {
                ODataStreamReferenceValue mediaResource = new ODataStreamReferenceValue();
                mediaResource.ContentType = payloadElement.StreamContentType;
                mediaResource.ETag = payloadElement.StreamETag;

                mediaResource.ReadLink = new Uri(payloadElement.StreamSourceLink);
                mediaResource.EditLink = new Uri(payloadElement.StreamEditLink);

                entry.MediaResource = mediaResource;
            }
            
            // TODO: add support for custom extensions at some point
            AddEntryMetadata(payloadElement, entry);

            if (this.items.Count > 0)
            {
                var parent = this.items.Peek();
                var navLink = parent as ODataNavigationLink;
                if (navLink != null)
                {
                    navLink.SetAnnotation(new ODataNavigationLinkExpandedItemObjectModelAnnotation() { ExpandedItem = entry });
                }
                else
                {
                    var feed = parent as ODataFeed;
                    ExceptionUtilities.CheckObjectNotNull(feed, "Feed was expected");
                    ODataFeedEntriesObjectModelAnnotation annotation = feed.GetAnnotation<ODataFeedEntriesObjectModelAnnotation>();
                    if (annotation == null)
                    {
                        annotation = new ODataFeedEntriesObjectModelAnnotation();
                        feed.SetAnnotation(annotation);
                    }
                    annotation.Add(entry);
                }
            }

            //Initialise stuff for the entry
            var originalPosition = this.currentPropertyPosition;
            this.currentPropertyPosition = 0;

            try
            {
                this.items.Push(entry);
                // visit the properties (incl. navigation properties and named stream properties)
                base.Visit(payloadElement);
            }
            finally
            {
                entry = (ODataEntry) this.items.Pop();
            }

            //Return to original values
            this.currentPropertyPosition = originalPosition;

            //If we are at the top level push this to items to make it the result.
            if (this.items.Count == 0)
            {
                this.items.Push(entry);
            }
        }
            /// <summary>
            /// Read a feed with an <see cref="ODataReader"/> positioned on the feed start.
            /// </summary>
            /// <param name="reader">The <see cref="ODataReader"/> to use for reading the feed.</param>
            /// <returns>An <see cref="ODataFeed"/>, possibly with annotations.</returns>
            private ODataFeed ReadFeed(ODataReader reader)
            {
                this.assert.AreEqual(ODataReaderState.FeedStart, reader.State, "Reader states don't match.");

                ODataFeed feed = (ODataFeed)reader.Item;
                ODataFeedPayloadOrderObjectModelAnnotation payloadOrderItems = this.storePayloadOrder ? new ODataFeedPayloadOrderObjectModelAnnotation() : null;
                ODataFeedEntriesObjectModelAnnotation feedEntries = new ODataFeedEntriesObjectModelAnnotation();

                AddFeedPayloadOrderItems(feed, payloadOrderItems);
                if (payloadOrderItems != null)
                {
                    payloadOrderItems.AddStartFeed();
                }

                while (reader.Read())
                {
                    if (reader.State == ODataReaderState.EntryStart)
                    {
                        AddFeedPayloadOrderItems(feed, payloadOrderItems);

                        ODataEntry entry = this.ReadEntry(reader);
                        if (payloadOrderItems != null)
                        {
                            payloadOrderItems.AddEntry(entry);
                        }

                        feedEntries.Add(entry);
                    }
                    else if (reader.State == ODataReaderState.FeedEnd)
                    {
                        // we are done reading the feed
                        break;
                    }
                    else
                    {
                        this.assert.Fail("Unexpected reader state '" + reader.State + "' for reading a feed.");
                    }
                }

                this.assert.AreEqual(ODataReaderState.FeedEnd, reader.State, "Reader states don't match.");
                ODataFeed feedAtEnd = (ODataFeed)reader.Item;
                this.assert.AreSame(feed, feedAtEnd, "Expected the same feed instance.");

                AddFeedPayloadOrderItems(feed, payloadOrderItems);

                feed.SetAnnotation(feedEntries);
                if (this.storePayloadOrder)
                {
                    feed.SetAnnotation(payloadOrderItems);
                }

                return feed;
            }
Пример #5
0
        /// <summary>
        /// Visits an entity instance: creates a new ODataResource instance, collects and sets all the properties,
        /// calls ODataWriter.WriteStart(), then visits the navigation properties and calls ODataWriter.WriteEnd()
        /// </summary>
        /// <param name="payloadElement">The entity instance to write.</param>
        public override void Visit(EntityInstance payloadElement)
        {
            // create an ODataResource and write it
            string editLinkString = payloadElement.GetEditLink();
            string selfLinkString = payloadElement.GetSelfLink();
            string entryId        = payloadElement.Id;

            var entry = new ODataResource()
            {
                Id                = string.IsNullOrEmpty(entryId) ? null : new Uri(entryId),
                ETag              = payloadElement.ETag,
                EditLink          = string.IsNullOrEmpty(editLinkString) ? null : new Uri(editLinkString),
                ReadLink          = string.IsNullOrEmpty(selfLinkString) ? null : new Uri(selfLinkString),
                TypeName          = payloadElement.FullTypeName,
                Properties        = new List <ODataProperty>(),
                SerializationInfo = new ODataResourceSerializationInfo()
                {
                    NavigationSourceEntityTypeName = payloadElement.FullTypeName,
                    NavigationSourceName           = "MySet",
                    ExpectedTypeName = payloadElement.FullTypeName
                }
            };

            if (payloadElement.IsMediaLinkEntry())
            {
                ODataStreamReferenceValue mediaResource = new ODataStreamReferenceValue();
                mediaResource.ContentType = payloadElement.StreamContentType;
                mediaResource.ETag        = payloadElement.StreamETag;

                mediaResource.ReadLink = new Uri(payloadElement.StreamSourceLink);
                mediaResource.EditLink = new Uri(payloadElement.StreamEditLink);

                entry.MediaResource = mediaResource;
            }

            if (this.items.Count > 0)
            {
                var parent  = this.items.Peek();
                var navLink = parent as ODataNestedResourceInfo;
                if (navLink != null)
                {
                    navLink.SetAnnotation(new ODataNavigationLinkExpandedItemObjectModelAnnotation()
                    {
                        ExpandedItem = entry
                    });
                }
                else
                {
                    var feed = parent as ODataResourceSet;
                    ExceptionUtilities.CheckObjectNotNull(feed, "Feed was expected");
                    ODataFeedEntriesObjectModelAnnotation annotation = feed.GetAnnotation <ODataFeedEntriesObjectModelAnnotation>();
                    if (annotation == null)
                    {
                        annotation = new ODataFeedEntriesObjectModelAnnotation();
                        feed.SetAnnotation(annotation);
                    }
                    annotation.Add(entry);
                }
            }

            //Initialise stuff for the entry
            var originalPosition = this.currentPropertyPosition;

            this.currentPropertyPosition = 0;

            try
            {
                this.items.Push(entry);
                // visit the properties (incl. navigation properties and named stream properties)
                base.Visit(payloadElement);
            }
            finally
            {
                entry = (ODataResource)this.items.Pop();
            }

            //Return to original values
            this.currentPropertyPosition = originalPosition;

            //If we are at the top level push this to items to make it the result.
            if (this.items.Count == 0)
            {
                this.items.Push(entry);
            }
        }