From beb9d75a90274eb2b8bd4548df7ebf9c94d5e648 Mon Sep 17 00:00:00 2001 From: Daniel Schick Date: Mon, 21 Aug 2023 08:26:32 +0200 Subject: [PATCH] Excel Import berth in RoleEditor Tool --- src/RoleEditor/MainWindow.xaml | 6 +-- src/RoleEditor/MainWindow.xaml.cs | 63 ++++++++++++++++++++++++++----- 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/src/RoleEditor/MainWindow.xaml b/src/RoleEditor/MainWindow.xaml index 7f3fd26..8c38d01 100644 --- a/src/RoleEditor/MainWindow.xaml +++ b/src/RoleEditor/MainWindow.xaml @@ -34,9 +34,9 @@ - - - + + + diff --git a/src/RoleEditor/MainWindow.xaml.cs b/src/RoleEditor/MainWindow.xaml.cs index 0252d67..a98188a 100644 --- a/src/RoleEditor/MainWindow.xaml.cs +++ b/src/RoleEditor/MainWindow.xaml.cs @@ -45,6 +45,7 @@ namespace RoleEditor { InitializeComponent(); _dbManager = new(); + System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); // for ExcelDataReader on .NET Core } private async void Window_Loaded(object sender, RoutedEventArgs e) @@ -573,7 +574,7 @@ namespace RoleEditor #region Excel import - private void buttonImportBerths_Click(object sender, RoutedEventArgs e) + private async void buttonImportBerths_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofd = new OpenFileDialog { @@ -595,6 +596,8 @@ namespace RoleEditor using (var reader = ExcelReaderFactory.CreateReader(stream)) { List importBerthList = new(); + int bCounter = 0; + int pCounter = 0; try { @@ -606,25 +609,65 @@ namespace RoleEditor { throw new InvalidDataException("Sheet must have at least 2 Columns of data"); } - Berth b = new Berth(); - if (reader.IsDBNull(0) && reader.IsDBNull(1)) continue; - if (!reader.IsDBNull(0)) b.Name = reader.GetString(0); - string participant_name; - if (!reader.IsDBNull(1)) participant_name = reader.GetString(1); - importBerthList.Add(b); + if (reader.IsDBNull(0) && reader.IsDBNull(1)) continue; + string berth_name = ""; + if (!reader.IsDBNull(0)) berth_name = reader.GetString(0); + if (berth_name.Equals("Liegeplatz", StringComparison.OrdinalIgnoreCase)) continue; + + string participant_name = ""; + if (!reader.IsDBNull(1)) participant_name = reader.GetString(1); + + // find berth in existing list + if (_berths.Any(predicate: x => (x.Name != null) && x.Name.Equals(berth_name, StringComparison.OrdinalIgnoreCase))) + continue; + + Berth b = new Berth(); + b.Name = berth_name; + bool found_participant = false; + + foreach(Participant p in this._participants) + { + if ((p.Name != null) && p.Name.Contains(participant_name, StringComparison.OrdinalIgnoreCase)) + { + b.Participant_Id = p.Id; + found_participant = true; + break; + } + } + + if (!found_participant) + { + // create new participant + Participant p = new Participant(); + p.Name = participant_name; + await p.Save(_dbManager); + _participants.Add(p); + pCounter++; + b.Participant_Id = p.Id; + } + + await b.Save(_dbManager); + _berths.Add(b); + bCounter++; + } } while (reader.NextResult()); + + if((pCounter > 0) || (bCounter > 0)) + { + MessageBox.Show($"Imported {bCounter} berths and added {pCounter} participants while doing so"); + } + } catch (Exception ex) { MessageBox.Show("Error reading Excel: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); - } - - + } } stream.Close(); + } }