Search Results for

    Show / Hide Table of Contents

    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 API.Entities.ITransaction to create custom columns for every defined API.Entities.ITransactionTypeValue

    By implementing this class for any custom type, the system will use this instead of the default template for importing/exporting and querying

    Inheritance
    object
    EntityDescriptorTemplateExtender
    DocumentAttachmentsDescriptorTemplateExtender
    DocumentDescriptorTemplateExtender
    DocumentOwnerDescriptorTemplateExtender
    User
    GLAccountExtender
    PeriodEndExtender
    TransactionAllocatedExtender
    TransactionAssociationExtender
    TransactionCode
    TransactionExtender
    Inherited Members
    object.GetType()
    object.MemberwiseClone()
    object.ToString()
    object.Equals(object)
    object.Equals(object, object)
    object.ReferenceEquals(object, object)
    object.GetHashCode()
    Namespace: LemonEdge.API.Descriptors
    Assembly: LemonEdge.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;
        private readonly IEntityDescriptorFactory _entityDescriptorFactory;
    
        public PeriodEnd(IEntityDescriptorFactory entityDescriptorFactory)
        {
            _entityDescriptorFactory = entityDescriptorFactory;
            _desc = entityDescriptorFactory.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 : short
    {
        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;
            }
        }
    }

    Constructors

    EntityDescriptorTemplateExtender()

    Declaration
    protected EntityDescriptorTemplateExtender()

    Properties

    ForDescriptor

    The entity that this template extender is for

    Declaration
    public abstract EntityDescriptor ForDescriptor { get; }
    Property Value
    Type Description
    EntityDescriptor

    Methods

    FunctionCall(long)

    Returns the function to call for retrieving this entity data

    Declaration
    protected virtual string FunctionCall(long accountID)
    Parameters
    Type Name Description
    long accountID

    The account id currently running

    Returns
    Type Description
    string

    The function to call for retrieving 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
    Task<IEnumerable<EntityImportColDefinition>>

    A list of all entity import column definitions for this entity to use instead of the default system ones

    GetImportExtender(params object[])

    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(params object[] args)
    Parameters
    Type Name Description
    object[] args
    Returns
    Type Description
    IEntityDescriptorImportExtender

    TableName(long, Guid, DateTimeOffset?, Guid?, 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 functions
    Declaration
    public virtual string TableName(long accountID, Guid teamID, DateTimeOffset? asOfDate, Guid? canvasID, Guid? userID)
    Parameters
    Type Name Description
    long accountID

    The account the system is currently running under

    Guid teamID

    The id of the team currently running

    DateTimeOffset? asOfDate

    An as of date this function should be called using

    Guid? canvasID

    The canvas the system is currently running in

    Guid? userID
    Returns
    Type Description
    string

    Table name or function to use when querying this data from the system

    Extension Methods

    LinqExtensions.AsArray<T>(T)
    LinqExtensions.ToArrayOfOne<T>(T)
    LinqExtensions.ToListOfOne<T>(T)
    MiscExtensions.SetIfNotEqual<T, TP>(T, Expression<Func<T, TP>>, TP)
    WeakReferenceExtensions.WeakReference(object)
    SQLExtensions.ToSQLValue(object, bool)
    ReflectionExtensions.ClearEventInvocations(object, string)
    StringExtensions.ToCSVFormatString(object, Type)
    In this article
    Back to top © LemonEdge Technologies. All rights reserved.