[file:] better folder structuring
This commit is contained in:
118
Tasks/AddressPatch.cs
Normal file
118
Tasks/AddressPatch.cs
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
60
Tasks/CsvBuilder.cs
Normal file
60
Tasks/CsvBuilder.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Logof_Client;
|
||||
|
||||
public class CsvBuilder
|
||||
{
|
||||
private readonly string Header;
|
||||
private readonly List<object> Instances;
|
||||
private readonly KasAddressList KasAddressList;
|
||||
|
||||
public CsvBuilder(string header, List<object> instances)
|
||||
{
|
||||
Header = header;
|
||||
Instances = instances;
|
||||
}
|
||||
|
||||
public CsvBuilder(string header, KasAddressList instances)
|
||||
{
|
||||
Header = header;
|
||||
KasAddressList = instances;
|
||||
}
|
||||
|
||||
public string? BuildKas()
|
||||
{
|
||||
var result = new StringBuilder();
|
||||
|
||||
result.AppendLine(Header);
|
||||
foreach (var l in KasAddressList.KasPersons)
|
||||
|
||||
result.AppendLine(
|
||||
l.refsid + "," +
|
||||
l.anrede + "," +
|
||||
l.titel + "," +
|
||||
l.vorname + "," +
|
||||
l.adel + "," +
|
||||
l.name + "," +
|
||||
l.namezus + "," +
|
||||
l.anredzus + "," +
|
||||
l.strasse + "," +
|
||||
l.strasse2 + "," +
|
||||
l.plz + "," +
|
||||
l.ort + "," +
|
||||
l.land + "," +
|
||||
l.pplz + "," +
|
||||
l.postfach + "," +
|
||||
l.name1 + "," +
|
||||
l.name2 + "," +
|
||||
l.name3 + "," +
|
||||
l.name4 + "," +
|
||||
l.name5 + "," +
|
||||
l.funktion + "," +
|
||||
l.funktion2 + "," +
|
||||
l.abteilung + "," +
|
||||
l.funktionad);
|
||||
|
||||
// weitere Cases
|
||||
return result.ToString();
|
||||
}
|
||||
}
|
||||
244
Tasks/DataImport.cs
Normal file
244
Tasks/DataImport.cs
Normal file
@@ -0,0 +1,244 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Logof_Client;
|
||||
|
||||
public class DataImport
|
||||
{
|
||||
public static (bool, KasAddressList) ImportKasAddressList(Uri pathToCsv, AddressPatch patch = null,
|
||||
char separator = ',')
|
||||
{
|
||||
if (patch == null)
|
||||
return ImportKasAddressListWithoutPatch(pathToCsv, separator);
|
||||
return ImportKasAddressListWithPatch(pathToCsv, patch, separator);
|
||||
}
|
||||
|
||||
private static (bool, KasAddressList) ImportKasAddressListWithoutPatch(Uri pathToCsv, char separator)
|
||||
{
|
||||
if (!File.Exists(pathToCsv.LocalPath))
|
||||
{
|
||||
Console.WriteLine($"File not found: {pathToCsv}");
|
||||
return (false, null);
|
||||
}
|
||||
|
||||
using var reader = new StreamReader(pathToCsv.LocalPath);
|
||||
var headerLine = reader.ReadLine();
|
||||
if (headerLine == null)
|
||||
{
|
||||
Console.WriteLine("File is empty.");
|
||||
return (false, null);
|
||||
}
|
||||
|
||||
var imported =
|
||||
new KasAddressList(KasAddressList.GenerateName(Path.GetFileNameWithoutExtension(pathToCsv.LocalPath)));
|
||||
|
||||
while (!reader.EndOfStream)
|
||||
{
|
||||
var line = reader.ReadLine();
|
||||
if (string.IsNullOrWhiteSpace(line))
|
||||
continue;
|
||||
|
||||
var parts = line.Split(separator).Select(p => p.Trim()).ToArray();
|
||||
|
||||
if (parts.Length < 24)
|
||||
{
|
||||
Console.WriteLine($"Not enough columns in line: {line}");
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var person = new KasPerson(KasPerson.GenerateNewID(imported.KasPersons.Count),
|
||||
ParseInt(parts[0]),
|
||||
parts[1],
|
||||
parts[2],
|
||||
parts[3],
|
||||
parts[4],
|
||||
parts[5],
|
||||
parts[6],
|
||||
parts[7],
|
||||
parts[8],
|
||||
parts[9],
|
||||
parts[10],
|
||||
parts[11],
|
||||
parts[12],
|
||||
parts[13],
|
||||
parts[14],
|
||||
parts[15],
|
||||
parts[16],
|
||||
parts[17],
|
||||
parts[18],
|
||||
parts[19],
|
||||
parts[20],
|
||||
parts[21],
|
||||
parts[22],
|
||||
parts[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 (bool, KasAddressList) ImportKasAddressListWithPatch(Uri pathToCsv, AddressPatch patch,
|
||||
char separator)
|
||||
{
|
||||
if (!File.Exists(pathToCsv.LocalPath))
|
||||
{
|
||||
Console.WriteLine($"File not found: {pathToCsv}");
|
||||
return (false, null);
|
||||
}
|
||||
|
||||
using var reader = new StreamReader(pathToCsv.LocalPath);
|
||||
var headerLine = reader.ReadLine();
|
||||
if (headerLine == null)
|
||||
{
|
||||
Console.WriteLine("File is empty.");
|
||||
return (false, null);
|
||||
}
|
||||
|
||||
var headers = headerLine.Split(separator).Select(h => h.Trim()).ToArray();
|
||||
|
||||
var imported =
|
||||
new KasAddressList(KasAddressList.GenerateName(Path.GetFileNameWithoutExtension(pathToCsv.LocalPath)));
|
||||
var patchType = typeof(AddressPatch);
|
||||
var binding = BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase;
|
||||
|
||||
var hasProperties = patchType.GetProperties(binding)
|
||||
.Where(p => p.PropertyType == typeof(bool) && p.Name.StartsWith("has_", StringComparison.OrdinalIgnoreCase))
|
||||
.ToArray();
|
||||
|
||||
//var last_refsid = 1000000;
|
||||
|
||||
while (!reader.EndOfStream)
|
||||
{
|
||||
var line = reader.ReadLine();
|
||||
if (string.IsNullOrWhiteSpace(line))
|
||||
continue;
|
||||
|
||||
var parts = line.Split(separator).Select(p => p.Trim()).ToArray();
|
||||
|
||||
var fieldValues = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
//var refsid_existing = false;
|
||||
|
||||
foreach (var hasProp in hasProperties)
|
||||
{
|
||||
var fieldName = hasProp.Name.Substring(4);
|
||||
|
||||
|
||||
var hasObj = hasProp.GetValue(patch);
|
||||
var has = hasObj is bool b && b;
|
||||
|
||||
var patchProp = patchType.GetProperty(fieldName + "_is", binding);
|
||||
|
||||
string desiredHeader = null;
|
||||
if (has && patchProp != null)
|
||||
desiredHeader = patchProp.GetValue(patch)?.ToString();
|
||||
else
|
||||
desiredHeader = fieldName;
|
||||
|
||||
var resolvedValue = "";
|
||||
|
||||
if (!string.IsNullOrEmpty(desiredHeader))
|
||||
{
|
||||
var idx = Array.FindIndex(headers,
|
||||
h => string.Equals(h, desiredHeader, StringComparison.OrdinalIgnoreCase));
|
||||
if (idx >= 0 && idx < parts.Length)
|
||||
{
|
||||
resolvedValue = parts[idx];
|
||||
}
|
||||
else
|
||||
{
|
||||
var altIdx = Array.FindIndex(headers, h =>
|
||||
string.Equals(h, fieldName, StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(h, fieldName + "_is", StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (altIdx >= 0 && altIdx < parts.Length)
|
||||
resolvedValue = parts[altIdx];
|
||||
else
|
||||
resolvedValue = "";
|
||||
}
|
||||
}
|
||||
|
||||
fieldValues[fieldName] = resolvedValue ?? "";
|
||||
}
|
||||
|
||||
string GetField(string name)
|
||||
{
|
||||
return fieldValues.TryGetValue(name, out var v) ? v : "";
|
||||
}
|
||||
|
||||
var refsid = 0;
|
||||
if (patch.has_refsid)
|
||||
refsid = ParseInt(GetField("refsid"));
|
||||
|
||||
try
|
||||
{
|
||||
var person = new KasPerson(KasPerson.GenerateNewID(imported.KasPersons.Count), refsid,
|
||||
GetField("anrede"),
|
||||
GetField("titel"),
|
||||
GetField("vorname"),
|
||||
GetField("adel"),
|
||||
GetField("name"),
|
||||
GetField("namezus"),
|
||||
GetField("anredzus"),
|
||||
GetField("strasse"),
|
||||
GetField("strasse2"),
|
||||
GetField("plz"),
|
||||
GetField("ort"),
|
||||
GetField("land"),
|
||||
GetField("pplz"),
|
||||
GetField("postfach"),
|
||||
GetField("name1"),
|
||||
GetField("name2"),
|
||||
GetField("name3"),
|
||||
GetField("name4"),
|
||||
GetField("name5"),
|
||||
GetField("funktion"),
|
||||
GetField("funktion2"),
|
||||
GetField("abteilung"),
|
||||
GetField("funktionad")
|
||||
);
|
||||
|
||||
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);
|
||||
|
||||
// int GenerateNewRefsid()
|
||||
// {
|
||||
// var biggest = last_refsid;
|
||||
// foreach (var set in Settings._instance.addressSets.addresses)
|
||||
// foreach (var address in set.KasPersons)
|
||||
// if (biggest < address.id)
|
||||
// biggest = address.id + 1;
|
||||
//
|
||||
// last_refsid = biggest + 1;
|
||||
// return last_refsid;
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
private static int ParseInt(string input)
|
||||
{
|
||||
return int.TryParse(input, out var result) ? result : 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user