Пример #1
0
 private IEnumerable<ODataEntityReferenceLink> GetLinksCollection(IEnumerator elements, bool hasMoved, ODataEntityReferenceLinks linksCollection)
 {
     object lastObject = null;
     IExpandedResult skipTokenExpandedResult = null;
 Label_PostSwitchInIterator:;
     if (hasMoved)
     {
         object current = elements.Current;
         IExpandedResult skipToken = null;
         if (current != null)
         {
             IExpandedResult expanded = current as IExpandedResult;
             if (expanded != null)
             {
                 current = Serializer.GetExpandedElement(expanded);
                 skipToken = this.GetSkipToken(expanded);
             }
         }
         this.IncrementSegmentResultCount();
         ODataEntityReferenceLink iteratorVariable4 = new ODataEntityReferenceLink {
             Url = this.GetEntityUri(current)
         };
         yield return iteratorVariable4;
         hasMoved = elements.MoveNext();
         lastObject = current;
         skipTokenExpandedResult = skipToken;
         goto Label_PostSwitchInIterator;
     }
     if (this.NeedNextPageLink(elements))
     {
         linksCollection.NextPageLink = this.GetNextLinkUri(lastObject, skipTokenExpandedResult, this.RequestDescription.ResultUri);
     }
 }
Пример #2
0
 internal static void ValidateEntityReferenceLinkNotNull(ODataEntityReferenceLink entityReferenceLink)
 {
     if (entityReferenceLink == null)
     {
         throw new ODataException(Microsoft.Data.OData.Strings.WriterValidationUtils_EntityReferenceLinksLinkMustNotBeNull);
     }
 }
Пример #3
0
 private void WriteLink(object element)
 {
     base.IncrementSegmentResultCount();
     ODataEntityReferenceLink link = new ODataEntityReferenceLink {
         Url = this.GetEntityUri(element)
     };
     this.writer.WriteEntityReferenceLink(link);
 }
 private void WriteEntityReferenceLinkImplementation(ODataEntityReferenceLink entityReferenceLink)
 {
     WriterValidationUtils.ValidateEntityReferenceLink(entityReferenceLink);
     base.JsonWriter.StartObjectScope();
     base.JsonWriter.WriteName("uri");
     base.JsonWriter.WriteValue(base.UriToAbsoluteUriString(entityReferenceLink.Url));
     base.JsonWriter.EndObjectScope();
 }
        /// <summary>
        /// Validates an entity reference link.
        /// </summary>
        /// <param name="link">The entity reference link to check.</param>
        internal static void ValidateEntityReferenceLink(ODataEntityReferenceLink link)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(link != null, "link != null");

            if (link.Url == null)
            {
                throw new ODataException(Strings.ReaderValidationUtils_EntityReferenceLinkMissingUri);
            }
        }
 private void WriteEntityReferenceLink(ODataEntityReferenceLink entityReferenceLink, bool isTopLevel)
 {
     WriterValidationUtils.ValidateEntityReferenceLink(entityReferenceLink);
     string ns = (base.UseClientFormatBehavior && isTopLevel) ? "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" : base.MessageWriterSettings.WriterBehavior.ODataNamespace;
     base.XmlWriter.WriteStartElement(string.Empty, "uri", ns);
     if (isTopLevel)
     {
         base.XmlWriter.WriteAttributeString("xmlns", ns);
     }
     base.XmlWriter.WriteString(base.UriToUrlAttributeValue(entityReferenceLink.Url));
     base.XmlWriter.WriteEndElement();
 }
        /// <inheritdoc/>
        public override void WriteObject(object graph, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
        {
            if (messageWriter == null)
            {
                throw Error.ArgumentNull("messageWriter");
            }

            if (writeContext == null)
            {
                throw Error.ArgumentNull("writeContext");
            }

            IEdmEntitySet entitySet = writeContext.EntitySet;
            if (entitySet == null)
            {
                throw new SerializationException(SRResources.EntitySetMissingDuringSerialization);
            }

            if (writeContext.Path == null)
            {
                throw new SerializationException(SRResources.ODataPathMissing);
            }

            IEdmNavigationProperty navigationProperty = writeContext.Path.GetNavigationProperty();
            if (navigationProperty == null)
            {
                throw new SerializationException(SRResources.NavigationPropertyMissingDuringSerialization);
            }

            if (graph != null)
            {
                ODataEntityReferenceLink entityReferenceLink = graph as ODataEntityReferenceLink;
                if (entityReferenceLink == null)
                {
                    Uri uri = graph as Uri;
                    if (uri == null)
                    {
                        throw new SerializationException(Error.Format(SRResources.CannotWriteType, GetType().Name, graph.GetType().FullName));
                    }

                    entityReferenceLink = new ODataEntityReferenceLink { Url = uri };
                }

                messageWriter.WriteEntityReferenceLink(entityReferenceLink, entitySet, navigationProperty);
            }
        }
Пример #8
0
        /// <summary>
        /// Write an entity reference link.
        /// </summary>
        /// <param name="parentNavigationLink">The parent navigation link which is being written around the entity reference link.</param>
        /// <param name="entityReferenceLink">The entity reference link to write.</param>
        protected override void WriteEntityReferenceInNavigationLinkContent(ODataNavigationLink parentNavigationLink, ODataEntityReferenceLink entityReferenceLink)
        {
            Debug.Assert(parentNavigationLink != null, "parentNavigationLink != null");
            Debug.Assert(entityReferenceLink != null, "entityReferenceLink != null");
            Debug.Assert(entityReferenceLink.Url != null, "We should have already verifies that the Url specified on the entity reference link is not null.");

            this.WriteNavigationLinkStart(parentNavigationLink, entityReferenceLink.Url);
            this.WriteNavigationLinkEnd();
        }
Пример #9
0
 internal override Task WriteEntityReferenceLinkAsync(ODataEntityReferenceLink link)
 {
     return TaskUtils.GetTaskForSynchronousOperationReturningTask(delegate {
         this.WriteEntityReferenceLinkImplementation(link);
         return this.FlushAsync();
     });
 }
Пример #10
0
 private void ReadAtNonExpandedNavigatLinkStart()
 {
     ODataNavigationLink currentNavigationLink = base.CurrentNavigationLink;
     IODataAtomReaderEntryState linkParentEntityScope = (IODataAtomReaderEntryState) base.LinkParentEntityScope;
     linkParentEntityScope.DuplicatePropertyNamesChecker.CheckForDuplicatePropertyNames(currentNavigationLink, false, currentNavigationLink.IsCollection);
     if (this.atomInputContext.ReadingResponse)
     {
         AtomScope currentScope = (AtomScope) base.CurrentScope;
         IEdmNavigationProperty navigationProperty = currentScope.NavigationProperty;
         if (((currentNavigationLink.IsCollection == false) && (navigationProperty != null)) && navigationProperty.Type.IsCollection())
         {
             throw new ODataException(Microsoft.Data.OData.Strings.ODataAtomReader_DeferredEntryInFeedNavigationLink);
         }
         this.ReplaceScope(ODataReaderState.NavigationLinkEnd);
     }
     else
     {
         ODataEntityReferenceLink item = new ODataEntityReferenceLink {
             Url = currentNavigationLink.Url
         };
         this.EnterScope(ODataReaderState.EntityReferenceLink, item, null);
     }
 }
Пример #11
0
 protected override void WriteEntityReferenceInNavigationLinkContent(ODataNavigationLink parentNavigationLink, ODataEntityReferenceLink entityReferenceLink)
 {
     this.WriteNavigationLinkStart(parentNavigationLink, entityReferenceLink.Url);
     this.WriteNavigationLinkEnd();
 }
        public void TestWriteEntityReferenceLink_InJsonLight_WithEntityAndNavigationProperty_DoesNotThrow()
        {
            // Arrange
            IODataResponseMessage response = CreateResponse();
            ODataMessageWriterSettings settings = CreateJsonLightSettings();
            IEdmModel model = CreateModel();
            ODataEntityReferenceLink link = new ODataEntityReferenceLink
            {
                Url = CreateFakeUri()
            };
            IEdmEntitySet entitySet = model.EntityContainers().Single().EntitySets().First();
            IEdmNavigationProperty navigationProperty =
                model.EntityContainers().Single().EntitySets().First().NavigationTargets.First().NavigationProperty;

            using (ODataMessageWriter writer = new ODataMessageWriter(response, settings, model))
            {
                // Act & Assert
                Assert.DoesNotThrow(() => writer.WriteEntityReferenceLink(link, entitySet, navigationProperty));
            }
        }
Пример #13
0
        /// <summary>
        /// Validates an entity reference link instance.
        /// </summary>
        /// <param name="entityReferenceLink">The entity reference link to validate.</param>
        internal static void ValidateEntityReferenceLink(ODataEntityReferenceLink entityReferenceLink)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(entityReferenceLink != null, "entityReferenceLink != null");

            if (entityReferenceLink.Url == null)
            {
                throw new ODataException(Strings.WriterValidationUtils_EntityReferenceLinkUrlMustNotBeNull);
            }
        }
Пример #14
0
 /// <summary>
 /// Write an entity reference link into a navigation link content.
 /// </summary>
 /// <param name="parentNavigationLink">The parent navigation link which is being written around the entity reference link.</param>
 /// <param name="entityReferenceLink">The entity reference link to write.</param>
 protected abstract void WriteEntityReferenceInNavigationLinkContent(ODataNavigationLink parentNavigationLink, ODataEntityReferenceLink entityReferenceLink);
Пример #15
0
        /// <summary>
        /// Write an entity reference link.
        /// </summary>
        /// <param name="entityReferenceLink">The entity reference link to write.</param>
        private void WriteEntityReferenceLinkImplementation(ODataEntityReferenceLink entityReferenceLink)
        {
            Debug.Assert(entityReferenceLink != null, "entityReferenceLink != null");

            if (this.outputContext.WritingResponse)
            {
                this.ThrowODataException(Strings.ODataWriterCore_EntityReferenceLinkInResponse, null);
            }

            this.CheckForNavigationLinkWithContent(ODataPayloadKind.EntityReferenceLink);
            Debug.Assert(
                this.CurrentScope.Item is ODataNavigationLink,
                "The CheckForNavigationLinkWithContent should have verified that entity reference link can only be written inside a navigation link.");

            if (!this.SkipWriting)
            {
                this.InterceptException(() =>
                {
                    WriterValidationUtils.ValidateEntityReferenceLink(entityReferenceLink);
                    this.WriteEntityReferenceInNavigationLinkContent((ODataNavigationLink)this.CurrentScope.Item, entityReferenceLink);
                });
            }
        }
Пример #16
0
 /// <summary>
 /// Writes an entity reference link, which is used to represent binding to an existing resource in a request payload.
 /// </summary>
 /// <param name="entityReferenceLink">The entity reference link to write.</param>
 /// <remarks>
 /// This method can only be called for writing request messages. The entity reference link must be surrounded
 /// by a navigation link written through WriteStart/WriteEnd.
 /// The <see cref="ODataNavigationLink.Url"/> will be ignored in that case and the Uri from the <see cref="ODataEntityReferenceLink.Url"/> will be used
 /// as the binding URL to be written.
 /// </remarks>
 public sealed override void WriteEntityReferenceLink(ODataEntityReferenceLink entityReferenceLink)
 {
     this.VerifyCanWriteEntityReferenceLink(entityReferenceLink, true);
     this.WriteEntityReferenceLinkImplementation(entityReferenceLink);
 }
Пример #17
0
 public abstract void WriteEntityReferenceLink(ODataEntityReferenceLink entityReferenceLink);
Пример #18
0
 internal override void WriteEntityReferenceLink(ODataEntityReferenceLink link)
 {
     this.WriteEntityReferenceLinkImplementation(link);
     this.Flush();
 }
Пример #19
0
 internal virtual Task WriteEntityReferenceLinkAsync(ODataEntityReferenceLink link)
 {
     throw this.CreatePayloadKindNotSupportedException(ODataPayloadKind.EntityReferenceLink);
 }
        private void WriteLink(ODataWriter entryWriter, Microsoft.Data.OData.ODataEntry entry, string linkName, IEnumerable<ReferenceLink> links)
        {
            var navigationProperty = (_model.FindDeclaredType(entry.TypeName) as IEdmEntityType).NavigationProperties()
                .BestMatch(x => x.Name, linkName, _session.Pluralizer);
            bool isCollection = navigationProperty.Type.Definition.TypeKind == EdmTypeKind.Collection;

            var linkType = GetNavigationPropertyEntityType(navigationProperty);
            var linkTypeWithKey = linkType;
            while (linkTypeWithKey.DeclaredKey == null && linkTypeWithKey.BaseEntityType() != null)
            {
                linkTypeWithKey = linkTypeWithKey.BaseEntityType();
            }

            entryWriter.WriteStart(new ODataNavigationLink()
            {
                Name = linkName,
                IsCollection = isCollection,
                Url = new Uri(ODataNamespace.Related + linkType, UriKind.Absolute),
            });

            foreach (var referenceLink in links)
            {
                var linkKey = linkTypeWithKey.DeclaredKey;
                var linkEntry = referenceLink.LinkData.ToDictionary();
                var contentId = GetContentId(referenceLink);
                string linkUri;
                if (contentId != null)
                {
                    linkUri = "$" + contentId;
                }
                else
                {
                    bool isSingleton;
                    var formattedKey = _session.Adapter.GetCommandFormatter().ConvertKeyValuesToUriLiteral(
                        linkKey.ToDictionary(x => x.Name, x => linkEntry[x.Name]), true);
                    var linkedCollectionName = _session.Metadata.GetLinkedCollectionName(
                        referenceLink.LinkData.GetType().Name, linkTypeWithKey.Name, out isSingleton);
                    linkUri = linkedCollectionName + (isSingleton ? string.Empty : formattedKey);
                }
                var link = new ODataEntityReferenceLink
                {
                    Url = Utils.CreateAbsoluteUri(_session.Settings.BaseUri.AbsoluteUri, linkUri)
                };

                entryWriter.WriteEntityReferenceLink(link);
            }

            entryWriter.WriteEnd();
        }
Пример #21
0
 public abstract Task WriteEntityReferenceLinkAsync(ODataEntityReferenceLink entityReferenceLink);
Пример #22
0
 private void WriteEntityReferenceLinkImplementation(ODataEntityReferenceLink link)
 {
     new ODataJsonEntityReferenceLinkSerializer(this).WriteEntityReferenceLink(link);
 }
Пример #23
0
 /// <summary>
 /// Asynchronously writes an entity reference link, which is used to represent binding to an existing resource in a request payload.
 /// </summary>
 /// <param name="entityReferenceLink">The entity reference link to write.</param>
 /// <returns>A task instance that represents the asynchronous write operation.</returns>
 /// <remarks>
 /// This method can only be called for writing request messages. The entity reference link must be surrounded
 /// by a navigation link written through WriteStart/WriteEnd.
 /// The <see cref="ODataNavigationLink.Url"/> will be ignored in that case and the Uri from the <see cref="ODataEntityReferenceLink.Url"/> will be used
 /// as the binding URL to be written.
 /// </remarks>
 public sealed override Task WriteEntityReferenceLinkAsync(ODataEntityReferenceLink entityReferenceLink)
 {
     this.VerifyCanWriteEntityReferenceLink(entityReferenceLink, false);
     return TaskUtils.GetTaskForSynchronousOperation(() => this.WriteEntityReferenceLinkImplementation(entityReferenceLink));
 }
Пример #24
0
 internal void WriteEntityReferenceLink(LinkDescriptor binding, ODataRequestMessageWrapper requestMessage)
 {
     using (ODataMessageWriter writer = CreateMessageWriter(requestMessage, this.requestInfo))
     {
         Uri resourceUri;
         EntityDescriptor entityDescriptor = this.requestInfo.EntityTracker.GetEntityDescriptor(binding.Target);
         if (entityDescriptor.GetLatestIdentity() != null)
         {
             resourceUri = entityDescriptor.GetResourceUri(this.requestInfo.BaseUriResolver, false);
         }
         else
         {
             resourceUri = Util.CreateUri("$" + entityDescriptor.ChangeOrder.ToString(CultureInfo.InvariantCulture), UriKind.Relative);
         }
         ODataEntityReferenceLink link = new ODataEntityReferenceLink {
             Url = resourceUri
         };
         writer.WriteEntityReferenceLink(link);
     }
 }
Пример #25
0
        /// <summary>
        /// Verifies that calling WriteEntityReferenceLink is valid.
        /// </summary>
        /// <param name="entityReferenceLink">The entity reference link to write.</param>
        /// <param name="synchronousCall">true if the call is to be synchronous; false otherwise.</param>
        private void VerifyCanWriteEntityReferenceLink(ODataEntityReferenceLink entityReferenceLink, bool synchronousCall)
        {
            ExceptionUtils.CheckArgumentNotNull(entityReferenceLink, "entityReferenceLink");

            this.VerifyNotDisposed();
            this.VerifyCallAllowed(synchronousCall);
        }
Пример #26
0
 internal void WriteNavigationLink(EntityDescriptor entityDescriptor, IEnumerable<LinkDescriptor> relatedLinks, ODataWriter odataWriter)
 {
     ClientTypeAnnotation clientTypeAnnotation = null;
     foreach (LinkDescriptor descriptor in relatedLinks)
     {
         descriptor.ContentGeneratedForSave = true;
         if (clientTypeAnnotation == null)
         {
             ClientEdmModel model = ClientEdmModel.GetModel(this.requestInfo.MaxProtocolVersion);
             clientTypeAnnotation = model.GetClientTypeAnnotation(model.GetOrCreateEdmType(entityDescriptor.Entity.GetType()));
         }
         ODataNavigationLink navigationLink = new ODataNavigationLink {
             Url = this.requestInfo.EntityTracker.GetEntityDescriptor(descriptor.Target).GetLatestEditLink(),
             IsCollection = new bool?(clientTypeAnnotation.GetProperty(descriptor.SourceProperty, false).IsEntityCollection),
             Name = descriptor.SourceProperty
         };
         odataWriter.WriteStart(navigationLink);
         ODataEntityReferenceLink entityReferenceLink = new ODataEntityReferenceLink {
             Url = navigationLink.Url
         };
         odataWriter.WriteEntityReferenceLink(entityReferenceLink);
         odataWriter.WriteEnd();
     }
 }
        public void TestWriteEntityReferenceLink_InJsonLight_WithoutEntitySetOrNavigationProperty_Throws()
        {
            // Arrange
            IODataResponseMessage response = CreateResponse();
            ODataMessageWriterSettings settings = CreateJsonLightSettings();
            IEdmModel model = CreateModel();
            ODataEntityReferenceLink link = new ODataEntityReferenceLink
            {
                Url = CreateFakeUri()
            };

            using (ODataMessageWriter writer = new ODataMessageWriter(response, settings, model))
            {
                // Act & Assert
                Assert.Throws<ODataException>(() => writer.WriteEntityReferenceLink(link));
            }
        }
        private void WriteLink(ODataWriter entryWriter, Microsoft.Data.OData.ODataEntry entry, string linkName, IEnumerable<ReferenceLink> links)
        {
            var navigationProperty = (_model.FindDeclaredType(entry.TypeName) as IEdmEntityType).NavigationProperties()
                .BestMatch(x => x.Name, linkName, _session.Pluralizer);
            bool isCollection = navigationProperty.Type.Definition.TypeKind == EdmTypeKind.Collection;

            var linkType = GetNavigationPropertyEntityType(navigationProperty);
            var linkTypeWithKey = linkType;
            while (linkTypeWithKey.DeclaredKey == null && linkTypeWithKey.BaseEntityType() != null)
            {
                linkTypeWithKey = linkTypeWithKey.BaseEntityType();
            }

            entryWriter.WriteStart(new ODataNavigationLink()
            {
                Name = linkName,
                IsCollection = isCollection,
                Url = new Uri("http://schemas.microsoft.com/ado/2007/08/dataservices/related/" + linkType, UriKind.Absolute),
            });

            foreach (var referenceLink in links)
            {
                var linkKey = linkTypeWithKey.DeclaredKey;
                var linkEntry = referenceLink.LinkData.ToDictionary();
                var contentId = GetContentId(referenceLink);
                string linkUri;
                if (contentId != null)
                {
                    linkUri = "$" + contentId;
                }
                else
                {
                    var linkSet = _model.EntityContainers()
                        .SelectMany(x => x.EntitySets())
                        .BestMatch(x => x.ElementType.Name, linkTypeWithKey.Name, _session.Pluralizer);
                    var formattedKey = _session.Adapter.GetCommandFormatter().ConvertKeyValuesToUriLiteral(
                        linkKey.ToDictionary(x => x.Name, x => linkEntry[x.Name]), true);
                    linkUri = linkSet.Name + formattedKey;
                }
                var link = new ODataEntityReferenceLink
                {
                    Url = Utils.CreateAbsoluteUri(_session.Settings.BaseUri.AbsoluteUri, linkUri)
                };

                entryWriter.WriteEntityReferenceLink(link);
            }

            entryWriter.WriteEnd();
        }
 /// <summary>
 /// Initializes a new instance of <see cref="ODataEntityReferenceLinkBase"/>.
 /// </summary>
 /// <param name="item">The wrapped item.</param>
 public ODataEntityReferenceLinkBase(ODataEntityReferenceLink item)
     : base(item)
 {
 }
#pragma warning disable 1998
        protected override async Task<Stream> WriteLinkContentAsync(string linkIdent)
        {
            var message = new ODataRequestMessage();
            using (var messageWriter = new ODataMessageWriter(message, GetWriterSettings(), _model))
            {
                var link = new ODataEntityReferenceLink { Url = Utils.CreateAbsoluteUri(_session.Settings.BaseUri.AbsoluteUri, linkIdent) };
                messageWriter.WriteEntityReferenceLink(link);

#if SILVERLIGHT
                return message.GetStream();
#else
                return await message.GetStreamAsync();
#endif
            }
        }