// Create an EntityItem from an ElasticTableEntity. public EntityItem(ElasticTableEntity entity) { this.Properties = entity.Properties; this.PartitionKey = entity.PartitionKey; this.RowKey = entity.RowKey; this.Timestamp = entity.Timestamp; this.ETag = entity.ETag; // Create and populate Fields dictionary, used for data binding. this.Fields = new Dictionary<string, string>(); // Add row key, partition key, and timestamp to the fields collection. this.Fields.Add("PartitionKey", entity.PartitionKey); this.Fields.Add("RowKey", entity.RowKey); this.Fields.Add("Timestamp", entity.Timestamp.ToString()); // Add each entity property to the fields collection. foreach (KeyValuePair<String, EntityProperty> prop in entity.Properties) { if (prop.Value == null || prop.Value.PropertyAsObject == null) { this.Fields.Add(prop.Key, NULL_VALUE); } else { this.Fields.Add(prop.Key, prop.Value.PropertyAsObject.ToString()); } } }
// Create an EntityItem from an ElasticTableEntity. public EntityItem(ElasticTableEntity entity) { this.Properties = entity.Properties; this.PartitionKey = entity.PartitionKey; this.RowKey = entity.RowKey; this.Timestamp = entity.Timestamp; this.ETag = entity.ETag; // Create and populate Fields dictionary, used for data binding. this.Fields = new Dictionary <string, string>(); // Add row key, partition key, and timestamp to the fields collection. this.Fields.Add("PartitionKey", entity.PartitionKey); this.Fields.Add("RowKey", entity.RowKey); this.Fields.Add("Timestamp", entity.Timestamp.ToString()); // Add each entity property to the fields collection. foreach (KeyValuePair <String, EntityProperty> prop in entity.Properties) { if (prop.Value == null || prop.Value.PropertyAsObject == null) { this.Fields.Add(prop.Key, NULL_VALUE); } else { this.Fields.Add(prop.Key, prop.Value.PropertyAsObject.ToString()); } } }
//***************** //* * //* InitForCopy * //* * //***************** // Initialize dialog for inserting a copy of an existing record. public void InitForCopy(CloudTable table, Dictionary<String, bool> tableColumnNames, ElasticTableEntity entity) { String fieldType = null; this.Entity = entity; this.IsAddNew = true; this.Table = table; this.Title = "Copy Entity"; this.CmdInsertUpdateEntity.Content = new TextBlock() { Text = "Insert Entity" }; this.Heading.Text = "Enter fields and values for a new entity."; if (tableColumnNames != null) { foreach (KeyValuePair<String, bool> col in tableColumnNames) { switch (col.Key) { case "PartitionKey": PartitionKey.Text = this.Entity.PartitionKey; break; case "RowKey": RowKey.Text = this.Entity.RowKey; break; case "Timestamp": break; default: { String value = NULL_VALUE; if (this.Entity.Properties.ContainsKey(col.Key)) { fieldType = this.Entity.Properties[col.Key].PropertyType.ToString(); switch (fieldType) { case "Binary": value = BitConverter.ToString(this.Entity.Properties[col.Key].BinaryValue).Replace("-", " "); break; default: value = this.Entity.Properties[col.Key].PropertyAsObject.ToString(); break; } if (value == null) { fieldType = "NULL"; } } if (value == NULL_VALUE) { value = String.Empty; fieldType = "Null"; } AddFieldRow(col.Key, fieldType, value); } break; } } } }
//****************************** //* * //* InsertUpdateEntity_Click * //* * //****************************** // Insert or update the entity in cloud storage. private void InsertUpdateEntity_Click(object sender, RoutedEventArgs e) { String action = "update entity"; if (IsAddNew) { action = "insert entity"; } // Construct entity ElasticTableEntity entity = new ElasticTableEntity(); entity.RowKey = RowKey.Text; entity.PartitionKey = PartitionKey.Text; int fieldId; String fieldName, fieldType, fieldValue; foreach (KeyValuePair<int, TextBox> field in fieldNames) { fieldId = field.Key; TextBox nameTextBox = field.Value; ComboBox typeComboBox = fieldTypes[fieldId]; TextBox valueTextBox = fieldValues[fieldId]; fieldName = nameTextBox.Text; if (String.IsNullOrEmpty(fieldName)) { MessageBox.Show("Cannot " + action + ": '" + fieldName + "' is not a valid propert name", "Invalid Property Name"); return; } ComboBoxItem item = typeComboBox.SelectedItem as ComboBoxItem; fieldType = item.Content as String; fieldValue = valueTextBox.Text; switch (fieldType) { case "Guid": { Guid guidValue; if (Guid.TryParse(fieldValue, out guidValue)) { entity[fieldName] = guidValue; } else { MessageBox.Show("Cannot update entity: " + fieldName + " does not contain a valid GUID value: " + fieldValue, "Invalid Value"); this.Cursor = Cursors.Arrow; return; } } break; case "String": entity[fieldName] = fieldValue; break; case "Binary": { try { string hexValues = fieldValue; string[] hexValuesSplit = hexValues.Split(' '); byte[] bytes = new byte[hexValuesSplit.Length]; int offset = 0; foreach (String hex in hexValuesSplit) { bytes[offset++] = (byte)Convert.ToInt32(hex, 16); } entity[fieldName] = bytes; } catch(Exception ex) { MessageBox.Show("Cannot " + action + ": " + fieldName + " does not contain a valid hexadecimal bytes representation: " + fieldValue, "Invalid Value"); this.Cursor = Cursors.Arrow; return; } } break; case "Boolean": { bool boolValue = false; switch (fieldValue.ToLower()) { case "1": case "true": case "yes": case "on": fieldValue = "True"; break; case "0": case "false": case "no": case "off": fieldValue = "False"; break; } if (Boolean.TryParse(fieldValue, out boolValue)) { entity[fieldName] = boolValue; } else { MessageBox.Show("Cannot " + action + ": " + fieldName + " does not contain a valid boolean value: " + fieldValue, "Invalid Value"); this.Cursor = Cursors.Arrow; return; } } break; case "DateTime": { DateTime dateValue; if (DateTime.TryParse(fieldValue, out dateValue)) { entity[fieldName] = dateValue; } else { MessageBox.Show("Cannot update entity: " + fieldName + " does not contain a valid DateTime value: " + fieldValue, "Invalid Value"); this.Cursor = Cursors.Arrow; return; } } break; case "Double": { double doubleValue = 0; if (Double.TryParse(fieldValue, out doubleValue)) { entity[fieldName] = doubleValue; } else { MessageBox.Show("Cannot " + action + ": " + fieldName + " does not contain a valid double-precision value: " + fieldValue, "Invalid Value"); this.Cursor = Cursors.Arrow; return; } } break; case "Int32": { int intValue = 0; if (Int32.TryParse(fieldValue, out intValue)) { entity[fieldName] = intValue; } else { MessageBox.Show("Cannot " + action + ": " + fieldName + " does not contain a valid Int32 value: " + fieldValue, "Invalid Value"); this.Cursor = Cursors.Arrow; return; } } break; case "Int64": { Int64 intValue = 0; if (Int64.TryParse(fieldValue, out intValue)) { entity[fieldName] = intValue; } else { MessageBox.Show("Cannot " + action + ": " + fieldName + " does not contain a valid Int64 value: " + fieldValue, "Invalid Value"); this.Cursor = Cursors.Arrow; return; } } break; case "Null": // Type "Null" means, do not add to entity. break; default: MessageBox.Show("Cannot " + action + ": unknown type '" + fieldType + "'"); this.Cursor = Cursors.Arrow; return; } } // next field try { if (IsAddNew) { // Insert entity and keep dialog open. this.Cursor = Cursors.Wait; Table.Execute(TableOperation.Insert(entity)); RecordsAdded++; Message.Text = "Records Added: " + RecordsAdded.ToString(); CmdClose.Content = new TextBlock() { Text = "Close" }; this.Cursor = Cursors.Arrow; RowKey.Focus(); } else { // Update entity and close dialog. this.Cursor = Cursors.Wait; entity.ETag = "*"; //Table.Execute(TableOperation.Merge(entity)); Table.Execute(TableOperation.Replace(entity)); RecordsUpdated++; Message.Text = "Records Updaed: " + RecordsUpdated.ToString(); CmdClose.Content = new TextBlock() { Text = "Close" }; this.Cursor = Cursors.Arrow; //RowKey.Focus(); DialogResult = true; } } catch (Exception ex) { this.Cursor = Cursors.Arrow; if (IsAddNew) { Message.Text = "Error inserting record: " + ex.Message; } else { Message.Text = "Error updating record: " + ex.Message; } RowKey.Focus(); } }
//***************** //* * //* WriteEntity * //* * //***************** // Write an entity, source from a dictionary of column names and values. private bool WriteEntity(string tableName, Dictionary<String, Object> dict, String partitionKeyColumnName, String rowKeyColumnName) { try { ElasticTableEntity entity = new ElasticTableEntity(); String fieldName, fieldValue; foreach (KeyValuePair<String, Object> field in dict) { fieldName = field.Key; fieldValue = field.Value as string; if (fieldName == partitionKeyColumnName) { entity.PartitionKey = fieldValue; } else if (fieldName == rowKeyColumnName) { entity.RowKey = fieldValue; } else if (fieldName == "Timestamp") { } else { entity[fieldName] = fieldValue; } } // next field CloudTable table = tableClient.GetTableReference(tableName); table.Execute(TableOperation.Insert(entity)); return true; } catch (Exception) { return false; } }
//***************** //* * //* WriteEntity * //* * //***************** // Write an entity, source from an array of column names and an array of values. private bool WriteEntity(string tableName, String[] columns, String[] values, String partitionKeyColumnName, String rowKeyColumnName) { try { ElasticTableEntity entity = new ElasticTableEntity(); String fieldName, fieldValue; int col = 0; foreach (String value in values) { fieldName = columns[col]; fieldValue = values[col]; if (fieldName == partitionKeyColumnName) { entity.PartitionKey = fieldValue; } else if (fieldName == rowKeyColumnName) { entity.RowKey = fieldValue; } else if (fieldName == "Timestamp") { } else { entity[fieldName] = fieldValue; } col++; } // next field CloudTable table = tableClient.GetTableReference(tableName); table.Execute(TableOperation.Insert(entity)); return true; } catch (Exception ex) { Console.WriteLine(ex.Message); return false; } }
//****************************** //* * //* InsertUpdateEntity_Click * //* * //****************************** // Insert or update the entity in cloud storage. private void InsertUpdateEntity_Click(object sender, RoutedEventArgs e) { String action = "update entity"; if (IsAddNew) { action = "insert entity"; } // Construct entity ElasticTableEntity entity = new ElasticTableEntity(); entity.RowKey = RowKey.Text; entity.PartitionKey = PartitionKey.Text; int fieldId; String fieldName, fieldType, fieldValue; foreach (KeyValuePair <int, TextBox> field in fieldNames) { fieldId = field.Key; TextBox nameTextBox = field.Value; ComboBox typeComboBox = fieldTypes[fieldId]; TextBox valueTextBox = fieldValues[fieldId]; fieldName = nameTextBox.Text; if (String.IsNullOrEmpty(fieldName)) { MessageBox.Show("Cannot " + action + ": '" + fieldName + "' is not a valid propert name", "Invalid Property Name"); return; } ComboBoxItem item = typeComboBox.SelectedItem as ComboBoxItem; fieldType = item.Content as String; fieldValue = valueTextBox.Text; switch (fieldType) { case "Guid": { Guid guidValue; if (Guid.TryParse(fieldValue, out guidValue)) { entity[fieldName] = guidValue; } else { MessageBox.Show("Cannot update entity: " + fieldName + " does not contain a valid GUID value: " + fieldValue, "Invalid Value"); this.Cursor = Cursors.Arrow; return; } } break; case "String": entity[fieldName] = fieldValue; break; case "Binary": { try { string hexValues = fieldValue; string[] hexValuesSplit = hexValues.Split(' '); byte[] bytes = new byte[hexValuesSplit.Length]; int offset = 0; foreach (String hex in hexValuesSplit) { bytes[offset++] = (byte)Convert.ToInt32(hex, 16); } entity[fieldName] = bytes; } catch (Exception ex) { MessageBox.Show("Cannot " + action + ": " + fieldName + " does not contain a valid hexadecimal bytes representation: " + fieldValue, "Invalid Value"); this.Cursor = Cursors.Arrow; return; } } break; case "Boolean": { bool boolValue = false; switch (fieldValue.ToLower()) { case "1": case "true": case "yes": case "on": fieldValue = "True"; break; case "0": case "false": case "no": case "off": fieldValue = "False"; break; } if (Boolean.TryParse(fieldValue, out boolValue)) { entity[fieldName] = boolValue; } else { MessageBox.Show("Cannot " + action + ": " + fieldName + " does not contain a valid boolean value: " + fieldValue, "Invalid Value"); this.Cursor = Cursors.Arrow; return; } } break; case "DateTime": { DateTime dateValue; if (DateTime.TryParse(fieldValue, out dateValue)) { entity[fieldName] = dateValue; } else { MessageBox.Show("Cannot update entity: " + fieldName + " does not contain a valid DateTime value: " + fieldValue, "Invalid Value"); this.Cursor = Cursors.Arrow; return; } } break; case "Double": { double doubleValue = 0; if (Double.TryParse(fieldValue, out doubleValue)) { entity[fieldName] = doubleValue; } else { MessageBox.Show("Cannot " + action + ": " + fieldName + " does not contain a valid double-precision value: " + fieldValue, "Invalid Value"); this.Cursor = Cursors.Arrow; return; } } break; case "Int32": { int intValue = 0; if (Int32.TryParse(fieldValue, out intValue)) { entity[fieldName] = intValue; } else { MessageBox.Show("Cannot " + action + ": " + fieldName + " does not contain a valid Int32 value: " + fieldValue, "Invalid Value"); this.Cursor = Cursors.Arrow; return; } } break; case "Int64": { Int64 intValue = 0; if (Int64.TryParse(fieldValue, out intValue)) { entity[fieldName] = intValue; } else { MessageBox.Show("Cannot " + action + ": " + fieldName + " does not contain a valid Int64 value: " + fieldValue, "Invalid Value"); this.Cursor = Cursors.Arrow; return; } } break; case "Null": // Type "Null" means, do not add to entity. break; default: MessageBox.Show("Cannot " + action + ": unknown type '" + fieldType + "'"); this.Cursor = Cursors.Arrow; return; } } // next field try { if (IsAddNew) { // Insert entity and keep dialog open. this.Cursor = Cursors.Wait; Table.Execute(TableOperation.Insert(entity)); RecordsAdded++; Message.Text = "Records Added: " + RecordsAdded.ToString(); CmdClose.Content = new TextBlock() { Text = "Close" }; this.Cursor = Cursors.Arrow; RowKey.Focus(); } else { // Update entity and close dialog. this.Cursor = Cursors.Wait; entity.ETag = "*"; //Table.Execute(TableOperation.Merge(entity)); Table.Execute(TableOperation.Replace(entity)); RecordsUpdated++; Message.Text = "Records Updaed: " + RecordsUpdated.ToString(); CmdClose.Content = new TextBlock() { Text = "Close" }; this.Cursor = Cursors.Arrow; //RowKey.Focus(); DialogResult = true; } } catch (Exception ex) { this.Cursor = Cursors.Arrow; if (IsAddNew) { Message.Text = "Error inserting record: " + ex.Message; } else { Message.Text = "Error updating record: " + ex.Message; } RowKey.Focus(); } }
//***************** //* * //* InitForCopy * //* * //***************** // Initialize dialog for inserting a copy of an existing record. public void InitForCopy(CloudTable table, Dictionary <String, bool> tableColumnNames, ElasticTableEntity entity) { String fieldType = null; this.Entity = entity; this.IsAddNew = true; this.Table = table; this.Title = "Copy Entity"; this.CmdInsertUpdateEntity.Content = new TextBlock() { Text = "Insert Entity" }; this.Heading.Text = "Enter fields and values for a new entity."; if (tableColumnNames != null) { foreach (KeyValuePair <String, bool> col in tableColumnNames) { switch (col.Key) { case "PartitionKey": PartitionKey.Text = this.Entity.PartitionKey; break; case "RowKey": RowKey.Text = this.Entity.RowKey; break; case "Timestamp": break; default: { String value = NULL_VALUE; if (this.Entity.Properties.ContainsKey(col.Key)) { fieldType = this.Entity.Properties[col.Key].PropertyType.ToString(); switch (fieldType) { case "Binary": value = BitConverter.ToString(this.Entity.Properties[col.Key].BinaryValue).Replace("-", " "); break; default: value = this.Entity.Properties[col.Key].PropertyAsObject.ToString(); break; } if (value == null) { fieldType = "NULL"; } } if (value == NULL_VALUE) { value = String.Empty; fieldType = "Null"; } AddFieldRow(col.Key, fieldType, value); } break; } } } }