218 lines
6.9 KiB
C#
218 lines
6.9 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using Avalonia.Controls;
|
|
using Logof_Client;
|
|
|
|
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)
|
|
{
|
|
try
|
|
{
|
|
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;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Log($"Error while importing address patch: {ex.Message}", Logger.LogType.Error);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
|
|
public override string ToString()
|
|
{
|
|
try
|
|
{
|
|
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();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Log($"Error while parsing: {ex.Message}", Logger.LogType.Error);
|
|
}
|
|
|
|
return "Error while parsing";
|
|
}
|
|
|
|
public string GetCSVHeader(string sep)
|
|
{
|
|
string output = "";
|
|
|
|
if(has_refsid){
|
|
output += refsid_is + sep + " ";
|
|
}
|
|
if(has_anrede){
|
|
output += anrede_is + sep + " ";
|
|
}
|
|
if(has_titel){
|
|
output += titel_is + sep + " ";
|
|
}
|
|
if(has_vorname){
|
|
output += vorname_is + sep + " ";
|
|
}
|
|
if(has_adel){
|
|
output += adel_is + sep + " ";
|
|
}
|
|
if(has_name){
|
|
output += name_is + sep + " ";
|
|
}
|
|
if(has_namezus){
|
|
output += namezus_is + sep + " ";
|
|
}
|
|
if(has_anredzus){
|
|
output += anredzus_is + sep + " ";
|
|
}
|
|
if(has_strasse){
|
|
output += strasse_is + sep + " ";
|
|
}
|
|
if(has_strasse2){
|
|
output += strasse2_is + sep + " ";
|
|
}
|
|
if(has_plz){
|
|
output += plz_is + sep + " ";
|
|
}
|
|
if(has_ort){
|
|
output += ort_is + sep + " ";
|
|
}
|
|
if(has_land){
|
|
output += land_is + sep + " ";
|
|
}
|
|
if(has_pplz){
|
|
output += pplz_is + sep + " ";
|
|
}
|
|
if(has_postfach){
|
|
output += postfach_is + sep + " ";
|
|
}
|
|
if(has_name1){
|
|
output += name1_is + sep + " ";
|
|
}
|
|
if(has_name2){
|
|
output += name2_is + sep + " ";
|
|
}
|
|
if(has_name3){
|
|
output += name3_is + sep + " ";
|
|
}
|
|
if(has_name4){
|
|
output += name4_is + sep + " ";
|
|
}
|
|
if(has_name5){
|
|
output += name5_is + sep + " ";
|
|
}
|
|
if(has_funktion){
|
|
output += funktion_is + sep + " ";
|
|
}
|
|
if(has_funktion2){
|
|
output += funktion2_is + sep + " ";
|
|
}
|
|
if(has_abteilung){
|
|
output += abteilung_is + sep + " ";
|
|
}
|
|
if(has_funktionad){
|
|
output += funktionad_is + sep + " ";
|
|
}
|
|
|
|
return output;
|
|
}
|
|
} |