process-data-standard-format changes for better unit testing
This commit is contained in:
@ -377,17 +377,25 @@ public class FileRead : Properties.IFileRead
|
||||
|
||||
internal string[] GetInProcessDirectory(string jobIdDirectory)
|
||||
{
|
||||
string[] results;
|
||||
List<string> results = new();
|
||||
if (!_IsEAFHosted)
|
||||
results = new string[] { jobIdDirectory };
|
||||
results = new string[] { jobIdDirectory }.ToList();
|
||||
else
|
||||
{
|
||||
string[] files;
|
||||
string logisticsSequence = _Logistics.Sequence.ToString();
|
||||
results = Directory.GetDirectories(jobIdDirectory, $"*{logisticsSequence}*", SearchOption.TopDirectoryOnly);
|
||||
string[] directories = Directory.GetDirectories(jobIdDirectory, $"*{logisticsSequence}*", SearchOption.TopDirectoryOnly);
|
||||
foreach (string directory in directories)
|
||||
{
|
||||
files = Directory.GetFiles(directory, "*", SearchOption.TopDirectoryOnly);
|
||||
if (files.Length == 0)
|
||||
continue;
|
||||
results.Add(directory);
|
||||
}
|
||||
}
|
||||
if ((results is null) || results.Length != 1)
|
||||
if ((results is null) || results.Count != 1)
|
||||
throw new Exception("Didn't find directory by logistics sequence");
|
||||
return results;
|
||||
return results.ToArray();
|
||||
}
|
||||
|
||||
protected static string[] GetMatches(FileConnectorConfiguration fileConnectorConfiguration)
|
||||
|
@ -61,8 +61,8 @@ internal class ProcessDataStandardFormat
|
||||
internal static string Archive(bool addSpaces = true, char separator = ' ') =>
|
||||
GetString(SearchFor.Archive, addSpaces, separator);
|
||||
|
||||
internal static ProcessDataStandardFormat GetEmpty() =>
|
||||
new(new(Array.Empty<string>()), new(Array.Empty<string>()), new(Array.Empty<string>()), new(Array.Empty<string>()), null, new(Array.Empty<string>()), null);
|
||||
internal static ProcessDataStandardFormat GetEmpty(Logistics logistics) =>
|
||||
new(new(Array.Empty<string>()), new(Array.Empty<string>()), new(Array.Empty<string>()), new(Array.Empty<string>()), null, new(logistics.Logistics1), null);
|
||||
|
||||
internal static List<string> PDSFToFixedWidth(string reportFullPath)
|
||||
{
|
||||
@ -214,25 +214,26 @@ internal class ProcessDataStandardFormat
|
||||
return results.AsReadOnly();
|
||||
}
|
||||
|
||||
internal static ProcessDataStandardFormat? GetProcessDataStandardFormat(string reportFullPath, ProcessDataStandardFormatMapping pdsfMapping)
|
||||
internal static ProcessDataStandardFormat GetProcessDataStandardFormat(string reportFullPath, ProcessDataStandardFormatMapping pdsfMapping)
|
||||
{
|
||||
ProcessDataStandardFormat? result;
|
||||
ProcessDataStandardFormat result;
|
||||
const int columnsLine = 6;
|
||||
FileInfo fileInfo = new(reportFullPath);
|
||||
ProcessDataStandardFormat processDataStandardFormat = GetProcessDataStandardFormat(fileInfo.LastWriteTime, pdsfMapping.NewColumnNames.Count, columnsLine, fileInfo.FullName, lines: null);
|
||||
JsonElement[]? jsonElements = GetArray(pdsfMapping.NewColumnNames.Count, processDataStandardFormat, lookForNumbers: false);
|
||||
if (jsonElements is null || jsonElements.Length == 0 || pdsfMapping.OldColumnNames.Count != pdsfMapping.ColumnIndices.Count)
|
||||
result = null;
|
||||
ProcessDataStandardFormat processDataStandardFormat = GetProcessDataStandardFormat(fileInfo.LastWriteTime, columnsLine, fileInfo.FullName, lines: null);
|
||||
JsonElement[]? jsonElements = pdsfMapping.OldColumnNames.Count != pdsfMapping.ColumnIndices.Count ? null : GetFullArray(processDataStandardFormat);
|
||||
JsonProperty[]? jsonProperties = jsonElements is null || jsonElements.Length == 0 ? null : jsonElements[0].EnumerateObject().ToArray();
|
||||
if (jsonElements is null || jsonProperties is null || jsonProperties.Length != pdsfMapping.NewColumnNames.Count)
|
||||
result = processDataStandardFormat;
|
||||
else
|
||||
{
|
||||
result = GetProcessDataStandardFormat(pdsfMapping, jsonElements, processDataStandardFormat);
|
||||
if (result.Sequence is null || result.Columns.Count == 0 || result.Body.Count == 0 || result.Logistics.Count == 0)
|
||||
result = null;
|
||||
result = processDataStandardFormat;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static ProcessDataStandardFormat GetProcessDataStandardFormat(DateTime lastWriteTime, int expectedColumns, int columnsLine, string path, string[]? lines)
|
||||
private static ProcessDataStandardFormat GetProcessDataStandardFormat(DateTime lastWriteTime, int columnsLine, string path, string[]? lines)
|
||||
{
|
||||
ProcessDataStandardFormat result;
|
||||
long sequence;
|
||||
@ -248,8 +249,6 @@ internal class ProcessDataStandardFormat
|
||||
else
|
||||
{
|
||||
segments = lines[columnsLine].Split('\t');
|
||||
if (segments.Length != expectedColumns)
|
||||
segments = Array.Empty<string>();
|
||||
for (int i = 0; i < columnsLine; i++)
|
||||
header.Add(lines[i]);
|
||||
}
|
||||
@ -285,7 +284,7 @@ internal class ProcessDataStandardFormat
|
||||
return result;
|
||||
}
|
||||
|
||||
private static JsonElement[]? GetArray(int expectedColumns, ProcessDataStandardFormat processDataStandardFormat, bool lookForNumbers)
|
||||
private static JsonElement[]? GetFullArray(ProcessDataStandardFormat processDataStandardFormat)
|
||||
{
|
||||
JsonElement[]? results;
|
||||
if (processDataStandardFormat.Body.Count == 0 || !processDataStandardFormat.Body[0].Contains('\t'))
|
||||
@ -293,36 +292,18 @@ internal class ProcessDataStandardFormat
|
||||
else
|
||||
{
|
||||
string value;
|
||||
string[] segments;
|
||||
List<string> segments;
|
||||
List<string> lines = new();
|
||||
StringBuilder stringBuilder = new();
|
||||
foreach (string bodyLine in processDataStandardFormat.Body)
|
||||
{
|
||||
_ = stringBuilder.Clear();
|
||||
_ = stringBuilder.Append('{');
|
||||
segments = bodyLine.Split('\t');
|
||||
if (segments.Length != expectedColumns)
|
||||
continue;
|
||||
if (!lookForNumbers)
|
||||
segments = bodyLine.Split('\t').ToList();
|
||||
for (int c = 0; c < segments.Count; c++)
|
||||
{
|
||||
for (int c = 0; c < segments.Length; c++)
|
||||
{
|
||||
value = segments[c].Replace("\"", "\\\"").Replace("\\", "\\\\");
|
||||
_ = stringBuilder.Append('"').Append(processDataStandardFormat.Columns[c]).Append("\":\"").Append(value).Append("\",");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int c = 0; c < segments.Length; c++)
|
||||
{
|
||||
value = segments[c].Replace("\"", "\\\"").Replace("\\", "\\\\");
|
||||
if (string.IsNullOrEmpty(value))
|
||||
_ = stringBuilder.Append('"').Append(processDataStandardFormat.Columns[c]).Append("\":").Append(value).Append("null,");
|
||||
else if (value.All(char.IsDigit))
|
||||
_ = stringBuilder.Append('"').Append(processDataStandardFormat.Columns[c]).Append("\":").Append(value).Append(',');
|
||||
else
|
||||
_ = stringBuilder.Append('"').Append(processDataStandardFormat.Columns[c]).Append("\":\"").Append(value).Append("\",");
|
||||
}
|
||||
value = segments[c].Replace("\"", "\\\"").Replace("\\", "\\\\");
|
||||
_ = stringBuilder.Append('"').Append(processDataStandardFormat.Columns[c]).Append("\":\"").Append(value).Append("\",");
|
||||
}
|
||||
_ = stringBuilder.Remove(stringBuilder.Length - 1, 1);
|
||||
_ = stringBuilder.AppendLine("}");
|
||||
|
Reference in New Issue
Block a user