/// <summary> /// Computes all projected or missing stream properties. /// </summary> /// <param name="nonComputedProperties">Non-computed properties from the entity.</param> /// <returns>The the computed stream properties for the entry.</returns> private IEnumerable <ODataProperty> GetComputedStreamProperties(IEnumerable <ODataProperty> nonComputedProperties) { if (this.computedStreamProperties == null) { // Remove all the projected properties that were already read from the payload IDictionary <string, IEdmStructuralProperty> projectedStreamProperties = this.entryMetadataContext.SelectedStreamProperties; if (nonComputedProperties != null) { foreach (ODataProperty payloadProperty in nonComputedProperties) { projectedStreamProperties.Remove(payloadProperty.Name); } } this.computedStreamProperties = new List <ODataProperty>(); if (projectedStreamProperties.Count > 0) { // Create all the missing stream properties and set the metadata builder foreach (string missingStreamPropertyName in projectedStreamProperties.Keys) { ODataStreamReferenceValue streamPropertyValue = new ODataStreamReferenceValue(); streamPropertyValue.SetMetadataBuilder(this, missingStreamPropertyName); this.computedStreamProperties.Add(new ODataProperty { Name = missingStreamPropertyName, Value = streamPropertyValue }); } } } return(this.computedStreamProperties); }
internal override ODataProperty GetNextUnprocessedStreamProperty() { if (this.unprocessedStreamProperties == null) { Debug.Assert(this.ResourceMetadataContext != null, "this.resourceMetadataContext != null"); this.unprocessedStreamProperties = this.ResourceMetadataContext.SelectedStreamProperties .Where(p => !this.ProcessedStreamProperties.Contains(p.Key)) .Select(p => p.Key) .GetEnumerator(); } if (this.unprocessedStreamProperties.MoveNext()) { string propertyName = unprocessedStreamProperties.Current; ODataStreamReferenceValue streamPropertyValue = new ODataStreamReferenceValue(); streamPropertyValue.SetMetadataBuilder(this, propertyName); // by default, let's retrieve the content type from vocabulary annotation var edmProperty = this.ResourceMetadataContext.SelectedStreamProperties[propertyName]; var mediaTypes = this.MetadataContext.Model.GetVocabularyStringCollection(edmProperty, CoreVocabularyModel.AcceptableMediaTypesTerm); if (mediaTypes.Count() == 1) { // Be noted: AcceptableMediaTypes might have more than one media type, // Convention (default) behavior only works if AcceptableMediaTypes is a collection of one. streamPropertyValue.ContentType = mediaTypes.ElementAt(0); } return(new ODataProperty { Name = propertyName, Value = streamPropertyValue }); } return(null); }
/// <summary> /// Computes all projected or missing stream properties. /// </summary> /// <param name="nonComputedProperties">Non-computed properties from the entity.</param> /// <returns>The the computed stream properties for the resource.</returns> private IEnumerable <ODataProperty> GetComputedStreamProperties(IEnumerable <ODataProperty> nonComputedProperties) { if (this.computedStreamProperties == null) { // Remove all the projected properties that were already read from the payload IDictionary <string, IEdmStructuralProperty> projectedStreamProperties = this.ResourceMetadataContext.SelectedStreamProperties; if (nonComputedProperties != null) { foreach (ODataProperty payloadProperty in nonComputedProperties) { projectedStreamProperties.Remove(payloadProperty.Name); } } this.computedStreamProperties = new List <ODataProperty>(); if (projectedStreamProperties.Count > 0) { // Create all the missing stream properties and set the metadata builder foreach (string missingStreamPropertyName in projectedStreamProperties.Keys) { ODataStreamReferenceValue streamPropertyValue = new ODataStreamReferenceValue(); streamPropertyValue.SetMetadataBuilder(this, missingStreamPropertyName); // by default, let's retrieve the content type from vocabulary annotation var edmProperty = projectedStreamProperties[missingStreamPropertyName]; var mediaTypes = this.MetadataContext.Model.GetVocabularyStringCollection(edmProperty, CoreVocabularyModel.AcceptableMediaTypesTerm); if (mediaTypes.Count() == 1) { // Be noted: AcceptableMediaTypes might have more than one media type, // Convention (default) behavior only works if AcceptableMediaTypes is a collection of one. streamPropertyValue.ContentType = mediaTypes.ElementAt(0); } this.computedStreamProperties.Add(new ODataProperty { Name = missingStreamPropertyName, Value = streamPropertyValue }); } } } return(this.computedStreamProperties); }
internal override ODataProperty GetNextUnprocessedStreamProperty() { if (this.unprocessedStreamProperties == null) { Debug.Assert(this.ResourceMetadataContext != null, "this.resourceMetadataContext != null"); this.unprocessedStreamProperties = this.ResourceMetadataContext.SelectedStreamProperties .Where(p => !this.ProcessedStreamProperties.Contains(p.Key)) .Select(p => p.Key) .GetEnumerator(); } if (this.unprocessedStreamProperties.MoveNext()) { string propertyName = unprocessedStreamProperties.Current; ODataStreamReferenceValue streamPropertyValue = new ODataStreamReferenceValue(); streamPropertyValue.SetMetadataBuilder(this, propertyName); return(new ODataProperty { Name = propertyName, Value = streamPropertyValue }); } return(null); }
/// <summary> /// Reads a stream property value from the property annotations. /// </summary> /// <param name="entryState">The state of the reader for entry to read.</param> /// <param name="streamPropertyName">The name of the stream property to read the value for.</param> /// <returns>The newly created stream reference value.</returns> private ODataStreamReferenceValue ReadStreamPropertyValue(IODataJsonLightReaderEntryState entryState, string streamPropertyName) { Debug.Assert(entryState != null, "entryState != null"); Debug.Assert(!string.IsNullOrEmpty(streamPropertyName), "!string.IsNullOrEmpty(streamPropertyName)"); // Fail on stream properties in requests as they cannot appear there. if (!this.ReadingResponse) { throw new ODataException(ODataErrorStrings.ODataJsonLightEntryAndFeedDeserializer_StreamPropertyInRequest); } ODataStreamReferenceValue streamReferenceValue = new ODataStreamReferenceValue(); Dictionary<string, object> propertyAnnotations = entryState.DuplicatePropertyNamesChecker.GetODataPropertyAnnotations(streamPropertyName); if (propertyAnnotations != null) { foreach (KeyValuePair<string, object> propertyAnnotation in propertyAnnotations) { switch (propertyAnnotation.Key) { case ODataAnnotationNames.ODataMediaEditLink: Debug.Assert(propertyAnnotation.Value is Uri && propertyAnnotation.Value != null, "The odata.mediaEditLink annotation should have been parsed as a non-null Uri."); streamReferenceValue.EditLink = (Uri)propertyAnnotation.Value; break; case ODataAnnotationNames.ODataMediaReadLink: Debug.Assert(propertyAnnotation.Value is Uri && propertyAnnotation.Value != null, "The odata.mediaReadLink annotation should have been parsed as a non-null Uri."); streamReferenceValue.ReadLink = (Uri)propertyAnnotation.Value; break; case ODataAnnotationNames.ODataMediaETag: Debug.Assert(propertyAnnotation.Value is string && propertyAnnotation.Value != null, "The odata.mediaEtag annotation should have been parsed as a non-null string."); streamReferenceValue.ETag = (string)propertyAnnotation.Value; break; case ODataAnnotationNames.ODataMediaContentType: Debug.Assert(propertyAnnotation.Value is string && propertyAnnotation.Value != null, "The odata.mediaContentType annotation should have been parsed as a non-null string."); streamReferenceValue.ContentType = (string)propertyAnnotation.Value; break; default: throw new ODataException(ODataErrorStrings.ODataJsonLightEntryAndFeedDeserializer_UnexpectedStreamPropertyAnnotation(streamPropertyName, propertyAnnotation.Key)); } } } ODataEntityMetadataBuilder builder = this.MetadataContext.GetEntityMetadataBuilderForReader(entryState, this.JsonLightInputContext.MessageReaderSettings.UseKeyAsSegment); // Note that we set the metadata builder even when streamProperty is null, which is the case when the stream property is undeclared. // For undeclared stream properties, we will apply conventional metadata evaluation just as declared stream properties. streamReferenceValue.SetMetadataBuilder(builder, streamPropertyName); return streamReferenceValue; }
/// <summary> /// Sets specified media resource on an entry and hooks up metadata builder. /// </summary> /// <param name="entryState">The entry state to use.</param> /// <param name="mediaResource">The media resource to set.</param> private void SetEntryMediaResource(IODataJsonLightReaderEntryState entryState, ODataStreamReferenceValue mediaResource) { Debug.Assert(entryState != null, "entryState != null"); Debug.Assert(mediaResource != null, "mediaResource != null"); ODataEntry entry = entryState.Entry; Debug.Assert(entry != null, "entry != null"); ODataEntityMetadataBuilder builder = this.MetadataContext.GetEntityMetadataBuilderForReader(entryState, this.JsonLightInputContext.MessageReaderSettings.UseKeyAsSegment); mediaResource.SetMetadataBuilder(builder, /*propertyName*/ null); entry.MediaResource = mediaResource; }
/// <summary> /// Computes all projected or missing stream properties. /// </summary> /// <param name="nonComputedProperties">Non-computed properties from the entity.</param> /// <returns>The the computed stream properties for the entry.</returns> private IEnumerable<ODataProperty> GetComputedStreamProperties(IEnumerable<ODataProperty> nonComputedProperties) { if (this.computedStreamProperties == null) { // Remove all the projected properties that were already read from the payload IDictionary<string, IEdmStructuralProperty> projectedStreamProperties = this.entryMetadataContext.SelectedStreamProperties; if (nonComputedProperties != null) { foreach (ODataProperty payloadProperty in nonComputedProperties) { projectedStreamProperties.Remove(payloadProperty.Name); } } this.computedStreamProperties = new List<ODataProperty>(); if (projectedStreamProperties.Count > 0) { // Create all the missing stream properties and set the metadata builder foreach (string missingStreamPropertyName in projectedStreamProperties.Keys) { ODataStreamReferenceValue streamPropertyValue = new ODataStreamReferenceValue(); streamPropertyValue.SetMetadataBuilder(this, missingStreamPropertyName); this.computedStreamProperties.Add(new ODataProperty { Name = missingStreamPropertyName, Value = streamPropertyValue }); } } } return this.computedStreamProperties; }