Removed GenealogicalDataCommunication.WriteFile

This commit is contained in:
2023-07-21 03:48:06 -07:00
parent 33a142549a
commit 61f5fa20de
9 changed files with 111 additions and 178 deletions

View File

@ -276,18 +276,18 @@ internal abstract class GenealogicalDataCommunication
return result;
}
internal static Models.GenealogicalDataCommunication GetGenealogicalDataCommunication(GenealogicalDataCommunicationLines genealogicalDataCommunicationLines)
internal static Models.GenealogicalDataCommunication GetGenealogicalDataCommunication(bool first, GenealogicalDataCommunicationLines genealogicalDataCommunicationLines)
{
Models.GenealogicalDataCommunication result;
DateTime? birth;
DateTime? death;
DateTime? changed;
char sex = string.IsNullOrEmpty(genealogicalDataCommunicationLines.Sex) || !genealogicalDataCommunicationLines.Sex.Contains("1 SEX ") ? 'U' : genealogicalDataCommunicationLines.Sex.Split("1 SEX ")[1][0];
string? uId = string.IsNullOrEmpty(genealogicalDataCommunicationLines.UId) || !genealogicalDataCommunicationLines.UId.Contains("1 _UID ") ? null : genealogicalDataCommunicationLines.UId.Split("1 _UID ")[1];
char sex = string.IsNullOrEmpty(genealogicalDataCommunicationLines.Sex) || !genealogicalDataCommunicationLines.Sex.Contains("1 SEX ") ? 'U' : genealogicalDataCommunicationLines.Sex.Split("1 SEX ")[1].First();
string? name = string.IsNullOrEmpty(genealogicalDataCommunicationLines.Name) || !genealogicalDataCommunicationLines.Name.Contains("1 NAME ") ? null : genealogicalDataCommunicationLines.Name.Split("1 NAME ")[1];
string? suffix = string.IsNullOrEmpty(genealogicalDataCommunicationLines.Suffix) || !genealogicalDataCommunicationLines.Suffix.Contains("2 NSFX ") ? null : genealogicalDataCommunicationLines.Suffix.Split("2 NSFX ")[1];
string? surName = string.IsNullOrEmpty(genealogicalDataCommunicationLines.SurName) || !genealogicalDataCommunicationLines.SurName.Contains("2 SURN ") ? null : genealogicalDataCommunicationLines.SurName.Split("2 SURN ")[1];
string? nickName = string.IsNullOrEmpty(genealogicalDataCommunicationLines.NickName) || !genealogicalDataCommunicationLines.NickName.Contains("2 NICK ") ? null : genealogicalDataCommunicationLines.NickName.Split("2 NICK ")[1];
string nickName = string.IsNullOrEmpty(genealogicalDataCommunicationLines.NickName) || !genealogicalDataCommunicationLines.NickName.Contains("2 NICK ") ? throw new NotSupportedException() : genealogicalDataCommunicationLines.NickName.Split("2 NICK ")[1];
string? givenName = string.IsNullOrEmpty(genealogicalDataCommunicationLines.GivenName) || !genealogicalDataCommunicationLines.GivenName.Contains("2 GIVN ") ? null : genealogicalDataCommunicationLines.GivenName.Split("2 GIVN ")[1];
string? id = string.IsNullOrEmpty(genealogicalDataCommunicationLines.Id) || !genealogicalDataCommunicationLines.Id.Contains("0 @I") || !genealogicalDataCommunicationLines.Id.Contains("@ INDI") ? null : genealogicalDataCommunicationLines.Id.Split('@')[1][1..];
string[] birthLines = (from l in genealogicalDataCommunicationLines.Birth where l.Contains("2 DATE ") select l.Split("2 DATE ")[1]).ToArray();
@ -310,6 +310,8 @@ internal abstract class GenealogicalDataCommunication
bool alive = death is null && !genealogicalDataCommunicationLines.Death.Any(l => l == "1 DEAT Y");
(int age, _) = IAge.GetAge(DateTime.Now.Ticks, birth.Value.Ticks);
int hours = IPersonBirthday.GetHour(alive, sex);
if (!first)
hours += 2;
if (death == birth)
death = death.Value.AddHours(hours);
birth = birth.Value.AddHours(hours);
@ -319,83 +321,20 @@ internal abstract class GenealogicalDataCommunication
death = birth;
}
death ??= !genealogicalDataCommunicationLines.Death.Any(l => l == "1 DEAT Y") ? null : birth;
result = new(id, uId, name, givenName, surName, suffix, nickName, sex, birth, death, changed);
result = new(birth,
changed,
death,
givenName,
id,
name,
nickName,
sex,
suffix,
surName,
uId);
return result;
}
internal static void WriteFile(string personKeyFormatted, Models.PersonName personName, string[]? individualsLines, bool isDefaultName, string directory, Models.GenealogicalDataCommunication genealogicalDataCommunication, bool verify, bool first)
{
if (verify)
{
if (genealogicalDataCommunication.SurName != personName.Last.Value)
throw new Exception($"{genealogicalDataCommunication.Name} - {personKeyFormatted}");
}
string jrOrSr;
if (string.IsNullOrEmpty(personName.Alias.Value))
jrOrSr = string.Empty;
else
{
if (personName.Alias.Value.Contains(" Jr"))
jrOrSr = " Jr";
else if (personName.Alias.Value.Contains(" Sr"))
jrOrSr = " Sr";
else
jrOrSr = string.Empty;
}
string code = IPersonBirthday.GetHour(genealogicalDataCommunication.Death is null, genealogicalDataCommunication.Sex).ToString("00");
if (directory.EndsWith("00"))
directory = string.Concat(directory[..^2], code);
else if (directory.EndsWith("01"))
directory = string.Concat(directory[..^2], code);
if (!Directory.Exists(directory))
_ = Directory.CreateDirectory(directory);
if (first && !personKeyFormatted.EndsWith(code))
personKeyFormatted = $"{personKeyFormatted[..^2]}{code}";
List<string> lines = new();
if (individualsLines is null || !individualsLines.Any())
lines.Add($"0 @I{personKeyFormatted}@ INDI");
else
{
if (!individualsLines[0].StartsWith("0 @I"))
throw new NotSupportedException();
lines.Add(individualsLines[0]);
}
lines.Add($"1 NAME {personName.First.Value} /{personName.Last.Value}/{jrOrSr}");
if (!string.IsNullOrEmpty(personName.First.Value))
lines.Add($"2 GIVN {personName.First.Value}");
if (!string.IsNullOrEmpty(personName.Last.Value))
lines.Add($"2 SURN {personName.Last.Value}");
if (!string.IsNullOrEmpty(jrOrSr))
lines.Add($"2 NSFX {jrOrSr.Trim()}");
lines.Add($"2 NICK {personKeyFormatted}");
lines.Add($"1 SEX {genealogicalDataCommunication.Sex}");
if (genealogicalDataCommunication.Birth is not null)
{
Models.PersonBirthday personBirthday = new(genealogicalDataCommunication.Birth.Value);
if (!IPersonBirthday.IsCounterPersonBirthday(personBirthday))
{
lines.Add("1 BIRT");
lines.Add($"2 DATE {genealogicalDataCommunication.Birth.Value:dd MMM yyyy}");
}
}
if (genealogicalDataCommunication.Death is not null)
{
if (genealogicalDataCommunication?.Death is null || genealogicalDataCommunication.Death == genealogicalDataCommunication.Birth || IPersonBirthday.IsCounterPersonBirthday(new(genealogicalDataCommunication.Death.Value)))
lines.Add("1 DEAT Y");
else
{
lines.Add("1 DEAT");
lines.Add($"2 DATE {genealogicalDataCommunication.Death.Value:dd MMM yyyy}");
}
}
if (isDefaultName)
lines.Add("9 NOTE");
if (individualsLines is not null)
lines.AddRange(GetFilteredOutMapped(individualsLines));
string text = string.Join(Environment.NewLine, lines);
_ = IPath.WriteAllText(Path.Combine(directory, $"{personKeyFormatted}.pged"), text, updateDateWhenMatches: false, compareBeforeWrite: true);
}
internal static bool CleanDisplayDirectoryAllFilesAndWriteTicksGed(string mappingDefaultName, string personBirthdayFormat, List<Models.PersonContainer> personContainers, string[] headerLines, List<string[]> familyGroupLines, string[] footerLines, long ticks, string a2PeopleContentDirectory)
{
bool result = false;