private static IEnumerable<EpmPropertyInformation> GetEpmPropertyInformation(MetadataItem metadataItem, string typeName, string memberName) { EpmAttributeNameBuilder iteratorVariable0 = new EpmAttributeNameBuilder(); while (true) { string iteratorVariable6; bool iteratorVariable1 = true; MetadataProperty iteratorVariable2 = EdmUtil.FindExtendedProperty(metadataItem, iteratorVariable0.EpmTargetPath); if (iteratorVariable2 == null) { break; } bool result = true; MetadataProperty iteratorVariable4 = EdmUtil.FindExtendedProperty(metadataItem, iteratorVariable0.EpmKeepInContent); if ((iteratorVariable4 != null) && !bool.TryParse(Convert.ToString(iteratorVariable4.Value, CultureInfo.InvariantCulture), out result)) { throw new InvalidOperationException((memberName == null) ? Strings.ObjectContext_InvalidValueForEpmPropertyType(iteratorVariable0.EpmKeepInContent, typeName) : Strings.ObjectContext_InvalidValueForEpmPropertyMember(iteratorVariable0.EpmKeepInContent, memberName, typeName)); } MetadataProperty iteratorVariable5 = EdmUtil.FindExtendedProperty(metadataItem, iteratorVariable0.EpmSourcePath); if (iteratorVariable5 == null) { if (memberName == null) { throw new InvalidOperationException(Strings.ObjectContext_MissingExtendedAttributeType(iteratorVariable0.EpmSourcePath, typeName)); } iteratorVariable1 = false; iteratorVariable6 = memberName; } else { iteratorVariable6 = Convert.ToString(iteratorVariable5.Value, CultureInfo.InvariantCulture); } string targetPath = Convert.ToString(iteratorVariable2.Value, CultureInfo.InvariantCulture); SyndicationItemProperty iteratorVariable8 = EpmTranslate.MapEpmTargetPathToSyndicationProperty(targetPath); MetadataProperty iteratorVariable9 = EdmUtil.FindExtendedProperty(metadataItem, iteratorVariable0.EpmContentKind); MetadataProperty iteratorVariable10 = EdmUtil.FindExtendedProperty(metadataItem, iteratorVariable0.EpmNsPrefix); MetadataProperty iteratorVariable11 = EdmUtil.FindExtendedProperty(metadataItem, iteratorVariable0.EpmNsUri); if ((iteratorVariable9 != null) && ((iteratorVariable10 != null) || (iteratorVariable11 != null))) { string str = (iteratorVariable10 != null) ? iteratorVariable0.EpmNsPrefix : iteratorVariable0.EpmNsUri; throw new InvalidOperationException((memberName == null) ? Strings.ObjectContext_InvalidAttributeForNonSyndicationItemsType(str, typeName) : Strings.ObjectContext_InvalidAttributeForNonSyndicationItemsMember(str, memberName, typeName)); } if (((iteratorVariable10 != null) || (iteratorVariable11 != null)) || (iteratorVariable8 == SyndicationItemProperty.CustomProperty)) { string iteratorVariable12 = (iteratorVariable10 != null) ? Convert.ToString(iteratorVariable10.Value, CultureInfo.InvariantCulture) : null; string iteratorVariable13 = (iteratorVariable11 != null) ? Convert.ToString(iteratorVariable11.Value, CultureInfo.InvariantCulture) : null; EpmPropertyInformation iteratorVariable14 = new EpmPropertyInformation { IsAtom = false, KeepInContent = result, SourcePath = iteratorVariable6, PathGiven = iteratorVariable1, TargetPath = targetPath, NsPrefix = iteratorVariable12, NsUri = iteratorVariable13 }; yield return iteratorVariable14; } else { SyndicationTextContentKind plaintext; if (iteratorVariable9 != null) { plaintext = EpmTranslate.MapEpmContentKindToSyndicationTextContentKind(Convert.ToString(iteratorVariable9.Value, CultureInfo.InvariantCulture), typeName, memberName); } else { plaintext = SyndicationTextContentKind.Plaintext; } EpmPropertyInformation iteratorVariable16 = new EpmPropertyInformation { IsAtom = true, KeepInContent = result, SourcePath = iteratorVariable6, PathGiven = iteratorVariable1, SyndicationItem = iteratorVariable8, ContentKind = plaintext }; yield return iteratorVariable16; } iteratorVariable0.MoveNext(); } }
/// <summary> /// Saves the EPM annotations on the given <paramref name="annotatable"/>. /// </summary> /// <param name="model">The <see cref="IEdmModel"/> containing the annotations.</param> /// <param name="annotatable">The <see cref="IEdmElement"/> to save the EPM annotations on.</param> /// <param name="mappings">All the EPM annotations to be saved.</param> /// <param name="skipSourcePath">true if the source path should be saved explicitly; otherwise false.</param> /// <param name="removePrefix">true if the prefix of the source path should be removed; otherwise false.</param> internal static void SaveEpmAnnotations(this IEdmModel model, IEdmElement annotatable, IEnumerable<EntityPropertyMappingAttribute> mappings, bool skipSourcePath, bool removePrefix) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(model != null, "model != null"); Debug.Assert(annotatable != null, "annotatable != null"); Debug.Assert(mappings != null, "mappings != null"); Debug.Assert(!skipSourcePath || mappings.Count() <= 1, "Skipping the source path is only valid if at most a single mapping exists."); EpmAttributeNameBuilder epmAttributeNameBuilder = new EpmAttributeNameBuilder(); // Get the annotation bindings for all serializable EPM annotations with a null value (which will remove them). // NOTE: we use a dictionary here and replace existing annotation bindings below to ensure we have at most // one binding per annotation name; the default annotation manager will process annotations in order and thus // the last binding for an annotation would win but custom annotation managers might not. Dictionary<string, IEdmDirectValueAnnotationBinding> epmAnnotationBindings = GetAnnotationBindingsToRemoveSerializableEpmAnnotations(model, annotatable); string localName; foreach (EntityPropertyMappingAttribute mapping in mappings) { // Check whether the mapping represents a custom mapping if (mapping.TargetSyndicationItem == SyndicationItemProperty.CustomProperty) { localName = epmAttributeNameBuilder.EpmTargetPath; epmAnnotationBindings[localName] = GetODataAnnotationBinding(annotatable, localName, mapping.TargetPath); localName = epmAttributeNameBuilder.EpmNsUri; epmAnnotationBindings[localName] = GetODataAnnotationBinding(annotatable, localName, mapping.TargetNamespaceUri); string targetNamespacePrefix = mapping.TargetNamespacePrefix; if (!string.IsNullOrEmpty(targetNamespacePrefix)) { localName = epmAttributeNameBuilder.EpmNsPrefix; epmAnnotationBindings[localName] = GetODataAnnotationBinding(annotatable, localName, targetNamespacePrefix); } } else { localName = epmAttributeNameBuilder.EpmTargetPath; epmAnnotationBindings[localName] = GetODataAnnotationBinding(annotatable, localName, mapping.TargetSyndicationItem.ToAttributeValue()); localName = epmAttributeNameBuilder.EpmContentKind; epmAnnotationBindings[localName] = GetODataAnnotationBinding(annotatable, localName, mapping.TargetTextContentKind.ToAttributeValue()); } if (!skipSourcePath) { string sourcePath = mapping.SourcePath; if (removePrefix) { sourcePath = sourcePath.Substring(sourcePath.IndexOf('/') + 1); } localName = epmAttributeNameBuilder.EpmSourcePath; epmAnnotationBindings[localName] = GetODataAnnotationBinding(annotatable, localName, sourcePath); } string keepInContent = mapping.KeepInContent ? AtomConstants.AtomTrueLiteral : AtomConstants.AtomFalseLiteral; localName = epmAttributeNameBuilder.EpmKeepInContent; epmAnnotationBindings[localName] = GetODataAnnotationBinding(annotatable, localName, keepInContent); epmAttributeNameBuilder.MoveNext(); } model.SetAnnotationValues(epmAnnotationBindings.Values); }
internal static void SaveEpmAnnotations(this IEdmModel model, IEdmElement annotatable, IEnumerable<EntityPropertyMappingAttribute> mappings, bool skipSourcePath, bool removePrefix) { EpmAttributeNameBuilder builder = new EpmAttributeNameBuilder(); Dictionary<string, IEdmDirectValueAnnotationBinding> annotationBindingsToRemoveSerializableEpmAnnotations = model.GetAnnotationBindingsToRemoveSerializableEpmAnnotations(annotatable); foreach (EntityPropertyMappingAttribute attribute in mappings) { string epmKeepInContent; if (attribute.TargetSyndicationItem == SyndicationItemProperty.CustomProperty) { epmKeepInContent = builder.EpmTargetPath; annotationBindingsToRemoveSerializableEpmAnnotations[epmKeepInContent] = GetODataAnnotationBinding(annotatable, epmKeepInContent, attribute.TargetPath); epmKeepInContent = builder.EpmNsUri; annotationBindingsToRemoveSerializableEpmAnnotations[epmKeepInContent] = GetODataAnnotationBinding(annotatable, epmKeepInContent, attribute.TargetNamespaceUri); string targetNamespacePrefix = attribute.TargetNamespacePrefix; if (!string.IsNullOrEmpty(targetNamespacePrefix)) { epmKeepInContent = builder.EpmNsPrefix; annotationBindingsToRemoveSerializableEpmAnnotations[epmKeepInContent] = GetODataAnnotationBinding(annotatable, epmKeepInContent, targetNamespacePrefix); } } else { epmKeepInContent = builder.EpmTargetPath; annotationBindingsToRemoveSerializableEpmAnnotations[epmKeepInContent] = GetODataAnnotationBinding(annotatable, epmKeepInContent, attribute.TargetSyndicationItem.ToAttributeValue()); epmKeepInContent = builder.EpmContentKind; annotationBindingsToRemoveSerializableEpmAnnotations[epmKeepInContent] = GetODataAnnotationBinding(annotatable, epmKeepInContent, attribute.TargetTextContentKind.ToAttributeValue()); } if (!skipSourcePath) { string sourcePath = attribute.SourcePath; if (removePrefix) { sourcePath = sourcePath.Substring(sourcePath.IndexOf('/') + 1); } epmKeepInContent = builder.EpmSourcePath; annotationBindingsToRemoveSerializableEpmAnnotations[epmKeepInContent] = GetODataAnnotationBinding(annotatable, epmKeepInContent, sourcePath); } string str4 = attribute.KeepInContent ? "true" : "false"; epmKeepInContent = builder.EpmKeepInContent; annotationBindingsToRemoveSerializableEpmAnnotations[epmKeepInContent] = GetODataAnnotationBinding(annotatable, epmKeepInContent, str4); builder.MoveNext(); } model.SetAnnotationValues(annotationBindingsToRemoveSerializableEpmAnnotations.Values); }
/// <summary> /// Writes the content for csdl corresponding to EpmProperties given in the <paramref name="epmInfos"/> /// </summary> /// <param name="xmlWriter">XmlWriter to write to</param> /// <param name="epmInfos">List of properties to write to csdl</param> /// <param name="skipSourcePath">Should source path be given</param> /// <param name="removePrefix">Remove prefix from source path for complex types</param> private static void WriteEpmProperties(XmlWriter xmlWriter, IEnumerable<EntityPropertyMappingAttribute> epmInfos, bool skipSourcePath, bool removePrefix) { EpmAttributeNameBuilder epmAttributeNameBuilder = new EpmAttributeNameBuilder(); foreach (EntityPropertyMappingAttribute attr in epmInfos) { // Decide b/w atom & non-atom if (attr.TargetSyndicationItem == SyndicationItemProperty.CustomProperty) { xmlWriter.WriteAttributeString( epmAttributeNameBuilder.EpmTargetPath, XmlConstants.DataWebMetadataNamespace, attr.TargetPath); xmlWriter.WriteAttributeString( epmAttributeNameBuilder.EpmNsUri, XmlConstants.DataWebMetadataNamespace, attr.TargetNamespaceUri); if (!String.IsNullOrEmpty(attr.TargetNamespacePrefix)) { xmlWriter.WriteAttributeString( epmAttributeNameBuilder.EpmNsPrefix, XmlConstants.DataWebMetadataNamespace, attr.TargetNamespacePrefix); } } else { xmlWriter.WriteAttributeString( epmAttributeNameBuilder.EpmTargetPath, XmlConstants.DataWebMetadataNamespace, ObjectContextServiceProvider.MapSyndicationPropertyToEpmTargetPath(attr.TargetSyndicationItem)); xmlWriter.WriteAttributeString( epmAttributeNameBuilder.EpmContentKind, XmlConstants.DataWebMetadataNamespace, ObjectContextServiceProvider.MapSyndicationTextContentKindToEpmContentKind(attr.TargetTextContentKind)); } if (!skipSourcePath) { xmlWriter.WriteAttributeString( epmAttributeNameBuilder.EpmSourcePath, XmlConstants.DataWebMetadataNamespace, removePrefix ? attr.SourcePath.Substring(attr.SourcePath.IndexOf('/') + 1) : attr.SourcePath); } xmlWriter.WriteAttributeString( epmAttributeNameBuilder.EpmKeepInContent, XmlConstants.DataWebMetadataNamespace, attr.KeepInContent ? XmlConstants.XmlTrueLiteral : XmlConstants.XmlFalseLiteral); epmAttributeNameBuilder.MoveNext(); } }
/// <summary> /// Obtains the epm information for a single property by reading csdl content /// </summary> /// <param name="extendedProperties">Collection of extended metadata properties for a resource</param> /// <param name="typeName">Type for which we are reading the metadata properties</param> /// <param name="memberName">Member for which we are reading the metadata properties</param> /// <returns>EpmPropertyInformation corresponding to read metadata properties</returns> private static IEnumerable <EpmPropertyInformation> GetEpmPropertyInformation(IEnumerable <MetadataProperty> extendedProperties, String typeName, String memberName) { EpmAttributeNameBuilder epmAttributeNameBuilder = new EpmAttributeNameBuilder(); while (true) { bool pathGiven = true; // EpmTargetPath is the only non-optional EPM attribute. If it is declared we need to take care of mapping. MetadataProperty epmTargetPathProperty = FindSingletonExtendedProperty( extendedProperties, epmAttributeNameBuilder.EpmTargetPath, typeName, memberName); if (epmTargetPathProperty != null) { // By default, we keep the copy in content for backwards compatibility bool epmKeepInContent = true; MetadataProperty epmKeepInContentProperty = FindSingletonExtendedProperty( extendedProperties, epmAttributeNameBuilder.EpmKeepInContent, typeName, memberName); if (epmKeepInContentProperty != null) { if (!Boolean.TryParse(Convert.ToString(epmKeepInContentProperty.Value, CultureInfo.InvariantCulture), out epmKeepInContent)) { throw new InvalidOperationException(memberName == null ? Strings.ObjectContext_InvalidValueForEpmPropertyType(epmAttributeNameBuilder.EpmKeepInContent, typeName) : Strings.ObjectContext_InvalidValueForEpmPropertyMember(epmAttributeNameBuilder.EpmKeepInContent, memberName, typeName)); } } MetadataProperty epmSourcePathProperty = FindSingletonExtendedProperty( extendedProperties, epmAttributeNameBuilder.EpmSourcePath, typeName, memberName); String epmSourcePath; if (epmSourcePathProperty == null) { if (memberName == null) { throw new InvalidOperationException(Strings.ObjectContext_MissingExtendedAttributeType(epmAttributeNameBuilder.EpmSourcePath, typeName)); } pathGiven = false; epmSourcePath = memberName; } else { epmSourcePath = Convert.ToString(epmSourcePathProperty.Value, CultureInfo.InvariantCulture); } String epmTargetPath = Convert.ToString(epmTargetPathProperty.Value, CultureInfo.InvariantCulture); // if the property is not a sydication property MapEpmTargetPathToSyndicationProperty // will return SyndicationItemProperty.CustomProperty SyndicationItemProperty targetSyndicationItem = MapEpmTargetPathToSyndicationProperty(epmTargetPath); MetadataProperty epmContentKindProperty = FindSingletonExtendedProperty( extendedProperties, epmAttributeNameBuilder.EpmContentKind, typeName, memberName); MetadataProperty epmNsPrefixProperty = FindSingletonExtendedProperty( extendedProperties, epmAttributeNameBuilder.EpmNsPrefix, typeName, memberName); MetadataProperty epmNsUriProperty = FindSingletonExtendedProperty( extendedProperties, epmAttributeNameBuilder.EpmNsUri, typeName, memberName); // ContentKind is mutually exclusive with NsPrefix and NsUri properties if (epmContentKindProperty != null) { if (epmNsPrefixProperty != null || epmNsUriProperty != null) { string epmPropertyName = epmNsPrefixProperty != null ? epmAttributeNameBuilder.EpmNsPrefix : epmAttributeNameBuilder.EpmNsUri; throw new InvalidOperationException(memberName == null ? Strings.ObjectContext_InvalidAttributeForNonSyndicationItemsType(epmPropertyName, typeName) : Strings.ObjectContext_InvalidAttributeForNonSyndicationItemsMember(epmPropertyName, memberName, typeName)); } } // epmNsPrefixProperty and epmNsUriProperty can be non-null only for non-Atom mapping. Since they are optional we need to check // if it was possible to map the target path to a syndication item name. if it was not (i.e. targetSyndicationItem == SyndicationItemProperty.CustomProperty) // this is a non-Atom kind of mapping. if (epmNsPrefixProperty != null || epmNsUriProperty != null || targetSyndicationItem == SyndicationItemProperty.CustomProperty) { String epmNsPrefix = epmNsPrefixProperty != null?Convert.ToString(epmNsPrefixProperty.Value, CultureInfo.InvariantCulture) : null; String epmNsUri = epmNsUriProperty != null?Convert.ToString(epmNsUriProperty.Value, CultureInfo.InvariantCulture) : null; yield return(new EpmPropertyInformation { IsAtom = false, KeepInContent = epmKeepInContent, SourcePath = epmSourcePath, PathGiven = pathGiven, TargetPath = epmTargetPath, NsPrefix = epmNsPrefix, NsUri = epmNsUri }); } else { SyndicationTextContentKind syndicationContentKind; if (epmContentKindProperty != null) { syndicationContentKind = MapEpmContentKindToSyndicationTextContentKind( Convert.ToString(epmContentKindProperty.Value, CultureInfo.InvariantCulture), typeName, memberName); } else { syndicationContentKind = SyndicationTextContentKind.Plaintext; } yield return(new EpmPropertyInformation { IsAtom = true, KeepInContent = epmKeepInContent, SourcePath = epmSourcePath, PathGiven = pathGiven, SyndicationItem = targetSyndicationItem, ContentKind = syndicationContentKind }); } epmAttributeNameBuilder.MoveNext(); } else { yield break; } } }