2023-05-08 15:54:57 -07:00

135 lines
5.4 KiB
C#

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<object> _Details;
List<object> Shared.Properties.IProcessData.Details => _Details;
private readonly ILog _Log;
public ProcessData()
{
}
public ProcessData(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection, long tickOffset)
{
fileInfoCollection.Clear();
_Details = new List<object>();
_Log = LogManager.GetLogger(typeof(ProcessData));
_ = Parse(fileRead, logistics, fileInfoCollection);
}
string IProcessData.GetCurrentReactor(IFileRead fileRead, Logistics logistics, Dictionary<string, string> reactors) => throw new Exception(string.Concat("See ", nameof(Parse)));
Tuple<string, Test[], JsonElement[], List<FileInfo>> IProcessData.GetResults(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection)
{
Tuple<string, Test[], JsonElement[], List<FileInfo>> results;
List<Test> tests = new();
foreach (object item in _Details)
tests.Add(Test.SRP2100);
List<IDescription> 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<Description> fileReadDescriptions = (from l in descriptions select (Description)l).ToList();
string json = JsonSerializer.Serialize(fileReadDescriptions, fileReadDescriptions.GetType());
JsonElement[] jsonElements = JsonSerializer.Deserialize<JsonElement[]>(json);
results = new Tuple<string, Test[], JsonElement[], List<FileInfo>>(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<FileInfo> 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<CSV>(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<Description> GetDescriptions(JsonElement[] jsonElements)
{
List<Description> 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<Description>(jsonElement.ToString(), jsonSerializerOptions);
if (description is null)
continue;
results.Add(description);
}
return results;
}
}