/// <summary> /// Updates the entity decriptor data with values from response headers /// </summary> /// <param name="entityDescriptorData">The descriptor data to update</param> /// <param name="headers">The response headers</param> public static void UpdateFromHeaders(this EntityDescriptorData entityDescriptorData, IDictionary <string, string> headers) { ExceptionUtilities.CheckArgumentNotNull(entityDescriptorData, "entityDescriptorData"); ExceptionUtilities.CheckArgumentNotNull(headers, "headers"); string etag; headers.TryGetValue(HttpHeaders.ETag, out etag); entityDescriptorData.SetETag(etag); // we do want to set null if the header is not present string location; if (headers.TryGetValue(HttpHeaders.Location, out location)) { entityDescriptorData.SetEditLink(new Uri(location)); } string dataServiceId; if (headers.TryGetValue(HttpHeaders.DataServiceId, out dataServiceId)) { entityDescriptorData.SetIdentity(new Uri(dataServiceId, UriKind.RelativeOrAbsolute)); } }
/// <summary> /// Updates the stream decriptor data with values from a response payload /// </summary> /// <param name="entityDescriptorData">The descriptor data to update</param> /// <param name="entityData">A representation of the payload</param> /// <param name="contextBaseUri">The base uri of the context</param> public static void UpdateFromPayload(this EntityDescriptorData entityDescriptorData, EntityInstance entityData, Uri contextBaseUri) { ExceptionUtilities.CheckArgumentNotNull(entityDescriptorData, "entityDescriptorData"); ExceptionUtilities.CheckArgumentNotNull(entityData, "entityData"); var xmlBase = entityData.Annotations.OfType <XmlBaseAnnotation>().SingleOrDefault(); entityDescriptorData.SetIdentity(GetAbsoluteUriForLink(contextBaseUri, xmlBase, entityData.Id)); entityDescriptorData.SetETag(entityData.ETag); entityDescriptorData.SetServerTypeName(entityData.FullTypeName); var editLink = entityData.EditLink; if (editLink != null) { entityDescriptorData.SetEditLink(GetAbsoluteUriForLink(contextBaseUri, xmlBase, editLink)); } var selfLink = entityData.Annotations.OfType <SelfLinkAnnotation>().SingleOrDefault(); if (selfLink != null) { entityDescriptorData.SetSelfLink(GetAbsoluteUriForLink(contextBaseUri, xmlBase, selfLink.Value)); } foreach (var navigation in entityData.Properties.OfType <NavigationPropertyInstance>()) { var linkInfoData = entityDescriptorData.LinkInfos.SingleOrDefault(l => l.Name == navigation.Name); if (linkInfoData == null) { linkInfoData = entityDescriptorData.CreateLinkInfoData(navigation.Name); } if (navigation.AssociationLink != null) { linkInfoData.RelationshipLink = GetAbsoluteUriForLink(contextBaseUri, xmlBase, navigation.AssociationLink.UriString); } string navigationLink = null; if (navigation.Value != null) { if (navigation.IsExpanded) { navigationLink = ((ExpandedLink)navigation.Value).UriString; } else { navigationLink = ((DeferredLink)navigation.Value).UriString; } } if (navigationLink != null) { linkInfoData.NavigationLink = GetAbsoluteUriForLink(contextBaseUri, xmlBase, navigationLink); } } entityDescriptorData.RemoveOperationDescriptorData(); foreach (var operation in entityData.ServiceOperationDescriptors) { entityDescriptorData.CreateOperationDescriptorData( new Uri(operation.Metadata, UriKind.RelativeOrAbsolute), new Uri(operation.Target, UriKind.RelativeOrAbsolute), operation.Title, operation.IsAction); } if (entityData.IsMediaLinkEntry()) { if (entityData.StreamSourceLink != null) { entityDescriptorData.ReadStreamUri = GetAbsoluteUriForLink(contextBaseUri, xmlBase, entityData.StreamSourceLink); } if (entityData.StreamEditLink != null) { entityDescriptorData.EditStreamUri = GetAbsoluteUriForLink(contextBaseUri, xmlBase, entityData.StreamEditLink); } entityDescriptorData.StreamETag = entityData.StreamETag; } foreach (var namedStream in entityData.Properties.OfType <NamedStreamInstance>()) { var streamDescriptor = entityDescriptorData.StreamDescriptors.SingleOrDefault(n => n.Name == namedStream.Name); if (streamDescriptor == null) { streamDescriptor = entityDescriptorData.CreateStreamDescriptorData(EntityStates.Unchanged, 0, namedStream.Name); } // apply self link's content type first, as the value from the edit link should 'win' if (namedStream.SourceLink != null) { streamDescriptor.SelfLink = GetAbsoluteUriForLink(contextBaseUri, xmlBase, namedStream.SourceLink); streamDescriptor.ContentType = namedStream.SourceLinkContentType; } if (namedStream.EditLink != null) { streamDescriptor.EditLink = GetAbsoluteUriForLink(contextBaseUri, xmlBase, namedStream.EditLink); streamDescriptor.ContentType = namedStream.EditLinkContentType; } streamDescriptor.ETag = namedStream.ETag; } // TODO: update property values from payload }