apc-viewer/APC Viewer/Pages/IPDSF.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 IPDSFPage : PageModel, Models.Properties.IIPDSFPage, IIPDSFPage
{
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 IPDSFPage(Singleton.Background background)
{
_Background = background;
_BackgroundMethods = background;
_Log = Serilog.Log.ForContext<IPDSFPage>();
}
#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, forIPDSF: true);
_Files = tuple.Item1;
_Grouped = tuple.Item2;
_Sorted = tuple.Item3;
_Directory = tuple.Item4;
}
}
private static StringBuilder GetIPDSFHtml(string? ipdsfFile)
{
StringBuilder result = new();
_ = result.AppendLine("<html><body><table border = '1'>");
if (string.IsNullOrEmpty(ipdsfFile))
throw new Exception("<tr><td>Invalid input</td></tr>");
else if (!System.IO.File.Exists(ipdsfFile))
_ = 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[] ipdsfLines = System.IO.File.ReadAllLines(ipdsfFile);
foreach (string ipdsfLine in ipdsfLines)
{
if (ipdsfLine.StartsWith("LOGISTICS_"))
{
logisticsSegment = ipdsfLine.Split('\t')[0];
if (!logistics.Contains(logisticsSegment))
{
logistics.Add(logisticsSegment);
_ = result.AppendLine("</table><hr /><table border = '1'>");
}
}
if (ipdsfLine.StartsWith("NUM_DATA_ROWS") || ipdsfLine.StartsWith("END_HEADER"))
{
body = false;
footer = true;
_ = result.AppendLine("</table><hr /><table border = '1'>");
}
if (header)
_ = result.Append("<tr><td>").Append(ipdsfLine.Replace("\t", "</td><td>")).AppendLine("&nbsp;</td></tr>");
else if (body)
_ = result.Append("<tr><td>").Append(ipdsfLine.Replace("\t", "</td><td>")).AppendLine("&nbsp;</td></tr>");
else if (footer)
{
if (ipdsfLine.StartsWith("DELIMITER"))
_ = result.Append("<tr><td>").Append(ipdsfLine.Replace(";", "</td><td>")).AppendLine("&nbsp;</td></tr>");
else
_ = result.Append("<tr><td>").Append(ipdsfLine.Replace("\t", "</td><td>").Replace("=", "</td><td>").Replace(";", "</td><td>")).AppendLine("&nbsp;</td></tr>");
}
else
throw new Exception();
if (ipdsfLine.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 ipdsfFile;
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
{
ipdsfFile = _Background.GetIPDSF(sequence);
result = GetIPDSFHtml(ipdsfFile);
}
}
return Content(result.ToString(), "text/html");
}
public FileResult OnGetDownloadFile(string? id = null)
{
string ipdsfFile;
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
ipdsfFile = _Background.GetIPDSF(sequence);
}
return File(ipdsfFile, "text/plain", Path.GetFileName(ipdsfFile));
}
public ContentResult OnGetViewCustom(string? ipdsf_file = null)
{
StringBuilder result = GetIPDSFHtml(ipdsf_file);
return Content(result.ToString(), "text/html");
}
public FileResult OnGetDownloadFileCustom(string? ipdsf_file = null)
{
if (string.IsNullOrEmpty(ipdsf_file))
throw new Exception("Error: Invalid input");
else if (!System.IO.File.Exists(ipdsf_file))
throw new Exception("Error: file does not exist");
return File(ipdsf_file, "text/plain", Path.GetFileName(ipdsf_file));
}
}