115 lines
3.8 KiB
C#
115 lines
3.8 KiB
C#
using System;
|
||
using System.IO;
|
||
|
||
namespace Logof_Client;
|
||
|
||
public class DataImport
|
||
{
|
||
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}");
|
||
return (false, null);
|
||
}
|
||
|
||
using var reader = new StreamReader(pathToCsv.LocalPath);
|
||
|
||
// Erste Zeile: CSV-Header lesen
|
||
var headerLine = reader.ReadLine();
|
||
if (headerLine == null)
|
||
{
|
||
Console.WriteLine("File is empty.");
|
||
return (false, null);
|
||
}
|
||
|
||
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();
|
||
if (string.IsNullOrWhiteSpace(line))
|
||
continue;
|
||
|
||
var parts = line.Split(separator);
|
||
|
||
// 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(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);
|
||
return (false, null);
|
||
}
|
||
}
|
||
|
||
return (true, imported);
|
||
}
|
||
|
||
|
||
private static int ParseInt(string input)
|
||
{
|
||
return int.TryParse(input, out var result) ? result : 0;
|
||
}
|
||
} |