fix(adminui): equipment CSV import reads RFC 4180 via CsvParser (R04 S6)

ImportEquipmentModal split each line on commas, so a quoted Manufacturer with
a comma silently shifted every later column. EquipmentCsvParser reads through
the shared Commons CsvParser (the raw-tag importer's authority): quoted
fields, doubled quotes, CRLF and blank lines parse; malformed quoting is an
error, not a guess. Header/shape messages are unchanged.

Refs #745
Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
This commit is contained in:
Joseph Doherty
2026-10-22 17:56:26 -04:00
parent 4cfa369354
commit 746267c2a2
3 changed files with 151 additions and 54 deletions
@@ -75,8 +75,6 @@
}
@code {
// Required, in order. v3: equipment no longer binds a driver, so DriverInstanceId is gone.
private static readonly string[] RequiredColumns = ["Name", "MachineCode", "UnsLineId"];
/// <summary>Whether the modal is shown. The host owns this flag.</summary>
[Parameter] public bool Visible { get; set; }
@@ -134,60 +132,12 @@
private Task CloseAsync() =>
_result is not null ? OnImported.InvokeAsync() : OnCancel.InvokeAsync();
/// <summary>
/// Parses the pasted CSV into <see cref="EquipmentInput"/> rows. Requires a header row whose first
/// three columns are Name, MachineCode, UnsLineId (case-insensitive); the optional
/// ZTag/SAPID/Manufacturer/Model follow. Blank optional cells parse as <c>null</c>. Returns
/// <c>null</c> (and sets <see cref="_parseError"/>) when the CSV is empty or the header is wrong.
/// </summary>
/// <summary>Parses the pasted CSV via <see cref="EquipmentCsvParser"/> (RFC 4180, R04 S6); sets
/// <see cref="_parseError"/> and returns null on an empty/malformed CSV or a wrong header.</summary>
private List<EquipmentInput>? ParseCsv(string csv)
{
if (string.IsNullOrWhiteSpace(csv)) { _parseError = "CSV is empty."; return null; }
var lines = csv.Replace("\r\n", "\n").Split('\n', StringSplitOptions.RemoveEmptyEntries);
if (lines.Length < 2) { _parseError = "Need a header row and at least one data row."; return null; }
var header = lines[0].Split(',').Select(c => c.Trim()).ToArray();
for (var i = 0; i < RequiredColumns.Length; i++)
{
if (i >= header.Length || !string.Equals(header[i], RequiredColumns[i], StringComparison.OrdinalIgnoreCase))
{
_parseError = $"Header column #{i + 1} must be '{RequiredColumns[i]}' (got '{(i < header.Length ? header[i] : "")}').";
return null;
}
}
var rows = new List<EquipmentInput>();
for (var lineIdx = 1; lineIdx < lines.Length; lineIdx++)
{
// NOTE: simple comma split — no RFC4180 quoting; values with commas are not supported.
var parts = lines[lineIdx].Split(',').Select(c => c.Trim()).ToArray();
if (parts.Length < RequiredColumns.Length)
{
_parseError = $"Row {lineIdx}: too few columns (got {parts.Length}, need {RequiredColumns.Length}).";
return null;
}
rows.Add(new EquipmentInput(
Name: parts[0],
MachineCode: parts[1],
UnsLineId: parts[2],
ZTag: NullIfEmpty(parts, 3),
SAPID: NullIfEmpty(parts, 4),
Manufacturer: NullIfEmpty(parts, 5),
Model: NullIfEmpty(parts, 6),
SerialNumber: null,
HardwareRevision: null,
SoftwareRevision: null,
YearOfConstruction: null,
AssetLocation: null,
ManufacturerUri: null,
DeviceManualUri: null,
Enabled: true));
}
var (rows, error) = EquipmentCsvParser.Parse(csv);
_parseError = error;
return rows;
}
private static string? NullIfEmpty(string[] parts, int idx) =>
idx < parts.Length && !string.IsNullOrWhiteSpace(parts[idx]) ? parts[idx] : null;
}
@@ -0,0 +1,85 @@
using ZB.MOM.WW.OtOpcUa.Commons.Csv;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
/// <summary>
/// Parses the equipment-import CSV (R04 S6). It used to be a naive comma split, so a quoted
/// <c>Manufacturer</c> containing a comma silently shifted every later column with no error. It now
/// reads through the RFC 4180 <see cref="CsvParser"/> — the same authority the raw-tag importer uses —
/// so quoted fields, embedded commas/newlines and doubled quotes parse correctly, and malformed quoting
/// is reported instead of guessed at.
/// </summary>
public static class EquipmentCsvParser
{
/// <summary>The leading header columns the import requires, in order (case-insensitive).</summary>
public static readonly IReadOnlyList<string> RequiredColumns = ["Name", "MachineCode", "UnsLineId"];
/// <summary>
/// Parses the CSV. Requires a header row whose first three columns are Name, MachineCode, UnsLineId;
/// the optional ZTag, SAPID, Manufacturer, Model follow. Cells are trimmed; blank optional cells parse
/// as null; blank lines are skipped.
/// </summary>
/// <param name="csv">The pasted CSV text.</param>
/// <returns>The rows, or a null list with an operator-facing error.</returns>
public static (List<EquipmentInput>? Rows, string? Error) Parse(string? csv)
{
if (string.IsNullOrWhiteSpace(csv)) return (null, "CSV is empty.");
IReadOnlyList<string[]> records;
try
{
records = CsvParser.Parse(csv);
}
catch (FormatException ex)
{
return (null, $"The CSV is malformed: {ex.Message}");
}
var lines = records
.Where(r => r.Length > 1 || r[0].Trim().Length > 0)
.Select(r => r.Select(c => c.Trim()).ToArray())
.ToList();
if (lines.Count < 2) return (null, "Need a header row and at least one data row.");
var header = lines[0];
for (var i = 0; i < RequiredColumns.Count; i++)
{
if (i >= header.Length || !string.Equals(header[i], RequiredColumns[i], StringComparison.OrdinalIgnoreCase))
{
return (null, $"Header column #{i + 1} must be '{RequiredColumns[i]}' (got '{(i < header.Length ? header[i] : "")}').");
}
}
var rows = new List<EquipmentInput>(lines.Count - 1);
for (var lineIdx = 1; lineIdx < lines.Count; lineIdx++)
{
var parts = lines[lineIdx];
if (parts.Length < RequiredColumns.Count)
{
return (null, $"Row {lineIdx}: too few columns (got {parts.Length}, need {RequiredColumns.Count}).");
}
rows.Add(new EquipmentInput(
Name: parts[0],
MachineCode: parts[1],
UnsLineId: parts[2],
ZTag: NullIfEmpty(parts, 3),
SAPID: NullIfEmpty(parts, 4),
Manufacturer: NullIfEmpty(parts, 5),
Model: NullIfEmpty(parts, 6),
SerialNumber: null,
HardwareRevision: null,
SoftwareRevision: null,
YearOfConstruction: null,
AssetLocation: null,
ManufacturerUri: null,
DeviceManualUri: null,
Enabled: true));
}
return (rows, null);
}
private static string? NullIfEmpty(string[] parts, int idx) =>
idx < parts.Length && !string.IsNullOrWhiteSpace(parts[idx]) ? parts[idx] : null;
}