Skip to content

Commit

Permalink
Changed how entities (metadata) are used. 17 files...
Browse files Browse the repository at this point in the history
  • Loading branch information
rappen committed May 11, 2022
1 parent 169ed90 commit 98f6379
Show file tree
Hide file tree
Showing 17 changed files with 140 additions and 119 deletions.
16 changes: 13 additions & 3 deletions FetchXmlBuilder/AppCode/ODataCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ private static string GetCondition(FetchEntityType entity, condition condition,
case AttributeTypeCode.Status:
result += "/Value";
break;

case AttributeTypeCode.Lookup:
result += "/Id";
break;
Expand All @@ -234,21 +235,27 @@ private static string GetCondition(FetchEntityType entity, condition condition,
case @operator.ge:
result += $" {condition.@operator} ";
break;

case @operator.neq:
result += " ne ";
break;

case @operator.@null:
result += " eq null";
break;

case @operator.notnull:
result += " ne null";
break;

case @operator.like:
result = $"substringof('{condition.value}', {attrMeta.SchemaName})";
break;

case @operator.notlike:
result = $"not substringof('{condition.value}', {attrMeta.SchemaName})";
break;

case @operator.@in:
case @operator.notin:
throw new Exception($"Condition operator '{condition.@operator}' is not yet supported by the OData generator");
Expand All @@ -270,12 +277,14 @@ private static string GetCondition(FetchEntityType entity, condition condition,
case AttributeTypeCode.Picklist:
result += condition.value;
break;

case AttributeTypeCode.Uniqueidentifier:
case AttributeTypeCode.Lookup:
case AttributeTypeCode.Customer:
case AttributeTypeCode.Owner:
result += $"(guid'{condition.value}')";
break;

case AttributeTypeCode.DateTime:
var date = DateTime.Parse(condition.value);
var datestr = string.Empty;
Expand All @@ -289,6 +298,7 @@ private static string GetCondition(FetchEntityType entity, condition condition,
}
result += $"datetime'{datestr}'";
break;

default:
result += $"'{condition.value}'";
break;
Expand Down Expand Up @@ -320,7 +330,7 @@ private static string GetOrder(FetchEntityType entity, FetchXmlBuilder sender)
private static string LogicalToSchemaName(string entity, FetchXmlBuilder sender)
{
GetEntityMetadata(entity, sender);
var entityMeta = sender.entities[entity];
var entityMeta = sender.GetEntity(entity);
return entityMeta.SchemaName;
}

Expand All @@ -341,7 +351,7 @@ private static void GetEntityMetadata(string entity, FetchXmlBuilder sender)
{
sender.LoadEntityDetails(entity, null, false);
}
if (!sender.entities.ContainsKey(entity))
if (sender.GetEntity(entity) == null)
{
throw new Exception($"No metadata for entity: {entity}");
}
Expand All @@ -350,7 +360,7 @@ private static void GetEntityMetadata(string entity, FetchXmlBuilder sender)
private static RelationshipMetadataBase LinkItemToRelation(string entityname, FetchLinkEntityType linkitem, FetchXmlBuilder sender)
{
GetEntityMetadata(entityname, sender);
var entity = sender.entities[entityname];
var entity = sender.GetEntity(entityname);
foreach (var relation in entity.OneToManyRelationships)
{
if (relation.ReferencedEntity == entityname &&
Expand Down
12 changes: 9 additions & 3 deletions FetchXmlBuilder/AppCode/Validations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ internal static ControlValidationResult GetWarning(TreeNode node, FetchXmlBuilde
{
case "fetch":
break;

case "entity":
if (string.IsNullOrWhiteSpace(name))
{
return new ControlValidationResult(ControlValidationLevel.Warning, "Entity Name must be included.");
}
break;

case "link-entity":
if (string.IsNullOrWhiteSpace(name) ||
string.IsNullOrWhiteSpace(node.Value("to")) ||
Expand All @@ -41,6 +43,7 @@ internal static ControlValidationResult GetWarning(TreeNode node, FetchXmlBuilde
return new ControlValidationResult(ControlValidationLevel.Info, "Links to records that aren't parents may cause paging issues.", "https://markcarrington.dev/2021/02/23/msdyn365-internals-paging-gotchas/#multiple_linked_entities");
}
break;

case "attribute":
if (string.IsNullOrWhiteSpace(name))
{
Expand Down Expand Up @@ -93,12 +96,14 @@ internal static ControlValidationResult GetWarning(TreeNode node, FetchXmlBuilde
}
}
break;

case "filter":
if (node.Nodes.Count == 0)
{
return new ControlValidationResult(ControlValidationLevel.Info, "Filter shound have at least one Condition.");
}
break;

case "condition":
if (string.IsNullOrWhiteSpace(attribute))
{
Expand All @@ -122,15 +127,16 @@ internal static ControlValidationResult GetWarning(TreeNode node, FetchXmlBuilde
{
return new ControlValidationResult(ControlValidationLevel.Warning, $"Attribute '{attribute}' is not in the table '{parententity}'.");
}

}
break;

case "value":
if (string.IsNullOrWhiteSpace(node.Value("#text")))
{
return new ControlValidationResult(ControlValidationLevel.Warning, "Value should be added.");
}
break;

case "order":
if (node.IsFetchAggregate())
{
Expand Down Expand Up @@ -193,7 +199,7 @@ private static bool HasPrimaryIdAttribute(TreeNode parent, FetchXmlBuilder fxb)
if (fxb == null || fxb.entities == null)
return true;

if (!fxb.entities.TryGetValue(entity, out var metadata))
if (!(fxb.GetEntity(entity) is EntityMetadata metadata))
return true;

if (string.IsNullOrWhiteSpace(metadata.PrimaryIdAttribute))
Expand Down Expand Up @@ -238,4 +244,4 @@ private static bool HasSortOnAttribute(TreeNode node)
return false;
}
}
}
}
4 changes: 2 additions & 2 deletions FetchXmlBuilder/Controls/attributeControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public attributeControl(TreeNode node, AttributeMetadata[] attributes, FetchXmlB
{
InitializeComponent();
this.attributes = attributes;
allattributes = fetchXmlBuilder.GetAllAttribues(node.LocalEntityName());
allattributes = fetchXmlBuilder.GetAllAttribues(node.LocalEntityName()).ToArray();
InitializeFXB(null, fetchXmlBuilder, tree, node);
}

Expand Down Expand Up @@ -56,7 +56,7 @@ protected override ControlValidationResult ValidateControl(Control control)
}
if (fxb.entities != null)
{
if (!allattributes.Any(a=>a.LogicalName == cmbAttribute.Text))
if (!allattributes.Any(a => a.LogicalName == cmbAttribute.Text))
{
return new ControlValidationResult(ControlValidationLevel.Warning, "Attribute", ControlValidationMessage.NotInMetadata);
}
Expand Down
2 changes: 1 addition & 1 deletion FetchXmlBuilder/Controls/conditionControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ enummeta.OptionSet is OptionSetMetadata options &&
var entities = fxb.GetDisplayEntities();
if (entities != null)
{
cmbValue.Items.AddRange(entities.Select(e => new EntityNameItem(e.Value)).ToArray());
cmbValue.Items.AddRange(entities.Select(e => new EntityNameItem(e)).ToArray());
var value = cmbValue.Text;
cmbValue.DropDownStyle = ComboBoxStyle.DropDownList;
cmbValue.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
Expand Down
10 changes: 3 additions & 7 deletions FetchXmlBuilder/Controls/entityControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected override void PopulateControls()
var entities = fxb.GetDisplayEntities();
if (entities != null)
{
cmbEntity.Items.AddRange(entities.Select(e => new EntityItem(e.Value)).ToArray());
cmbEntity.Items.AddRange(entities.Select(e => new EntityItem(e)).ToArray());
}
}

Expand All @@ -40,7 +40,7 @@ protected override ControlValidationResult ValidateControl(Control control)

if (!(cmbEntity.SelectedItem is EntityItem) && fxb.entities != null)
{
if (!fxb.entities.Any(e => e.Key == cmbEntity.Text))
if (!fxb.entities.Any(e => e.LogicalName == cmbEntity.Text))
{
return new ControlValidationResult(ControlValidationLevel.Warning, "Entity", ControlValidationMessage.NotInMetadata);
}
Expand Down Expand Up @@ -69,11 +69,7 @@ public override MetadataBase Metadata()
{
return item.Meta;
}
if (fxb.entities != null && fxb.entities.TryGetValue(cmbEntity.Text, out EntityMetadata meta))
{
return meta;
}
return base.Metadata();
return fxb.GetEntity(cmbEntity.Text) ?? base.Metadata();
}

public override void Focus()
Expand Down
14 changes: 5 additions & 9 deletions FetchXmlBuilder/Controls/linkEntityControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected override void PopulateControls()
{
foreach (var entity in entities)
{
cmbEntity.Items.Add(entity.Value.LogicalName);
cmbEntity.Items.Add(entity.LogicalName);
}
}

Expand Down Expand Up @@ -294,8 +294,8 @@ private void cmbRelationship_DropDownClosed(object sender, EventArgs e)
protected override ControlValidationResult ValidateControl(Control control)
{
var parententityname = Node.Parent.LocalEntityName();
var parententity = fxb.entities != null && fxb.entities.ContainsKey(parententityname) ? fxb.entities[parententityname] : null;
var currententity = fxb.entities != null && fxb.entities.ContainsKey(cmbEntity.Text) ? fxb.entities[cmbEntity.Text] : null;
var parententity = fxb.GetEntity(parententityname);
var currententity = fxb.GetEntity(cmbEntity.Text);
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 Down Expand Up @@ -400,7 +400,7 @@ public override MetadataBase Metadata()
{
var parententity = Node.Parent.Value("name");
if (!string.IsNullOrWhiteSpace(cmbTo.Text) &&
fxb.entities.TryGetValue(parententity, out EntityMetadata pmeta) &&
fxb.GetEntity(parententity) is EntityMetadata pmeta &&
pmeta.Attributes.FirstOrDefault(a => a.LogicalName == cmbTo.Text) is AttributeMetadata pameta)
{
return pameta;
Expand All @@ -419,11 +419,7 @@ private MetadataBase GetEntityMetadata()
{
return item.Meta;
}
if (fxb.entities != null && fxb.entities.TryGetValue(cmbEntity.Text, out EntityMetadata meta))
{
return meta;
}
return null;
return fxb.GetEntity(cmbEntity.Text) ?? base.Metadata();
}

private void cmbEntity_Enter(object sender, EventArgs e)
Expand Down
4 changes: 2 additions & 2 deletions FetchXmlBuilder/Controls/orderControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public orderControl(TreeNode node, AttributeMetadata[] attributes, FetchXmlBuild
InitializeComponent();
friendly = FetchXmlBuilder.friendlyNames;
this.attributes = attributes;
allattributes = fetchXmlBuilder.GetAllAttribues(node.LocalEntityName());
allattributes = fetchXmlBuilder.GetAllAttribues(node.LocalEntityName()).ToArray();
InitializeFXB(null, fetchXmlBuilder, tree, node);
}

Expand Down Expand Up @@ -101,7 +101,7 @@ protected override ControlValidationResult ValidateControl(Control control)
{
return new ControlValidationResult(ControlValidationLevel.Error, "Alias", ControlValidationMessage.IsRequired);
}

if (!cmbAlias.Items.OfType<string>().Any(i => i == cmbAlias.Text))
{
return new ControlValidationResult(ControlValidationLevel.Warning, "Alias", ControlValidationMessage.InValid);
Expand Down
4 changes: 1 addition & 3 deletions FetchXmlBuilder/Controls/valueControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Cinteros.Xrm.FetchXmlBuilder.DockControls;
using Microsoft.Xrm.Sdk.Metadata;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

Expand Down Expand Up @@ -54,9 +53,8 @@ private void RefreshValues()
cmbValue.AutoCompleteMode = AutoCompleteMode.None;

var entities = fxb.GetDisplayEntities();
if (entities != null && entities.ContainsKey(_entityName))
if (entities?.FirstOrDefault(e => e.LogicalName.Equals(_entityName)) is EntityMetadata entity)
{
var entity = entities[_entityName];
var attribute = entity.Attributes.SingleOrDefault(a => a.LogicalName == _attributeName);

if (attribute != null)
Expand Down
6 changes: 4 additions & 2 deletions FetchXmlBuilder/DockControls/TreeBuilderControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ private void DisplayDefinition(XmlDocument fetchDoc)
if (fxb.entities != null)
{
var entitys = TreeNodeHelper.GetEntitysForFetch(fetchDoc);
entitys = entitys?.Where(e => !string.IsNullOrEmpty(e) && fxb.entities.ContainsKey(e) && fxb.entities[e].Attributes == null)?.ToList();
entitys = entitys?.Where(e => !string.IsNullOrEmpty(e) && fxb.GetEntity(e)?.Attributes == null)?.ToList();
entitys?.ForEach(e => fxb.LoadEntityDetails(e, null, false, false));
}
XmlNode definitionXmlNode = fetchDoc.DocumentElement;
Expand Down Expand Up @@ -600,7 +600,7 @@ private void HandleNodeSelection(TreeNode node)
}
break;
}
AttributeMetadata[] attributes = fxb.GetDisplayAttributes(entity);
AttributeMetadata[] attributes = fxb.GetDisplayAttributes(entity).ToArray();
if (node.Name == "attribute")
{
ctrl = new attributeControl(node, attributes, fxb, this);
Expand Down Expand Up @@ -769,9 +769,11 @@ private void SetWarning(TreeNode node)
case ControlValidationLevel.Error:
lblWarning.ImageKey = "error";
break;

case ControlValidationLevel.Warning:
lblWarning.ImageKey = "warning";
break;

case ControlValidationLevel.Info:
lblWarning.ImageKey = "info";
break;
Expand Down
Loading

0 comments on commit 98f6379

Please sign in to comment.