Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
rappen committed Sep 20, 2020
2 parents 4d84ef9 + b3d4f97 commit 29623e2
Show file tree
Hide file tree
Showing 11 changed files with 740 additions and 555 deletions.
25 changes: 25 additions & 0 deletions FetchXmlBuilder/AppCode/EntityNameItem.cs
@@ -0,0 +1,25 @@
using Cinteros.Xrm.XmlEditorUtils;
using Microsoft.Xrm.Sdk.Metadata;

namespace Cinteros.Xrm.FetchXmlBuilder.AppCode
{
public class EntityNameItem : IComboBoxItem
{
private EntityMetadata meta = null;

public EntityNameItem(EntityMetadata Entity)
{
meta = Entity;
}

public override string ToString()
{
return FetchXmlBuilder.GetEntityDisplayName(meta);
}

public string GetValue()
{
return meta.ObjectTypeCode.Value.ToString();
}
}
}
60 changes: 46 additions & 14 deletions FetchXmlBuilder/AppCode/OData4CodeGenerator.cs
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Cinteros.Xrm.FetchXmlBuilder.AppCode
{
Expand Down Expand Up @@ -257,15 +258,31 @@ private static string GetFilter(FetchEntityType entity, FetchXmlBuilder sender,
var and = true;
foreach (filter filteritem in filteritems)
{
resultList.Append(GetFilter(entity.name, filteritem, sender));
var filterText = GetFilter(entity.name, filteritem, sender);

if (String.IsNullOrWhiteSpace(filterText))
{
continue;
}

if (resultList.Length > 0)
{
resultList.Append(" and ");
}

resultList.Append(filterText);

if (filteritem.type == filterType.or)
and = false;
}

var result = resultList.ToString();
if (result.StartsWith("(") && result.EndsWith(")"))

if (filteritems.Count == 1 && result.StartsWith("(") && result.EndsWith(")"))
{
result = result.Substring(1, result.Length - 2);
}

if (!String.IsNullOrEmpty(expandFilter))
{
if (!and)
Expand Down Expand Up @@ -352,16 +369,31 @@ private static string GetCondition(string entityName, condition condition, Fetch
result += " ne null";
break;
case @operator.like:
result = $"contains({navigationProperty + attrMeta.LogicalName}, '{condition.value}')";
break;
case @operator.notlike:
result = $"not contains({navigationProperty + attrMeta.LogicalName}, '{condition.value}')";
result = $"contains({HttpUtility.UrlEncode(navigationProperty + attrMeta.LogicalName)}, {FormatValue(typeof(string), condition.value)})";

if (condition.@operator == @operator.notlike)
{
result = "not " + result;
}
break;
case @operator.beginswith:
result = $"startswith({navigationProperty + attrMeta.LogicalName}, '{condition.value}')";
case @operator.notbeginwith:
result = $"startswith({HttpUtility.UrlEncode(navigationProperty + attrMeta.LogicalName)}, {FormatValue(typeof(string), condition.value)})";

if (condition.@operator == @operator.notbeginwith)
{
result = "not " + result;
}
break;
case @operator.endswith:
result = $"endswith({navigationProperty + attrMeta.LogicalName}, '{condition.value}')";
case @operator.notendwith:
result = $"endswith({HttpUtility.UrlEncode(navigationProperty + attrMeta.LogicalName)}, {FormatValue(typeof(string), condition.value)})";

if (condition.@operator == @operator.notendwith)
{
result = "not " + result;
}
break;
case @operator.above:
function = "Above";
Expand All @@ -374,7 +406,7 @@ private static string GetCondition(string entityName, condition condition, Fetch
functionParameters = Int32.MaxValue;
break;
case @operator.containvalues:
function = "ContainsValues";
function = "ContainValues";
functionParameters = Int32.MaxValue;
break;
case @operator.notcontainvalues:
Expand Down Expand Up @@ -637,13 +669,13 @@ private static string GetCondition(string entityName, condition condition, Fetch
if (!String.IsNullOrEmpty(function))
{
if (functionParameters == Int32.MaxValue)
return $"Microsoft.Dynamics.CRM.{function}(PropertyName='{navigationProperty + attrMeta.LogicalName}',PropertyValues=[{String.Join(",", condition.Items.Select(i => FormatValue(functionParameterType, i.Value)))}])";
return $"Microsoft.Dynamics.CRM.{HttpUtility.UrlEncode(function)}(PropertyName='{HttpUtility.UrlEncode(navigationProperty + attrMeta.LogicalName)}',PropertyValues=[{String.Join(",", condition.Items.Select(i => FormatValue(functionParameterType, i.Value)))}])";
else if (functionParameters == 0)
return $"Microsoft.Dynamics.CRM.{function}(PropertyName='{navigationProperty + attrMeta.LogicalName}')";
return $"Microsoft.Dynamics.CRM.{HttpUtility.UrlEncode(function)}(PropertyName='{HttpUtility.UrlEncode(navigationProperty + attrMeta.LogicalName)}')";
else if (functionParameters == 1)
return $"Microsoft.Dynamics.CRM.{function}(PropertyName='{navigationProperty + attrMeta.LogicalName}',PropertyValue={FormatValue(functionParameterType, condition.value)})";
return $"Microsoft.Dynamics.CRM.{HttpUtility.UrlEncode(function)}(PropertyName='{HttpUtility.UrlEncode(navigationProperty + attrMeta.LogicalName)}',PropertyValue={FormatValue(functionParameterType, condition.value)})";
else
return $"Microsoft.Dynamics.CRM.{function}(PropertyName='{navigationProperty + attrMeta.LogicalName}',{String.Join(",", condition.Items.Select((i, idx) => $"Property{idx + 1}={FormatValue(functionParameterType, i.Value)}"))})";
return $"Microsoft.Dynamics.CRM.{HttpUtility.UrlEncode(function)}(PropertyName='{HttpUtility.UrlEncode(navigationProperty + attrMeta.LogicalName)}',{String.Join(",", condition.Items.Select((i, idx) => $"Property{idx + 1}={FormatValue(functionParameterType, i.Value)}"))})";
}

if (!string.IsNullOrEmpty(condition.value) && !result.Contains("("))
Expand Down Expand Up @@ -709,7 +741,7 @@ private static string GetPropertyName(AttributeMetadata attr)
private static string FormatValue(Type type, string s)
{
if (type == typeof(string))
return "'" + s.Replace("'", "''") + "'";
return "'" + HttpUtility.UrlEncode(s.Replace("'", "''")) + "'";

if (type == typeof(DateTime))
{
Expand All @@ -727,7 +759,7 @@ private static string FormatValue(Type type, string s)
if (type == typeof(Guid))
return Guid.Parse(s).ToString();

return Convert.ChangeType(s, type).ToString();
return HttpUtility.UrlEncode(Convert.ChangeType(s, type).ToString());
}

private static string GetOrder(FetchEntityType entity, FetchXmlBuilder sender)
Expand Down
3 changes: 2 additions & 1 deletion FetchXmlBuilder/AppCode/OperatorItem.cs
Expand Up @@ -315,7 +315,8 @@ public static OperatorItem[] GetConditionsByAttributeType(AttributeTypeCode valu
valueType != AttributeTypeCode.Lookup &&
valueType != AttributeTypeCode.Customer &&
valueType != AttributeTypeCode.Owner &&
valueType != AttributeTypeCode.Uniqueidentifier)
valueType != AttributeTypeCode.Uniqueidentifier &&
valueType != AttributeTypeCode.EntityName)
{
validConditionsList.Add(new OperatorItem(ConditionOperator.BeginsWith));
validConditionsList.Add(new OperatorItem(ConditionOperator.DoesNotBeginWith));
Expand Down
1 change: 1 addition & 0 deletions FetchXmlBuilder/AppCode/TreeNodeHelper.cs
Expand Up @@ -181,6 +181,7 @@ public static void SetNodeText(TreeNode node, FetchXmlBuilder fxb)
{
var parent = GetAttributeFromNode(node.Parent.Parent, "name");
attr = fxb.GetAttributeDisplayName(parent, attr);
valueOf = fxb.GetAttributeDisplayName(parent, valueOf);
}
if (!string.IsNullOrEmpty(ent))
{
Expand Down
7 changes: 6 additions & 1 deletion FetchXmlBuilder/Controls/FetchXmlElementControlBase.cs
Expand Up @@ -151,10 +151,15 @@ protected virtual void SaveInternal(bool keyPress)
{
if (IsInitialized)
{
SendSaveMessage(ControlUtils.GetAttributesCollection(this.Controls, true), keyPress);
SendSaveMessage(GetAttributesCollection(), keyPress);
}
}

protected virtual Dictionary<string, string> GetAttributesCollection()
{
return ControlUtils.GetAttributesCollection(this.Controls, true);
}

protected virtual ControlValidationResult ValidateControl(Control control)
{
return null;
Expand Down

0 comments on commit 29623e2

Please sign in to comment.