Exemple #1
0
        /// <summary>
        /// Retrieves an annotation value for an EDM element. Returns null if no annotation with the given name exists for the given element.
        /// </summary>
        /// <param name="element">The annotated element.</param>
        /// <param name="namespaceName">Namespace that the annotation belongs to.</param>
        /// <param name="localName">Local name of the annotation.</param>
        /// <returns>Returns the annotation that corresponds to the provided name. Returns null if no annotation with the given name exists for the given element.</returns>
        public object GetAnnotationValue(IEdmElement element, string namespaceName, string localName)
        {
            // Fetch the annotations dictionary once and only once, because this.annotationsDictionary might get updated by another thread.
            VersioningDictionary <IEdmElement, object> annotationsDictionary = this.annotationsDictionary;

            return(this.GetAnnotationValue(element, namespaceName, localName, annotationsDictionary));
        }
Exemple #2
0
            public IEnumerable <IEdmDirectValueAnnotation> GetDirectValueAnnotations(IEdmElement element)
            {
                var innerElement = _parent.ConvertFixedEdmElementToInnerEdmElement(element);

                return(_innerAnnotationsManager.GetDirectValueAnnotations(innerElement));
                //.Union(_innerAnnotationsManager.GetDirectValueAnnotations(innerElement), AnnotationFullNameEqualityComparer.Instance);
            }
Exemple #3
0
        /// <summary>
        /// Returns the annotation in the OData metadata namespace with the specified <paramref name="localName" />.
        /// </summary>
        /// <param name="model">The <see cref="IEdmModel"/> containing the annotation.</param>
        /// <param name="annotatable">The <see cref="IEdmElement"/> to get the annotation from.</param>
        /// <param name="localName">The local name of the annotation to find.</param>
        /// <param name="value">The value of the annotation in the OData metadata namespace and with the specified <paramref name="localName"/>.</param>
        /// <returns>true if an annotation with the specified local name was found; otherwise false.</returns>
        internal static bool TryGetAnnotation(this IEdmModel model, IEdmElement annotatable, string localName, out string value)
        {
            Debug.Assert(model != null, "model != null");
            Debug.Assert(annotatable != null, "annotatable != null");
            Debug.Assert(!String.IsNullOrEmpty(localName), "!string.IsNullOrEmpty(localName)");

            object annotationValue = model.GetAnnotationValue(annotatable, CsdlConstants.ODataMetadataNamespace, localName);

            if (annotationValue == null)
            {
                value = null;
                return(false);
            }

            IEdmStringValue annotationStringValue = annotationValue as IEdmStringValue;

            if (annotationStringValue == null)
            {
                // invalid annotation type found
                throw new InvalidOperationException(Strings.EdmUtil_InvalidAnnotationValue(localName, annotationValue.GetType().FullName));
            }

            value = annotationStringValue.Value;
            return(true);
        }
Exemple #4
0
        private object GetAnnotationValue(IEdmElement element, string namespaceName, string localName, VersioningDictionary <IEdmElement, object> annotationsDictionary)
        {
            object value;
            IEdmDirectValueAnnotation edmDirectValueAnnotation = EdmDirectValueAnnotationsManager.FindTransientAnnotation(EdmDirectValueAnnotationsManager.GetTransientAnnotations(element, annotationsDictionary), namespaceName, localName);

            if (edmDirectValueAnnotation == null)
            {
                IEnumerable <IEdmDirectValueAnnotation> attachedAnnotations = this.GetAttachedAnnotations(element);
                if (attachedAnnotations != null)
                {
                    IEnumerator <IEdmDirectValueAnnotation> enumerator = attachedAnnotations.GetEnumerator();
                    using (enumerator)
                    {
                        while (enumerator.MoveNext())
                        {
                            IEdmDirectValueAnnotation current = enumerator.Current;
                            if (!(current.NamespaceUri == namespaceName) || !(current.Name == localName))
                            {
                                continue;
                            }
                            value = current.Value;
                            return(value);
                        }
                        return(null);
                    }
                    return(value);
                }
                return(null);
            }
            else
            {
                return(edmDirectValueAnnotation.Value);
            }
        }
Exemple #5
0
        /// <summary>
        /// Sets the MIME type annotation of the <paramref name="annotatable"/> to <paramref name="mimeType"/>.
        /// </summary>
        /// <param name="model">The <see cref="IEdmModel"/> containing the annotation.</param>
        /// <param name="annotatable">The <see cref="IEdmElement"/> to modify.</param>
        /// <param name="mimeType">The MIME type value to set as annotation value; if null, an existing annotation will be removed.</param>
        /// <remarks>The MIME type annotation is only supported on service operations and primitive properties for serialization purposes.</remarks>
        public static void SetMimeType(this IEdmModel model, IEdmElement annotatable, string mimeType)
        {
            ExceptionUtils.CheckArgumentNotNull(model, "model");
            ExceptionUtils.CheckArgumentNotNull(annotatable, "annotatable");

            model.SetODataAnnotation(annotatable, EdmConstants.MimeTypeAttributeName, mimeType);
        }
Exemple #6
0
 public static void AddMarketplaceAnnotation(this IEdmModel model, IEdmElement element, string name, string value)
 {
     if (!string.IsNullOrEmpty(value))
     {
         model.SetAnnotationValue(element, MarketplaceNamespace, name, new EdmStringConstant(EdmCoreModel.Instance.GetString(true), value));
     }
 }
Exemple #7
0
        /// <summary>
        /// Sets the HttpMethod annotation of the <paramref name="annotatable"/> to <paramref name="httpMethod"/>.
        /// </summary>
        /// <param name="model">The <see cref="IEdmModel"/> contatining the annotation.</param>
        /// <param name="annotatable">The <see cref="IEdmElement"/> to modify.</param>
        /// <param name="httpMethod">The HttpMethod value to set as annotation value; if null, an existing annotation will be removed.</param>
        /// <remarks>The HttpMethod annotation is only supported on service operations for serialization purposes.</remarks>
        public static void SetHttpMethod(this IEdmModel model, IEdmElement annotatable, string httpMethod)
        {
            ExceptionUtils.CheckArgumentNotNull(model, "model");
            ExceptionUtils.CheckArgumentNotNull(annotatable, "annotatable");

            model.SetODataAnnotation(annotatable, EdmConstants.HttpMethodAttributeName, httpMethod);
        }
Exemple #8
0
        private object GetAnnotationValue(IEdmElement element, string namespaceName, string localName, VersioningDictionary <IEdmElement, object> annotationsDictionary)
        {
            IEdmDirectValueAnnotation annotation = FindTransientAnnotation(GetTransientAnnotations(element, annotationsDictionary), namespaceName, localName);

            if (annotation != null)
            {
                return(annotation.Value);
            }

            IEnumerable <IEdmDirectValueAnnotation> immutableAnnotations = this.GetAttachedAnnotations(element);

            if (immutableAnnotations != null)
            {
                foreach (IEdmDirectValueAnnotation existingAnnotation in immutableAnnotations)
                {
                    if (existingAnnotation.NamespaceUri == namespaceName && existingAnnotation.Name == localName)
                    {
                        // No need to check that the immutable annotation isn't Dead, because if it were
                        // the tombstone would have been found in the transient annotations.
                        return(existingAnnotation.Value);
                    }
                }
            }

            return(null);
        }
Exemple #9
0
            public void SetAnnotationValue(IEdmElement element, string namespaceName, string localName, object value)
            {
                // Set the inner value to for the same to null, to avoid duplicates/conflicts.
                IEdmElement innerElement = _parent.ConvertFixedEdmElementToInnerEdmElement(element);

                _innerAnnotationsManager.SetAnnotationValue(innerElement, namespaceName, localName, value);
            }
Exemple #10
0
            /// <summary>
            /// Retrieves an annotation value for an EDM element. Returns null if no annotation with the given name exists.
            /// </summary>
            /// <param name="element">The annotated element.</param>
            /// <param name="namespaceName">Namespace that the annotation belongs to.</param>
            /// <param name="localName">Local name of the annotation.</param>
            /// <returns>Returns the annotation that corresponds to the provided name. Returns null if no annotation with the given name exists. </returns>
            public object GetAnnotationValue(IEdmElement element, string namespaceName, string localName)
            {
                List <IEdmDirectValueAnnotation> elementAnnotations = this.FindAnnotations(element, false);
                IEdmDirectValueAnnotation        annotation         = FindAnnotation(elementAnnotations, namespaceName, localName);

                return(annotation != null ? annotation.Value : null);
            }
Exemple #11
0
        /// <summary>
        /// Retrieves the transient annotations for an EDM element.
        /// </summary>
        /// <param name="element">The annotated element.</param>
        /// <param name="annotationsDictionary">The dictionary for looking up the element's annotations.</param>
        /// <returns>The transient annotations for the element, in a form managed by the annotations manager.</returns>
        /// <remarks>This method is static to guarantee that the annotations dictionary is not fetched more than once per lookup operation.</remarks>
        private static object GetTransientAnnotations(IEdmElement element, VersioningDictionary <IEdmElement, object> annotationsDictionary)
        {
            object transientAnnotations;

            annotationsDictionary.TryGetValue(element, out transientAnnotations);
            return(transientAnnotations);
        }
Exemple #12
0
        /// <summary>
        /// Sets the <paramref name="boolValue "/> as value of the <paramref name="annotationLocalName"/> annotation
        /// on the <paramref name="annotatable"/>.
        /// </summary>
        /// <param name="model">The model containing the annotation.</param>
        /// <param name="annotatable">The annotatable to set the annotation on.</param>
        /// <param name="annotationLocalName">The local name of the annotation to set.</param>
        /// <param name="boolValue">The value of the annotation to set.</param>
        private static void SetBooleanAnnotation(IEdmModel model, IEdmElement annotatable, string annotationLocalName, bool boolValue)
        {
            Debug.Assert(model != null, "model != null");
            Debug.Assert(annotatable != null, "annotatable != null");
            Debug.Assert(annotationLocalName != null, "annotationLocalName != null");

            model.SetODataAnnotation(annotatable, annotationLocalName, boolValue ? EdmConstants.TrueLiteral : null);
        }
		public EdmDirectValueAnnotationBinding(IEdmElement element, string namespaceUri, string name)
		{
			EdmUtil.CheckArgumentNull<IEdmElement>(element, "element");
			EdmUtil.CheckArgumentNull<string>(namespaceUri, "namespaceUri");
			EdmUtil.CheckArgumentNull<string>(name, "name");
			this.element = element;
			this.namespaceUri = namespaceUri;
			this.name = name;
		}
Exemple #14
0
 public void SetAnnotationValue(IEdmElement element, string namespaceName, string localName, object value)
 {
     lock (this.annotationsDictionaryLock)
     {
         VersioningDictionary <IEdmElement, object> versioningDictionary = this.annotationsDictionary;
         this.SetAnnotationValue(element, namespaceName, localName, value, ref versioningDictionary);
         this.annotationsDictionary = versioningDictionary;
     }
 }
Exemple #15
0
        private void VisitDirectAnnotations(IEdmElement element)
        {
            IEnumerable <IEdmDirectValueAnnotation> annotations = _model.DirectValueAnnotations(element);

            foreach (var annotation in annotations)
            {
                VisitDirectAnnotation(annotation);
            }
        }
Exemple #16
0
 public EdmDirectValueAnnotationBinding(IEdmElement element, string namespaceUri, string name)
 {
     EdmUtil.CheckArgumentNull <IEdmElement>(element, "element");
     EdmUtil.CheckArgumentNull <string>(namespaceUri, "namespaceUri");
     EdmUtil.CheckArgumentNull <string>(name, "name");
     this.element      = element;
     this.namespaceUri = namespaceUri;
     this.name         = name;
 }
Exemple #17
0
 internal static void SetODataAnnotation(this IEdmModel model, IEdmElement annotatable, string localName, string value)
 {
     IEdmStringValue value2 = null;
     if (value != null)
     {
         value2 = new EdmStringConstant(EdmCoreModel.Instance.GetString(true), value);
     }
     model.SetAnnotationValue(annotatable, "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata", localName, value2);
 }
Exemple #18
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ODataSerializationFeatureContext"/> class.
        /// </summary>
        /// <param name="element">The <see cref="IEdmElement">element</see> represented by the instance.</param>
        /// <param name="serializerContext">The associated <see cref="ODataSerializerContext">serializer context</see>.</param>
        /// <param name="complexTypeSerializer">The serializer that can be used to serialize OData complex types.</param>
        public ODataSerializationFeatureContext(IEdmElement element, ODataSerializerContext serializerContext, ODataComplexTypeSerializer complexTypeSerializer)
        {
            Arg.NotNull(element, nameof(element));
            Arg.NotNull(serializerContext, nameof(serializerContext));
            Arg.NotNull(complexTypeSerializer, nameof(complexTypeSerializer));

            EdmElement            = element;
            SerializerContext     = serializerContext;
            ComplexTypeSerializer = complexTypeSerializer;
        }
Exemple #19
0
        internal static void SetODataAnnotation(this IEdmModel model, IEdmElement annotatable, string localName, string value)
        {
            IEdmStringValue value2 = null;

            if (value != null)
            {
                value2 = new EdmStringConstant(EdmCoreModel.Instance.GetString(true), value);
            }
            model.SetAnnotationValue(annotatable, "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata", localName, value2);
        }
        protected override IEnumerable<IEdmDirectValueAnnotation> GetAttachedAnnotations(IEdmElement element)
        {
            CsdlSemanticsElement csdlElement = element as CsdlSemanticsElement;
            if (csdlElement != null)
            {
                return csdlElement.DirectValueAnnotations;
            }

            return Enumerable.Empty<IEdmDirectValueAnnotation>();
        }
Exemple #21
0
        private static IEdmDirectValueAnnotationBinding GetODataAnnotationBinding(IEdmElement annotatable, string localName, string value)
        {
            IEdmStringValue value2 = null;

            if (value != null)
            {
                value2 = new EdmStringConstant(EdmCoreModel.Instance.GetString(true), value);
            }
            return(new EdmDirectValueAnnotationBinding(annotatable, "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata", localName, value2));
        }
Exemple #22
0
 internal static IEnumerable<IEdmDirectValueAnnotation> GetODataAnnotations(this IEdmModel model, IEdmElement annotatable)
 {
     IEnumerable<IEdmDirectValueAnnotation> enumerable = model.DirectValueAnnotations(annotatable);
     if (enumerable == null)
     {
         return null;
     }
     return (from a in enumerable
         where a.NamespaceUri == "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
         select a);
 }
Exemple #23
0
        private void SetAnnotationValue(IEdmElement element, string namespaceName, string localName, object value, ref VersioningDictionary <IEdmElement, object> annotationsDictionary)
        {
            object transientAnnotations = EdmDirectValueAnnotationsManager.GetTransientAnnotations(element, annotationsDictionary);
            object obj = transientAnnotations;

            EdmDirectValueAnnotationsManager.SetAnnotation(this.GetAttachedAnnotations(element), ref transientAnnotations, namespaceName, localName, value);
            if (transientAnnotations != obj)
            {
                annotationsDictionary = annotationsDictionary.Set(element, transientAnnotations);
            }
        }
Exemple #24
0
 private static bool CanFilter(ODataContext context, IEdmElement element)
 {
     if (context.Settings.EdmFilter != null)
     {
         return(context.Settings.EdmFilter(element));
     }
     else
     {
         return(true);
     }
 }
Exemple #25
0
 public void Annotate(IMetadataElement metadataElement, IEdmElement edmElement, IEdmModel edmModel)
 {
     if (metadataElement is EntityElement || metadataElement is EnumTypeElement)
     {
         var clrType = _clrTypeProvider.Get(metadataElement.Identity);
         if (clrType != null)
         {
             edmModel.SetAnnotationValue(edmElement, new ClrTypeAnnotation(clrType));
         }
     }
 }
Exemple #26
0
 /// <summary>
 /// Can filter the <see cref="IEdmElement"/> or not.
 /// </summary>
 /// <param name="element">The Edm element.</param>
 /// <returns>True/false.</returns>
 public virtual bool CanFilter(IEdmElement element)
 {
     if (_settings.EdmFilter != null)
     {
         return(_settings.EdmFilter(element));
     }
     else
     {
         return(true);
     }
 }
Exemple #27
0
        /// <summary>
        /// Sets an annotation value for an EDM element. If the value is null, no annotation is added and an existing annotation with the same name is removed.
        /// </summary>
        /// <param name="element">The annotated element.</param>
        /// <param name="namespaceName">Namespace that the annotation belongs to.</param>
        /// <param name="localName">Name of the annotation within the namespace.</param>
        /// <param name="value">New annotation to set.</param>
        public void SetAnnotationValue(IEdmElement element, string namespaceName, string localName, object value)
        {
            lock (this.annotationsDictionaryLock)
            {
                // Use a local variable to store any interim changes to the annotations dictionary, and perform one atomic update
                // to the field. Otherwise, other threads could see a dictionary in an interim state.
                VersioningDictionary <IEdmElement, object> annotationsDictionary = this.annotationsDictionary;
                this.SetAnnotationValue(element, namespaceName, localName, value, ref annotationsDictionary);

                this.annotationsDictionary = annotationsDictionary;
            }
        }
 public void Annotate(IMetadataElement metadataElement, IEdmElement edmElement, IEdmModel edmModel)
 {
     if (metadataElement is EntityElement || metadataElement is EnumTypeElement)
     {
         var typeName = metadataElement.Identity.Id.Segments.Last();
         var clrType = _clrTypes.SingleOrDefault(x => x.Name.Equals(typeName, StringComparison.OrdinalIgnoreCase));
         if (clrType != null)
         {
             edmModel.SetAnnotationValue(edmElement, new ClrTypeAnnotation(clrType));
         }
     }
 }
        private void SetImmediateAnnotations(IEdmElement edmAnnotatable, IEdmElement stockAnnotatable, IEdmModel edmModel, EdmModel stockModel)
        {
            IEnumerable<IEdmDirectValueAnnotation> annotations = edmModel.DirectValueAnnotations(edmAnnotatable);

            foreach (IEdmDirectValueAnnotation annotation in annotations)
            {
                var annotationValue = annotation.Value;
                string annotationNamespace = annotation.NamespaceUri;
                string annotationName = annotation.Name;
                stockModel.SetAnnotationValue(stockAnnotatable, annotationNamespace, annotationName, annotationValue);
            }
        }
Exemple #30
0
        private void EndElement(IEdmElement element)
        {
            this.VisitPrimitiveElementAnnotations(this.Model.DirectValueAnnotations(element));
            IEdmVocabularyAnnotatable vocabularyAnnotatableElement = element as IEdmVocabularyAnnotatable;

            if (vocabularyAnnotatableElement != null)
            {
                this.VisitElementVocabularyAnnotations(this.Model.FindDeclaredVocabularyAnnotations(vocabularyAnnotatableElement).Where(a => a.IsInline(this.Model)));
            }

            this.schemaWriter.WriteEndElement();
        }
        private void SetImmediateAnnotations(IEdmElement edmAnnotatable, IEdmElement stockAnnotatable, IEdmModel edmModel, EdmModel stockModel)
        {
            IEnumerable <IEdmDirectValueAnnotation> annotations = edmModel.DirectValueAnnotations(edmAnnotatable);

            foreach (IEdmDirectValueAnnotation annotation in annotations)
            {
                var    annotationValue     = annotation.Value;
                string annotationNamespace = annotation.NamespaceUri;
                string annotationName      = annotation.Name;
                stockModel.SetAnnotationValue(stockAnnotatable, annotationNamespace, annotationName, annotationValue);
            }
        }
Exemple #32
0
        private static bool TryGetBooleanAnnotation(IEdmModel model, IEdmElement annotatable, string annotationLocalName, out bool boolValue)
        {
            string str;

            if (model.TryGetODataAnnotation(annotatable, annotationLocalName, out str))
            {
                boolValue = XmlConvert.ToBoolean(str);
                return(true);
            }
            boolValue = false;
            return(false);
        }
 public void Annotate(IMetadataElement metadataElement, IEdmElement edmElement, IEdmModel edmModel)
 {
     if (metadataElement is EntityElement || metadataElement is EnumTypeElement)
     {
         var typeName = metadataElement.Identity.Id.Segments.Last();
         var clrType  = _clrTypes.SingleOrDefault(x => x.Name.Equals(typeName, StringComparison.OrdinalIgnoreCase));
         if (clrType != null)
         {
             edmModel.SetAnnotationValue(edmElement, new ClrTypeAnnotation(clrType));
         }
     }
 }
        public void Annotate(IMetadataElement metadataElement, IEdmElement edmElement, IEdmModel edmModel)
        {
            if (metadataElement is EntityElement || metadataElement is EnumTypeElement)
            {
                edmModel.SetAnnotationValue(edmElement, AnnotationNamespace, AnnotationAttribute, metadataElement.Identity.Id);

                var clrType = _clrTypeProvider.Get(metadataElement.Identity);
                if (clrType != null)
                {
                    edmModel.SetAnnotationValue(edmElement, new ClrTypeAnnotation(clrType));
                }
            }
        }
Exemple #35
0
        /// <summary>
        /// Gets all the serializable annotations in the OData metadata namespace on the <paramref name="annotatable"/>.
        /// </summary>
        /// <param name="model">The <see cref="IEdmModel"/> containing the annotations."/></param>
        /// <param name="annotatable">The <see cref="IEdmElement"/> to get the annotations from.</param>
        /// <returns>All annotations in the OData metadata namespace; or null if no annotations are found.</returns>
        internal static IEnumerable<IEdmDirectValueAnnotation> GetODataAnnotations(this IEdmModel model, IEdmElement annotatable)
        {
            Debug.Assert(model != null, "model != null");
            Debug.Assert(annotatable != null, "annotatable != null");
            
            IEnumerable<IEdmDirectValueAnnotation> annotations = model.DirectValueAnnotations(annotatable);
            if (annotations == null)
            {
                return null;
            }

            return annotations.Where(a => a.NamespaceUri == AtomConstants.ODataMetadataNamespace);
        }
Exemple #36
0
        private static void LoadEpmAnnotations(this IEdmModel model, IEdmElement annotatable, ODataEntityPropertyMappingCollection mappings, string typeName, IEdmProperty property)
        {
            IEnumerable <EpmAnnotationValues> enumerable = model.ParseSerializableEpmAnnotations(annotatable, typeName, property);

            if (enumerable != null)
            {
                foreach (EpmAnnotationValues values in enumerable)
                {
                    EntityPropertyMappingAttribute mapping = ValidateAnnotationValues(values, typeName, property);
                    mappings.Add(mapping);
                }
            }
        }
Exemple #37
0
        private static IEdmStructuredType GetStructuredType(IEdmElement element)
        {
            Contract.Requires(element != null);
            Contract.Ensures(Contract.Result <IEdmStructuredType>() != null);

            var type = element as IEdmStructuredType;

            if (type == null)
            {
                return((IEdmStructuredType)((IEdmStructuredTypeReference)element).Definition);
            }

            return(type);
        }
Exemple #38
0
        private void SetAnnotationValue(IEdmElement element, string namespaceName, string localName, object value, ref VersioningDictionary <IEdmElement, object> annotationsDictionary)
        {
            object transientAnnotations          = GetTransientAnnotations(element, annotationsDictionary);
            object transientAnnotationsBeforeSet = transientAnnotations;

            SetAnnotation(this.GetAttachedAnnotations(element), ref transientAnnotations, namespaceName, localName, value);

            // There is at least one case (removing an annotation that was not present to begin with) where the transient annotations are not changed,
            // so test to see if updating the dictionary is necessary.
            if (transientAnnotations != transientAnnotationsBeforeSet)
            {
                annotationsDictionary = annotationsDictionary.Set(element, transientAnnotations);
            }
        }
Exemple #39
0
 public static string GetMimeType(this IEdmModel model, IEdmElement annotatable)
 {
     string str;
     ExceptionUtils.CheckArgumentNotNull<IEdmModel>(model, "model");
     ExceptionUtils.CheckArgumentNotNull<IEdmElement>(annotatable, "annotatable");
     if (!model.TryGetODataAnnotation(annotatable, "MimeType", out str))
     {
         return null;
     }
     if (str == null)
     {
         throw new ODataException(Microsoft.Data.OData.Strings.ODataUtils_NullValueForMimeTypeAnnotation);
     }
     return str;
 }
        /// <summary>
        /// Sets the annotation with the OData metadata namespace and the specified <paramref name="localName" /> on the <paramref name="annotatable"/>.
        /// </summary>
        /// <param name="model">The <see cref="IEdmModel"/> containing the annotations."/></param>
        /// <param name="annotatable">The <see cref="IEdmElement"/> to set the annotation on.</param>
        /// <param name="localName">The local name of the annotation to set.</param>
        /// <param name="value">The value of the annotation to set.</param>
        internal static void SetODataAnnotation(this IEdmModel model, IEdmElement annotatable, string localName, string value)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(model != null, "model != null"); 
            Debug.Assert(annotatable != null, "annotatable != null");
            Debug.Assert(!string.IsNullOrEmpty(localName), "!string.IsNullOrEmpty(localName)");

            IEdmStringValue stringValue = null;
            if (value != null)
            {
                IEdmStringTypeReference typeReference = EdmCoreModel.Instance.GetString(/*nullable*/true);
                stringValue = new EdmStringConstant(typeReference, value);
            }

            model.SetAnnotationValue(annotatable, AtomConstants.ODataMetadataNamespace, localName, stringValue);
        }
        private IEdmVocabularyAnnotatable ConvertToStockVocabularyAnnotatable(IEdmElement edmAnnotatable, EdmModel stockModel)
        {
            // TODO: Need to provide a better way to get more details on IEdmVocabularyAnnotation.Target.
            IEdmVocabularyAnnotatable stockAnnotatable = null;
            if (edmAnnotatable is IEdmEntityType)
            {
                var edmEntityType = edmAnnotatable as IEdmEntityType;
                stockAnnotatable = stockModel.FindType(edmEntityType.FullName()) as IEdmVocabularyAnnotatable;
                ExceptionUtilities.CheckObjectNotNull(stockAnnotatable, "The FindType method must be successful.");
            }
            else if (edmAnnotatable is IEdmProperty)
            {
                var edmProperty = edmAnnotatable as IEdmProperty;
                if (edmProperty.DeclaringType is IEdmSchemaElement)
                {
                    var stockSchemaElement = stockModel.FindType(((IEdmSchemaElement)edmProperty.DeclaringType).FullName());
                    ExceptionUtilities.CheckObjectNotNull(stockAnnotatable, "The FindType method must be successful.");
                    stockAnnotatable = ((IEdmStructuredType)stockSchemaElement).FindProperty(edmProperty.Name);
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
            else if (edmAnnotatable is IEdmEntitySet)
            {
                var edmEntitySet = edmAnnotatable as IEdmEntitySet;
                // TODO: No backpointer to the Entity Container from EntitySet in the API. Is this OK?
                stockAnnotatable = stockModel.EntityContainer.EntitySets().Single(m => m.Name == edmEntitySet.Name);
            }
            else if (edmAnnotatable is IEdmEnumType)
            {
                var edmEnumType = edmAnnotatable as IEdmEnumType;
                stockAnnotatable = stockModel.FindType(edmEnumType.FullName());
            }
            else
            {
                throw new NotImplementedException();
            }

            return stockAnnotatable;
        }
        /// <summary>
        /// Gets annotations associated with an element.
        /// </summary>
        /// <param name="element">The annotated element.</param>
        /// <returns>The immediate value annotations for the element.</returns>
        public IEnumerable<IEdmDirectValueAnnotation> GetDirectValueAnnotations(IEdmElement element)
        {
            // Fetch the annotations dictionary once and only once, because this.annotationsDictionary might get updated by another thread.
            VersioningDictionary<IEdmElement, object> annotationsDictionary = this.annotationsDictionary;

            IEnumerable<IEdmDirectValueAnnotation> immutableAnnotations = this.GetAttachedAnnotations(element);
            object transientAnnotations = GetTransientAnnotations(element, annotationsDictionary);

            if (immutableAnnotations != null)
            {
                foreach (IEdmDirectValueAnnotation existingAnnotation in immutableAnnotations)
                {
                    if (!IsDead(existingAnnotation.NamespaceUri, existingAnnotation.Name, transientAnnotations))
                    {
                        yield return existingAnnotation;
                    }
                }
            }

            foreach (IEdmDirectValueAnnotation existingAnnotation in TransientAnnotations(transientAnnotations))
            {
                yield return existingAnnotation;
            }
        }
        /// <summary>
        /// Returns the annotation in the OData metadata namespace with the specified <paramref name="localName" />.
        /// </summary>
        /// <param name="model">The <see cref="IEdmModel"/> containing the annotation.</param>
        /// <param name="annotatable">The <see cref="IEdmElement"/> to get the annotation from.</param>
        /// <param name="localName">The local name of the annotation to find.</param>
        /// <param name="value">The value of the annotation in the OData metadata namespace and with the specified <paramref name="localName"/>.</param>
        /// <returns>true if an annotation with the specified local name was found; otherwise false.</returns>
        internal static bool TryGetODataAnnotation(this IEdmModel model, IEdmElement annotatable, string localName, out string value)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(model != null, "model != null"); 
            Debug.Assert(annotatable != null, "annotatable != null");
            Debug.Assert(!string.IsNullOrEmpty(localName), "!string.IsNullOrEmpty(localName)");

            object annotationValue = model.GetAnnotationValue(annotatable, AtomConstants.ODataMetadataNamespace, localName);
            if (annotationValue == null)
            {
                value = null;
                return false;
            }

            IEdmStringValue annotationStringValue = annotationValue as IEdmStringValue;
            if (annotationStringValue == null)
            {
                // invalid annotation type found
                throw new ODataException(Strings.ODataAtomWriterMetadataUtils_InvalidAnnotationValue(localName, annotationValue.GetType().FullName));
            }

            value = annotationStringValue.Value;
            return true;
        }
        /// <summary>
        /// Retrieves an annotation value for an EDM element. Returns null if no annotation with the given name exists for the given element.
        /// </summary>
        /// <param name="element">The annotated element.</param>
        /// <param name="namespaceName">Namespace that the annotation belongs to.</param>
        /// <param name="localName">Local name of the annotation.</param>
        /// <returns>Returns the annotation that corresponds to the provided name. Returns null if no annotation with the given name exists for the given element.</returns>
        public object GetAnnotationValue(IEdmElement element, string namespaceName, string localName)
        {
            // Fetch the annotations dictionary once and only once, because this.annotationsDictionary might get updated by another thread.
            VersioningDictionary<IEdmElement, object> annotationsDictionary = this.annotationsDictionary;

            return this.GetAnnotationValue(element, namespaceName, localName, annotationsDictionary);
        }
 private static Tuple<IEdmElement, string, string> CreateKey(IEdmElement element, string namespaceName,
     string localName)
 {
     return new Tuple<IEdmElement, string, string>(element, namespaceName, localName);
 }
 public void SetAnnotationValue(IEdmElement element, string namespaceName, string localName, object value)
 {
     annotations[CreateKey(element, namespaceName, localName)] = value;
 }
 public IEnumerable<IEdmDirectValueAnnotation> GetDirectValueAnnotations(IEdmElement element)
 {
     throw new NotImplementedException();
 }
Exemple #48
0
 internal static bool TryGetODataAnnotation(this IEdmModel model, IEdmElement annotatable, string localName, out string value)
 {
     object obj2 = model.GetAnnotationValue(annotatable, "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata", localName);
     if (obj2 == null)
     {
         value = null;
         return false;
     }
     IEdmStringValue value2 = obj2 as IEdmStringValue;
     if (value2 == null)
     {
         throw new ODataException(Microsoft.Data.OData.Strings.ODataAtomWriterMetadataUtils_InvalidAnnotationValue(localName, obj2.GetType().FullName));
     }
     value = value2.Value;
     return true;
 }
 /// <summary>
 /// Method returns true if the <paramref name="element"/> is known to have structural errors associated with it.
 /// </summary>
 /// <param name="element">The element to test.</param>
 /// <returns>True if the <paramref name="element"/> has structural errors associated with it.</returns>
 public bool IsBad(IEdmElement element)
 {
     return this.isBad(element);
 }
 /// <summary>
 /// Retrieves the transient annotations for an EDM element.
 /// </summary>
 /// <param name="element">The annotated element.</param>
 /// <param name="annotationsDictionary">The dictionary for looking up the element's annotations.</param>
 /// <returns>The transient annotations for the element, in a form managed by the annotations manager.</returns>
 /// <remarks>This method is static to guarantee that the annotations dictionary is not fetched more than once per lookup operation.</remarks>
 private static object GetTransientAnnotations(IEdmElement element, VersioningDictionary<IEdmElement, object> annotationsDictionary)
 {
     object transientAnnotations;
     annotationsDictionary.TryGetValue(element, out transientAnnotations);
     return transientAnnotations;
 }
        private void SetAnnotationValue(IEdmElement element, string namespaceName, string localName, object value, ref VersioningDictionary<IEdmElement, object> annotationsDictionary)
        {
            object transientAnnotations = GetTransientAnnotations(element, annotationsDictionary);
            object transientAnnotationsBeforeSet = transientAnnotations;
            SetAnnotation(this.GetAttachedAnnotations(element), ref transientAnnotations, namespaceName, localName, value);

            // There is at least one case (removing an annotation that was not present to begin with) where the transient annotations are not changed,
            // so test to see if updating the dictionary is necessary.
            if (transientAnnotations != transientAnnotationsBeforeSet)
            {
                annotationsDictionary = annotationsDictionary.Set(element, transientAnnotations);
            }
        }
        private object GetAnnotationValue(IEdmElement element, string namespaceName, string localName, VersioningDictionary<IEdmElement, object> annotationsDictionary)
        {
            IEdmDirectValueAnnotation annotation = FindTransientAnnotation(GetTransientAnnotations(element, annotationsDictionary), namespaceName, localName);
            if (annotation != null)
            {
                return annotation.Value;
            }

            IEnumerable<IEdmDirectValueAnnotation> immutableAnnotations = this.GetAttachedAnnotations(element);
            if (immutableAnnotations != null)
            {
                foreach (IEdmDirectValueAnnotation existingAnnotation in immutableAnnotations)
                {
                    if (existingAnnotation.NamespaceUri == namespaceName && existingAnnotation.Name == localName)
                    {
                        // No need to check that the immutable annotation isn't Dead, because if it were
                        // the tombstone would have been found in the transient annotations.
                        return existingAnnotation.Value;
                    }
                }
            }

            return null;
        }
        private int CompareElements(IEdmElement left, IEdmElement right)
        {
            if (left == right)
            {
                return 0;
            }

            /* Left and right are distinct. */

            int leftHash = left.GetHashCode();
            int rightHash = right.GetHashCode();

            if (leftHash < rightHash)
            {
                return -1;
            }

            if (leftHash > rightHash)
            {
                return 1;
            }

            /* Left and right are distinct and have identical hash codes. */

            IEdmNamedElement leftNamed = left as IEdmNamedElement;
            IEdmNamedElement rightNamed = right as IEdmNamedElement;

            if (leftNamed == null)
            {
                if (rightNamed != null)
                {
                    return -1;
                }
            }
            else if (rightNamed == null)
            {
                return 1;
            }
            else
            {
                /* Left and right are both named. */

                int nameComparison = string.Compare(leftNamed.Name, rightNamed.Name, StringComparison.Ordinal);

                if (nameComparison != 0)
                {
                    return nameComparison;
                }
            }
            
            /* Left and right are distinct, have identical hash codes, and have identical names. */

            /* The first element to occur in the unsorted list is the greatest. */

            while (true)
            {
                foreach (IEdmElement element in this.unsortedElements)
                {
                    if (element == left)
                    {
                        return 1;
                    }

                    if (element == right)
                    {
                        return -1;
                    }
                }

                lock (this.unsortedElementsLock)
                {
                    this.unsortedElements = this.unsortedElements.Add(left);
                }
            }
        }
        /// <summary>
        /// Sets an annotation value for an EDM element. If the value is null, no annotation is added and an existing annotation with the same name is removed.
        /// </summary>
        /// <param name="element">The annotated element.</param>
        /// <param name="namespaceName">Namespace that the annotation belongs to.</param>
        /// <param name="localName">Name of the annotation within the namespace.</param>
        /// <param name="value">New annotation to set.</param>
        public void SetAnnotationValue(IEdmElement element, string namespaceName, string localName, object value)
        {
            lock (this.annotationsDictionaryLock)
            {
                // Use a local variable to store any interim changes to the annotations dictionary, and perform one atomic update
                // to the field. Otherwise, other threads could see a dictionary in an interim state.
                VersioningDictionary<IEdmElement, object> annotationsDictionary = this.annotationsDictionary;
                this.SetAnnotationValue(element, namespaceName, localName, value, ref annotationsDictionary);

                this.annotationsDictionary = annotationsDictionary;
            }
        }
        private IEdmElement GetComplexTypeAnnotatableElement(string targetName, AnnotatableElementType targetType, IEdmElement element)
        {
            var complexType = (element as IEdmComplexType);

            if (null != complexType)
            {
                if (targetType.Equals(AnnotatableElementType.Property))
                {
                    return complexType.FindProperty(targetName);
                }
            }

            return null;
        }
 public StoreGeneratedPatternAnnotation(IEdmElement element, DatabaseGeneratedOption option)
 {
     this.element = element;
     this.value = new EdmStringConstant(EdmCoreModel.Instance.GetString(isNullable: false), option.ToString());
 }
        private IEdmElement GetEntityTypeAnnotatableElement(string targetName, AnnotatableElementType targetType, IEdmElement element)
        {
            var entityType = (element as IEdmEntityType);

            if (null != entityType)
            {
                if (targetType.Equals(AnnotatableElementType.Property))
                {
                    return entityType.FindProperty(targetName);
                }
                else if (targetType.Equals(AnnotatableElementType.NavigationProperty))
                {
                    var navigationProperties = entityType.NavigationProperties().Where(n => n.Name.Equals(targetName));
                    return navigationProperties.Count() == 0 ? null : navigationProperties.First();
                }
            }

            return null;
        }
 /// <summary>
 /// Retrieves the annotations that are directly attached to an element.
 /// </summary>
 /// <param name="element">The element in question.</param>
 /// <returns>The annotations that are directly attached to an element (outside the control of the manager).</returns>
 protected virtual IEnumerable<IEdmDirectValueAnnotation> GetAttachedAnnotations(IEdmElement element)
 {
     return null;
 }
        private IEdmElement GetFunctionImportAnnotatableElement(string targetName, AnnotatableElementType targetType, IEdmElement element)
        {
            var operationImport = (element as IEdmOperationImport);

            if (null != operationImport)
            {
                if (targetType.Equals(AnnotatableElementType.Parameter))
                {
                    return operationImport.Operation.FindParameter(targetName);
                }
            }

            return null;
        }
            public object GetAnnotationValue(IEdmElement element, string namespaceName, string localName)
            {
                object value;

                if (!annotations.TryGetValue(CreateKey(element, namespaceName, localName), out value))
                {
                    return null;
                }

                return value;
            }