Skip to content

Commit

Permalink
Fixed #669 Validations improved, and not too harsh when not showing
Browse files Browse the repository at this point in the history
  • Loading branch information
rappen committed Apr 13, 2022
1 parent aeb273a commit d587996
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 82 deletions.
35 changes: 35 additions & 0 deletions FetchXmlBuilder/Controls/FetchXmlElementControlBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,49 @@ public enum ControlValidationLevel

public class ControlValidationResult
{
private const string IsRequired = "{0} is required";
private const string InValid = "{0} is not valid";
private const string NotInMetadata = "{0} is not in the database.";
private const string NotShowingNow = "{0} is not currently shown.";

public ControlValidationResult(ControlValidationLevel level, string message, string url = null)
{
Level = level;
Message = message;
Url = url;
}

public ControlValidationResult(ControlValidationLevel level, string control, ControlValidationMessage message, string url = null)
{
Level = level;
switch (message)
{
case ControlValidationMessage.IsRequired:
Message = string.Format(IsRequired, control);
break;
case ControlValidationMessage.InValid:
Message = string.Format(InValid, control);
break;
case ControlValidationMessage.NotInMetadata:
Message = string.Format(NotInMetadata, control);
break;
case ControlValidationMessage.NotShowingNow:
Message = string.Format(NotShowingNow, control);
break;
}
Url = url;
}

public ControlValidationLevel Level { get; }
public string Message { get; }
public string Url { get; }
}

public enum ControlValidationMessage
{
IsRequired,
InValid,
NotInMetadata,
NotShowingNow
}
}
15 changes: 12 additions & 3 deletions FetchXmlBuilder/Controls/attributeControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Cinteros.Xrm.FetchXmlBuilder.Controls
public partial class attributeControl : FetchXmlElementControlBase
{
private readonly AttributeMetadata[] attributes;
private readonly AttributeMetadata[] allattributes;
private bool aggregate;

public attributeControl() : this(null, null, null, null)
Expand All @@ -20,6 +21,7 @@ public attributeControl(TreeNode node, AttributeMetadata[] attributes, FetchXmlB
{
InitializeComponent();
this.attributes = attributes;
allattributes = fetchXmlBuilder.GetAllAttribues(node.LocalEntityName());
InitializeFXB(null, fetchXmlBuilder, tree, node);
}

Expand Down Expand Up @@ -50,11 +52,18 @@ protected override ControlValidationResult ValidateControl(Control control)
{
if (string.IsNullOrWhiteSpace(cmbAttribute.Text))
{
return new ControlValidationResult(ControlValidationLevel.Error, "Attribute is required");
return new ControlValidationResult(ControlValidationLevel.Error, "Attribute", ControlValidationMessage.IsRequired);
}
if (fxb.Service != null && !cmbAttribute.Items.OfType<AttributeItem>().Any(a => a.ToString() == cmbAttribute.Text))
if (fxb.Service != null)
{
return new ControlValidationResult(ControlValidationLevel.Warning, "Attribute is not valid");
if (!allattributes.Any(a=>a.LogicalName == cmbAttribute.Text))
{
return new ControlValidationResult(ControlValidationLevel.Warning, "Attribute", ControlValidationMessage.NotInMetadata);
}
if (!cmbAttribute.Items.OfType<AttributeItem>().Any(a => a.ToString() == cmbAttribute.Text))
{
return new ControlValidationResult(ControlValidationLevel.Info, "Attribute", ControlValidationMessage.NotShowingNow);
}
}
}
else if (control == txtAlias)
Expand Down
8 changes: 4 additions & 4 deletions FetchXmlBuilder/Controls/conditionControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,24 @@ protected override ControlValidationResult ValidateControl(Control control)
{
if (string.IsNullOrWhiteSpace(cmbAttribute.Text))
{
return new ControlValidationResult(ControlValidationLevel.Error, "Attribute is required");
return new ControlValidationResult(ControlValidationLevel.Error, "Attribute", ControlValidationMessage.IsRequired);
}

if (fxb.Service != null && !cmbAttribute.Items.OfType<AttributeItem>().Any(i => i.ToString() == cmbAttribute.Text))
{
return new ControlValidationResult(ControlValidationLevel.Warning, "Attribute is not valid");
return new ControlValidationResult(ControlValidationLevel.Warning, "Attribute", ControlValidationMessage.InValid);
}
}
else if (control == cmbOperator || control == cmbValue)
{
if (control == cmbOperator && string.IsNullOrWhiteSpace(cmbOperator.Text))
{
return new ControlValidationResult(ControlValidationLevel.Error, "Operator is required");
return new ControlValidationResult(ControlValidationLevel.Error, "Operator", ControlValidationMessage.IsRequired);
}

if (control == cmbOperator && !cmbOperator.Items.OfType<OperatorItem>().Any(i => i.ToString() == cmbOperator.Text))
{
return new ControlValidationResult(ControlValidationLevel.Error, "Operator is not valid");
return new ControlValidationResult(ControlValidationLevel.Error, "Operator", ControlValidationMessage.InValid);
}

if (cmbOperator.SelectedItem != null && cmbOperator.SelectedItem is OperatorItem oper && (!oper.IsMultipleValuesType || Node.Nodes.Count > 0))
Expand Down
15 changes: 13 additions & 2 deletions FetchXmlBuilder/Controls/entityControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,23 @@ protected override ControlValidationResult ValidateControl(Control control)
{
if (string.IsNullOrWhiteSpace(cmbEntity.Text))
{
return new ControlValidationResult(ControlValidationLevel.Error, "Entity is required");
return new ControlValidationResult(ControlValidationLevel.Error, "Entity", ControlValidationMessage.IsRequired);
}

if (!(cmbEntity.SelectedItem is EntityItem) && fxb.Service != null)
{
if (!fxb.entities.Any(e => e.Key == cmbEntity.Text))
{
return new ControlValidationResult(ControlValidationLevel.Warning, "Entity", ControlValidationMessage.NotInMetadata);
}
if (!cmbEntity.Items.OfType<string>().Any(i => i == cmbEntity.Text))
{
return new ControlValidationResult(ControlValidationLevel.Info, "Entity", ControlValidationMessage.NotShowingNow);
}
}
if (fxb.Service != null && !cmbEntity.Items.OfType<EntityItem>().Any(i => i.ToString() == cmbEntity.Text))
{
return new ControlValidationResult(ControlValidationLevel.Warning, "Entity is not valid");
return new ControlValidationResult(ControlValidationLevel.Warning, "Entity", ControlValidationMessage.IsRequired);
}
}

Expand Down
32 changes: 25 additions & 7 deletions FetchXmlBuilder/Controls/linkEntityControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ private void cmbRelationship_DropDownClosed(object sender, EventArgs e)

protected override ControlValidationResult ValidateControl(Control control)
{
var parententityname = Node.Parent.LocalEntityName();
var parententity = fxb.Service != null && fxb.entities != null && fxb.entities.ContainsKey(parententityname) ? fxb.entities[parententityname] : null;
var currententity = fxb.Service != null && fxb.entities != null && fxb.entities.ContainsKey(cmbEntity.Text) ? fxb.entities[cmbEntity.Text] : null;
if (control == cmbRelationship && string.IsNullOrWhiteSpace(cmbEntity.Text) && string.IsNullOrWhiteSpace(cmbFrom.Text) && string.IsNullOrWhiteSpace(cmbTo.Text))
{
return new ControlValidationResult(ControlValidationLevel.Info, "Select a relationship to populate fields below");
Expand All @@ -302,12 +305,19 @@ protected override ControlValidationResult ValidateControl(Control control)
{
if (string.IsNullOrWhiteSpace(cmbEntity.Text))
{
return new ControlValidationResult(ControlValidationLevel.Error, "Entity is required");
return new ControlValidationResult(ControlValidationLevel.Error, "Entity", ControlValidationMessage.IsRequired);
}

if (fxb.Service != null && !cmbEntity.Items.OfType<string>().Any(i => i == cmbEntity.Text))
if (fxb.Service != null)
{
return new ControlValidationResult(ControlValidationLevel.Warning, "Entity is not valid");
if (currententity == null)
{
return new ControlValidationResult(ControlValidationLevel.Warning, "Entity", ControlValidationMessage.NotInMetadata);
}
if (!cmbEntity.Items.OfType<string>().Any(i => i == cmbEntity.Text))
{
return new ControlValidationResult(ControlValidationLevel.Info, "Entity", ControlValidationMessage.NotShowingNow);
}
}
}

Expand All @@ -321,13 +331,17 @@ protected override ControlValidationResult ValidateControl(Control control)
}
else
{
return new ControlValidationResult(ControlValidationLevel.Error, "From attribute is required");
return new ControlValidationResult(ControlValidationLevel.Error, "From attribute", ControlValidationMessage.IsRequired);
}
}

if (currententity != null && !currententity.Attributes.Any(a => a.LogicalName == cmbFrom.Text))
{
return new ControlValidationResult(ControlValidationLevel.Warning, "From attribute", ControlValidationMessage.NotInMetadata);
}
if (fxb.Service != null && !cmbFrom.Items.OfType<string>().Any(i => i == cmbFrom.Text))
{
return new ControlValidationResult(ControlValidationLevel.Warning, "From attribute is not valid");
return new ControlValidationResult(ControlValidationLevel.Info, "From attribute", ControlValidationMessage.NotShowingNow);
}
}

Expand All @@ -341,13 +355,17 @@ protected override ControlValidationResult ValidateControl(Control control)
}
else
{
return new ControlValidationResult(ControlValidationLevel.Error, "To attribute is required");
return new ControlValidationResult(ControlValidationLevel.Error, "To attribute", ControlValidationMessage.IsRequired);
}
}

if (parententity != null && !parententity.Attributes.Any(a => a.LogicalName == cmbTo.Text))
{
return new ControlValidationResult(ControlValidationLevel.Warning, "To attribute", ControlValidationMessage.NotInMetadata);
}
if (fxb.Service != null && !cmbTo.Items.OfType<string>().Any(i => i == cmbTo.Text))
{
return new ControlValidationResult(ControlValidationLevel.Warning, "To attribute is not valid");
return new ControlValidationResult(ControlValidationLevel.Info, "To attribute", ControlValidationMessage.NotShowingNow);
}
}

Expand Down
23 changes: 15 additions & 8 deletions FetchXmlBuilder/Controls/orderControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ public partial class orderControl : FetchXmlElementControlBase
{
private bool friendly;
private AttributeMetadata[] attributes;
private readonly AttributeMetadata[] allattributes;

public orderControl() : this(null, null, null, null)
{
}

public orderControl(TreeNode node, AttributeMetadata[] attributes, FetchXmlBuilder fetchXmlBuilder, TreeBuilderControl tree)
{
InitializeComponent();
friendly = FetchXmlBuilder.friendlyNames;
this.attributes = attributes;

InitializeComponent();
allattributes = fetchXmlBuilder.GetAllAttribues(node.LocalEntityName());
InitializeFXB(null, fetchXmlBuilder, tree, node);
}

Expand Down Expand Up @@ -79,25 +80,31 @@ protected override ControlValidationResult ValidateControl(Control control)
{
if (string.IsNullOrWhiteSpace(cmbAttribute.Text))
{
return new ControlValidationResult(ControlValidationLevel.Error, "Attribute is required");
return new ControlValidationResult(ControlValidationLevel.Error, "Attribute", ControlValidationMessage.IsRequired);
}

if (fxb.Service != null && !cmbAttribute.Items.OfType<AttributeItem>().Any(i => i.ToString() == cmbAttribute.Text))
if (fxb.Service != null)
{
return new ControlValidationResult(ControlValidationLevel.Warning, "Attribute is not valid");
if (!allattributes.Any(a => a.LogicalName == cmbAttribute.Text))
{
return new ControlValidationResult(ControlValidationLevel.Warning, "Attribute", ControlValidationMessage.NotInMetadata);
}
if (!cmbAttribute.Items.OfType<AttributeItem>().Any(a => a.ToString() == cmbAttribute.Text))
{
return new ControlValidationResult(ControlValidationLevel.Info, "Attribute", ControlValidationMessage.NotShowingNow);
}
}
}

if (control == cmbAlias && cmbAlias.Enabled)
{
if (string.IsNullOrWhiteSpace(cmbAlias.Text))
{
return new ControlValidationResult(ControlValidationLevel.Error, "Alias is required");
return new ControlValidationResult(ControlValidationLevel.Error, "Alias", ControlValidationMessage.IsRequired);
}

if (!cmbAlias.Items.OfType<string>().Any(i => i == cmbAlias.Text))
{
return new ControlValidationResult(ControlValidationLevel.Warning, "Alias is not valid");
return new ControlValidationResult(ControlValidationLevel.Warning, "Alias", ControlValidationMessage.InValid);
}
}

Expand Down
57 changes: 20 additions & 37 deletions FetchXmlBuilder/DockControls/TreeBuilderControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -568,23 +568,13 @@ private void HandleNodeSelection(TreeNode node)
break;

case "link-entity":
if (node.Parent != null)
if (node.Parent != null && node.Parent.LocalEntityName() is string parententity && fxb.NeedToLoadEntity(parententity))
{
switch (node.Parent.Name)
if (!fxb.working)
{
case "entity":
case "link-entity":
var entityName = node.Parent.Value("name");
if (fxb.NeedToLoadEntity(entityName))
{
if (!fxb.working)
{
fxb.LoadEntityDetails(entityName, RefreshSelectedNode);
}
break;
}
break;
fxb.LoadEntityDetails(parententity, RefreshSelectedNode);
}
break;
}
var linkEntityName = node.Value("name");
if (fxb.NeedToLoadEntity(linkEntityName))
Expand All @@ -600,31 +590,24 @@ private void HandleNodeSelection(TreeNode node)

case "attribute":
case "order":
if (node.Parent != null)
if (node.LocalEntityName() is string entity && !string.IsNullOrWhiteSpace(entity))
{
switch (node.Parent.Name)
if (fxb.NeedToLoadEntity(entity))
{
if (!fxb.working)
{
fxb.LoadEntityDetails(entity, RefreshSelectedNode);
}
break;
}
AttributeMetadata[] attributes = fxb.GetDisplayAttributes(entity);
if (node.Name == "attribute")
{
ctrl = new attributeControl(node, attributes, fxb, this);
}
else if (node.Name == "order")
{
case "entity":
case "link-entity":
var entityName = node.Parent.Value("name");
if (fxb.NeedToLoadEntity(entityName))
{
if (!fxb.working)
{
fxb.LoadEntityDetails(entityName, RefreshSelectedNode);
}
break;
}
AttributeMetadata[] attributes = fxb.GetDisplayAttributes(entityName);
if (node.Name == "attribute")
{
ctrl = new attributeControl(node, attributes, fxb, this);
}
else if (node.Name == "order")
{
ctrl = new orderControl(node, attributes, fxb, this);
}
break;
ctrl = new orderControl(node, attributes, fxb, this);
}
}
break;
Expand Down
Loading

0 comments on commit d587996

Please sign in to comment.