apc-viewer/APC Viewer/Pages/PDSF.cshtml.cs
2022-03-29 07:23:13 -07:00

164 lines
6.4 KiB
C#

using APCViewer.Models.Stateless.Methods;
using IFX.Shared;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Serilog.Context;
using Shared;
using System.Text;
using System.Text.Json;
namespace APCViewer.Pages;
public class PDSFPage : PageModel, Models.Properties.IPDSFPage, Models.Stateless.Methods.IPDSFPage
{
protected string? _Directory;
protected int _Files;
protected Dictionary<string, Dictionary<string, List<Shared.Logistics>>> _Grouped;
protected List<Tuple<string[], Shared.Logistics>> _Sorted;
public string? Directory => _Directory;
public int Files => _Files;
public Dictionary<string, Dictionary<string, List<Shared.Logistics>>> Grouped => _Grouped;
public List<Tuple<string[], Shared.Logistics>> Sorted => _Sorted;
private readonly Serilog.ILogger _Log;
private readonly Singleton.Background _Background;
private readonly Models.Methods.IBackground _BackgroundMethods;
#nullable disable
public PDSFPage(Singleton.Background background)
{
_Background = background;
_BackgroundMethods = background;
_Log = Serilog.Log.ForContext<PDSFPage>();
}
#nullable enable
public override string ToString()
{
string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });
return result;
}
public void OnGet(string? directory = null, string? filter = null, bool is_gaN = false, bool is_Si = false)
{
string? methodName = IMethodName.GetActualAsyncMethodName();
using (LogContext.PushProperty("MethodName", methodName))
{
_Log.Debug("() => ...");
Tuple<int, Dictionary<string, Dictionary<string, List<Logistics>>>, List<Tuple<string[], Logistics>>, string?> tuple = _BackgroundMethods.SetViewBag(directory, filter, isGaN: is_gaN, isSi: is_Si, forPDSF: true);
_Files = tuple.Item1;
_Grouped = tuple.Item2;
_Sorted = tuple.Item3;
_Directory = tuple.Item4;
}
}
private static StringBuilder GetPDSFHtml(string? pdsfFile)
{
StringBuilder result = new();
_ = result.AppendLine("<html><body><table border = '1'>");
if (string.IsNullOrEmpty(pdsfFile))
throw new Exception("<tr><td>Invalid input</td></tr>");
else if (!System.IO.File.Exists(pdsfFile))
_ = result.AppendLine("<tr><td>File doesn't exist!</td></tr>");
else
{
bool header = true;
bool body = false;
bool footer = false;
string logisticsSegment;
List<string> logistics = new();
string[] pdsfLines = System.IO.File.ReadAllLines(pdsfFile);
foreach (string pdsfLine in pdsfLines)
{
if (pdsfLine.StartsWith("LOGISTICS_"))
{
logisticsSegment = pdsfLine.Split('\t')[0];
if (!logistics.Contains(logisticsSegment))
{
logistics.Add(logisticsSegment);
_ = result.AppendLine("</table><hr /><table border = '1'>");
}
}
if (pdsfLine.StartsWith("NUM_DATA_ROWS") || pdsfLine.StartsWith("END_HEADER"))
{
body = false;
footer = true;
_ = result.AppendLine("</table><hr /><table border = '1'>");
}
if (header)
_ = result.Append("<tr><td>").Append(pdsfLine.Replace("\t", "</td><td>")).AppendLine("&nbsp;</td></tr>");
else if (body)
_ = result.Append("<tr><td>").Append(pdsfLine.Replace("\t", "</td><td>")).AppendLine("&nbsp;</td></tr>");
else if (footer)
{
if (pdsfLine.StartsWith("DELIMITER"))
_ = result.Append("<tr><td>").Append(pdsfLine.Replace(";", "</td><td>")).AppendLine("&nbsp;</td></tr>");
else
_ = result.Append("<tr><td>").Append(pdsfLine.Replace("\t", "</td><td>").Replace("=", "</td><td>").Replace(";", "</td><td>")).AppendLine("&nbsp;</td></tr>");
}
else
throw new Exception();
if (pdsfLine.StartsWith("END_OFFSET"))
{
header = false;
body = true;
_ = result.AppendLine("</table><hr /><table border = '1'>");
}
}
}
_ = result.AppendLine("</table></body>");
return result;
}
public ContentResult OnGetView(string? id = null)
{
string pdsfFile;
StringBuilder result = new();
if (string.IsNullOrEmpty(id) || !id.Contains('_'))
_ = result.AppendLine("<html><body><table border = '1'><tr><td>A) Error: Invalid input</td></tr></table></body>");
else
{
if (!long.TryParse(id.Split('_')[1], out long sequence))
_ = result.AppendLine("<html><body><table border = '1'><tr><td>B) Error: Invalid input</td></tr></table></body>");
else
{
pdsfFile = _Background.GetPDSF(sequence);
result = GetPDSFHtml(pdsfFile);
}
}
return Content(result.ToString(), "text/html");
}
public FileResult OnGetDownloadFile(string? id = null)
{
string pdsfFile;
if (string.IsNullOrEmpty(id) || !id.Contains('_'))
throw new Exception("A) Error: Invalid input");
else
{
if (!long.TryParse(id.Split('_')[1], out long sequence))
throw new Exception("B) Error: Invalid input");
else
pdsfFile = _Background.GetPDSF(sequence);
}
return File(pdsfFile, "text/plain", Path.GetFileName(pdsfFile));
}
public ContentResult OnGetViewCustom(string? pdsf_file = null)
{
StringBuilder result = GetPDSFHtml(pdsf_file);
return Content(result.ToString(), "text/html");
}
public FileResult OnGetDownloadFileCustom(string? pdsf_file = null)
{
if (string.IsNullOrEmpty(pdsf_file))
throw new Exception("Error: Invalid input");
else if (!System.IO.File.Exists(pdsf_file))
throw new Exception("Error: file does not exist");
return File(pdsf_file, "text/plain", Path.GetFileName(pdsf_file));
}
}