Exemplo n.º 1
0
        void OnReadingEntity(object sender, ReadingWritingEntityEventArgs e)
        {
            var bitbucketProp = e.Entity.GetType().GetProperty("DynamicProperties");

            IDictionary <string, object> bitbucket = null;

            if (bitbucketProp != null)
            {
                bitbucket = bitbucketProp.GetValue(e.Entity, null) as IDictionary <string, object>;
            }

            if (bitbucket != null)
            {
                var properties = e.Entity.GetType().GetProperties();

                var q = from p in e.Data.Element(AtomNamespace + "content")
                        .Element(AstoriaMetadataNamespace + "properties")
                        .Elements()
                        where properties.All(pp => pp.Name != p.Name.LocalName)
                        select new {
                    Name     = p.Name.LocalName,
                    IsNull   = string.Equals("true", p.Attribute(AstoriaMetadataNamespace + "null") == null ? null : p.Attribute(AstoriaMetadataNamespace + "null").Value, StringComparison.OrdinalIgnoreCase),
                    TypeName = p.Attribute(AstoriaMetadataNamespace + "type") == null ? null : p.Attribute(AstoriaMetadataNamespace + "type").Value,
                    p.Value
                };

                foreach (var dp in q)
                {
                    bitbucket[dp.Name] = GetTypedEdmValue(dp.TypeName, dp.Value, dp.IsNull);
                }
            }
        }
Exemplo n.º 2
0
        // This manually parses the XML that comes back.
        // This function uses code from this blog entry:
        // http://blogs.msdn.com/b/avkashchauhan/archive/2011/03/28/reading-and-saving-table-storage-entities-without-knowing-the-schema-or-updating-tablestorageentity-schema-at-runtime.aspx
        public static void OnReadingEntity(object sender, ReadingWritingEntityEventArgs args)
        {
            GenericEntity entity = args.Entity as GenericEntity;

            if (entity == null)
            {
                return;
            }

            // read each property, type and value in the payload
            var properties = args.Entity.GetType().GetProperties();
            var q          = from p in args.Data.Element(AtomNamespace + "content")
                             .Element(MetadataNamespace + "properties")
                             .Elements()
                             where properties.All(pp => pp.Name != p.Name.LocalName)
                             select new
            {
                Name     = p.Name.LocalName,
                IsNull   = string.Equals("true", p.Attribute(MetadataNamespace + "null") == null ? null : p.Attribute(MetadataNamespace + "null").Value, StringComparison.OrdinalIgnoreCase),
                TypeName = p.Attribute(MetadataNamespace + "type") == null ? null : p.Attribute(MetadataNamespace + "type").Value,
                p.Value
            };

            foreach (var dp in q)
            {
                string value = dp.Value;
                if (!string.IsNullOrWhiteSpace(value))
                {
                    value = string.Empty;
                }
                entity.properties[dp.Name] = dp.Value;
            }
        }
Exemplo n.º 3
0
        private void ctx_WritingEntity(object sender, ReadingWritingEntityEventArgs args)
        {
            GenericWriterEntity entity = args.Entity as GenericWriterEntity;

            if (entity == null)
            {
                return;
            }

            XElement properties = args.Data.Descendants(GenericTableReader.MetadataNamespace + "properties").First();

            for (int iColumnn = 0; iColumnn < _edmTypeNames.Length; iColumnn++)
            {
                string edmTypeName = _edmTypeNames[iColumnn];
                if (edmTypeName == null)
                {
                    continue;
                }

                string value      = entity._source.Values[iColumnn];
                string columnName = _columnNames[iColumnn];

                // framework will handle row + partition keys.
                XElement e = new XElement(GenericTableReader.DataNamespace + columnName, value);
                e.Add(new XAttribute(GenericTableReader.MetadataNamespace + "type", edmTypeName));

                properties.Add(e);
            }
        }
Exemplo n.º 4
0
        static void context_ReadingEntity(object sender, ReadingWritingEntityEventArgs args)
        {
            TableEntity entity = args.Entity as TableEntity;

            if (entity == null)
            {
                return;
            }

            // read each property, type and value in the payload
            var properties = args.Entity.GetType().GetProperties();
            var q          = from p in args.Data.Element(AtomNamespace + "content")
                             .Element(AstoriaMetadataNamespace + "properties")
                             .Elements()
                             where properties.All(pp => pp.Name != p.Name.LocalName)
                             select new
            {
                Name = p.Name.LocalName,
                p.Value
            };

            foreach (var dp in q)
            {
                entity[dp.Name] = dp.Value;
            }
        }
Exemplo n.º 5
0
        public virtual void ReadEntity(TableServiceContext context, ReadingWritingEntityEventArgs args)
        {
            var ns     = XNamespace.Get(DATASERVICESNS);
            var survey = args.Entity as IUDFModel;

            if (survey != null && survey.UserDefinedFields != null && survey.UserDefinedFields.Count > 0)
            {
                foreach (var udfItem in survey.UserDefinedFields)
                {
                    var udfField = args.Data.Descendants(ns + udfItem.Name).FirstOrDefault();
                    if (udfField != null)
                    {
                        try
                        {
                            udfItem.SetUDFValue(udfField.Value);
                        }
                        catch (Exception ex)
                        {
                            TraceHelper.TraceWarning("Cannot map value '{0}' to field '{1}': {2}", udfField.Value, udfItem.Name, ex.Message);
                            udfItem.ClearValue();
                        }
                    }
                    else
                    {
                        udfItem.ClearValue();
                    }
                }
            }
        }
        /// <summary>
        /// Called when the data services data context has finished trying to deserialize and entity
        /// from table storage.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Data.Services.Client.ReadingWritingEntityEventArgs"/> instance containing the event data.</param>
        private static void OnReadingEntity(object sender, ReadingWritingEntityEventArgs e)
        {
            GenericEntity entity = e.Entity as GenericEntity;

            if (null == entity)
            {
                return;
            }

            entity.TableName = e.Data.Element(AtomNS + "link").Attribute("title").Value;

            var q = from p in e.Data.Element(AtomNS + "content").Element(mNS + "properties").Elements()
                    select new
            {
                Name     = p.Name.LocalName,
                IsNull   = string.Equals("true", p.Attribute(mNS + "null") == null ? null : p.Attribute(mNS + "null").Value, StringComparison.OrdinalIgnoreCase),
                TypeName = p.Attribute(mNS + "type") == null ? "Edm.String" : p.Attribute(mNS + "type").Value,
                p.Value
            };

            foreach (var dp in q)
            {
                entity[dp.Name] = dp.TypeName;
            }
        }
 void DSCatalogClient_ReadingEntity(object sender, ReadingWritingEntityEventArgs e)
 {
     if (MergeOption != MergeOption.NoTracking)
     {
         ChangeTracker.Attach(e.Entity);
     }
 }
Exemplo n.º 8
0
        void _context_WritingEntity(object sender, ReadingWritingEntityEventArgs e)
        {
            // e.Data gives you the XElement for the Serialization of the Entity
            //Using XLinq  , you can  add/Remove properties to the element Payload
            XName    xnEntityProperties = XName.Get("properties", e.Data.GetNamespaceOfPrefix("m").NamespaceName);
            XElement xePayload          = null;

            foreach (PropertyInfo property in e.Entity.GetType().GetProperties())
            {
                //object[] doNotSerializeAttributes = property.GetCustomAttributes(typeof(DoNotSerializeAttribute), false);
                //if (doNotSerializeAttributes.Length > 0)
                //{
                //    if (xePayload == null)
                //    {
                //        xePayload = e.Data.Descendants().Where<XElement>(xe => xe.Name == xnEntityProperties).First<XElement>();
                //    }
                //    //The XName of the property we are going to remove from the payload
                //    XName xnProperty = XName.Get(property.Name, e.Data.GetNamespaceOfPrefix("d").NamespaceName);
                //    //Get the Property of the entity  you don't want sent to the server
                //    foreach (XElement xeRemoveThisProperty in xePayload.Descendants(xnProperty).ToList())
                //    {
                //        //Remove this property from the Payload sent to the server
                //        xeRemoveThisProperty.Remove();
                //    }
                //}
            }
        }
Exemplo n.º 9
0
        private void OnReadingEntity(object sender, ReadingWritingEntityEventArgs args)
        {
            IMediaContextContainer mediaContextContainer = args.Entity as IMediaContextContainer;

            if (mediaContextContainer != null)
            {
                mediaContextContainer.SetMediaContext(this._mediaContext);
            }
        }
        private void OnReadingEntity(object sender, ReadingWritingEntityEventArgs args)
        {
            ICloudMediaContextInit init = args.Entity as ICloudMediaContextInit;

            if (init != null)
            {
                init.InitCloudMediaContext(this._cloudMediaContext);
            }
        }
        private void OnReadingEntity(object sender, ReadingWritingEntityEventArgs e)
        {
            var package = (DataServicePackage)e.Entity;

            // REVIEW: This is the only way (I know) to download the package on demand
            // GetReadStreamUri cannot be evaluated inside of OnReadingEntity. Lazily evaluate it inside DownloadPackage
            package.Context    = Context;
            package.Downloader = _packageDownloader;
        }
        private void OnReadingEntity(object sender, ReadingWritingEntityEventArgs e)
        {
            var package = (DataServicePackage)e.Entity;

            var downloadUri = e.Data.Element(e.Data.Name.Namespace.GetName("content"))
                              .Attribute(System.Xml.Linq.XName.Get("src")).Value;

            package.DownloadUrl = new Uri(downloadUri);
            package.Downloader  = _packageDownloader;
        }
Exemplo n.º 13
0
        public override void ReadEntity(CloudTable context, ReadingWritingEntityEventArgs args)
        {
            var surveyRow = args.Entity as SurveyRow;

            if (surveyRow == null)
            {
                throw new InvalidOperationException("Strategy should be used to read/write a SurveyRow model.");
            }

            surveyRow.UserDefinedFields = this.UDFDictionary.InstanceFieldsFor <SurveyRow>(surveyRow.PartitionKey);

            base.ReadEntity(context, args);
        }
Exemplo n.º 14
0
        public virtual void WriteEntity(TableServiceContext context, ReadingWritingEntityEventArgs args)
        {
            var ns     = XNamespace.Get(DATASERVICESNS);
            var nsmd   = XNamespace.Get(DATASERVICESMETADATANS);
            var survey = args.Entity as SurveyRow;

            if (survey != null && survey.UserDefinedFields != null && survey.UserDefinedFields.Count > 0)
            {
                var properties = args.Data.Descendants(nsmd + "properties").First();
                foreach (var udfItem in survey.UserDefinedFields)
                {
                    var udfField = new XElement(ns + udfItem.Name, udfItem.Value);
                    udfField.Add(new XAttribute(nsmd + "type", udfItem.GetEdmType()));
                    properties.Add(udfField);
                }
            }
        }
Exemplo n.º 15
0
        static void context_WritingEntity(object sender, ReadingWritingEntityEventArgs e)
        {
            TableEntity entity = e.Entity as TableEntity;

            if (entity == null)
            {
                return;
            }

            XElement properties = e.Data.Element(AtomNamespace + "content").Element(AstoriaMetadataNamespace + "properties");

            properties.Element(AstoriaDataNamespace + "Values").Remove();

            foreach (KeyValuePair <string, string> pair in entity.GetProperties())
            {
                properties.Add(new XElement(AstoriaDataNamespace + pair.Key, pair.Value));
            }
        }
Exemplo n.º 16
0
        void HandleReadEntity(object sender, ReadingWritingEntityEventArgs e)
        {
            if (!(e.Entity is WadTableEntity))
            {
                return;
            }

            var properties = e.Data.Descendants(XName.Get("properties", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata")).FirstOrDefault();

            if (properties == null)
            {
                return;
            }

            var entity = e.Entity as WadTableEntity;

            foreach (var property in properties.Elements())
            {
                entity.Properties.Add(property.Name.LocalName, property.Value);
            }
        }
Exemplo n.º 17
0
        // Method that handles the WritingEntity event.
        void PhotoDataContainer_WritingEntity(object sender,
                                              ReadingWritingEntityEventArgs e)
        {
            // Define an XName for the properties element in the response payload.
            XName properties = XName.Get("properties",
                                         e.Data.GetNamespaceOfPrefix("m").NamespaceName);

            // Get the payload element that contains the properties of the entity to be sent.
            XElement payload = e.Data.DescendantsAndSelf()
                               .Where <XElement>(xe => xe.Name == properties).First <XElement>();

            // Define an XName for the property to remove from the payload.
            XName propertyName = XName.Get("StreamUri",
                                           e.Data.GetNamespaceOfPrefix("d").NamespaceName);

            //Get the element of the property to remove.
            XElement removeProperty = payload.Descendants()
                                      .Where <XElement>(xe => xe.Name == propertyName).First <XElement>();

            // Remove the property from the payload.
            removeProperty.Remove();
        }
Exemplo n.º 18
0
        private void TableContext_ReadingEntity(object sender, ReadingWritingEntityEventArgs e)
        {
            XName    xnProps = XName.Get("properties", e.Data.GetNamespaceOfPrefix("m").NamespaceName);
            XElement xeProps = e.Data.Descendants().Where(xe => xe.Name == xnProps).First();
            var      props   = new List <Property>();
            Guid     ID      = Guid.Empty;

            foreach (XElement prop in xeProps.Nodes())
            {
                if (prop.Name.LocalName == "PartitionKey")
                {
                    ((TableEntity)e.Entity).PartitionKey = prop.Value;
                }
                else if (prop.Name.LocalName == "RowKey")
                {
                    ((TableEntity)e.Entity).RowKey = prop.Value;
                }
                else if (prop.Name.LocalName.ToLower() == "EntityId".ToLower())
                {
                    ID = new Guid(prop.Value);
                }
                else if (prop.Name.LocalName == "Timestamp")
                {
                    ((TableEntity)e.Entity).Timestamp = DateTime.Parse(prop.Value);
                }
                else
                {
                    props.Add(new Property(prop.Name.LocalName, prop.Value));
                }
            }
            var entity = new Entity(ID);

            foreach (Property prop in props)
            {
                entity.AddProperty(prop.Name, prop.Value);
            }
            ((TableEntity)e.Entity).SetEntity(entity);
        }
Exemplo n.º 19
0
        private void GenericTableContext_ReadingEntity(object sender, ReadingWritingEntityEventArgs e)
        {
            var entity = e.Entity as GenericEntity;

            if (entity != null)
            {
                e.Data
                .Element(AtomNamespace + "content")
                .Element(AstoriaMetadataNamespace + "properties")
                .Elements()
                .Select(p =>
                        new
                {
                    Name     = p.Name.LocalName,
                    IsNull   = string.Equals("true", p.Attribute(AstoriaMetadataNamespace + "null") == null ? null : p.Attribute(AstoriaMetadataNamespace + "null").Value, StringComparison.OrdinalIgnoreCase),
                    TypeName = p.Attribute(AstoriaMetadataNamespace + "type") == null ? null : p.Attribute(AstoriaMetadataNamespace + "type").Value,
                    p.Value
                })
                .Select(dp => new Column(dp.Name, dp.TypeName, dp.Value.ToString()))
                .ToList()
                .ForEach(column => entity[column.ColumnName] = column);
            }
        }
Exemplo n.º 20
0
        private void OnWritingEntity(object sender, ReadingWritingEntityEventArgs e)
        {
            XName    xnProps = XName.Get("properties", e.Data.GetNamespaceOfPrefix("m").NamespaceName);
            XElement xeProps = e.Data.Descendants().Where(xe => xe.Name == xnProps).First();

            foreach (TableEntity.NameTypeValueTuple tuple in ((TableEntity)e.Entity).GetProperties())
            {
                if (tuple.Value is DateTime)
                {
                    tuple.Value = ConvertToUtc((DateTime)tuple.Value);
                }
                if (tuple.Type != null)
                {
                    xeProps.Add(new XElement(e.Data.GetNamespaceOfPrefix("d") + tuple.Name,
                                             new XAttribute(e.Data.GetNamespaceOfPrefix("m") + "type", tuple.Type),
                                             tuple.Value));
                }
                else
                {
                    xeProps.Add(new XElement(e.Data.GetNamespaceOfPrefix("d") + tuple.Name, tuple.Value));
                }
            }
        }