diff --git a/AddressPatch.cs b/AddressPatch.cs new file mode 100644 index 0000000..6b9f7e9 --- /dev/null +++ b/AddressPatch.cs @@ -0,0 +1,118 @@ +using System; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; + +public class AddressPatch +{ + public string refsid_is { get; set; } = ""; + public string anrede_is { get; set; } = ""; + public string titel_is { get; set; } = ""; + public string vorname_is { get; set; } = ""; + public string adel_is { get; set; } = ""; + public string name_is { get; set; } = ""; + public string namezus_is { get; set; } = ""; + public string anredzus_is { get; set; } = ""; + public string strasse_is { get; set; } = ""; + public string strasse2_is { get; set; } = ""; + public string plz_is { get; set; } = ""; + public string ort_is { get; set; } = ""; + public string land_is { get; set; } = ""; + public string pplz_is { get; set; } = ""; + public string postfach_is { get; set; } = ""; + public string name1_is { get; set; } = ""; + public string name2_is { get; set; } = ""; + public string name3_is { get; set; } = ""; + public string name4_is { get; set; } = ""; + public string name5_is { get; set; } = ""; + public string funktion_is { get; set; } = ""; + public string funktion2_is { get; set; } = ""; + public string abteilung_is { get; set; } = ""; + public string funktionad_is { get; set; } = ""; + + public bool has_refsid { get; set; } + public bool has_anrede { get; set; } + public bool has_titel { get; set; } + public bool has_vorname { get; set; } + public bool has_adel { get; set; } + public bool has_name { get; set; } + public bool has_namezus { get; set; } + public bool has_anredzus { get; set; } + public bool has_strasse { get; set; } + public bool has_strasse2 { get; set; } + public bool has_plz { get; set; } + public bool has_ort { get; set; } + public bool has_land { get; set; } + public bool has_pplz { get; set; } + public bool has_postfach { get; set; } + public bool has_name1 { get; set; } + public bool has_name2 { get; set; } + public bool has_name3 { get; set; } + public bool has_name4 { get; set; } + public bool has_name5 { get; set; } + public bool has_funktion { get; set; } + public bool has_funktion2 { get; set; } + public bool has_abteilung { get; set; } + public bool has_funktionad { get; set; } + + public static AddressPatch Import(Uri filename) + { + var patch = new AddressPatch(); + + // Alle Zeilen aus der Datei laden + var lines = File.ReadAllLines(filename.LocalPath); + + // Alle Properties der Klasse (Strings und bools) + var properties = typeof(AddressPatch).GetProperties(BindingFlags.Public | BindingFlags.Instance); + + // Nur die Properties, die mit _is enden (also die String-Werte) + var stringProps = properties.Where(p => p.PropertyType == typeof(string) && p.Name.EndsWith("_is")); + + foreach (var prop in stringProps) + { + // Beispiel: prop.Name = "name_is" + var baseName = prop.Name.Substring(0, prop.Name.Length - 3); // "name" + + // In der Datei wird nach "name:" gesucht (ohne _is) + var line = lines.FirstOrDefault(l => l.StartsWith(baseName + ":")); + if (line != null) + { + // Wert extrahieren (alles nach dem Doppelpunkt) + var value = line.Substring(line.IndexOf(':') + 1).Trim(); + + // Wert im Patch-Objekt setzen + prop.SetValue(patch, value); + + // Passendes has_ Feld aktivieren, z.B. "has_name" + var hasProp = properties.FirstOrDefault(p => p.Name == "has_" + baseName); + if (hasProp != null && hasProp.PropertyType == typeof(bool)) hasProp.SetValue(patch, true); + } + } + + return patch; + } + + + public override string ToString() + { + var properties = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); + var stringProps = properties.Where(p => p.PropertyType == typeof(string) && p.Name.EndsWith("_is")); + + var lines = new StringBuilder(); + + foreach (var prop in stringProps) + { + // passendes has_ Feld + var hasProp = + properties.FirstOrDefault(p => p.Name == "has_" + prop.Name.Substring(0, prop.Name.Length - 3)); + if (hasProp != null && (bool)hasProp.GetValue(this)) + { + var value = (string)prop.GetValue(this); + lines.AppendLine($"{prop.Name} => {value}"); + } + } + + return lines.ToString().TrimEnd(); + } +} \ No newline at end of file diff --git a/DataImport.cs b/DataImport.cs index 4852fbb..245331c 100644 --- a/DataImport.cs +++ b/DataImport.cs @@ -5,8 +5,10 @@ namespace Logof_Client; public class DataImport { - public static (bool, KasAddressList) ImportKasAddressList(Uri pathToCsv, char separator = ',') + public static (bool, KasAddressList) ImportKasAddressList(Uri pathToCsv, AddressPatch patch = null, + char separator = ',') { + // Prüfen, ob die Datei existiert if (!File.Exists(pathToCsv.LocalPath)) { Console.WriteLine($"File not found: {pathToCsv}"); @@ -14,6 +16,8 @@ public class DataImport } using var reader = new StreamReader(pathToCsv.LocalPath); + + // Erste Zeile: CSV-Header lesen var headerLine = reader.ReadLine(); if (headerLine == null) { @@ -21,7 +25,12 @@ public class DataImport return (false, null); } - var imported = new KasAddressList(); + var headers = headerLine.Split(separator); // Header in Spaltennamen aufteilen (z. B. name, vorname, ort, ...) + + // Neue Adressliste anlegen + var imported = new KasAddressList(Path.GetFileNameWithoutExtension(pathToCsv.LocalPath)); + + // Zeilenweise durchgehen while (!reader.EndOfStream) { var line = reader.ReadLine(); @@ -30,46 +39,67 @@ public class DataImport var parts = line.Split(separator); - if (parts.Length < 24) - { - Console.WriteLine($"No enough columns in line: {line}"); - continue; - } + // Werte-Array vorbereiten – immer 24 Felder für KasPerson + var values = new string[24]; + for (var i = 0; i < 24; i++) + if (i < parts.Length) + values[i] = parts[i]; + else + values[i] = ""; // fehlende Spalten leer lassen + + // Patch anwenden (falls vorhanden) + if (patch != null) + for (var i = 0; i < headers.Length; i++) + { + var header = headers[i].Trim(); // z. B. "name", "vorname", "ort" + + // Das zugehörige Patch-Feld heißt z. B. "name_is" + var patchProperty = typeof(AddressPatch).GetProperty(header + "_is"); + var hasProperty = typeof(AddressPatch).GetProperty("has_" + header); + + if (patchProperty != null && hasProperty != null) + // Prüfen, ob das Patch-Feld aktiv ist + if ((bool)hasProperty.GetValue(patch)) + // Dann den Wert aus dem Patch übernehmen + values[i] = (string)patchProperty.GetValue(patch); + } try { + // KasPerson erstellen – Reihenfolge der Werte muss mit dem Konstruktor übereinstimmen var person = new KasPerson( - ParseInt(parts[0]), - parts[1], - parts[2], - parts[3], - parts[4], - parts[5], - parts[6], - parts[7], - parts[8], - parts[9], - ParseInt(parts[10]), - parts[11], - parts[12], - ParseInt(parts[13]), - parts[14], - parts[15], - parts[16], - parts[17], - parts[18], - parts[19], - parts[20], - parts[21], - parts[22], - parts[23] + ParseInt(values[0]), + values[1], + values[2], + values[3], + values[4], + values[5], + values[6], + values[7], + values[8], + values[9], + ParseInt(values[10]), + values[11], + values[12], + ParseInt(values[13]), + values[14], + values[15], + values[16], + values[17], + values[18], + values[19], + values[20], + values[21], + values[22], + values[23] ); + imported.KasPersons.Add(person); } catch (Exception ex) { Console.WriteLine($"Error while parsing line: {line} - {ex.Message}"); - Console.WriteLine($"{ex.StackTrace}"); + Console.WriteLine(ex.StackTrace); return (false, null); } } @@ -77,6 +107,7 @@ public class DataImport return (true, imported); } + private static int ParseInt(string input) { return int.TryParse(input, out var result) ? result : 0; diff --git a/MainWindow.axaml b/MainWindow.axaml index 9ae7985..e9adb57 100644 --- a/MainWindow.axaml +++ b/MainWindow.axaml @@ -44,7 +44,7 @@ @@ -63,11 +63,13 @@ - - +