コード例 #1
0
 public GenericEntity(GenericEntity e)
 {
     this.PartitionKey = e.PartitionKey;
     this.RowKey = e.RowKey;
     this.Timestamp = e.Timestamp;
     this.properties = e.properties;
 }
コード例 #2
0
 public GenericEntity(GenericEntity e)
 {
     this.PartitionKey = e.PartitionKey;
     this.RowKey       = e.RowKey;
     this.Timestamp    = e.Timestamp;
     this.properties   = e.properties;
 }
コード例 #3
0
        private void GenericTableContext_ReadingEntity(object sender, System.Data.Services.Client.ReadingWritingEntityEventArgs e)
        {
            // TODO: Make these statics
            XNamespace AtomNamespace            = "http://www.w3.org/2005/Atom";
            XNamespace AstoriaDataNamespace     = "http://schemas.microsoft.com/ado/2007/08/dataservices";
            XNamespace AstoriaMetadataNamespace = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";

            GenericEntity entity = e.Entity as GenericEntity;

            if (entity == null)
            {
                return;
            }

            entity.SetTableName(e.Data.Element(AtomNamespace + "link").Attribute("title").Value);

            // read each property, type and value in the payload
            //var properties = e.Entity.GetType().GetProperties();
            //where properties.All(pp => pp.Name != p.Name.LocalName)
            var q = from p in e.Data.Element(AtomNamespace + "content")
                    .Element(AstoriaMetadataNamespace + "properties")
                    .Elements()
                    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)
            {
                entity[dp.Name] = GetType(dp.TypeName);
            }
        }
コード例 #4
0
        public EntityViewModel(GenericEntity entity, Dictionary<string, Column> columnNames)
        {
            Properties = new ObservableCollection<Property>();
            ColumnNames = new List<Column>();
            if (columnNames != null)
            {
                foreach (KeyValuePair<string, Column> kvp in columnNames)
                {
                    if (!String.IsNullOrEmpty(kvp.Key))
                    {
                        ColumnNames.Add(kvp.Value);
                    }
                }
            }

            Entity = entity;

            base.DisplayName = entity.Key();
        }
コード例 #5
0
        private void NewEntityCommandExecute()
        {
            GenericEntity entity = new GenericEntity();
            EditEntityDialog dlg = new EditEntityDialog("new");
            dlg.Owner = MainWindow.Window;
            dlg.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            dlg.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            EntityViewModel evm = new EntityViewModel(entity, ViewModel.TableColumnNames);
            dlg.DataContext = evm;
            if (dlg.ShowDialog().Value)
            {
                string tableName = (FolderTree.SelectedItem as TreeItem).Text;

                ViewModel.NewEntity(tableName, evm.UpdatedEntity);
            }
        }
コード例 #6
0
        public bool DeleteEntity(string tableName, GenericEntity targetEntity)
        {
            try
            {
                DetailSpinnerVisible = Visibility.Visible;
                ClearStatus();

                CloudTableClient tableClient = CloudStorageAccount.CreateCloudTableClient();
                TableServiceContext tableServiceContext = CreateTableServiceContext(tableClient);

                IQueryable<GenericEntity> entities =
                    (from entity in tableServiceContext.CreateQuery<GenericEntity>(tableName)
                     where entity.PartitionKey == targetEntity.PartitionKey &&
                           entity.RowKey == targetEntity.RowKey
                     select entity);

                GenericEntity entityToDelete = entities.FirstOrDefault();
                if (entityToDelete != null)
                {
                    tableServiceContext.DeleteObject(entityToDelete);
                    tableServiceContext.SaveChanges();
                }

                ReportSuccess("Entity " + targetEntity.Key() + " deleted");

                return true;
            }
            catch (Exception ex)
            {
                ReportException(ex);
                RefreshDetail(tableName);
                return false;
            }
        }
コード例 #7
0
        // Upload entities from a plain XML file. Take all children of the root to be entities.
        void Background_UploadEntitiesPlainXML(object sender, DoWorkEventArgs e)
        {
            try
            {
                int count = 0;
                int errors = 0;

                bool havePartitionKey = false;
                bool haveRowKey = false;

                CloudTableClient tableClient = CloudStorageAccount.CreateCloudTableClient();
                TableServiceContext tableServiceContext = CreateTableServiceContext(tableClient);
                tableClient.CreateTableIfNotExist(UploadTable);

                XDocument doc = XDocument.Parse(File.ReadAllText(UploadFile));

                XElement rootNode = doc.Root;

                List<XElement> entities = new List<XElement>(from XElement ent in rootNode.Elements() select ent);

                GenericEntity entity;
                string name;
                XAttribute type;
                object value;
                int row = 1;

                foreach (XElement en in entities)
                {
                    havePartitionKey = false;
                    haveRowKey = false;

                    entity = new GenericEntity();

                    foreach (XElement prop in en.Elements())
                    {
                        switch (prop.Name.LocalName)
                        {
                            case "PartitionKey":
                                entity.PartitionKey = prop.Value;
                                havePartitionKey = true;
                                break;
                            case "RowKey":
                                entity.RowKey = prop.Value;
                                haveRowKey = true;
                                break;
                            default:
                                name = prop.Name.LocalName;
                                value = prop.Value;
                                type = prop.Attribute("type");
                                if (prop.Attribute("null") != null && Convert.ToBoolean(prop.Attribute("null").Value))
                                {
                                    value = null;
                                }
                                else
                                {
                                    if (type != null)
                                    {
                                        value = Column.ConvertToStandardType(prop.Value, type.Value);
                                    }
                                }
                                entity[name] = value;
                                break;
                        }
                    }

                    if (!havePartitionKey)
                    {
                        entity.PartitionKey = string.Empty;
                    }

                    if (!haveRowKey)
                    {
                        entity.RowKey = row.ToString();
                    }

                    try
                    {
                        tableServiceContext.AddObject(UploadTable, entity);
                        tableServiceContext.SaveChanges();
                        count++;
                    }
                    catch (Exception ex)
                    {
                        if (errors == 0)
                        {
                            ReportException(ex);
                        }

                        tableServiceContext = CreateTableServiceContext(tableClient);

                        errors++;
                    }

                    row++;
                }

                RefreshDetail(UploadTable);

                if (errors > 0)
                {
                    if (count > 0)
                    {
                        ReportWarning("Upload complete, " + numberof(count, "entity", "entities") + " added, " + numberof(errors, "error", "errors"));
                    }
                    else
                    {
                        ReportError("Upload failed, " + numberof(count, "entity", "entities") + " added, " + numberof(errors, "error", "errors"));
                    }
                }
                else
                {
                    ReportSuccess("Upload complete, " + numberof(count, "entity", "entities") + " added");
                }
            }
            catch (Exception ex)
            {
                ReportException(ex);
            }
        }
コード例 #8
0
        // Upload entities from a Comma-Separated Values (CSV) file.
        void Background_UploadEntitiesCSV(object sender, DoWorkEventArgs e)
        {
            try
            {
                int count = 0;
                int errors = 0;

                CloudTableClient tableClient = CloudStorageAccount.CreateCloudTableClient();
                TableServiceContext tableServiceContext = CreateTableServiceContext(tableClient);
                tableClient.CreateTableIfNotExist(UploadTable);

                System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");

                TextReader tr = File.OpenText(UploadFile);

                List<Column> columns = new List<Column>();
                string line;

                if (UploadColumnHeaderRow)
                {
                    line = tr.ReadLine();

                    string[] columnItems;

                    foreach (string columnDef in r.Split(line))
                    {
                        columnItems = columnDef.Split(':');
                        switch (columnItems.Length)
                        {
                            case 1:
                                columns.Add(new Column(columnItems[0]));
                                break;
                            case 2:
                                columns.Add(new Column(columnItems[0], columnItems[1], null));
                                break;
                        }
                    }
                }

                int col;
                int row = 1;
                string value;
                string[] values;

                GenericEntity entity;
                while ((line = tr.ReadLine()) != null)
                {
                    try
                    {
                        values = r.Split(line);

                        entity = new GenericEntity();

                        if (UploadColumnHeaderRow)
                        {
                            col = 0;
                            foreach (Column columnDef in columns)
                            {
                                if (col < values.Length)
                                {
                                    switch (columnDef.Name)
                                    {
                                        case "PartitionKey":
                                            entity.PartitionKey = values[col];
                                            break;
                                        case "RowKey":
                                            entity.RowKey = values[col];
                                            break;
                                        //case "Timestamp":
                                        //    break;
                                        default:
                                            value = values[col];
                                            if (value == "null")
                                            {
                                                entity.Properties[columnDef.Name] = null;
                                            }
                                            else
                                            {
                                                if (value.StartsWith("\"") && value.EndsWith("\""))
                                                {
                                                    if (value.Length <= 2)
                                                    {
                                                        value = String.Empty;
                                                    }
                                                    else
                                                    {
                                                        value = value.Substring(1, value.Length - 2);
                                                    }
                                                }
                                                entity.Properties[columnDef.Name] = Column.ConvertToStandardType(value, columnDef.Type);
                                            }
                                            break;
                                    }
                                }
                                col++;
                            }
                        }
                        else
                        {
                            // No defined columns - generate field names.

                            entity.PartitionKey = String.Empty;
                            entity.RowKey = row.ToString();
                            row++;

                            col = 1;
                            string fieldName;
                            foreach (string fieldValue in values)
                            {
                                fieldName = "Field" + col.ToString();
                                entity[fieldName] = fieldValue;
                                col++;
                            }
                        }

                        tableServiceContext.AddObject(UploadTable, entity);
                        tableServiceContext.SaveChanges();
                        count++;
                    }
                    catch (Exception ex)
                    {
                        if (errors == 0)
                        {
                            ReportException(ex);
                        }

                        tableServiceContext = CreateTableServiceContext(tableClient);

                        errors++;
                    }
                }

                RefreshDetail(UploadTable);

                if (errors > 0)
                {
                    if (count > 0)
                    {
                        ReportWarning("Upload complete, " + numberof(count, "entity", "entities") + " added, " + numberof(errors, "error", "errors"));
                    }
                    else
                    {
                        ReportError("Upload failed, " + numberof(count, "entity", "entities") + " added, " + numberof(errors, "error", "errors"));
                    }
                }
                else
                {
                    ReportSuccess("Upload complete, " + numberof(count, "entity", "entities") + " added");
                }
            }
            catch (Exception ex)
            {
                ReportException(ex);
            }
        }
コード例 #9
0
        public void UpdateEntity(string tableName, GenericEntity originalEntity, GenericEntity updatedEntity)
        {
            if (CloudStorageAccount == null) return;

            try
            {
                bool keyChanged = true;

                if (originalEntity.PartitionKey == updatedEntity.PartitionKey &&
                    originalEntity.RowKey == updatedEntity.RowKey)
                {
                    keyChanged = false;
                }

                ClearStatus();
                CloudTableClient tableClient = CloudStorageAccount.CreateCloudTableClient();
                TableServiceContext tableServiceContext = CreateTableServiceContext(tableClient);

                IQueryable<GenericEntity> entities =
                    (from entity in tableServiceContext.CreateQuery<GenericEntity>(tableName)
                     where entity.PartitionKey == originalEntity.PartitionKey && entity.RowKey == originalEntity.RowKey
                     select entity);
                List<GenericEntity> entitiesList = entities.ToList<GenericEntity>();

                if (entitiesList != null && entitiesList.Count() > 0)
                {
                    GenericEntity readEntity = entitiesList[0];

                    if (keyChanged)
                    {
                        // Key change, so add the new then delete the old.
                        tableServiceContext.AddObject(tableName, updatedEntity);
                        tableServiceContext.DeleteObject(readEntity);
                        tableServiceContext.SaveChanges();
                        ReportSuccess("Entity " + originalEntity.Key() + " updated, new key " + updatedEntity.Key());
                    }
                    else
                    {
                        // Key unchanged, so just update the entity.
                        readEntity.Properties = updatedEntity.Properties;
                        tableServiceContext.UpdateObject(readEntity);
                        tableServiceContext.SaveChanges(SaveChangesOptions.ReplaceOnUpdate);
                        ReportSuccess("Entity " + originalEntity.Key() + " updated, key unchanged");
                    }

                    RefreshDetail(tableName);
                }
                else
                {
                    ReportWarning("Entity " + originalEntity.Key() + " was not found");
                }
            }
            catch (Exception ex)
            {
                ReportException(ex);
            }
        }
コード例 #10
0
        public void NewEntity(string tableName, GenericEntity entity)
        {
            if (CloudStorageAccount == null) return;

            try
            {
                ClearStatus();
                CloudTableClient tableClient = CloudStorageAccount.CreateCloudTableClient();
                TableServiceContext tableServiceContext = CreateTableServiceContext(tableClient);

                tableServiceContext.AddObject(tableName, entity);
                tableServiceContext.SaveChanges();

                RefreshDetail(tableName);

                ReportSuccess("New entity added to table");
            }
            catch (Exception ex)
            {
                ReportException(ex);
            }
        }
コード例 #11
0
        // Upload entities from an AtomPub XML file. Take all children of the root to be entities.
        void Background_UploadEntitiesAtomPub(object sender, DoWorkEventArgs e)
        {
            try
            {
                int count = 0;
                int errors = 0;

                bool havePartitionKey = false;
                bool haveRowKey = false;

                XName typeAttrName = XName.Get("type", AstoriaMetadataNamespace.NamespaceName);
                XName nullAttrName = XName.Get("null", AstoriaMetadataNamespace.NamespaceName);

                CloudTableClient tableClient = CloudStorageAccount.CreateCloudTableClient();
                TableServiceContext tableServiceContext = CreateTableServiceContext(tableClient);
                var table = tableClient.GetTableReference(UploadTable);
                table.CreateIfNotExists();

                XDocument doc = XDocument.Parse(File.ReadAllText(UploadFile));

                XElement rootNode = doc.Root;

                List<XElement> entities = new List<XElement>(from XElement ent in rootNode.Elements() select ent);

                GenericEntity entity;
                string name;
                XAttribute typeAttr, nullAttr;
                object value;
                int row = 1;
                XElement content, properties;

                foreach (XElement entry in entities)
                {
                    havePartitionKey = false;
                    haveRowKey = false;

                    entity = new GenericEntity();

                    if (entry != null && entry.Name.LocalName == "entry")
                    {
                        content = entry.Element(AtomNamespace + "content");
                        if (content != null && content.Name.LocalName == "content")
                        {
                            properties = content.Element(AstoriaMetadataNamespace + "properties");
                            if (properties != null)
                            {
                                foreach (XElement prop in properties.Elements())
                                {
                                    switch (prop.Name.LocalName)
                                    {
                                        case "PartitionKey":
                                            entity.PartitionKey = prop.Value;
                                            havePartitionKey = true;
                                            break;
                                        case "RowKey":
                                            entity.RowKey = prop.Value;
                                            haveRowKey = true;
                                            break;
                                        default:
                                            name = prop.Name.LocalName;
                                            value = prop.Value;
                                            typeAttr = prop.Attribute(typeAttrName);
                                            nullAttr = prop.Attribute(nullAttrName);
                                            if (nullAttr != null &&
                                                Convert.ToBoolean(prop.Attribute(nullAttrName).Value))
                                            {
                                                value = null;
                                            }
                                            else
                                            {
                                                if (typeAttr != null)
                                                {
                                                    value = Column.ConvertToStandardType(prop.Value, typeAttr.Value);
                                                }
                                            }
                                            entity[name] = value;
                                            break;
                                    }
                                }

                                if (!havePartitionKey)
                                {
                                    entity.PartitionKey = string.Empty;
                                }

                                if (!haveRowKey)
                                {
                                    entity.RowKey = row.ToString();
                                }

                                try
                                {
                                    tableServiceContext.AddObject(UploadTable, entity);
                                    tableServiceContext.SaveChanges();
                                    count++;
                                }
                                catch (Exception ex)
                                {
                                    if (errors == 0)
                                    {
                                        ReportException(ex);
                                    }

                                    tableServiceContext = CreateTableServiceContext(tableClient);

                                    errors++;
                                }

                                row++;
                            }
                        }
                    }
                }

                RefreshDetail(UploadTable);

                if (errors > 0)
                {
                    if (count > 0)
                    {
                        ReportWarning("Upload complete, " + numberof(count, "entity", "entities") + " added, " + numberof(errors, "error", "errors"));
                    }
                    else
                    {
                        ReportError("Upload failed, " + numberof(count, "entity", "entities") + " added, " + numberof(errors, "error", "errors"));
                    }
                }
                else
                {
                    ReportSuccess("Upload complete, " + numberof(count, "entity", "entities") + " added");
                }
            }
            catch (Exception ex)
            {
                ReportException(ex);
            }
        }