From 2fe98ca6ae118f45bf9fd753153fc3b72f3c51ee Mon Sep 17 00:00:00 2001 From: Daniel Schick Date: Fri, 26 May 2023 09:17:17 +0200 Subject: [PATCH] fixed up the edit control for value mappings and added the star * as invalid value for a given key --- ENI2/Controls/ValueMappingsControl.xaml | 14 ++- ENI2/Controls/ValueMappingsControl.xaml.cs | 106 +++++++++++++++++---- bsmd.database/DBManagerAsync.cs | 6 +- bsmd.database/ValueMapping.cs | 84 +++++++++++++--- 4 files changed, 176 insertions(+), 34 deletions(-) diff --git a/ENI2/Controls/ValueMappingsControl.xaml b/ENI2/Controls/ValueMappingsControl.xaml index 4d728e52..29bbe005 100644 --- a/ENI2/Controls/ValueMappingsControl.xaml +++ b/ENI2/Controls/ValueMappingsControl.xaml @@ -43,11 +43,21 @@ + + + - + + diff --git a/ENI2/Controls/ValueMappingsControl.xaml.cs b/ENI2/Controls/ValueMappingsControl.xaml.cs index aadb69f1..5aeec6c4 100644 --- a/ENI2/Controls/ValueMappingsControl.xaml.cs +++ b/ENI2/Controls/ValueMappingsControl.xaml.cs @@ -1,20 +1,12 @@ -using System; +using bsmd.database; +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; -using System.Windows.Data; -using System.Windows.Documents; using System.Windows.Input; -using System.Windows.Media; using System.Windows.Media.Imaging; -using System.Windows.Navigation; -using System.Windows.Shapes; - -using bsmd.database; namespace ENI2.Controls { @@ -54,6 +46,25 @@ namespace ENI2.Controls delItem.Click += DelItem_Click; this.dataGridValueMappings.ContextMenu.Items.Add(delItem); + + MenuItem invalidItem = new MenuItem + { + Header = "Set as invalid key", + Icon = new Image { Source = new BitmapImage(new Uri("pack://application:,,,/Resources/sign_warning_radiation.png")) } + }; + invalidItem.Click += InvalidItem_Click; + this.dataGridValueMappings.ContextMenu.Items.Add(invalidItem); + + } + + #region context menu event handler + + private void InvalidItem_Click(object sender, RoutedEventArgs e) + { + if (this.dataGridValueMappings.SelectedItem is ValueMapping vm) + { + vm.Value = "*"; + } } private async void DelItem_Click(object sender, RoutedEventArgs e) @@ -74,23 +85,58 @@ namespace ENI2.Controls private void AddItem_Click(object sender, RoutedEventArgs e) { - ValueMapping vm = new ValueMapping(); + ValueMapping.MappingType? mappingType = (ValueMapping.MappingType?) this.comboBoxType.SelectedItem; + if (mappingType == null) return; + ValueMapping vm = ValueMapping.Create(mappingType.Value); _mappings.Add(vm); } + #endregion + private void dataGridValueMappings_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) { // we need to check that there are no keys entered twice or changed into something that is already here - if(e.Column == columnKey) + string newValue = ((TextBox)e.EditingElement).Text; + if (newValue == null) return; + + if (e.Column == columnKey) { - TextBox t = e.EditingElement as TextBox; - string editedCellValue = t.Text; ValueMapping editedMapping = e.Row.Item as ValueMapping; - editedMapping.IsDirty = true; foreach(ValueMapping vm in _mappings) { - if (vm.Key == editedCellValue) + if (vm == editedMapping) continue; // dont compare with myself + if (vm.Key == newValue) + { + ((TextBox)e.EditingElement).Text = editedMapping.Key; e.Cancel = true; // hopefully this avoids writing back to the model + } + } + } + if(e.Column == columnValue) + { + ValueMapping editedMapping = e.Row.Item as ValueMapping; + + ValueMapping.MappingType? mappingType = (ValueMapping.MappingType)this.comboBoxType.SelectedItem; + if ((mappingType != null) && (newValue != null)) + { + switch (mappingType) + { + case ValueMapping.MappingType.GENDER: + if (Util.GlobalStructures.GenderDict.ContainsKey(newValue)) + editedMapping.ValueText = Util.GlobalStructures.GenderDict[newValue]; + else + editedMapping.ValueText = ""; + break; + case ValueMapping.MappingType.DOCUMENT_TYPE: + if (Util.GlobalStructures.IDDocTypeDict.ContainsKey(newValue)) + editedMapping.ValueText = Util.GlobalStructures.IDDocTypeDict[newValue]; + else + editedMapping.ValueText = ""; + break; + default: + editedMapping.ValueText = newValue; + break; + } } } } @@ -119,9 +165,26 @@ namespace ENI2.Controls ValueMapping.MappingType mappingType = (ValueMapping.MappingType)this.comboBoxType.SelectedItem; List mappings = await DBManagerAsync.LoadValuesForType(mappingType); foreach (ValueMapping vm in mappings) + { + // add "Klartext" + switch(mappingType) + { + case ValueMapping.MappingType.GENDER: + if (Util.GlobalStructures.GenderDict.ContainsKey(vm.Value)) + vm.ValueText = Util.GlobalStructures.GenderDict[vm.Value]; + break; + case ValueMapping.MappingType.DOCUMENT_TYPE: + if (Util.GlobalStructures.IDDocTypeDict.ContainsKey(vm.Value)) + vm.ValueText = Util.GlobalStructures.IDDocTypeDict[vm.Value]; + break; + default: + vm.ValueText = vm.Value; + break; + } + _mappings.Add(vm); + } } - } private void dataGridValueMappings_BeginningEdit(object sender, DataGridBeginningEditEventArgs e) @@ -131,11 +194,18 @@ namespace ENI2.Controls private async void buttonSave_Click(object sender, RoutedEventArgs e) { + int totalSaves = 0; foreach(ValueMapping vm in _mappings) { if (vm.Key.IsNullOrEmpty()) continue; if (vm.IsNew || vm.IsDirty) - await DBManagerAsync.SaveAsync(vm); + { + totalSaves += await DBManagerAsync.SaveAsync(vm); + } + } + if(totalSaves > 0) + { + MessageBox.Show($"{totalSaves} value mappings saved", "Success", MessageBoxButton.OK, MessageBoxImage.Information); } } } diff --git a/bsmd.database/DBManagerAsync.cs b/bsmd.database/DBManagerAsync.cs index b7599b0a..d3934b76 100644 --- a/bsmd.database/DBManagerAsync.cs +++ b/bsmd.database/DBManagerAsync.cs @@ -31,7 +31,9 @@ namespace bsmd.database { SqlCommand cmd = new SqlCommand(); entity.PrepareSave(cmd); - return await PerformNonQueryAsync(cmd); + int result = await PerformNonQueryAsync(cmd); + if (result == 1) entity.IsDirty = false; + return result; } public static async Task DeleteAsync(DatabaseEntity entity) @@ -98,7 +100,7 @@ namespace bsmd.database ValueMapping vm = new ValueMapping(); vm.PrepareLoadCommand(cmd, Message.LoadFilter.BY_TYPE, mappingType); SqlDataReader reader = await PerformCommandAsync(cmd); - return (await vm.LoadListAsync(reader)).ConvertAll(x => (ValueMapping)x); + return (await vm.LoadListAsync(reader)).ConvertAll(x => (ValueMapping)x); } #endregion diff --git a/bsmd.database/ValueMapping.cs b/bsmd.database/ValueMapping.cs index 1edd2a27..35b5980b 100644 --- a/bsmd.database/ValueMapping.cs +++ b/bsmd.database/ValueMapping.cs @@ -5,14 +5,13 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Data.SqlClient; -using System.Linq; -using System.Text; using System.Threading.Tasks; namespace bsmd.database { - public class ValueMapping : DatabaseEntityAsync + public class ValueMapping : DatabaseEntityAsync, INotifyPropertyChanged { #region Construction @@ -40,10 +39,14 @@ namespace bsmd.database private static Dictionary> _dicts; private static Dictionary> _invalidKeys; + private string _key, _value, _valueText; + #endregion #region Properties - + + public event PropertyChangedEventHandler PropertyChanged; + public static Dictionary> Dicts { get @@ -67,8 +70,7 @@ namespace bsmd.database _invalidKeys = new Dictionary>(); // get the keys initialized that cannot be assigned foreach (MappingType type in Enum.GetValues(typeof(MappingType))) - _invalidKeys[type] = new List(); - _invalidKeys[MappingType.COUNTRY].Add("UK"); // can be mapped to GB and to UA .. we don't know what they want + _invalidKeys[type] = new List(); } return _invalidKeys; } @@ -76,9 +78,37 @@ namespace bsmd.database public MappingType EntryType { get; private set; } - public string Key { get; set; } + public string Key + { + get { return _key; } + set + { + if (!value.Equals(_key)) this.IsDirty = true; + this._key = value; + this.OnPropertyChanged("Key"); + } + } - public string Value { get; set; } + public string Value + { + get { return _value; } + set + { + if (!value.Equals(_value)) this.IsDirty = true; + _value = value; + OnPropertyChanged("Value"); + } + } + + public string ValueText + { + get { return _valueText; } + set + { + this._valueText = value; + this.OnPropertyChanged("ValueText"); + } + } public DateTime? Created { get; private set; } @@ -102,12 +132,28 @@ namespace bsmd.database Key = key, Value = value }; - Dicts[type][key] = vm; + + if (value.Equals("*")) + InvalidKeys[type].Add(key); + else + Dicts[type][key] = vm; return await DBManagerAsync.SaveAsync(vm) == 1; } return false; } + /// + /// Create through manual editing in global scope + /// + public static ValueMapping Create(MappingType type) + { + ValueMapping vm = new ValueMapping() + { + EntryType = type + }; + return vm; + } + /// /// deletes an entry and removes it from the database /// @@ -141,7 +187,12 @@ namespace bsmd.database Dicts[type].Clear(); List mappings = await DBManagerAsync.LoadValuesForType(type); foreach (ValueMapping mapping in mappings) - Dicts[type][mapping.Key] = mapping; + { + if (mapping.Value.Equals("*")) + InvalidKeys[type].Add(mapping.Key); + else + Dicts[type][mapping.Key] = mapping; + } } } @@ -196,8 +247,8 @@ namespace bsmd.database vm.id = reader.GetGuid(0); if (!reader.IsDBNull(1)) vm.EntryType = (ValueMapping.MappingType)reader.GetByte(1); - if (!reader.IsDBNull(2)) vm.Key = reader.GetString(2); - if (!reader.IsDBNull(3)) vm.Value = reader.GetString(3); + if (!reader.IsDBNull(2)) vm._key = reader.GetString(2); + if (!reader.IsDBNull(3)) vm._value = reader.GetString(3); if (!reader.IsDBNull(4)) vm.Created = reader.GetDateTime(4); if (!reader.IsDBNull(5)) vm.Changed = reader.GetDateTime(5); @@ -207,5 +258,14 @@ namespace bsmd.database #endregion + #region protected methods + + protected void OnPropertyChanged(string name = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); + } + + #endregion + } }