Class EntityDescriptorTemplateExtender
This is the main class for extending the behaviour of the default system EntityDescriptorTemplate automatically created for all entities
This allows you to override the default behaviour to add custom actions to be processed during imports, or to create a custom function, instead of the table data, to return custom fields which can be specified here instead of the actual entity properties.
This is used by ITransaction to create custom columns for every defined ITransactionTypeValueBy implementing this class for any custom type, the system will use this instead of the default template for importing/exporting and querying
Inheritance
Inherited Members
Namespace: LemonEdge.Core.Descriptors
Assembly: API.dll
Syntax
public abstract class EntityDescriptorTemplateExtender
Remarks
For instance the system uses this to provide a custom ability to recalculate period end processes during import routines:
public class PeriodEnd : LemonEdge.Core.Descriptors.EntityDescriptorTemplateExtender
{
private readonly EntityDescriptor _desc;
public PeriodEnd() => _desc = LemonEdge.Core.Descriptors.EntityDescriptorFactory.Instance.GetDescriptor(typeof(API.Entities.IPeriodEnd));
public override EntityDescriptor ForDescriptor => _desc;
public override async Task<IEnumerable<EntityImportColDefinition>> GetImportDefinition(IReadOnlyCache cache)
{
var desc = ForDescriptor;
var headers = new List<EntityImportColDefinition>(desc.GetImportHeaders());
foreach (var prop in desc.Properties)
{
if (prop.PropertyName == nameof(API.Entities.IPeriodEnd.IncludeItemChildTransactionsIDTypeID))
{
var stringProp = prop.Clone();
stringProp.PropertyType = typeof(String);
stringProp.PropertyName = "IncludeItemChildTransactionsType";
stringProp.UserFriendlyName = "Include Item Child Transactions (Type)";
headers.Add(new PropEntityImportColDefinition(false, stringProp));
stringProp = prop.Clone();
stringProp.PropertyType = typeof(String);
headers.Add(new PropEntityImportColDefinition(false, stringProp));
}
else
{
EntityImportColDefinition header = API.Core.BaseEntity.IsBasePropertyName(prop.PropertyName) ?
(EntityImportColDefinition)(prop.PropertyName != nameof(API.Core.IBaseEntity.ID) ? new CorePropEntityImportColDefinition(prop) : null) :
new PropEntityImportColDefinition(desc.TypeID, true, prop);
if (header != null)
{
headers.Add(header);
if (prop.IsEntityLink) header.AddChild(new EntityLinkProperty(prop, header));
if (desc.Relationships.Where(x => x.SourceColumn == prop.PropertyName).FirstOrDefault() is EntityRelationship rel)
EntityDescriptorTemplate.AddPropertyRelationships(header, headers, rel, prop);
}
}
}
headers.AddRange(await EntityDescriptorTemplate.GetImportExternalDataSources(cache));
var ca = new EntityImportActionColDefinition();
ca.SetDefaultValueToEnumOptions<PeriodEndImportAction>();
headers.Add(ca);
return headers;
}
public override IEntityDescriptorImportExtender GetImportExtender() => new PeriodEndImportExtender();
}
public enum PeriodEndImportAction : shor
{
Recalculate
}
public class PeriodEndImportExtender : IEntityDescriptorImportExtender
{
private IBaseEntity _item;
private IReadOnlyCache _cache;
private IEntityUpdater _updater;
private LemonEdge.API.Core.UserInfo _user;
public bool AllowOpen(IBaseEntity item) => true;
public Task Init(IReadOnlyCache cache, LemonEdge.API.Core.UserInfo user, Dictionary<string, Guid> importedContextItemCache)
{
_cache = cache;
_user = user;
return Task.FromResult(1);
}
public Task OnContextChange(IEntityUpdater updater, IBaseEntity item, (bool Loaded, bool SetToNull, bool Ignore, object Value)[] currentValues, Func<string, int> getCurrentValueIndexFromHeaderName)
{
_item = item;
_updater = updater;
return Task.FromResult(1);
}
public short GetImportOrder(EntityImportColDefinition col)
{
switch (col?.ColumnMnemonic)
{
case "IncludeItemChildTransactionsType": return 1;
case nameof(API.Entities.IPeriodEnd.IncludeItemChildTransactionsIDTypeID): return 2;
case EntityImportActionColDefinition.COLUMN_NAME: return 10;
default: return 3;
}
}
public object GetValue(string propName)
{
if (_item == null) return null;
switch (propName)
{
case string s when s == "IncludeItemChildTransactionsType" || s == EntityImportActionColDefinition.COLUMN_NAME:
return "";
default:
return _item.GetType().GetValue(propName, _item);
}
}
public async Task<bool> SetValue(string propName, object propValue)
{
switch (propName)
{
case EntityImportActionColDefinition.COLUMN_NAME:
if (!string.IsNullOrEmpty(propValue?.ToString()) && EnumHelper.Parse<PeriodEndImportAction>(propValue.ToString()) is PeriodEndImportAction action)
{
switch (action)
{
case PeriodEndImportAction.Recalculate:
var processor = (API.Processors.Transactional.IPeriodEndProcessor)_updater.GetProcessor(_item, typeof(API.Processors.Transactional.IPeriodEndProcessor));
processor.AddToDataSetToProcess(_item, EntityOperation.Update);
await processor.Load();
await processor.Calculate();
break;
default: throw new ArgumentOutOfRangeException(action.ToString());
}
}
return false;
case "IncludeItemChildTransactionsType":
var desc2 = EntityDescriptorFactory.Instance.GetDescriptors().FirstOrDefault(x => x.SetName == propValue?.ToString()?.Trim());
((API.Entities.IPeriodEnd)_item).IncludeItemChildTransactionsIDTypeID = desc2?.TypeID;
return false;
default:
_item.GetType().SetValue(propName, _item, propValue);
return false;
}
}
}
Properties
ForDescriptor
The entity that this template extender is for
Declaration
public abstract EntityDescriptor ForDescriptor { get; }
Property Value
Type | Description |
---|---|
EntityDescriptor |
Methods
FunctionCall(Int64)
Returns the function to call for retrieveing this entity data
Declaration
protected virtual string FunctionCall(long accountID)
Parameters
Type | Name | Description |
---|---|---|
System.Int64 | accountID | The account id currently running |
Returns
Type | Description |
---|---|
System.String | The function to call for retrieveing this entity data |
GetImportDefinition(IReadOnlyCache)
Returns a list of all entity import column definitions for this entity to use instead of the default system ones
Declaration
public abstract Task<IEnumerable<EntityImportColDefinition>> GetImportDefinition(IReadOnlyCache cache)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyCache | cache | A local cache |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<EntityImportColDefinition>> | A list of all entity import column definitions for this entity to use instead of the default system ones |
GetImportExtender()
Returns a new instance of a import extender which is used by the import process for actually retrieving and updating the properties values defined in GetImportDefinition(IReadOnlyCache)
Declaration
public abstract IEntityDescriptorImportExtender GetImportExtender()
Returns
Type | Description |
---|---|
IEntityDescriptorImportExtender |
TableName(Int64, Guid, Nullable<DateTimeOffset>, Nullable<Guid>)
The table name or function to use when querying this data from the system
By default this is always the function names for returning the data from the entities
However this can be changed to run custom functionsDeclaration
public virtual string TableName(long accountID, Guid teamID, DateTimeOffset? asOfDate, Guid? canvasID)
Parameters
Type | Name | Description |
---|---|---|
System.Int64 | accountID | The account the system is currently running under |
System.Guid | teamID | The id of the team currently running |
System.Nullable<System.DateTimeOffset> | asOfDate | An as of date this function should be called using |
System.Nullable<System.Guid> | canvasID | The canvas the system is currently running in |
Returns
Type | Description |
---|---|
System.String | Table name or function to use when querying this data from the system |