diff --git a/ENI2/Controls/ValueMappingsControl.xaml.cs b/ENI2/Controls/ValueMappingsControl.xaml.cs index 9c1783e5..7d657f9f 100644 --- a/ENI2/Controls/ValueMappingsControl.xaml.cs +++ b/ENI2/Controls/ValueMappingsControl.xaml.cs @@ -23,7 +23,7 @@ namespace ENI2.Controls /// public partial class ValueMappingsControl : UserControl { - private ObservableCollection _mappings = new ObservableCollection(); + private readonly ObservableCollection _mappings = new ObservableCollection(); private DataGridCellInfo activeCellAtEdit; public ValueMappingsControl() @@ -58,14 +58,13 @@ namespace ENI2.Controls private async void DelItem_Click(object sender, RoutedEventArgs e) { - ValueMapping vm = this.dataGridValueMappings.SelectedItem as ValueMapping; - if(vm != null) + if (this.dataGridValueMappings.SelectedItem is ValueMapping vm) { - if(MessageBox.Show($"Are you sure to delete {vm.Key} -> {vm.Value}?", Properties.Resources.textConfirmation, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == + if (MessageBox.Show($"Are you sure to delete {vm.Key} -> {vm.Value}?", Properties.Resources.textConfirmation, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes) { int result = await DBManagerAsync.DeleteAsync(vm); - if(result == 1) + if (result == 1) { _mappings.Remove(vm); } diff --git a/ENI2/ENI2.csproj b/ENI2/ENI2.csproj index c423ca3a..5f32b611 100644 --- a/ENI2/ENI2.csproj +++ b/ENI2/ENI2.csproj @@ -84,7 +84,7 @@ - F2C2D0164244EC89955EF50201EE24C2A300FF0B + 62DE8527C377957850DB503DA52FF66F664BD459 true diff --git a/bsmd.Tool/LocodeSQliteImport.cs b/bsmd.Tool/LocodeSQliteImport.cs index 94087354..08512b56 100644 --- a/bsmd.Tool/LocodeSQliteImport.cs +++ b/bsmd.Tool/LocodeSQliteImport.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using System.Data; using System.Data.SQLite; using log4net; +using System.Text.RegularExpressions; namespace bsmd.Tool { @@ -81,13 +82,18 @@ namespace bsmd.Tool insertCmd.Parameters.AddRange(new[] { p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18 }); updateCmd.Parameters.AddRange(new[] { p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, idParam }); + SQLiteCommand delCmd = new SQLiteCommand(connection); + delCmd.CommandText = "DELETE FROM locodes WHERE id = @ID"; + SQLiteParameter delIdParam = new SQLiteParameter("@ID", DbType.Int32); + delCmd.Parameters.Add(delIdParam); + string[] csvLines = File.ReadAllLines(csvFilePath); int updateCnt = 0, insertCnt = 0; for (int i = 0; i < csvLines.Length; i++) { string line = csvLines[i]; - string[] elems = line.Split(','); + string[] elems = Regex.Split(line, ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))"); if (elems.Length < 12) continue; string country = elems[1].Trim().Replace("\"", ""); if (country.Length < 2) continue; @@ -100,12 +106,30 @@ namespace bsmd.Tool lcode.Value = code; // Eingabeformat: https://service.unece.org/trade/locode/Service/LocodeColumn.htm - - object lookupResult = lookupCmd.ExecuteScalar(); - if ((lookupResult != null) && (lookupResult != DBNull.Value)) + int? lid = null; + using (SQLiteDataReader lookupReader = lookupCmd.ExecuteReader()) + { + while (lookupReader.Read()) + { + if (!lookupReader.IsDBNull(0)) + { + int tmp_id = lookupReader.GetInt32(0); + if (!lid.HasValue) { lid = tmp_id; } + else + { + // Dublette, die löschen wir gleich hier: + delIdParam.Value = tmp_id; + if(delCmd.ExecuteNonQuery() == 0) + { + _log.WarnFormat("Delete of {0} affected no rows.", tmp_id); + } + } + } + } + } + + if (lid.HasValue && lid > 0) { - int lid = Convert.ToInt32(lookupResult); - p3.Value = elems[3].Trim().Replace("\"", ""); p4.Value = elems[4].Trim().Replace("\"", ""); p5.Value = elems[5].Trim().Replace("\"", ""); @@ -181,13 +205,11 @@ namespace bsmd.Tool } Console.WriteLine($"deleting {deleteIds.Count} obsolete entries"); - - SQLiteCommand delCmd = new SQLiteCommand(connection); - delCmd.CommandText = "DELETE FROM locodes where id = @DELID"; + // diejenigen löschen, die nicht mehr in der Quell CSV Datei auftauchen foreach (int deleteId in deleteIds) { - delCmd.Parameters.AddWithValue("@DELID", deleteId); + delIdParam.Value = deleteId; if (delCmd.ExecuteNonQuery() != 1) _log.WarnFormat("{0} affected no rows", deleteId); } diff --git a/bsmd.database/ValueMapping.cs b/bsmd.database/ValueMapping.cs index a219308d..486945b1 100644 --- a/bsmd.database/ValueMapping.cs +++ b/bsmd.database/ValueMapping.cs @@ -38,6 +38,7 @@ namespace bsmd.database #region Fields private static readonly Dictionary> _dicts = new Dictionary>(); + private static Dictionary> _invalidKeys = null; #endregion @@ -48,6 +49,22 @@ namespace bsmd.database get { return _dicts; } } + public static Dictionary> InvalidKeys + { + get + { + if (_invalidKeys == null) + { + _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"); + } + return _invalidKeys; + } + } + public MappingType EntryType { get; private set; } public string Key { get; set; } diff --git a/misc/db.sqlite b/misc/db.sqlite index bb21acd9..47f397b6 100644 Binary files a/misc/db.sqlite and b/misc/db.sqlite differ