BaseEntityForm.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Linq.Expressions;
  3. namespace InABox.Core
  4. {
  5. // EntityForm no longer is an IOneToMany, so any subclasses must add this if they want to.
  6. public interface IEntityForm
  7. {
  8. string Description { get; set; }
  9. }
  10. [Caption("Digital Forms")]
  11. public abstract class BaseEntityForm<TParent, TParentLink, TThis> : Entity, IRemotable, IPersistent, ISequenceable, IStringAutoIncrement<TThis>,
  12. IEntityForm,
  13. IDigitalFormInstance<TParentLink>, ILicense<DigitalFormsLicense>
  14. where TThis : BaseEntityForm<TParent,TParentLink, TThis>
  15. where TParent : Entity
  16. where TParentLink : BaseObject, IEntityLink<TParent>, new()
  17. {
  18. public Expression<Func<TThis, string>> AutoIncrementField() => x => x.Number;
  19. public abstract string AutoIncrementPrefix();
  20. public virtual string AutoIncrementFormat() => "{0:D6}";
  21. public Filter<TThis>? AutoIncrementFilter() => null;
  22. public int AutoIncrementDefault() => 1;
  23. [EditorSequence(1)]
  24. [CodeEditor(Editable = Editable.Disabled)]
  25. public String Number { get; set; }
  26. [EditorSequence(2)]
  27. public string Description { get; set; } = "";
  28. public abstract TParentLink Parent { get; }
  29. [EditorSequence(1)]
  30. [EntityRelationship(DeleteAction.Cascade)]
  31. public DigitalFormLink Form => InitializeField(ref _form, nameof(Form));
  32. private DigitalFormLink _form;
  33. [NullEditor]
  34. public string FormData { get; set; }
  35. [NullEditor]
  36. public string? BlobData { get; set; }
  37. [EditorSequence(2)]
  38. [Caption("Started")]
  39. [DateTimeEditor]
  40. public DateTime FormStarted { get; set; } = DateTime.MinValue;
  41. [EditorSequence(3)]
  42. [Caption("Completed")]
  43. [DateTimeEditor]
  44. public DateTime FormCompleted { get; set; }
  45. [EditorSequence(4)]
  46. [Caption("Processed")]
  47. [DateTimeEditor]
  48. public DateTime FormProcessed { get; set; }
  49. [EditorSequence(5)]
  50. [Caption("Cancelled")]
  51. [DateTimeEditor]
  52. public DateTime FormCancelled { get; set; }
  53. [EditorSequence(3)]
  54. [Caption("User")]
  55. public UserLink FormCompletedBy => InitializeField(ref _formCompletedBy, nameof(FormCompletedBy));
  56. private UserLink _formCompletedBy;
  57. [NullEditor]
  58. public Location Location => InitializeField(ref _location, nameof(Location));
  59. private Location _location;
  60. public IDigitalFormDataModel CreateDataModel(DigitalFormVariable[] variables, Entity? parent = null)
  61. {
  62. var t = typeof(DigitalFormDataModel<,,>).MakeGenericType(typeof(TParent), typeof(TParentLink), GetType());
  63. if (parent != null)
  64. return (Activator.CreateInstance(t, parent, this, variables) as IDigitalFormDataModel)!;
  65. return (Activator.CreateInstance(t, Parent.ID, ID, variables) as IDigitalFormDataModel)!;
  66. }
  67. public Guid ParentID()
  68. {
  69. return Parent.ID;
  70. }
  71. public Type ParentType() => typeof(TParent);
  72. [DurationEditor(Visible = Visible.Optional, Editable = Editable.Hidden)]
  73. public TimeSpan FormOpen { get; set; } = TimeSpan.Zero;
  74. [NullEditor]
  75. public long Sequence { get; set; }
  76. }
  77. // The only difference between this "standard" EntityForm and the "MovableEntityForm" below is that the Parent
  78. // here is disabled, effectively locking the digital form to its parent. A MovableEntityForm (currently only used
  79. // for Job Forms) allows the form to be swicthed between different parents.
  80. public abstract class FixedEntityForm<TParent, TParentLink, TThis> : BaseEntityForm<TParent, TParentLink, TThis>
  81. where TThis : BaseEntityForm<TParent,TParentLink, TThis>
  82. where TParent : Entity
  83. where TParentLink : BaseObject, IEntityLink<TParent>, new()
  84. {
  85. [NullEditor]
  86. [EntityRelationship(DeleteAction.Cascade)]
  87. public override TParentLink Parent => InitializeField(ref _tParentLink, nameof(TParentLink));
  88. private TParentLink _tParentLink;
  89. }
  90. public abstract class MovableEntityForm<TParent, TParentLink, TThis> : BaseEntityForm<TParent, TParentLink, TThis>
  91. where TThis : BaseEntityForm<TParent,TParentLink, TThis>
  92. where TParent : Entity
  93. where TParentLink : BaseObject, IEntityLink<TParent>, new()
  94. {
  95. [EntityRelationship(DeleteAction.Cascade)]
  96. public override TParentLink Parent => InitializeField(ref _tParentLink, nameof(TParentLink));
  97. private TParentLink _tParentLink;
  98. }
  99. }