SaveParents

Removed GetFilteredOutMapped
Removed GetNonSpecificPeopleCollection
This commit is contained in:
2023-07-26 23:34:58 -07:00
parent a18fb1e756
commit 9071784255
14 changed files with 478 additions and 338 deletions

View File

@ -1,5 +1,8 @@
using System.Collections.ObjectModel;
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
namespace View_by_Distance.Shared.Models.Stateless.Methods;
@ -20,13 +23,16 @@ internal abstract class GenealogicalDataCommunication
return results.ToArray();
}
private static List<GenealogicalDataCommunicationRelation> GetRelations(Dictionary<string, string> idToNick, List<string[]> familyGroupLines)
private static List<GenealogicalDataCommunicationRelation> GetRelations(string personBirthdayFormat, ReadOnlyDictionary<string, string> personKeyFormattedToPersonFullName, Dictionary<string, string> idToNick, List<string[]> familyGroupLines)
{
List<GenealogicalDataCommunicationRelation> results = new();
string? nick;
long? personKey;
string relation;
string? fullName;
string[] segments;
string[] familyLines;
Models.PersonBirthday? personBirthday;
for (int i = 0; i < familyGroupLines.Count; i++)
{
familyLines = familyGroupLines[i];
@ -38,10 +44,13 @@ internal abstract class GenealogicalDataCommunication
if (!idToNick.TryGetValue(segments[1], out nick))
continue;
relation = segments.First()[2..].Trim();
personBirthday = IPersonBirthday.GetPersonBirthday(personBirthdayFormat, nick);
personKey = personBirthday?.Value.Ticks;
fullName = personKeyFormattedToPersonFullName.GetValueOrDefault(nick);
if (j + 1 >= familyLines.Length || familyLines[j + 1].Length < 3 || familyLines[j + 1][..3] != "2 _")
results.Add(new(i, relation, segments[1], nick, null));
results.Add(new(i, relation, segments[1], nick, personKey, fullName, null));
else
results.Add(new(i, relation, segments[1], nick, familyLines[j + 1][2..]));
results.Add(new(i, relation, segments[1], nick, personKey, fullName, familyLines[j + 1][2..]));
}
}
return results;
@ -55,7 +64,7 @@ internal abstract class GenealogicalDataCommunication
return new(results);
}
internal static (string[], ReadOnlyDictionary<string, string[]>, List<string[]>, string[], List<GenealogicalDataCommunicationRelation> genealogicalDataCommunicationRelations) GetIndividuals(string genealogicalDataCommunicationFile, bool requireNickName)
internal static (string[], ReadOnlyDictionary<string, string[]>, List<string[]>, string[], List<GenealogicalDataCommunicationRelation> genealogicalDataCommunicationRelations) GetIndividuals(string personBirthdayFormat, string genealogicalDataCommunicationFile, ReadOnlyCollection<Models.PersonContainer> personContainers, bool requireNickName)
{
ReadOnlyDictionary<string, string[]> results;
string? nick;
@ -66,6 +75,7 @@ internal abstract class GenealogicalDataCommunication
List<string[]> familyGroupLines = new();
Dictionary<string, string> idToNick = new();
Dictionary<string, List<string>> keyValuePairs = new();
ReadOnlyDictionary<string, string> personKeyFormattedToPersonFullName = IPersonContainer.GetPersonKeyFormattedToPersonFullName(personBirthdayFormat, personContainers);
string[] sourceLines = string.IsNullOrEmpty(genealogicalDataCommunicationFile) || !File.Exists(genealogicalDataCommunicationFile) ? Array.Empty<string>() : File.ReadAllLines(genealogicalDataCommunicationFile);
string[] headerLines = GetHeaderLines(startsWith, sourceLines);
for (int i = headerLines.Length; i < sourceLines.Length; i++)
@ -125,60 +135,16 @@ internal abstract class GenealogicalDataCommunication
throw new NotSupportedException();
}
results = Convert(keyValuePairs);
List<GenealogicalDataCommunicationRelation> genealogicalDataCommunicationRelations = GetRelations(idToNick, familyGroupLines);
List<GenealogicalDataCommunicationRelation> genealogicalDataCommunicationRelations = GetRelations(personBirthdayFormat, personKeyFormattedToPersonFullName, idToNick, familyGroupLines);
return (headerLines, results, familyGroupLines, footerLines.ToArray(), genealogicalDataCommunicationRelations);
}
private static string[] GetFilteredOutMapped(string[] individualsLines)
{
List<string> results = new();
for (int i = 0; i < individualsLines.Length; i++)
{
if (individualsLines[i].StartsWith("0 @I"))
continue;
else if (individualsLines[i].StartsWith("1 _UID"))
continue;
else if (individualsLines[i].StartsWith("1 NAME"))
continue;
else if (individualsLines[i].StartsWith("2 GIVN"))
continue;
else if (individualsLines[i].StartsWith("2 SURN"))
continue;
else if (individualsLines[i].StartsWith("2 NSFX"))
continue;
else if (individualsLines[i].StartsWith("2 NICK"))
continue;
else if (individualsLines[i].StartsWith("1 SEX"))
continue;
else if (individualsLines[i] == "1 BIRT")
{
if (individualsLines.Length > i + 1 && individualsLines[i + 1].StartsWith("2 DATE"))
i += 1;
continue;
}
else if (individualsLines[i].StartsWith("1 DEAT"))
{
if (individualsLines.Length > i + 1 && individualsLines[i + 1].StartsWith("2 DATE"))
i += 1;
continue;
}
else if (individualsLines[i] == "1 CHAN")
{
if (individualsLines.Length > i + 1 && individualsLines[i + 1].StartsWith("2 DATE"))
i += 1;
continue;
}
results.Add(individualsLines[i]);
}
return results.ToArray();
}
internal static List<string> GetMappedLines(string genealogicalDataCommunicationFile, bool requireNickName)
internal static List<string> GetMappedLines(string personBirthdayFormat, string genealogicalDataCommunicationFile, ReadOnlyCollection<Models.PersonContainer> personContainers, bool requireNickName)
{
List<string> results = new();
Models.PersonBirthday personBirthday = new(DateTime.Now);
GenealogicalDataCommunicationLines genealogicalDataCommunicationLines;
(string[] headerLines, ReadOnlyDictionary<string, string[]> individuals, List<string[]> familyGroupLines, string[] footerLines, _) = GetIndividuals(genealogicalDataCommunicationFile, requireNickName);
(string[] headerLines, ReadOnlyDictionary<string, string[]> individuals, List<string[]> familyGroupLines, string[] footerLines, _) = GetIndividuals(personBirthdayFormat, genealogicalDataCommunicationFile, personContainers, requireNickName);
results.AddRange(headerLines);
foreach (KeyValuePair<string, string[]> keyValuePair in individuals)
{
@ -335,9 +301,10 @@ internal abstract class GenealogicalDataCommunication
return result;
}
internal static bool CleanDisplayDirectoryAllFilesAndWriteTicksGed(string mappingDefaultName, string personBirthdayFormat, List<Models.PersonContainer> personContainers, string[] headerLines, List<string[]> familyGroupLines, string[] footerLines, long ticks, string a2PeopleContentDirectory)
internal static bool CleanDisplayDirectoryAllFilesAndWriteTicksGed(string mappingDefaultName, string personBirthdayFormat, ReadOnlyCollection<Models.PersonContainer> personContainers, string[] headerLines, List<string[]> familyGroupLines, string[] footerLines, long ticks, string a2PeopleContentDirectory)
{
bool result = false;
string directory;
string[] mdFiles;
string[] txtFiles;
const int zero = 0;
@ -411,8 +378,218 @@ internal abstract class GenealogicalDataCommunication
for (int i = 0; i < familyGroupLines.Count; i++)
lines.AddRange(familyGroupLines[i]);
lines.AddRange(footerLines);
File.WriteAllLines(Path.Combine(a2PeopleContentDirectory, $"{dateTime.Year}-GenealogicalDataCommunication", $"{dateTime.Year}-Week-{weekOfYear}", $"{ticks}.ged"), lines);
directory = Path.Combine(a2PeopleContentDirectory, $"{dateTime.Year}-GenealogicalDataCommunication", $"{dateTime.Year}-Week-{weekOfYear}");
if (!Directory.Exists(directory))
_ = Directory.CreateDirectory(directory);
File.WriteAllLines(Path.Combine(directory, $"{ticks}.ged"), lines);
return result;
}
internal static ReadOnlyDictionary<int, List<GenealogicalDataCommunicationRelation>> GetFamilyIndexToCollection(List<GenealogicalDataCommunicationRelation> genealogicalDataCommunicationRelations)
{
Dictionary<int, List<GenealogicalDataCommunicationRelation>> results = new();
List<GenealogicalDataCommunicationRelation>? relations;
foreach (GenealogicalDataCommunicationRelation genealogicalDataCommunicationRelation in genealogicalDataCommunicationRelations)
{
if (!results.TryGetValue(genealogicalDataCommunicationRelation.FamilyIndex, out relations))
{
results.Add(genealogicalDataCommunicationRelation.FamilyIndex, new());
if (!results.TryGetValue(genealogicalDataCommunicationRelation.FamilyIndex, out relations))
throw new NotSupportedException();
}
relations.Add(genealogicalDataCommunicationRelation);
}
return new(results);
}
internal static ReadOnlyDictionary<long, List<GenealogicalDataCommunicationRelation>> GetCollection(string personBirthdayFormat, List<GenealogicalDataCommunicationRelation> genealogicalDataCommunicationRelations)
{
Dictionary<long, List<GenealogicalDataCommunicationRelation>> results = new();
long personKey;
string personKeyFormatted;
Models.PersonBirthday? personBirthday;
List<GenealogicalDataCommunicationRelation>? collection;
foreach (GenealogicalDataCommunicationRelation genealogicalDataCommunicationRelation in genealogicalDataCommunicationRelations)
{
personKeyFormatted = genealogicalDataCommunicationRelation.NickName;
personBirthday = IPersonBirthday.GetPersonBirthday(personBirthdayFormat, personKeyFormatted);
if (personBirthday is null)
continue;
personKey = personBirthday.Value.Ticks;
if (!results.TryGetValue(personKey, out collection))
{
results.Add(personKey, new());
if (!results.TryGetValue(personKey, out collection))
throw new NotSupportedException();
}
collection.Add(genealogicalDataCommunicationRelation);
}
return new(results);
}
internal static ReadOnlyDictionary<long, List<GenealogicalDataCommunicationRelation>> GetCollection(string personBirthdayFormat, ReadOnlyDictionary<int, List<GenealogicalDataCommunicationRelation>> familyIndexToCollection)
{
Dictionary<long, List<GenealogicalDataCommunicationRelation>> results = new();
long personKey;
string personKeyFormatted;
Models.PersonBirthday? personBirthday;
List<GenealogicalDataCommunicationRelation>? collection;
foreach (KeyValuePair<int, List<GenealogicalDataCommunicationRelation>> keyValuePair in familyIndexToCollection)
{
foreach (GenealogicalDataCommunicationRelation genealogicalDataCommunicationRelation in keyValuePair.Value)
{
personKeyFormatted = genealogicalDataCommunicationRelation.NickName;
personBirthday = IPersonBirthday.GetPersonBirthday(personBirthdayFormat, personKeyFormatted);
if (personBirthday is null)
continue;
personKey = personBirthday.Value.Ticks;
if (!results.TryGetValue(personKey, out collection))
{
results.Add(personKey, new());
if (!results.TryGetValue(personKey, out collection))
throw new NotSupportedException();
}
collection.Add(genealogicalDataCommunicationRelation);
}
}
return new(results);
}
internal static string GetMergeWithLineTwo(GenealogicalDataCommunicationRelation genealogicalDataCommunicationRelation, GenealogicalDataCommunicationRelation relation)
{
string result;
const char father = 'F';
const char mother = 'M';
string wife = IGenealogicalDataCommunication.Wife;
string husband = IGenealogicalDataCommunication.Husband;
if (string.IsNullOrEmpty(genealogicalDataCommunicationRelation.LineTwo))
{
if (relation.Relation == wife)
result = nameof(mother);
else if (relation.Relation == husband)
result = nameof(father);
else
result = string.Empty;
}
else
{
StringBuilder stringBuilder = new();
if (genealogicalDataCommunicationRelation.LineTwo[1] == father)
{
if (relation.Relation == wife)
_ = stringBuilder.Append(nameof(mother));
else if (relation.Relation == husband)
_ = stringBuilder.Append(genealogicalDataCommunicationRelation.LineTwo.Split(' ').Last()).Append(' ').Append(nameof(father));
}
else if (genealogicalDataCommunicationRelation.LineTwo[1] == mother)
{
if (relation.Relation == husband)
_ = stringBuilder.Append(nameof(father));
else if (relation.Relation == wife)
_ = stringBuilder.Append(genealogicalDataCommunicationRelation.LineTwo.Split(' ').Last()).Append(' ').Append(nameof(mother));
}
result = stringBuilder.ToString();
}
return result;
}
private static void WriteAll(long ticks, string a2PeopleContentDirectory, List<string[]> frontMatterLinesCollections, string weekOfYear)
{
string json;
string[] segments;
string? directory;
string frontMatterLastLine;
DateTime dateTime = new(ticks);
List<string> allFrontMatterLines = new();
foreach (string[] frontMatterLines in frontMatterLinesCollections)
{
allFrontMatterLines.Add("{");
frontMatterLastLine = frontMatterLines.Last();
foreach (string frontMatterLine in frontMatterLines)
{
segments = frontMatterLine.Split(": ");
if (segments.Length != 2)
continue;
if (frontMatterLine == frontMatterLastLine)
allFrontMatterLines.Add($"\"{string.Join("\": ", segments)}");
else
allFrontMatterLines.Add($"\"{string.Join("\": ", segments)},");
}
allFrontMatterLines.Add("},");
}
directory = Path.Combine(a2PeopleContentDirectory, $"{dateTime.Year}-Markdown", $"{dateTime.Year}-Week-{weekOfYear}", ticks.ToString());
if (!Directory.Exists(directory))
_ = Directory.CreateDirectory(directory);
json = $"[{string.Join(Environment.NewLine, allFrontMatterLines.ToArray(), 0, allFrontMatterLines.Count - 1)}{allFrontMatterLines.Last()[..^1]}]";
Models.GenealogicalDataCommunication[]? genealogicalDataCommunications = JsonSerializer.Deserialize(json, GenealogicalDataCommunicationSourceCollectionGenerationContext.Default.GenealogicalDataCommunicationArray);
if (genealogicalDataCommunications is null)
throw new NullReferenceException(nameof(genealogicalDataCommunications));
json = JsonSerializer.Serialize(genealogicalDataCommunications, GenealogicalDataCommunicationSourceCollectionGenerationContext.Default.GenealogicalDataCommunicationArray);
_ = IPath.WriteAllText(Path.Combine(directory, $"{ticks}.json"), json, updateDateWhenMatches: false, compareBeforeWrite: true);
}
internal static void MaybeWriteMarkDownFiles(string mappingDefaultName, string personBirthdayFormat, long ticks, ReadOnlyCollection<Models.PersonContainer> personContainers, ReadOnlyDictionary<string, string[]> individuals, ReadOnlyDictionary<int, List<GenealogicalDataCommunicationRelation>> familyIndexToCollection, string a2PeopleContentDirectory)
{
bool? male;
bool? first;
string fullName;
string[]? lines;
string[] matches;
string? directory;
bool isDefaultName;
const int zero = 0;
string personKeyFormatted;
string lowerHyphenFullName;
List<string> frontMatterLines;
string pattern = "[^a-z0-9-]";
DateTime dateTime = new(ticks);
Calendar calendar = new CultureInfo("en-US").Calendar;
List<(string? Id, string[] FrontMatterLines)> collection = new();
Models.GenealogicalDataCommunication genealogicalDataCommunication;
GenealogicalDataCommunicationLines? genealogicalDataCommunicationLines;
string weekOfYear = calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00");
ReadOnlyDictionary<string, string> personKeyFormattedToPersonFullName = IPersonContainer.GetPersonKeyFormattedToPersonFullName(personBirthdayFormat, personContainers);
foreach (Models.PersonContainer personContainer in personContainers.OrderByDescending(l => l.Key))
{
if (personContainer.Key is null || personContainer.Birthdays is null || personContainer.Person is null || personContainer.PersonDirectory is null || !personContainer.Birthdays.Any())
continue;
male = personContainer.PersonDirectory.Sex == 'U' ? null : personContainer.PersonDirectory.Sex == 'M' || (personContainer.PersonDirectory.Sex == 'F' ? false : throw new Exception());
first = personContainer.PersonDirectory.First == 'U' ? null : personContainer.PersonDirectory.First == 'Y' || (personContainer.PersonDirectory.First == 'N' ? false : throw new Exception());
if (first is null)
continue;
isDefaultName = IPerson.IsDefaultName(mappingDefaultName, personContainer.DisplayDirectoryName);
personKeyFormatted = IPersonBirthday.GetFormatted(personBirthdayFormat, personContainer.Key.Value);
matches = (from l in personContainer.DisplayDirectoryAllFiles where l.Contains(personKeyFormatted) select l).ToArray();
if (!matches.Any())
continue;
directory = Path.GetDirectoryName(matches[zero]);
if (directory is null)
continue;
if (!individuals.TryGetValue(personKeyFormatted, out lines))
continue;
genealogicalDataCommunicationLines = lines is null ? null : GetGenealogicalDataCommunicationLines(personContainer.Birthdays[zero], lines);
if (genealogicalDataCommunicationLines is null)
continue;
genealogicalDataCommunication = GetGenealogicalDataCommunication(first.Value, genealogicalDataCommunicationLines);
if (genealogicalDataCommunication.Sex != personContainer.PersonDirectory.Sex)
continue;
if (genealogicalDataCommunication.Birth is not null && !directory.EndsWith(genealogicalDataCommunication.Birth.Value.Hour.ToString()))
continue;
if (genealogicalDataCommunication.Death is null && personContainer.PersonDirectory.Status == 'D' || genealogicalDataCommunication.Death is not null && personContainer.PersonDirectory.Status == 'A')
continue;
fullName = PersonName.GetFullName(personContainer.Person.Name);
lowerHyphenFullName = $"{Regex.Replace(fullName.ToLower(), pattern, "-")}";
frontMatterLines = MarkDown.GetFrontMatterLines(ticks, fullName, lowerHyphenFullName, genealogicalDataCommunication);
if (!frontMatterLines.Any())
continue;
collection.Add((genealogicalDataCommunication.Id, frontMatterLines.ToArray()));
MarkDown.WriteFile(personKeyFormatted, ticks, a2PeopleContentDirectory, calendar, pattern, personKeyFormattedToPersonFullName, familyIndexToCollection, genealogicalDataCommunication, fullName, lowerHyphenFullName, frontMatterLines);
}
if (collection.Any())
{
List<string[]> frontMatterLinesCollections = (from l in collection orderby l.Id.Length, l.Id where l.Id is not null select l.FrontMatterLines).ToList();
WriteAll(ticks, a2PeopleContentDirectory, frontMatterLinesCollections, weekOfYear);
}
}
}