using Adaptation.Shared; using Adaptation.Shared.Methods; using log4net; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Text.Json; using System.Text.Json.Serialization; namespace Adaptation.FileHandlers.csv; public partial class ProcessData : IProcessData { private readonly List _Details; List Shared.Properties.IProcessData.Details => _Details; private readonly ILog _Log; public ProcessData() { } public ProcessData(IFileRead fileRead, Logistics logistics, List fileInfoCollection, long tickOffset) { _Details = new List(); _Log = LogManager.GetLogger(typeof(ProcessData)); _ = Parse(fileRead, logistics, fileInfoCollection); } string IProcessData.GetCurrentReactor(IFileRead fileRead, Logistics logistics, Dictionary reactors) => throw new Exception(string.Concat("See ", nameof(Parse))); Tuple> IProcessData.GetResults(IFileRead fileRead, Logistics logistics, List fileInfoCollection) { Tuple> results; List tests = new(); foreach (object item in _Details) tests.Add(Test.SRP2100); List descriptions = fileRead.GetDescriptions(fileRead, tests, this); if (tests.Count != descriptions.Count) throw new Exception(); for (int i = 0; i < tests.Count; i++) { if (descriptions[i] is not Description description) throw new Exception(); if (description.Test != (int)tests[i]) throw new Exception(); } List fileReadDescriptions = (from l in descriptions select (Description)l).ToList(); string json = JsonSerializer.Serialize(fileReadDescriptions, fileReadDescriptions.GetType()); JsonElement[] jsonElements = JsonSerializer.Deserialize(json); results = new Tuple>(logistics.Logistics1[0], tests.ToArray(), jsonElements, fileInfoCollection); return results; } private static void OnlyOnce(string directory) { CSV csv; string json; string fileName; string innerDirectory; string fileNameWithoutExtension; foreach (string path in Directory.GetFiles(directory, "*.csv", SearchOption.TopDirectoryOnly)) { csv = CSV.GetCSV(path); fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path); innerDirectory = Path.Combine(directory, fileNameWithoutExtension); _ = Directory.CreateDirectory(innerDirectory); fileName = Path.Combine(innerDirectory, $"{fileNameWithoutExtension}.csv"); File.Move(path, fileName); fileName = Path.Combine(innerDirectory, $"{fileNameWithoutExtension}.json"); json = JsonSerializer.Serialize(csv, new JsonSerializerOptions { WriteIndented = true }); File.WriteAllText(fileName, json); } foreach (string path in Directory.GetFiles(directory, "*", SearchOption.AllDirectories)) { innerDirectory = Path.GetDirectoryName(path); fileName = Path.Combine(innerDirectory, $"{Path.GetFileName(innerDirectory)}{Path.GetExtension(path)}"); File.Move(path, fileName); } foreach (string path in Directory.GetFiles(directory, "*.csv", SearchOption.AllDirectories)) { innerDirectory = Path.GetDirectoryName(path); Directory.SetLastWriteTime(innerDirectory, new FileInfo(path).LastWriteTime); } } #pragma warning disable IDE0060 private CSV Parse(IFileRead fileRead, Logistics logistics, List fileInfoCollection) #pragma warning restore IDE0060 { CSV result; result = CSV.GetCSV(logistics.ReportFullPath); string directory = Path.GetDirectoryName(logistics.ReportFullPath); #if OnlyOnce OnlyOnce(directory); #endif string fileName = Path.Combine(directory, $"{Path.GetFileNameWithoutExtension(logistics.ReportFullPath)}.json"); string json = JsonSerializer.Serialize(result, new JsonSerializerOptions { WriteIndented = true }); result = JsonSerializer.Deserialize(json); if (result is null) throw new NullReferenceException(nameof(result)); File.WriteAllText(fileName, json); #if NotSure fileInfoCollection.Add(new(fileName)); #endif fileInfoCollection.Add(logistics.FileInfo); _Details.Add(fileName); return result; } #nullable enable internal static List GetDescriptions(JsonElement[] jsonElements) { List results = new(); Description? description; JsonSerializerOptions jsonSerializerOptions = new() { NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString }; foreach (JsonElement jsonElement in jsonElements) { if (jsonElement.ValueKind != JsonValueKind.Object) throw new Exception(); description = JsonSerializer.Deserialize(jsonElement.ToString(), jsonSerializerOptions); if (description is null) continue; results.Add(description); } return results; } }