fixed up the edit control for value mappings and added the star * as invalid value for a given key
This commit is contained in:
parent
1913328577
commit
2fe98ca6ae
@ -43,11 +43,21 @@
|
|||||||
|
|
||||||
</Grid>
|
</Grid>
|
||||||
<local:ENIDataGrid Grid.Row="1" Margin="2,8,2,2" x:Name="dataGridValueMappings" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"
|
<local:ENIDataGrid Grid.Row="1" Margin="2,8,2,2" x:Name="dataGridValueMappings" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"
|
||||||
SelectionMode="Extended" AutoGenerateColumns="False" CellEditEnding="dataGridValueMappings_CellEditEnding" CanUserAddRows="True"
|
SelectionMode="Extended" AutoGenerateColumns="False" CellEditEnding="dataGridValueMappings_CellEditEnding" CanUserAddRows="False"
|
||||||
MouseDoubleClick="dataGridValueMappings_MouseDoubleClick" BeginningEdit="dataGridValueMappings_BeginningEdit">
|
MouseDoubleClick="dataGridValueMappings_MouseDoubleClick" BeginningEdit="dataGridValueMappings_BeginningEdit">
|
||||||
|
<local:ENIDataGrid.RowStyle>
|
||||||
|
<Style TargetType="DataGridRow">
|
||||||
|
<Style.Triggers>
|
||||||
|
<DataTrigger Binding="{Binding Value}" Value="*">
|
||||||
|
<Setter Property="Background" Value="Pink"></Setter>
|
||||||
|
</DataTrigger>
|
||||||
|
</Style.Triggers>
|
||||||
|
</Style>
|
||||||
|
</local:ENIDataGrid.RowStyle>
|
||||||
<DataGrid.Columns>
|
<DataGrid.Columns>
|
||||||
<DataGridTextColumn x:Name="columnKey" Header="Key" Binding="{Binding Key, Mode=TwoWay}" IsReadOnly="False" />
|
<DataGridTextColumn x:Name="columnKey" Header="Key" Binding="{Binding Key, Mode=TwoWay}" IsReadOnly="False" />
|
||||||
<DataGridTextColumn x:Name="columnValue" Header="Value" Binding="{Binding Value, Mode=TwoWay}" IsReadOnly="False" />
|
<DataGridTextColumn x:Name="columnValue" Header="Value key" Binding="{Binding Value, Mode=TwoWay}" IsReadOnly="False" />
|
||||||
|
<DataGridTextColumn x:Name="columnValueText" Header="Value text" Binding="{Binding ValueText, Mode=TwoWay}" IsReadOnly="True" />
|
||||||
<DataGridTextColumn Header="Created" Binding="{Binding Created}" IsReadOnly="True" />
|
<DataGridTextColumn Header="Created" Binding="{Binding Created}" IsReadOnly="True" />
|
||||||
<DataGridTextColumn Header="Changed" Binding="{Binding Changed}" IsReadOnly="True" />
|
<DataGridTextColumn Header="Changed" Binding="{Binding Changed}" IsReadOnly="True" />
|
||||||
</DataGrid.Columns>
|
</DataGrid.Columns>
|
||||||
|
|||||||
@ -1,20 +1,12 @@
|
|||||||
using System;
|
using bsmd.database;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
using System.Windows.Data;
|
|
||||||
using System.Windows.Documents;
|
|
||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
using System.Windows.Media;
|
|
||||||
using System.Windows.Media.Imaging;
|
using System.Windows.Media.Imaging;
|
||||||
using System.Windows.Navigation;
|
|
||||||
using System.Windows.Shapes;
|
|
||||||
|
|
||||||
using bsmd.database;
|
|
||||||
|
|
||||||
namespace ENI2.Controls
|
namespace ENI2.Controls
|
||||||
{
|
{
|
||||||
@ -54,6 +46,25 @@ namespace ENI2.Controls
|
|||||||
|
|
||||||
delItem.Click += DelItem_Click;
|
delItem.Click += DelItem_Click;
|
||||||
this.dataGridValueMappings.ContextMenu.Items.Add(delItem);
|
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)
|
private async void DelItem_Click(object sender, RoutedEventArgs e)
|
||||||
@ -74,26 +85,61 @@ namespace ENI2.Controls
|
|||||||
|
|
||||||
private void AddItem_Click(object sender, RoutedEventArgs e)
|
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);
|
_mappings.Add(vm);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
private void dataGridValueMappings_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
|
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
|
// 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;
|
ValueMapping editedMapping = e.Row.Item as ValueMapping;
|
||||||
editedMapping.IsDirty = true;
|
|
||||||
foreach(ValueMapping vm in _mappings)
|
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
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void dataGridValueMappings_MouseDoubleClick(object sender, MouseButtonEventArgs e)
|
private void dataGridValueMappings_MouseDoubleClick(object sender, MouseButtonEventArgs e)
|
||||||
{
|
{
|
||||||
@ -119,9 +165,26 @@ namespace ENI2.Controls
|
|||||||
ValueMapping.MappingType mappingType = (ValueMapping.MappingType)this.comboBoxType.SelectedItem;
|
ValueMapping.MappingType mappingType = (ValueMapping.MappingType)this.comboBoxType.SelectedItem;
|
||||||
List<ValueMapping> mappings = await DBManagerAsync.LoadValuesForType(mappingType);
|
List<ValueMapping> mappings = await DBManagerAsync.LoadValuesForType(mappingType);
|
||||||
foreach (ValueMapping vm in mappings)
|
foreach (ValueMapping vm in mappings)
|
||||||
_mappings.Add(vm);
|
{
|
||||||
|
// 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)
|
private void dataGridValueMappings_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
|
||||||
@ -131,11 +194,18 @@ namespace ENI2.Controls
|
|||||||
|
|
||||||
private async void buttonSave_Click(object sender, RoutedEventArgs e)
|
private async void buttonSave_Click(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
|
int totalSaves = 0;
|
||||||
foreach(ValueMapping vm in _mappings)
|
foreach(ValueMapping vm in _mappings)
|
||||||
{
|
{
|
||||||
if (vm.Key.IsNullOrEmpty()) continue;
|
if (vm.Key.IsNullOrEmpty()) continue;
|
||||||
if (vm.IsNew || vm.IsDirty)
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -31,7 +31,9 @@ namespace bsmd.database
|
|||||||
{
|
{
|
||||||
SqlCommand cmd = new SqlCommand();
|
SqlCommand cmd = new SqlCommand();
|
||||||
entity.PrepareSave(cmd);
|
entity.PrepareSave(cmd);
|
||||||
return await PerformNonQueryAsync(cmd);
|
int result = await PerformNonQueryAsync(cmd);
|
||||||
|
if (result == 1) entity.IsDirty = false;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<int> DeleteAsync(DatabaseEntity entity)
|
public static async Task<int> DeleteAsync(DatabaseEntity entity)
|
||||||
|
|||||||
@ -5,14 +5,13 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
using System.Data.SqlClient;
|
using System.Data.SqlClient;
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace bsmd.database
|
namespace bsmd.database
|
||||||
{
|
{
|
||||||
public class ValueMapping : DatabaseEntityAsync
|
public class ValueMapping : DatabaseEntityAsync, INotifyPropertyChanged
|
||||||
{
|
{
|
||||||
|
|
||||||
#region Construction
|
#region Construction
|
||||||
@ -40,10 +39,14 @@ namespace bsmd.database
|
|||||||
private static Dictionary<MappingType, Dictionary<string, ValueMapping>> _dicts;
|
private static Dictionary<MappingType, Dictionary<string, ValueMapping>> _dicts;
|
||||||
private static Dictionary<MappingType, List<string>> _invalidKeys;
|
private static Dictionary<MappingType, List<string>> _invalidKeys;
|
||||||
|
|
||||||
|
private string _key, _value, _valueText;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
|
|
||||||
|
public event PropertyChangedEventHandler PropertyChanged;
|
||||||
|
|
||||||
public static Dictionary<MappingType, Dictionary<string, ValueMapping>> Dicts
|
public static Dictionary<MappingType, Dictionary<string, ValueMapping>> Dicts
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
@ -68,7 +71,6 @@ namespace bsmd.database
|
|||||||
// get the keys initialized that cannot be assigned
|
// get the keys initialized that cannot be assigned
|
||||||
foreach (MappingType type in Enum.GetValues(typeof(MappingType)))
|
foreach (MappingType type in Enum.GetValues(typeof(MappingType)))
|
||||||
_invalidKeys[type] = new List<string>();
|
_invalidKeys[type] = new List<string>();
|
||||||
_invalidKeys[MappingType.COUNTRY].Add("UK"); // can be mapped to GB and to UA .. we don't know what they want
|
|
||||||
}
|
}
|
||||||
return _invalidKeys;
|
return _invalidKeys;
|
||||||
}
|
}
|
||||||
@ -76,9 +78,37 @@ namespace bsmd.database
|
|||||||
|
|
||||||
public MappingType EntryType { get; private set; }
|
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; }
|
public DateTime? Created { get; private set; }
|
||||||
|
|
||||||
@ -102,12 +132,28 @@ namespace bsmd.database
|
|||||||
Key = key,
|
Key = key,
|
||||||
Value = value
|
Value = value
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (value.Equals("*"))
|
||||||
|
InvalidKeys[type].Add(key);
|
||||||
|
else
|
||||||
Dicts[type][key] = vm;
|
Dicts[type][key] = vm;
|
||||||
return await DBManagerAsync.SaveAsync(vm) == 1;
|
return await DBManagerAsync.SaveAsync(vm) == 1;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create through manual editing in global scope
|
||||||
|
/// </summary>
|
||||||
|
public static ValueMapping Create(MappingType type)
|
||||||
|
{
|
||||||
|
ValueMapping vm = new ValueMapping()
|
||||||
|
{
|
||||||
|
EntryType = type
|
||||||
|
};
|
||||||
|
return vm;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// deletes an entry and removes it from the database
|
/// deletes an entry and removes it from the database
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -141,9 +187,14 @@ namespace bsmd.database
|
|||||||
Dicts[type].Clear();
|
Dicts[type].Clear();
|
||||||
List<ValueMapping> mappings = await DBManagerAsync.LoadValuesForType(type);
|
List<ValueMapping> mappings = await DBManagerAsync.LoadValuesForType(type);
|
||||||
foreach (ValueMapping mapping in mappings)
|
foreach (ValueMapping mapping in mappings)
|
||||||
|
{
|
||||||
|
if (mapping.Value.Equals("*"))
|
||||||
|
InvalidKeys[type].Add(mapping.Key);
|
||||||
|
else
|
||||||
Dicts[type][mapping.Key] = mapping;
|
Dicts[type][mapping.Key] = mapping;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -196,8 +247,8 @@ namespace bsmd.database
|
|||||||
vm.id = reader.GetGuid(0);
|
vm.id = reader.GetGuid(0);
|
||||||
|
|
||||||
if (!reader.IsDBNull(1)) vm.EntryType = (ValueMapping.MappingType)reader.GetByte(1);
|
if (!reader.IsDBNull(1)) vm.EntryType = (ValueMapping.MappingType)reader.GetByte(1);
|
||||||
if (!reader.IsDBNull(2)) vm.Key = reader.GetString(2);
|
if (!reader.IsDBNull(2)) vm._key = reader.GetString(2);
|
||||||
if (!reader.IsDBNull(3)) vm.Value = reader.GetString(3);
|
if (!reader.IsDBNull(3)) vm._value = reader.GetString(3);
|
||||||
if (!reader.IsDBNull(4)) vm.Created = reader.GetDateTime(4);
|
if (!reader.IsDBNull(4)) vm.Created = reader.GetDateTime(4);
|
||||||
if (!reader.IsDBNull(5)) vm.Changed = reader.GetDateTime(5);
|
if (!reader.IsDBNull(5)) vm.Changed = reader.GetDateTime(5);
|
||||||
|
|
||||||
@ -207,5 +258,14 @@ namespace bsmd.database
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region protected methods
|
||||||
|
|
||||||
|
protected void OnPropertyChanged(string name = null)
|
||||||
|
{
|
||||||
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user