Skip to content

Commit c9e81f3

Browse files
committed
Refactor Custom Field checking
Fix oversimplified method to catch most things, but it still reports renamed custom fields as "bad"; check those first (need to test) Change-Id: Ib601edfa1aa670e87c7252922597769a9e259297
1 parent 358a410 commit c9e81f3

File tree

1 file changed

+68
-64
lines changed

1 file changed

+68
-64
lines changed

Src/Common/Controls/XMLViews/XmlBrowseViewBaseVc.cs

Lines changed: 68 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -591,94 +591,98 @@ internal int ListItemsClass
591591
}
592592

593593
/// <summary>
594-
/// check to see if column spec is still valid and useable.
594+
/// check to see if column spec is still valid.
595595
/// </summary>
596-
/// <param name="node"></param>
597596
/// <returns>True if we can't prove the column is invalid</returns>
598-
internal bool IsValidColumnSpec(XmlNode node)
597+
internal bool IsValidColumnSpec(XmlNode colSpec)
598+
{
599+
if (GetPartFromParentNode(colSpec, ListItemsClass) == null)
600+
return false; // invalid node, don't add.
601+
// If it is a Custom Field, check that it is valid and has the correct label
602+
if (IsCustomField(colSpec, out var isValid))
603+
{
604+
return isValid;
605+
}
606+
return IsValidColumnSpec_Oversimplified(colSpec);
607+
/*/
608+
//var possibleColumns = PossibleColumnSpecs; // REVIEW (Hasso) 2025.11: why do we need a local copy of this pointer?
609+
// first, check to see if we can find some part or child node information
610+
// to process. Eg. Custom field column nodes that refer to parts that no longer exist
611+
// because the custom field has been removed so the parts cannot be generated
612+
if (GetPartFromParentNode(colSpec, ListItemsClass) == null)
613+
return false; // invalid node, don't add.
614+
// If it is a Custom Field, check that it is valid and has the correct label
615+
if (TryColumnForCustomField(colSpec, ListItemsClass, out var customFieldNode, out var propWs))
616+
{
617+
return IsValidCustomField(colSpec, customFieldNode, propWs);
618+
}
619+
if (CheckForBadReversalIndex(colSpec))
620+
return false;
621+
return true; // valid as far as we can tell.
622+
/**/
623+
}
624+
625+
private bool IsValidColumnSpec_Oversimplified(XmlNode colSpec)
599626
{
600627
// In the simple case, `node`s label should match a label in PossibleColumnSpecs. There may be more complicated cases.
601628
// The existing code sure seems complicated and is entirely untested.
602-
var label = XmlUtils.GetLocalizedAttributeValue(node, "label", null) ??
603-
XmlUtils.GetMandatoryAttributeValue(node, "label");
604-
var oLabel = XmlUtils.GetAttributeValue(node, "originalLabel");
605-
//MenuItem mi = new MenuItem(label, ConfigItemClicked);
606-
if (XmlViewsUtils.FindNodeWithAttrVal(ColumnSpecs, "label", label) != null ||
607-
XmlViewsUtils.FindNodeWithAttrVal(ColumnSpecs, "originalLabel", label) != null)
608-
return PossibleColumnSpecs.Contains(node);
609-
//// first, check to see if we can find some part or child node information
610-
//// to process. Eg. Custom field column nodes that refer to parts that no longer exist
611-
//// because the custom field has been removed so the parts cannot be generated
612-
//XmlNode partNode = this.GetPartFromParentNode(node, this.ListItemsClass);
613-
//if (partNode == null)
614-
// return false; // invalid node, don't add.
615-
//bool badCustomField = CheckForBadCustomField(possibleColumns, node);
616-
//if (badCustomField)
617-
// return false; // invalid custom field, don't add.
618-
//bool badReversalIndex = CheckForBadReversalIndex(possibleColumns, node);
619-
//if (badReversalIndex)
620-
// return false;
621-
return true; // valid as far as we can tell.
629+
var label = XmlUtils.GetMandatoryAttributeValue(colSpec, "label");
630+
var originalLabel = XmlUtils.GetAttributeValue(colSpec, "originalLabel");
631+
return (XmlViewsUtils.FindNodeWithAttrVal(PossibleColumnSpecs, "label", label) != null ||
632+
XmlViewsUtils.FindNodeWithAttrVal(PossibleColumnSpecs, "label", originalLabel) != null);
622633
}
623634

624635
/// <summary>
625-
/// Check for a nonexistent custom field. (Custom fields can be deleted.) As a side-effect,
626-
/// if the node refers to a valid custom field, the label attribute is adjusted to what we
627-
/// want the user to see.
636+
/// Check whether the column spec is a custom field, and if so, if it is still valid (Custom fields can be deleted).
637+
/// If the node refers to a valid custom field, the label attribute is adjusted to what we want the user to see.
628638
/// </summary>
629-
/// <param name="possibleColumns"></param>
630-
/// <param name="node"></param>
631-
/// <returns>true if this node refers to a nonexistent custom field</returns>
632-
private bool CheckForBadCustomField(List<XmlNode> possibleColumns, XmlNode node)
639+
private bool IsCustomField(XmlNode colSpec, out bool isValidCustomField)
633640
{
634-
// see if this node is based on a layout. If so, get its part
635-
PropWs propWs;
636-
XmlNode columnForCustomField = null;
637-
if (this.TryColumnForCustomField(node, this.ListItemsClass, out columnForCustomField, out propWs))
641+
if (!TryColumnForCustomField(colSpec, ListItemsClass, out var columnForCustomField, out var propWs))
638642
{
639-
if (columnForCustomField != null)
640-
{
641-
string fieldName = XmlUtils.GetAttributeValue(columnForCustomField, "field");
642-
string className = XmlUtils.GetAttributeValue(columnForCustomField, "class");
643-
if (!String.IsNullOrEmpty(fieldName) && !String.IsNullOrEmpty(className))
644-
{
645-
if ((m_mdc as IFwMetaDataCacheManaged).FieldExists(className, fieldName, false))
646-
{
647-
ColumnConfigureDialog.GenerateColumnLabel(node, m_cache);
648-
return false;
649-
}
650-
}
651-
return true;
652-
}
653-
else if (propWs != null)
643+
isValidCustomField = false;
644+
return false;
645+
}
646+
647+
if (columnForCustomField != null)
648+
{
649+
var fieldName = XmlUtils.GetAttributeValue(columnForCustomField, "field");
650+
var className = XmlUtils.GetAttributeValue(columnForCustomField, "class");
651+
if (!string.IsNullOrEmpty(fieldName) && !string.IsNullOrEmpty(className) &&
652+
((IFwMetaDataCacheManaged)m_mdc).FieldExists(className, fieldName, false))
654653
{
655-
XmlUtils.AppendAttribute(node, "originalLabel", GetNewLabelFromMatchingCustomField(possibleColumns, propWs.flid));
656-
ColumnConfigureDialog.GenerateColumnLabel(node, m_cache);
654+
ColumnConfigureDialog.GenerateColumnLabel(colSpec, m_cache);
655+
isValidCustomField = true;
657656
}
658657
else
659658
{
660-
// it's an invalid custom field.
661-
return true;
659+
isValidCustomField = false;
662660
}
663661
}
664-
return false;
662+
else if (propWs == null)
663+
{
664+
isValidCustomField = false;
665+
}
666+
else
667+
{
668+
XmlUtils.AppendAttribute(colSpec, "originalLabel", GetNewLabelFromMatchingCustomField(propWs.flid));
669+
ColumnConfigureDialog.GenerateColumnLabel(colSpec, m_cache);
670+
isValidCustomField = true;
671+
}
672+
return true;
665673
}
666674

667-
private string GetNewLabelFromMatchingCustomField(List<XmlNode> possibleColumns, int flid)
675+
private string GetNewLabelFromMatchingCustomField(int flid)
668676
{
669-
foreach (XmlNode possibleColumn in possibleColumns)
677+
foreach (var possibleColumn in PossibleColumnSpecs)
670678
{
671679
// Desired node may be a child of a child... (See LT-6447.)
672-
PropWs propWs;
673-
XmlNode columnForCustomField;
674-
if (TryColumnForCustomField(possibleColumn, ListItemsClass, out columnForCustomField, out propWs))
680+
if (TryColumnForCustomField(possibleColumn, ListItemsClass, out _, out var propWs))
675681
{
676682
// the flid of the updated custom field node matches the given flid of the old node.
677683
if (propWs != null && propWs.flid == flid)
678684
{
679-
string label = XmlUtils.GetLocalizedAttributeValue(possibleColumn,
680-
"label", null);
681-
return label;
685+
return XmlUtils.GetLocalizedAttributeValue(possibleColumn, "label", null);
682686
}
683687
}
684688
}
@@ -687,11 +691,11 @@ private string GetNewLabelFromMatchingCustomField(List<XmlNode> possibleColumns,
687691

688692
/// <summary>
689693
/// Check for an invalid reversal index. (Reversal indexes can be deleted.)
694+
/// REVIEW (Hasso) 2025-11: when does a column spec refer to a reversal index?
690695
/// </summary>
691-
/// <param name="possibleColumns"></param>
692696
/// <param name="node"></param>
693697
/// <returns>true if this node refers to a nonexistent reversal index.</returns>
694-
private bool CheckForBadReversalIndex(List<XmlNode> possibleColumns, XmlNode node)
698+
private bool CheckForBadReversalIndex(XmlNode node)
695699
{
696700
// Look for a child node which is similar to this (value of ws attribute may differ):
697701
// <string field="ReversalEntriesText" ws="$ws=es"/>

0 commit comments

Comments
 (0)