123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- using System;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Runtime.InteropServices;
- namespace InABox.Core
- {
- [UserTracking(typeof(User))]
- public class CustomProperty : Entity, IProperty, IPersistent, IRemotable, ISequenceable, ILicense<CoreLicense>
- {
- public Type? _class;
- private BaseEditor? _editor;
- private Func<object, object> _getter;
- private string _name = "";
- private Action<object, object> _setter;
- private string _type;
- private Type type = typeof(object);
- private string _caption;
- private long _sequence;
- private string _page;
- public CustomProperty()
- {
- Visible = true;
- Editable = true;
- Required = false;
- HasEditor = true;
- }
- [MemoEditor(Visible = Core.Visible.Default)]
- [EditorSequence(5)]
- public string Comment { get; set; }
- [CheckBoxEditor(Visible = Core.Visible.Default)]
- [EditorSequence(8)]
- public bool Visible
- {
- get => Editor.Visible == Core.Visible.Hidden ? false : true;
- set => Editor.Visible = value ? Core.Visible.Optional : Core.Visible.Hidden;
- }
- [CheckBoxEditor(Visible = Core.Visible.Optional)]
- [EditorSequence(9)]
- public bool Editable
- {
- get => Editor.Editable == Core.Editable.Enabled;
- set => Editor.Editable = value ? Core.Editable.Enabled : Core.Editable.Hidden;
- }
- [ComboLookupEditor(typeof(PropertyClassLookups))]
- [EditorSequence(1)]
- public string Class
- {
- get => _class != null ? _class.EntityName() : "";
- set
- {
- CoreUtils.TryGetEntity(value, out _class);
- CheckExpressions();
- }
- }
- [EditorSequence(2)]
- public string Name
- {
- get => _name;
- set
- {
- _name = value;
- CheckExpressions();
- }
- }
- [ComboLookupEditor(typeof(PropertyTypeLookups), Visible = Core.Visible.Default)]
- [EditorSequence(3)]
- public string Type
- {
- get => _type;
- set
- {
- _type = value;
- try
- {
- type = CoreUtils.GetEntityOrNull(_type) ?? typeof(object);
- }
- catch
- {
- type = typeof(object);
- }
- _editor = EditorUtils.GetEditor(type);
- IsEntityLink = type.GetInterfaces().Contains(typeof(IEntityLink));
- }
- }
- [DoNotPersist]
- [DoNotSerialize]
- public Type PropertyType
- {
- get => type;
- set
- {
- type = value;
- _type = value.EntityName();
- _editor = EditorUtils.GetEditor(type);
- IsEntityLink = type.GetInterfaces().Contains(typeof(IEntityLink));
- }
- }
- [NullEditor]
- public BaseEditor Editor
- {
- get
- {
- if (_editor == null)
- {
- _editor = EditorUtils.GetEditor(PropertyType);
- if (_editor == null)
- _editor = new NullEditor();
- _editor.Caption = Caption;
- _editor.EditorSequence = (int)Sequence;
- _editor.Page = Page;
- //_editor.Editable = Editable ? Core.Editable.Enabled : Core.Editable.Disabled;
- //_editor.Visible = Visible ? Core.Visible.Optional : Core.Visible.Disabled;
- }
- return _editor;
- }
- set => _editor = value;
- }
- [NullEditor]
- public bool HasEditor { get; set; }
- [TextBoxEditor(Visible = Core.Visible.Optional)]
- [EditorSequence(6)]
- public string Caption
- {
- get => _caption;
- set
- {
- _caption = value;
- if (_editor != null)
- {
- _editor.Caption = _caption;
- }
- }
- }
- [TextBoxEditor(Visible = Core.Visible.Optional)]
- [EditorSequence(7)]
- public string Page
- {
- get => _page;
- set
- {
- _page = value;
- if(_editor != null)
- {
- _editor.Page = _page;
- }
- }
- }
- [CheckBoxEditor(Visible = Core.Visible.Optional)]
- [EditorSequence(8)]
- public bool Required { get; set; }
- [NullEditor]
- [EditorSequence(9)]
- public LoggablePropertyAttribute? Loggable { get; set; }
- [NullEditor]
- public long Sequence
- {
- get => _sequence;
- set
- {
- _sequence = value;
- if (_editor != null)
- {
- _editor.EditorSequence = (int)_sequence;
- }
- }
- }
- public IProperty? Parent { get; set; }
- public bool IsEntityLink { get; set; }
- public Expression Expression()
- {
- if (_class is null)
- throw new Exception("No class for CustomProperty");
- return CoreUtils.CreateIndexExpression(_class, "UserProperties", Name);
- }
- public Func<object, object> Getter()
- {
- return _getter;
- }
- public Action<object, object> Setter()
- {
- return _setter;
- }
- public override string ToString()
- {
- return string.Format("{0}.{1}", Class, Name);
- }
- private void CheckExpressions()
- {
- if (_class == null)
- return;
- if (string.IsNullOrEmpty(Name))
- return;
- try
- {
- _getter = Expressions.Getter(_class, "UserProperties", Name);
- _setter = Expressions.Setter(_class, "UserProperties", Name);
- }
- catch (Exception e)
- {
- Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
- }
- }
- }
- public class CustomPropertyLookups : EntityLookup<CustomProperty>
- {
- public override Columns<CustomProperty> DefineColumns()
- {
- return base.DefineColumns()
- .Add(x => x.Class)
- .Add(x => x.Name);
- }
- //public string FormatLookup(Dictionary<string, object> values, IEnumerable<string> exclude)
- //{
- // return LookupFactory.DefaultFormatLookup(values, exclude.Concat(new String[] { "ID" }));
- //}
- public override Filter<CustomProperty>? DefineFilter()
- {
- return null;
- }
- public override SortOrder<CustomProperty> DefineSortOrder()
- {
- return new SortOrder<CustomProperty>(x => x.Class).ThenBy(x => x.Sequence);
- }
- }
- }
|