229 lines
11 KiB
C#
229 lines
11 KiB
C#
using Phares.Shared;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using View_by_Distance.Property.Models;
|
|
using View_by_Distance.Shared.Models;
|
|
using View_by_Distance.Shared.Models.Methods;
|
|
|
|
namespace View_by_Distance.Instance.Models;
|
|
|
|
/// <summary>
|
|
// List<G2_Identify>
|
|
/// </summary>
|
|
public class G2_Identify : Shared.Models.Properties.IIdentify, IIdentify
|
|
{
|
|
|
|
protected int _DirectoryCount;
|
|
protected string _ParentDirectoryName;
|
|
protected string _Person;
|
|
protected string _PossibleYear;
|
|
protected string _RelativePath;
|
|
public int DirectoryCount => _DirectoryCount;
|
|
public string ParentDirectoryName => _ParentDirectoryName;
|
|
public string Person => _Person;
|
|
public string PossibleYear => _PossibleYear;
|
|
public string RelativePath => _RelativePath;
|
|
|
|
private readonly Serilog.ILogger? _Log;
|
|
private readonly Configuration _Configuration;
|
|
private readonly JsonSerializerOptions _WriteIndentedJsonSerializerOptions;
|
|
|
|
#nullable disable
|
|
[JsonConstructor]
|
|
public G2_Identify(int directoryCount, string parentDirectoryName, string person, string possibleYear, string relativePath)
|
|
{
|
|
_DirectoryCount = directoryCount;
|
|
_ParentDirectoryName = parentDirectoryName;
|
|
_Person = person;
|
|
_PossibleYear = possibleYear;
|
|
_RelativePath = relativePath;
|
|
}
|
|
|
|
internal G2_Identify(Configuration configuration)
|
|
{
|
|
_DirectoryCount = 0;
|
|
_ParentDirectoryName = string.Empty;
|
|
_Person = string.Empty;
|
|
_PossibleYear = string.Empty;
|
|
_RelativePath = string.Empty;
|
|
_Configuration = configuration;
|
|
_Log = Serilog.Log.ForContext<G2_Identify>();
|
|
_WriteIndentedJsonSerializerOptions = new JsonSerializerOptions { WriteIndented = true };
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });
|
|
return result;
|
|
}
|
|
|
|
private FileInfo GetNamed()
|
|
{
|
|
FileInfo result;
|
|
string[] jsonFiles = Directory.GetFiles(_Configuration.PropertyConfiguration.RootDirectory, "*Named*.json", SearchOption.TopDirectoryOnly);
|
|
if (!jsonFiles.Any())
|
|
result = null;
|
|
else
|
|
result = new FileInfo(jsonFiles[0]);
|
|
return result;
|
|
}
|
|
|
|
private void CheckLastWriteTimes(Property.Models.Configuration configuration, IsEnvironment isEnvironment, A2_People a2People, FileInfo named, string g2IdentifySingletonDirectory)
|
|
{
|
|
string json;
|
|
FileInfo fileInfo;
|
|
DateTime dateTime = DateTime.MinValue;
|
|
string[] jsonFiles = Directory.GetFiles(g2IdentifySingletonDirectory, "*.json", SearchOption.AllDirectories);
|
|
foreach (string jsonFile in jsonFiles)
|
|
{
|
|
fileInfo = new(jsonFile);
|
|
if (dateTime < fileInfo.LastWriteTime)
|
|
dateTime = fileInfo.LastWriteTime;
|
|
}
|
|
if (named.LastWriteTime > dateTime)
|
|
{
|
|
if (!isEnvironment.DebuggerWasAttachedDuringConstructor)
|
|
throw new Exception("Only allowed when debugger is attached during constructor!");
|
|
foreach (string file in jsonFiles)
|
|
File.Delete(file);
|
|
}
|
|
json = File.ReadAllText(named.FullName);
|
|
Person[] people = a2People.GetPeople(configuration);
|
|
Dictionary<string, string[]> resultKeyValuePairs = new();
|
|
string[] peopleBirthdates = (from l in people select Shared.Models.Stateless.Methods.IPersonBirthday.GetFormated(l.Birthday)).ToArray();
|
|
Dictionary<string, string[]> sourceKeyValuePairs = JsonSerializer.Deserialize<Dictionary<string, string[]>>(json);
|
|
foreach (KeyValuePair<string, string[]> keyValuePair in sourceKeyValuePairs)
|
|
{
|
|
if (!(from l in keyValuePair.Value where peopleBirthdates.Contains(l) select false).Any())
|
|
continue;
|
|
resultKeyValuePairs.Add(keyValuePair.Key, keyValuePair.Value);
|
|
}
|
|
if (resultKeyValuePairs.Count != sourceKeyValuePairs.Count)
|
|
{
|
|
json = JsonSerializer.Serialize(resultKeyValuePairs, _WriteIndentedJsonSerializerOptions);
|
|
if (!isEnvironment.DebuggerWasAttachedDuringConstructor)
|
|
throw new Exception("Only allowed when debugger is attached during constructor!");
|
|
_ = Property.Models.Stateless.IPath.WriteAllText(named.FullName, json, compareBeforeWrite: true);
|
|
}
|
|
}
|
|
|
|
internal List<G2_Identify> GetIdentifiedCollection(Property.Models.Configuration configuration, IsEnvironment isEnvironment, A2_People a2People)
|
|
{
|
|
List<G2_Identify> results = new();
|
|
string json;
|
|
string[] people;
|
|
string[] jsonFiles;
|
|
int directoryCount;
|
|
string possibleYear;
|
|
G2_Identify identify;
|
|
FileInfo named = GetNamed();
|
|
string testDirectoryFullName;
|
|
string checkDirectoryFullName;
|
|
Dictionary<int, string[]> keyValuePairs;
|
|
List<string> missing = new();
|
|
List<G_Index> indices = new();
|
|
string directoryName = Path.GetFileName(_Configuration.PropertyConfiguration.RootDirectory);
|
|
string g2IdentifySingletonDirectory = Property.Models.Stateless.IResult.GetResultsDateGroupDirectory(configuration, nameof(G2_Identify), "[]");
|
|
string jsonRootDirectory = Path.Combine(string.Concat(_Configuration.PropertyConfiguration.RootDirectory, " - Copied"), string.Concat(directoryName, " - 4) Info"), _Configuration.PropertyConfiguration.DateGroup, "[]");
|
|
if (named is not null && named.Exists)
|
|
CheckLastWriteTimes(configuration, isEnvironment, a2People, named, g2IdentifySingletonDirectory);
|
|
if (Directory.Exists(jsonRootDirectory))
|
|
{
|
|
jsonFiles = Directory.GetFiles(jsonRootDirectory, "*.json", SearchOption.AllDirectories);
|
|
for (int i = 0; i < jsonFiles.Length; i++)
|
|
{
|
|
json = Shared.Models.Stateless.Methods.IIndex.GetJson(jsonFiles[i], fileInfo: null);
|
|
indices.AddRange(JsonSerializer.Deserialize<List<G_Index>>(json));
|
|
}
|
|
if (named is not null && named.Exists)
|
|
{
|
|
json = File.ReadAllText(named.FullName);
|
|
keyValuePairs = JsonSerializer.Deserialize<Dictionary<int, string[]>>(json);
|
|
foreach (G_Index index in indices)
|
|
{
|
|
if (index?.Index is null)
|
|
continue;
|
|
if (!keyValuePairs.ContainsKey(index.Index.Value))
|
|
{
|
|
missing.Add(index.Index.Value.ToString());
|
|
continue;
|
|
}
|
|
if (index.RelativePaths is null)
|
|
{
|
|
missing.Add(index.Index.Value.ToString());
|
|
continue;
|
|
}
|
|
people = keyValuePairs[index.Index.Value];
|
|
foreach (string relativePath in index.RelativePaths)
|
|
{
|
|
foreach (string person in people)
|
|
{
|
|
directoryCount = 0;
|
|
checkDirectoryFullName = string.Concat(_Configuration.PropertyConfiguration.RootDirectory, relativePath);
|
|
for (int i = 0; i < int.MaxValue; i++)
|
|
{
|
|
testDirectoryFullName = Path.GetDirectoryName(checkDirectoryFullName);
|
|
if (testDirectoryFullName == _Configuration.PropertyConfiguration.RootDirectory)
|
|
break;
|
|
directoryCount += 1;
|
|
checkDirectoryFullName = testDirectoryFullName;
|
|
}
|
|
possibleYear = checkDirectoryFullName.Split(' ').Last();
|
|
if (possibleYear.Length != 4)
|
|
possibleYear = "0000";
|
|
identify = new G2_Identify(directoryCount, Path.GetFileName(checkDirectoryFullName), person, possibleYear, relativePath);
|
|
results.Add(identify);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
results = (from l in results orderby l.PossibleYear descending, l.DirectoryCount, l.ParentDirectoryName, l.RelativePath select l).ToList();
|
|
return results;
|
|
}
|
|
|
|
internal void WriteAllText(Property.Models.Configuration configuration, string outputResolution, List<G2_Identify> identifiedCollection)
|
|
{
|
|
string key;
|
|
string json;
|
|
string jsonFile;
|
|
string directoryFullName;
|
|
string fileNameWithoutExtension;
|
|
string aPropertySingletonDirectory = Property.Models.Stateless.IResult.GetResultsDateGroupDirectory(configuration, nameof(A_Property), "{}");
|
|
string g2IdentifyCollectionDirectory = Property.Models.Stateless.IResult.GetResultsDateGroupDirectory(configuration, nameof(G2_Identify), "[]");
|
|
Dictionary<string, List<KeyValuePair<string, string>>> keyValuePairs = new();
|
|
foreach (G2_Identify identified in identifiedCollection)
|
|
{
|
|
key = Path.GetDirectoryName(identified.RelativePath);
|
|
if (!keyValuePairs.ContainsKey(key))
|
|
keyValuePairs.Add(key, new List<KeyValuePair<string, string>>());
|
|
keyValuePairs[key].Add(new KeyValuePair<string, string>(identified.RelativePath, identified.Person));
|
|
}
|
|
foreach (KeyValuePair<string, List<KeyValuePair<string, string>>> keyValuePair in keyValuePairs)
|
|
{
|
|
directoryFullName = string.Concat(aPropertySingletonDirectory, keyValuePair.Key);
|
|
if (!Directory.Exists(directoryFullName))
|
|
continue;
|
|
foreach (KeyValuePair<string, string> keyValue in keyValuePair.Value)
|
|
{
|
|
fileNameWithoutExtension = Path.GetFileNameWithoutExtension(keyValue.Key);
|
|
jsonFile = Path.Combine(directoryFullName, $"{fileNameWithoutExtension}.json");
|
|
if (!File.Exists(jsonFile))
|
|
{
|
|
directoryFullName = string.Empty;
|
|
break;
|
|
}
|
|
}
|
|
if (string.IsNullOrEmpty(directoryFullName))
|
|
continue;
|
|
directoryFullName = Path.GetDirectoryName(string.Concat(g2IdentifyCollectionDirectory, keyValuePair.Key));
|
|
if (!Directory.Exists(directoryFullName))
|
|
_ = Directory.CreateDirectory(directoryFullName);
|
|
jsonFile = string.Concat(g2IdentifyCollectionDirectory, keyValuePair.Key, ".json");
|
|
json = JsonSerializer.Serialize(keyValuePair.Value, _WriteIndentedJsonSerializerOptions);
|
|
if (!Property.Models.Stateless.IPath.WriteAllText(jsonFile, json, compareBeforeWrite: true))
|
|
continue;
|
|
}
|
|
}
|
|
|
|
} |