Facebook logic, IsDefaultName, GetFiles update and

HardCoded tests
This commit is contained in:
2023-04-01 11:29:00 -07:00
parent b0b6864841
commit ba5bc6347c
36 changed files with 1396 additions and 402 deletions

View File

@ -1,3 +1,5 @@
using System.Text.Json;
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class Person
@ -49,19 +51,20 @@ internal abstract class Person
File.WriteAllLines(exportFile, cleanLines);
}
internal static (string[] headerLines, Dictionary<string, List<string>> individuals, string[] footerLines) GetIndividuals(string gedCOMFile)
internal static (string[] headerLines, Dictionary<string, List<string>> individuals, string[] footerLines) GetIndividuals(string? gedCOMFile)
{
Dictionary<string, List<string>> results = new();
string? nick;
int startAt = 0;
List<string> lines = new();
const string startsWith = "0 @";
List<string> headerLines = new();
List<string> footerLines = new();
string[] sourceLines = File.ReadAllLines(gedCOMFile);
string[] sourceLines = string.IsNullOrEmpty(gedCOMFile) ? Array.Empty<string>() : File.ReadAllLines(gedCOMFile);
for (int i = 0; i < sourceLines.Length; i++)
{
lines.Add(sourceLines[i]);
if (sourceLines[i].EndsWith("@ INDI"))
if (sourceLines[i].StartsWith(startsWith))
{
lines.RemoveAt(lines.Count - 1);
headerLines.AddRange(lines);
@ -72,41 +75,44 @@ internal abstract class Person
}
for (int i = startAt; i < sourceLines.Length; i++)
{
if (!sourceLines[i].StartsWith("0 @"))
if (!sourceLines[i].StartsWith(startsWith))
continue;
nick = null;
lines.Add(sourceLines[i]);
for (int j = i + 1; j < sourceLines.Length; j++)
if (sourceLines[i].EndsWith("@ FAM"))
{
if (sourceLines[j].StartsWith("0 @I"))
break;
lines.Add(sourceLines[j]);
if (!sourceLines[j].StartsWith("2 NICK "))
continue;
nick = sourceLines[j][7..];
for (int j = i + 1; j < sourceLines.Length; j++)
lines.Add(sourceLines[j]);
footerLines.AddRange(lines);
break;
}
if (string.IsNullOrEmpty(nick))
else if (sourceLines[i].EndsWith("@ INDI"))
{
if (lines[^1] != "0 TRLR")
throw new Exception(string.Join(Environment.NewLine, lines));
else
for (int j = i + 1; j < sourceLines.Length; j++)
{
footerLines.AddRange(lines);
break;
if (sourceLines[j].StartsWith(startsWith))
break;
lines.Add(sourceLines[j]);
if (!sourceLines[j].StartsWith("2 NICK "))
continue;
nick = sourceLines[j][7..];
}
if (string.IsNullOrEmpty(nick))
throw new Exception(string.Join(Environment.NewLine, lines));
results.Add(nick, new());
if (lines.Count > 25)
lines.Clear();
results[nick].AddRange(lines);
lines.Clear();
}
results.Add(nick, new());
results[nick].AddRange(lines);
lines.Clear();
else
throw new NotSupportedException();
}
return (headerLines.ToArray(), results, footerLines.ToArray());
}
private static void WriteGedFile(string personKeyFormatted, Models.PersonBirthday personBirthday, string[]? filteredIndividualsLines, Models.PersonName name, string[] matches)
private static void WriteGedComFile(string personKeyFormatted, Models.PersonBirthday personBirthday, string[]? filteredIndividualsLines, Models.PersonName name, bool isDefaultName, string directory)
{
string? directory = Path.GetDirectoryName(matches[0]);
if (directory is null)
throw new Exception();
string? sexLine;
string? deathLine = null;
string jrOrSr;
@ -115,13 +121,13 @@ internal abstract class Person
else
{
if (name.Alias.Value.Contains(" Jr"))
jrOrSr = "Jr";
jrOrSr = " Jr";
else if (name.Alias.Value.Contains(" Sr"))
jrOrSr = "Sr";
jrOrSr = " Sr";
else
jrOrSr = string.Empty;
}
string nameLine = $"1 NAME {name.First.Value}/{name.Last.Value}/{jrOrSr}";
string nameLine = $"1 NAME {name.First.Value} /{name.Last.Value}/{jrOrSr}";
if (personKeyFormatted[^2..] is "23" or "21" or "19" or "17" or "15")
{
sexLine = "1 SEX M";
@ -160,18 +166,18 @@ internal abstract class Person
sexLine = "1 SEX U";
else
{
string sex;
string code;
if (deathLine is null or not "1 DEAT Y")
sex = sexLine[6] is 'M' ? "05" : sexLine[6] is 'F' ? "04" : sexLine[6] is 'U' ? "02" : throw new NotImplementedException();
code = sexLine[6] is 'M' ? "05" : sexLine[6] is 'F' ? "04" : sexLine[6] is 'U' ? "02" : throw new NotImplementedException();
else
sex = sexLine[6] is 'M' ? "15" : sexLine[6] is 'F' ? "14" : sexLine[6] is 'U' ? "03" : throw new NotImplementedException();
code = sexLine[6] is 'M' ? "15" : sexLine[6] is 'F' ? "14" : sexLine[6] is 'U' ? "03" : throw new NotImplementedException();
if (directory.EndsWith("00"))
directory = string.Concat(directory[..^2], sex);
directory = string.Concat(directory[..^2], code);
else if (directory.EndsWith("01"))
directory = string.Concat(directory[..^2], sex);
directory = string.Concat(directory[..^2], code);
else
throw new NotImplementedException();
personKeyFormatted = $"{personKeyFormatted[..^2]}{sex}";
personKeyFormatted = $"{personKeyFormatted[..^2]}{code}";
}
}
List<string> pGedLines = new();
@ -197,6 +203,8 @@ internal abstract class Person
pGedLines.Add("1 BIRT");
pGedLines.Add($"2 DATE {personBirthday.Value:dd MMM yyyy}");
}
if (isDefaultName)
pGedLines.Add("9 NOTE");
if (filteredIndividualsLines is not null)
{
for (int i = 1; i < filteredIndividualsLines.Length; i++)
@ -257,13 +265,11 @@ internal abstract class Person
{
if (personContainer.Key is null || personContainer.Birthdays is null || personContainer.Person is null || !personContainer.Birthdays.Any())
continue;
if (personContainer.DisplayDirectoryName == mappingDefaultName || personContainer.Person.Name.Alias.Value == "Z")
if (IPerson.IsDefaultName(mappingDefaultName, personContainer.DisplayDirectoryName) || IPerson.IsDefaultName(mappingDefaultName, personContainer.Person))
continue;
if (distinct.Contains(personContainer.Key.Value))
continue;
distinct.Add(personContainer.Key.Value);
if (!personKeyToIds.ContainsKey(personContainer.Key.Value))
continue;
personBirthday = personContainer.Birthdays[zero];
personKeyFormatted = IPersonBirthday.GetFormatted(personBirthdayFormat, personBirthday);
by = IPersonBirthday.IsCounterPersonBirthday(personBirthday) ? resultAllInOne : "People";
@ -275,6 +281,8 @@ internal abstract class Person
continue;
pGedLines = File.ReadAllLines(pGedFiles[0]);
lines.AddRange(pGedLines);
if (!personKeyToIds.ContainsKey(personContainer.Key.Value))
lines.Add("1 NOTE");
// segments = personContainer.DisplayDirectoryName.Split(_Configuration.PersonCharacters.ToArray());
// if (segments.Length < 2)
// directory = Path.Combine(rootDirectory, $"000 {personKeyFormatted} {personContainer.DisplayDirectoryName}");
@ -288,9 +296,10 @@ internal abstract class Person
File.WriteAllLines(Path.Combine(a2PeopleContentDirectory, $"{ticks}.ged"), lines);
}
internal static Models.Person GetPerson(string[] personDisplayDirectoryAllFiles, string? personKeyFormatted, long personKey, Models.PersonBirthday personBirthday, string[] segments, string[]? filteredIndividualsLines)
internal static Models.Person GetPerson(string mappingDefaultName, char[] personCharacters, string personDisplayDirectoryName, string[] personDisplayDirectoryAllFiles, string? personKeyFormatted, long personKey, Models.PersonBirthday personBirthday, string[]? filteredIndividualsLines)
{
Models.Person result;
string[] matches;
const int zero = 0;
List<Models.PersonURL> urls = new();
Models.PersonId id = new(personKey);
@ -298,10 +307,29 @@ internal abstract class Person
List<Models.PersonNumber> numbers = new();
List<Models.PersonComment> comments = new();
List<Models.PersonAddress> addresses = new();
Models.PersonName name = PersonName.Create(segments[zero]);
string[] matches = (from l in personDisplayDirectoryAllFiles where !string.IsNullOrEmpty(personKeyFormatted) && l.Contains(personKeyFormatted) select l).ToArray();
string checkFileName = $"{personDisplayDirectoryName[zero]}.json";
bool isDefaultName = IPerson.IsDefaultName(mappingDefaultName, personDisplayDirectoryName);
string nameWithoutApproximateYears = !isDefaultName ? personDisplayDirectoryName.Split(personCharacters)[zero] : personDisplayDirectoryName;
matches = (from l in personDisplayDirectoryAllFiles where Path.GetFileName(l) == checkFileName select l).ToArray();
Models.PersonName? name;
if (!matches.Any())
name = PersonName.Create(nameWithoutApproximateYears);
else
{
string json = File.ReadAllText(matches[zero]);
name = JsonSerializer.Deserialize<Models.PersonName>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
name ??= PersonName.Create(nameWithoutApproximateYears);
if (name.Last is null || string.IsNullOrEmpty(name.Last.Value))
name = PersonName.Create(nameWithoutApproximateYears);
}
matches = (from l in personDisplayDirectoryAllFiles where !string.IsNullOrEmpty(personKeyFormatted) && l.Contains(personKeyFormatted) select l).ToArray();
if (!string.IsNullOrEmpty(personKeyFormatted) && matches.Any())
WriteGedFile(personKeyFormatted, personBirthday, filteredIndividualsLines, name, matches);
{
string? directory = Path.GetDirectoryName(matches[zero]);
if (directory is null)
throw new Exception();
WriteGedComFile(personKeyFormatted, personBirthday, filteredIndividualsLines, name, isDefaultName, directory);
}
result = new(id, personBirthday, name, comments, urls, numbers, emails, addresses);
return result;
}