Skip to content

Commit

Permalink
Supporting LCG (v0.1)
Browse files Browse the repository at this point in the history
  • Loading branch information
rappen committed Jan 15, 2023
1 parent 6b52aae commit ab5bcb4
Show file tree
Hide file tree
Showing 16 changed files with 803 additions and 44 deletions.
93 changes: 66 additions & 27 deletions FetchXmlBuilder/Converters/CSharpCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ public class CSharpCodeGenerator

public static string GetCSharpQueryExpression(QueryExpression QEx, List<EntityMetadata> entities, FXBSettings settings)
{
if (settings.CodeGenerators.QExFlavor == QExFlavorEnum.LCGconstants)
{
throw new ArgumentOutOfRangeException("Flavor", "LCG is not yet implemented.");
}
if (settings.CodeGenerators.QExStyle == QExStyleEnum.QueryExpression &&
settings.CodeGenerators.QExFlavor == QExFlavorEnum.EarlyBound)
{
Expand Down Expand Up @@ -112,7 +108,7 @@ internal string GetQueryCode()
else
{
querycode += $";{CRLF}";
querycode += string.Join(CRLF, queryproperties.Select(p => p + $";")) + CRLF;
querycode += string.Join(CRLF, queryproperties.Select(p => $"{qename}.{p};")) + CRLF;
querycode += GetColumnsLbL(qex.EntityName, qex.ColumnSet, qename, OwnersType.Root);
querycode += GetFilterLbL(qex.EntityName, qex.Criteria, qename, OwnersType.Root);
querycode += GetOrdersLbL(qex.EntityName, qex.Orders, qename, true);
Expand All @@ -127,12 +123,12 @@ internal string GetQueryCode()
private List<string> GetObjectInitializer(string qename, QueryExpression qex, int indentslevel)
{
return new List<string>
{
GetColumnsOI(qex.EntityName, qex.ColumnSet, OwnersType.Root, indentslevel),
GetFilterOI(qex.EntityName, qex.Criteria, qename, OwnersType.Root, indentslevel),
GetOrdersOI(qex.EntityName, qex.Orders, qename, indentslevel),
GetLinkEntitiesOI(qex.LinkEntities, qename, indentslevel)
}.Where(o => !string.IsNullOrWhiteSpace(o)).ToList();
{
GetColumnsOI(qex.EntityName, qex.ColumnSet, OwnersType.Root, indentslevel),
GetFilterOI(qex.EntityName, qex.Criteria, qename, OwnersType.Root, indentslevel),
GetOrdersOI(qex.EntityName, qex.Orders, qename, indentslevel),
GetLinkEntitiesOI(qex.LinkEntities, qename, indentslevel)
}.Where(o => !string.IsNullOrWhiteSpace(o)).ToList();
}

#endregion General
Expand Down Expand Up @@ -993,18 +989,39 @@ internal string GetCodeEntity(string entityname)
{
if (metas.FirstOrDefault(e => e.LogicalName.Equals(entityname)) is EntityMetadata entity)
{
if (settings.QExFlavor == QExFlavorEnum.EBGconstants)
{
return entity.SchemaName + "." + settings.EBG_EntityLogicalNames;
}
else if (settings.QExFlavor == QExFlavorEnum.EarlyBound)
switch (settings.QExFlavor)
{
return entity.SchemaName;
case QExFlavorEnum.EBGconstants:
return entity.SchemaName + "." + settings.EBG_EntityLogicalNames;

case QExFlavorEnum.LCGconstants:
return GetCodeEntityLCGClassName(entity) + ".EntityName";

case QExFlavorEnum.EarlyBound:
return entity.SchemaName;
}
}
return "\"" + entityname + "\"";
}

private string GetCodeEntityLCGClassName(EntityMetadata entity)
{
var result = entity.LogicalName;
switch (settings.LCG_Settings.ConstantName)
{
case LCG.NameType.DisplayName:
result = LCG.Extensions.StringToCSharpIdentifier(entity.DisplayName?.UserLocalizedLabel?.Label ?? entity.LogicalName);
break;

case LCG.NameType.SchemaName:
result = entity.SchemaName;
break;
}
result = LCG.Extensions.StripPrefix(result, settings.LCG_Settings);
result = LCG.Extensions.CamelCaseIt(result, settings.LCG_Settings);
return result;
}

private string GetCodeEntityPrefix(string entityname)
{
if (metas.FirstOrDefault(e => e.LogicalName.Equals(entityname)) is EntityMetadata entity)
Expand All @@ -1029,6 +1046,28 @@ internal string GetCodeAttribute(string entityname, string attributename, bool a
}
return attribute.SchemaName;

case QExFlavorEnum.LCGconstants:
var entityclassname = GetCodeEntityLCGClassName(entity);
if (attribute.IsPrimaryId == true)
{
return entityclassname + ".PrimaryKey";
}
if (attribute.IsPrimaryName == true)
{
return entityclassname + ".PrimaryName";
}
switch (settings.LCG_Settings.ConstantName)
{
case LCG.NameType.DisplayName:
return entityclassname + "." + LCG.Extensions.StringToCSharpIdentifier(attribute.DisplayName?.UserLocalizedLabel?.Label ?? attribute.LogicalName);

case LCG.NameType.SchemaName:
return entityclassname + "." + LCG.Extensions.CamelCaseIt(LCG.Extensions.StripPrefix(attribute.SchemaName, settings.LCG_Settings), settings.LCG_Settings);

default:
return entityclassname + "." + LCG.Extensions.CamelCaseIt(LCG.Extensions.StripPrefix(attribute.LogicalName, settings.LCG_Settings), settings.LCG_Settings);
}

default:
return entity.SchemaName + "." + settings.EBG_AttributeLogicalNameClass + attribute.SchemaName;
}
Expand Down Expand Up @@ -1111,7 +1150,7 @@ internal static QExStyle[] GetComboBoxItems()
{
Tag = QExStyleEnum.QueryExpressionFactory,
Creator = "Daryl LaBar",
ClassName = "daryllabar/DLaB.Xrm",
ClassName = "DLaB.Xrm",
HelpUrl = "https://github.com/daryllabar/DLaB.Xrm/wiki/Query-Helpers",
},
new QExStyle
Expand Down Expand Up @@ -1165,21 +1204,21 @@ internal static QExFlavor[] GetComboBoxItems()
new QExFlavor
{
Name = "Late Bound EBG constants",
Creator = "Daryl LaBar",
Creator = "Daryl LaBar's tool EBG",
HelpUrl = "https://github.com/daryllabar/DLaB.Xrm.XrmToolBoxTools/wiki/Early-Bound-Generator",
Tag = QExFlavorEnum.EBGconstants
},
//new QExFlavor
//{
// Name = "Late Bound LCG constants",
// Creator = "Jonas Rapp",
// HelpUrl = "https://github.com/rappen/LCG-UDG",
// Tag = QExFlavorEnum.LCGconstants
//},
new QExFlavor
{
Name = "Late Bound LCG constants",
Creator = "Jonas Rapp's tool LCG",
HelpUrl = "https://github.com/rappen/LCG-UDG",
Tag = QExFlavorEnum.LCGconstants
},
new QExFlavor
{
Name = "Early Bound",
Creator = "Microsoft",
Creator = "Microsoft's classes",
HelpUrl = "https://learn.microsoft.com/en-us/power-apps/developer/data-platform/org-service/generate-early-bound-classes",
Tag = QExFlavorEnum.EarlyBound
}
Expand Down
31 changes: 31 additions & 0 deletions FetchXmlBuilder/Converters/LCG/CommonSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Collections.Generic;

namespace Rappen.XTB.LCG
{
public class CommonSettings
{
public CommonSettings()
{
}

internal string InlineConfigBegin = @"/***** LCG-configuration-BEGIN *****\";
internal string InlineConfigEnd = @"\***** LCG-configuration-END *****/";
public string[] CamelCaseWords { get; set; } = new string[] { "parent", "customer", "owner", "state", "status", "name", "phone", "address", "code", "postal", "mail", "modified", "created", "permission", "type", "method", "verson", "number", "first", "last", "middle", "contact", "account", "system", "user", "fullname", "preferred", "processing", "annual", "plugin", "step", "key", "details", "message", "description", "constructor", "execution", "secure", "configuration", "behalf", "count", "percent", "internal", "external", "trace", "entity", "primary", "secondary", "lastused", "credit", "credited", "donot", "exchange", "import", "invoke", "invoked", "private", "market", "marketing", "revenue", "business", "price", "level", "pricelevel", "territory", "version", "conversion", "workorder", "team" };
public string[] CamelCaseWordEnds { get; set; } = new string[] { "id" };
public string[] InternalAttributes { get; set; } = new string[] { "importsequencenumber", "owneridname", "owneridtype", "owneridyominame", "createdbyname", "createdbyyominame", "createdonbehalfby", "createdonbehalfbyname", "createdonbehalfbyyominame", "modifiedbyname", "modifiedbyyominame", "modifiedonbehalfby", "modifiedonbehalfbyname", "modifiedonbehalfbyyominame", "overriddencreatedon", "owningbusinessunit", "owningteam", "owninguser", "regardingobjectidname", "regardingobjectidyominame", "regardingobjecttypecode", "timezoneruleversionnumber", "transactioncurrencyidname", "utcconversiontimezonecode", "versionnumber" };
}
}

/*
FileContainer
FileHeader
DataContainer
EntityContainer
EntityDetails
Attributes
Relationships
OptionSets
OptionSetValues
*/
51 changes: 51 additions & 0 deletions FetchXmlBuilder/Converters/LCG/ConfigurationUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using McTools.Xrm.Connection;
using System.IO;

namespace Rappen.XTB.Helper
{
public static class ConfigurationUtils
{
public static T GetEmbeddedConfiguration<T>(string filename, string begintoken, string endtoken)
{
var csfile = File.ReadAllText(filename);
var configstr = csfile.GetTextBetween(begintoken, endtoken, false);
if (string.IsNullOrEmpty(configstr))
{
throw new FileLoadException("Could not find configuration token in file.", filename);
}
var configname = GetSimpleClassName<T>();
configstr = configstr.GetTextBetween($"<{configname}", $"</{configname}>", true);
if (string.IsNullOrEmpty(configstr))
{
throw new FileLoadException($"Could not find {configname} XML in file.", filename);
}
var inlinesettings = (T)XmlSerializerHelper.Deserialize(configstr, typeof(T));
return inlinesettings;
}

private static string GetSimpleClassName<T>()
{
var configname = typeof(T).ToString();
var confignameparts = configname.Split('.');
configname = confignameparts[confignameparts.Length - 1];
return configname;
}

private static string GetTextBetween(this string text, string begin, string end, bool includebeginend)
{
var beginpos = text.IndexOf(begin);
if (beginpos < 0)
{
return string.Empty;
}
text = text.Substring(beginpos + (includebeginend ? 0 : begin.Length));
var endpos = text.IndexOf(end);
if (endpos < 0)
{
return string.Empty;
}
text = text.Substring(0, endpos + (includebeginend ? end.Length : 0)).Trim();
return text;
}
}
}
Loading

0 comments on commit ab5bcb4

Please sign in to comment.