Fix host relation matching and display in related record panels, Resolve host fields through HostType/HostId, add numeric-aware ID matching for relationship links, and restore host names in related grids instead of raw host IDs.

This commit is contained in:
dingo
2026-04-08 14:35:50 -04:00
parent fdb3030496
commit a883823051
@@ -440,6 +440,11 @@
private string GetDisplayValue(IInventoryEntity entity, FieldDefinition field)
{
var rawValue = EntityValueResolver.GetValue(entity, field.PropertyName, field.JsonPropertyName);
if (string.Equals(field.PropertyName, "HostName", StringComparison.OrdinalIgnoreCase))
{
return ResolveHostDisplay(entity, rawValue);
}
if (string.IsNullOrEmpty(rawValue))
return string.Empty;
@@ -467,39 +472,8 @@
return GetDisplayValue(entity, field);
}
var hostType = GetPropertyValue(entity, "HostType");
var hostId = GetPropertyValue(entity, "HostId");
var hostName = EntityValueResolver.GetValue(entity, field.PropertyName, field.JsonPropertyName);
if (string.IsNullOrWhiteSpace(hostType) || string.IsNullOrWhiteSpace(hostId))
{
return hostName;
}
var hostContentType = TryMapHostTypeToContentType(hostType);
if (hostContentType is null)
{
return string.IsNullOrWhiteSpace(hostName)
? $"{hostType} #{hostId}"
: $"{hostType} #{hostId} ({hostName})";
}
if (AllData.TryGetValue(hostContentType.Value, out var relatedEntities))
{
var hostEntity = relatedEntities.FirstOrDefault(item =>
string.Equals(item.Id.ToString(), hostId, StringComparison.OrdinalIgnoreCase));
if (hostEntity is not null)
{
var hostDefinition = ContentTypeRegistry.GetDefinition(hostContentType.Value);
var displayName = GetEntityDisplayName(hostEntity, hostDefinition);
return $"{hostDefinition.DisplayName}: {displayName}";
}
}
return string.IsNullOrWhiteSpace(hostName)
? $"{hostType} #{hostId}"
: $"{hostType} #{hostId} ({hostName})";
return ResolveHostDisplay(entity, hostName);
}
private List<IInventoryEntity> SortRelatedItems(List<IInventoryEntity> items, ContentTypeDefinition definition)
@@ -957,9 +931,87 @@
return HostTypesMatch(left, right);
}
if (propertyName.EndsWith("Id", StringComparison.OrdinalIgnoreCase))
{
return IdsMatch(left, right);
}
return string.Equals(left, right, StringComparison.OrdinalIgnoreCase);
}
private string ResolveHostDisplay(IInventoryEntity entity, string? fallbackHostName)
{
var hostType = GetPropertyValue(entity, "HostType");
var hostId = GetPropertyValue(entity, "HostId");
if (string.IsNullOrWhiteSpace(hostType) || string.IsNullOrWhiteSpace(hostId))
{
return fallbackHostName ?? string.Empty;
}
var hostContentType = TryMapHostTypeToContentType(hostType);
if (hostContentType is null)
{
return string.IsNullOrWhiteSpace(fallbackHostName)
? $"{hostType} #{hostId}"
: $"{hostType} #{hostId} ({fallbackHostName})";
}
if (AllData.TryGetValue(hostContentType.Value, out var relatedEntities))
{
var hostEntity = relatedEntities.FirstOrDefault(item => IdsMatch(item.Id.ToString(), hostId));
if (hostEntity is not null)
{
var hostDefinition = ContentTypeRegistry.GetDefinition(hostContentType.Value);
var displayName = GetEntityDisplayName(hostEntity, hostDefinition);
return $"{hostDefinition.DisplayName}: {displayName}";
}
}
return string.IsNullOrWhiteSpace(fallbackHostName)
? $"{hostType} #{hostId}"
: $"{hostType} #{hostId} ({fallbackHostName})";
}
private static bool IdsMatch(string? left, string? right)
{
if (TryParseId(left, out var leftId) && TryParseId(right, out var rightId))
{
return leftId == rightId;
}
return string.Equals(left?.Trim(), right?.Trim(), StringComparison.OrdinalIgnoreCase);
}
private static bool TryParseId(string? input, out int value)
{
value = 0;
if (string.IsNullOrWhiteSpace(input))
{
return false;
}
input = input.Trim();
if (int.TryParse(input, out value))
{
return true;
}
if (decimal.TryParse(input, out var decimalValue) &&
decimalValue == decimal.Truncate(decimalValue) &&
decimalValue >= int.MinValue &&
decimalValue <= int.MaxValue)
{
value = decimal.ToInt32(decimalValue);
return true;
}
return false;
}
private static bool HostTypesMatch(string? left, string? right)
{
var leftType = NormalizeHostType(left);