Skip to content

Commit

Permalink
#3 Adding metadata in Shuffle Builder to make building easier.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Rapp committed May 2, 2018
1 parent e92d38c commit a22d294
Show file tree
Hide file tree
Showing 8 changed files with 413 additions and 98 deletions.
173 changes: 173 additions & 0 deletions XTB/Builder/AppCode/MetadataHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Metadata.Query;
using System.Collections.Generic;
using System.Linq;

namespace Innofactor.Crm.Shuffle.Builder.AppCode
{
public static class MetadataHelper
{
#region Public Fields

public static string[] attributeProperties = {
"AttributeType",
"AttributeTypeName",
//"AutoNumberFormat",
"CalculationOf",
"DateTimeBehavior",
"DefaultFormValue",
"DefaultValue",
"Description",
"DisplayName",
"Format",
"IsCustomAttribute",
"IsCustomizable",
"IsManaged",
"IsPrimaryId",
"IsPrimaryName",
"IsValidForCreate",
"LogicalName",
"MaxLength",
"MaxValue",
"MinValue",
"OptionSet",
"Precision",
"RequiredLevel",
"SchemaName",
"Targets",
};

public static string[] entityDetails = { "Attributes" };

public static string[] entityProperties = {
"Description",
"DisplayName",
"EntityColor",
"IntroducedVersion",
"IsActivity",
"IsActivityParty",
"IsCustomEntity",
"IsCustomizable",
"IsIntersect",
"IsManaged",
"IsPrivate",
"IsValidForAdvancedFind",
"LogicalName",
"ObjectTypeCode",
"OwnershipType",
"PrimaryIdAttribute",
"PrimaryNameAttribute",
"SchemaName"
};

#endregion Public Fields

#region Private Fields

private static readonly Dictionary<string, EntityMetadata> entities = new Dictionary<string, EntityMetadata>();

#endregion Private Fields

#region Public Methods

public static AttributeMetadata GetAttribute(IOrganizationService service, string entity, string attribute, object value)
{
if (value is AliasedValue)
{
var aliasedValue = value as AliasedValue;
entity = aliasedValue.EntityLogicalName;
attribute = aliasedValue.AttributeLogicalName;
}
return GetAttribute(service, entity, attribute);
}

public static AttributeMetadata GetAttribute(IOrganizationService service, string entity, string attribute)
{
GetEntityMetadataFromServer(service, entity);
return GetAttribute(entities, entity, attribute);
}

public static AttributeMetadata GetAttribute(Dictionary<string, EntityMetadata> entities, string entity, string attribute)
{
if (entities == null
|| !entities.TryGetValue(entity, out var metadata)
|| metadata.Attributes == null)
{
return null;
}

return metadata.Attributes.FirstOrDefault(metaattribute => metaattribute.LogicalName == attribute);
}

public static RetrieveMetadataChangesResponse LoadEntities(IOrganizationService service, int majorversion)
{
if (service == null)
{
return null;
}

var eqe = new EntityQueryExpression
{
Properties = new MetadataPropertiesExpression(entityProperties)
};

if (majorversion > 5)
{
eqe.Criteria.Conditions.Add(new MetadataConditionExpression("IsPrivate", MetadataConditionOperator.NotEquals, true));
}
var req = new RetrieveMetadataChangesRequest()
{
Query = eqe,
ClientVersionStamp = null
};
return service.Execute(req) as RetrieveMetadataChangesResponse;
}

public static RetrieveMetadataChangesResponse LoadEntityDetails(IOrganizationService service, string entityName)
{
if (service == null)
{
return null;
}

var eqe = new EntityQueryExpression
{
Properties = new MetadataPropertiesExpression(entityProperties)
};
eqe.Properties.PropertyNames.AddRange(entityDetails);
eqe.Criteria.Conditions.Add(new MetadataConditionExpression("LogicalName", MetadataConditionOperator.Equals, entityName));
var aqe = new AttributeQueryExpression
{
Properties = new MetadataPropertiesExpression(attributeProperties)
};
aqe.Criteria.Conditions.Add(new MetadataConditionExpression("IsLogical", MetadataConditionOperator.NotEquals, true));
eqe.AttributeQuery = aqe;
var req = new RetrieveMetadataChangesRequest
{
Query = eqe,
ClientVersionStamp = null
};
return service.Execute(req) as RetrieveMetadataChangesResponse;
}

private static void GetEntityMetadataFromServer(IOrganizationService service, string entity)
{
if (entities.ContainsKey(entity))
{
return;
}

var response = LoadEntityDetails(service, entity);
if (response?.EntityMetadata != null
&& response.EntityMetadata.Count == 1
&& response.EntityMetadata[0].LogicalName == entity)
{
entities.Add(entity, response.EntityMetadata[0]);
}
}

#endregion Public Methods
}
}
30 changes: 16 additions & 14 deletions XTB/Builder/Controls/DataBlockControl.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 31 additions & 3 deletions XTB/Builder/Controls/DataBlockControl.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
using System;
using Microsoft.Xrm.Sdk.Metadata;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Innofactor.Crm.Shuffle.Builder.Controls
{
public partial class DataBlockControl : ControlBase
{
public DataBlockControl(Dictionary<string, string> collection, ShuffleBuilder shuffleBuilder)
: base(collection, shuffleBuilder) { }
List<EntityMetadata> entities;

public DataBlockControl(Dictionary<string, string> collection, ShuffleBuilder shuffleBuilder, List<EntityMetadata> entities)
: base(collection, shuffleBuilder)
{
this.entities = entities;
}

public override ControlCollection GetControls()
{
Expand All @@ -17,6 +25,26 @@ public override ControlCollection GetControls()
return Controls;
}

public override void PopulateControls()
{
if (shuffleBuilder.Entities == null)
{
shuffleBuilder.LoadEntities(PopulateControls);
return;
}
cmbEntity.Items.Clear();
cmbEntity.Items.AddRange(shuffleBuilder.Entities.OrderBy(e => e.LogicalName).Select(e => e.LogicalName).ToArray());
if (cmbEntity.Items.Count > 0)
{
cmbEntity.DropDownStyle = ComboBoxStyle.DropDown;
}
else
{
cmbEntity.DropDownStyle = ComboBoxStyle.Simple;
}
base.PopulateControls();
}

private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
{
txtIntersect.Enabled = cmbType.Text == "Intersect";
Expand Down
28 changes: 14 additions & 14 deletions XTB/Builder/Controls/ExportAttributeControl.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a22d294

Please sign in to comment.