Bläddra i källkod

Improved StringAutoIncrement Handling for Entities

frogsoftware 2 år sedan
förälder
incheckning
2578703dda

+ 7 - 7
InABox.Core/DigitalForms/DFUtils.cs

@@ -15,7 +15,7 @@ namespace InABox.Core
     }
 
     public abstract class EntityFormUtils<TForm, TEntity, TEntityLink> : IEntityFormUtils
-        where TForm : EntityForm<TEntity, TEntityLink>
+        where TForm : EntityForm<TEntity, TEntityLink, TForm>
         where TEntity : Entity, new()
         where TEntityLink : EntityLink<TEntity>, new()
     {
@@ -31,7 +31,7 @@ namespace InABox.Core
     }
 
     public class DelegateEntityFormUtils<TForm, TEntity, TEntityLink> : EntityFormUtils<TForm, TEntity, TEntityLink>
-        where TForm : EntityForm<TEntity, TEntityLink>
+        where TForm : EntityForm<TEntity, TEntityLink, TForm>
         where TEntity : Entity, new()
         where TEntityLink : EntityLink<TEntity>, new()
     {
@@ -118,7 +118,7 @@ namespace InABox.Core
             DelegateEntityFormUtils<TForm, TEntity, TEntityLink>.CanEditEvent editFormFunc,
             DelegateEntityFormUtils<TForm, TEntity, TEntityLink>.NewEntityEvent? newEntityFunc = null,
             DelegateEntityFormUtils<TForm, TEntity, TEntityLink>.OnSaveEvent? beforeSaveFunc = null)
-            where TForm : EntityForm<TEntity, TEntityLink>
+            where TForm : EntityForm<TEntity, TEntityLink, TForm>
             where TEntity : Entity, new()
             where TEntityLink : EntityLink<TEntity>, new()
         {
@@ -126,7 +126,7 @@ namespace InABox.Core
         }
         
         public static void AddFormUtils<TForm, TEntity, TEntityLink>(EntityFormUtils<TForm, TEntity, TEntityLink> formUtils)
-            where TForm : EntityForm<TEntity, TEntityLink>
+            where TForm : EntityForm<TEntity, TEntityLink, TForm>
             where TEntity : Entity, new()
             where TEntityLink : EntityLink<TEntity>, new()
         {
@@ -143,7 +143,7 @@ namespace InABox.Core
         }
         
         public static bool CanEditForm<TForm, TEntity, TEntityLink>(TForm Form, TEntity Entity)
-            where TForm : EntityForm<TEntity, TEntityLink>
+            where TForm : EntityForm<TEntity, TEntityLink, TForm>
             where TEntity : Entity, new()
             where TEntityLink : EntityLink<TEntity>, new()
         {
@@ -160,7 +160,7 @@ namespace InABox.Core
         }
 
         public static TEntity NewEntity<TForm, TEntity, TEntityLink>(DigitalForm form)
-            where TForm : EntityForm<TEntity, TEntityLink>
+            where TForm : EntityForm<TEntity, TEntityLink, TForm>
             where TEntity : Entity, new()
             where TEntityLink : EntityLink<TEntity>, new()
         {
@@ -176,7 +176,7 @@ namespace InABox.Core
         }
         
         public static void OnSave<TForm, TEntity, TEntityLink>(TForm form, TEntity entity)
-            where TForm : EntityForm<TEntity, TEntityLink>
+            where TForm : EntityForm<TEntity, TEntityLink, TForm>
             where TEntity : Entity, new()
             where TEntityLink : EntityLink<TEntity>, new()
         {

+ 3 - 1
InABox.Core/DigitalForms/Forms/DigitalFormLayout.cs

@@ -45,9 +45,11 @@ namespace InABox.Core
             return new Filter<DigitalFormLayout>(x => x.Form.ID).IsEqualTo(Form.ID);
         }
 
+        public string? AutoIncrementPrefix() => "L";
+
         public string AutoIncrementFormat()
         {
-            return "L{0:D3}";
+            return "{0:D3}";
         }
 
         public IEntityDuplicator GetDuplicator()

+ 3 - 1
InABox.Core/DigitalForms/Forms/DigitalFormVariable.cs

@@ -108,9 +108,11 @@ namespace InABox.Core
             return new Filter<DigitalFormVariable>(x => x.Form.ID).IsEqualTo(Form.ID);
         }
 
+        public string? AutoIncrementPrefix() => "V";
+
         public string AutoIncrementFormat()
         {
-            return "V{0:D3}";
+            return "{0:D3}";
         }
 
         public object? ParseValue(object? value)

+ 7 - 4
InABox.Core/DigitalForms/Forms/EntityForm.cs

@@ -11,18 +11,21 @@ namespace InABox.Core
     }
 
     [Caption("Digital Forms")]
-    public abstract class EntityForm<TParent, TParentLink> : Entity, IRemotable, IPersistent, ISequenceable, IStringAutoIncrement<EntityForm<TParent,TParentLink>>,
+    public abstract class EntityForm<TParent, TParentLink, TThis> : Entity, IRemotable, IPersistent, ISequenceable, IStringAutoIncrement<TThis>,
         IEntityForm,
         IDigitalFormInstance<TParentLink>, ILicense<DigitalFormsLicense>
+        where TThis : EntityForm<TParent,TParentLink, TThis>
         where TParent : Entity
         where TParentLink : IEntityLink<TParent>, new()
     {
 
-        public Expression<Func<EntityForm<TParent, TParentLink>, String>> AutoIncrementField() => x => x.Number;
+        public Expression<Func<TThis, String>> AutoIncrementField() => x => x.Number;
 
-        public abstract string AutoIncrementFormat();
+        public abstract String AutoIncrementPrefix();
+
+        public virtual string AutoIncrementFormat() => "{0:D6}";
         
-        public Filter<EntityForm<TParent, TParentLink>> AutoIncrementFilter() => null;
+        public Filter<TThis> AutoIncrementFilter() => null;
         
         [EditorSequence(1)]
         [CodeEditor(Editable = Editable.Disabled)]

+ 2 - 1
InABox.Core/Entity.cs

@@ -347,7 +347,8 @@ namespace InABox.Core
 
     public interface IStringAutoIncrement
     {
-        string AutoIncrementFormat();
+        string AutoIncrementPrefix();
+        string AutoIncrementFormat();        
     }
     
     public interface IStringAutoIncrement<T> : IAutoIncrement<T, string>, IStringAutoIncrement

+ 13 - 17
InABox.Database/Stores/Store.cs

@@ -409,16 +409,18 @@ namespace InABox.Database
             ProcessStringAutoIncrement(entities);
         }
 
-        public static string AutoIncrementPrefix { get; set; }
+        //public static string AutoIncrementPrefix { get; set; }
         
         private bool ProcessStringAutoIncrement(params T[] entities)
         {
             if (!entities.Any())
                 return false;
 
-            var autoinc = entities.First() as IStringAutoIncrement<T>;
-            if (autoinc != null)
+            if (entities.First() is IStringAutoIncrement<T> autoinc)
             {
+
+                var prefix = autoinc.AutoIncrementPrefix() ?? "";
+                
                 var prop = CoreUtils.GetPropertyFromExpression<T, string>(autoinc.AutoIncrementField());
                 var bRequired = false;
                 foreach (var entity in entities)
@@ -426,19 +428,13 @@ namespace InABox.Database
                 if (bRequired)
                 {
                     
-                    var filter = new Filter<T>(prop.Name).IsGreaterThanOrEqualTo(String.Format("{0}0", AutoIncrementPrefix))
-                        .And(prop.Name).IsLessThanOrEqualTo(String.Format("{0}9999999999", AutoIncrementPrefix));
+                    var filter = new Filter<T>(prop.Name).IsGreaterThanOrEqualTo(String.Format("{0}0",prefix))
+                        .And(prop.Name).IsLessThanOrEqualTo(String.Format("{0}9999999999", prefix));
                     
                     var filter2 = autoinc.AutoIncrementFilter();
                     if (filter2 != null)
                         filter = filter.And(filter2);
-
-                    // if (!string.IsNullOrWhiteSpace(AutoIncrementPrefix))
-                    // {
-                    //     var prefixfilter = new Filter<T>(prop.Name).BeginsWith(AutoIncrementPrefix);
-                    //     filter = filter == null ? prefixfilter : filter.And(prefixfilter);
-                    // }
-
+                    
                     var newvalue = 0;
                     var row = Provider.Query(
                         filter,
@@ -449,16 +445,17 @@ namespace InABox.Database
                     if (row != null)
                     {
                         var id = row.Get<string>(prop.Name);
-                        if (!string.IsNullOrWhiteSpace(AutoIncrementPrefix))
-                            id = id.Substring(AutoIncrementPrefix.Length);
+                        if (!string.IsNullOrWhiteSpace(prefix))
+                            id = id.Substring(prefix.Length);
                         id = new string(id.Where(c => char.IsDigit(c)).ToArray());
                         int.TryParse(id, out newvalue);
                     }
+                    
                     foreach (var entity in entities)
                         if (string.IsNullOrWhiteSpace(prop.GetValue(entity) as string))
                         {
                             newvalue++;
-                            prop.SetValue(entity, AutoIncrementPrefix + string.Format(autoinc.AutoIncrementFormat(), newvalue));
+                            prop.SetValue(entity, prefix + string.Format(autoinc.AutoIncrementFormat(), newvalue));
                         }
 
                     return true;
@@ -472,8 +469,7 @@ namespace InABox.Database
         {
             if (!entities.Any())
                 return false;
-            var autoinc = entities.First() as INumericAutoIncrement<T>;
-            if (autoinc != null)
+            if (entities.First() is INumericAutoIncrement<T> autoinc)
             {
                 var prop = CoreUtils.GetPropertyFromExpression(autoinc.AutoIncrementField());
                 

+ 3 - 3
inabox.wpf/DigitalForms/DigitalFormUtils.cs

@@ -513,11 +513,11 @@ namespace InABox.DynamicGrid
         public static DataModel? GetDataModel(String appliesto, IEnumerable<DigitalFormVariable> variables)
         {
             _entityForms ??= CoreUtils.Entities
-                .Where(x => x.IsSubclassOfRawGeneric(typeof(EntityForm<,>)))
+                .Where(x => x.IsSubclassOfRawGeneric(typeof(EntityForm<,,>)))
                 .ToList();
             var entityForm = _entityForms
-                .Where(x => x.GetSuperclassDefinition(typeof(EntityForm<,>))?.GenericTypeArguments[0].Name == appliesto)
-                .FirstOrDefault();
+                .FirstOrDefault(x => x.GetSuperclassDefinition(typeof(EntityForm<,,>))
+                    ?.GenericTypeArguments[0].Name == appliesto);
             if(entityForm is not null)
             {
                 var model = (Activator.CreateInstance(typeof(DigitalFormReportDataModel<>).MakeGenericType(entityForm), Filter.Create(entityForm).None(), null) as DataModel)!;

+ 1 - 1
inabox.wpf/DynamicGrid/DynamicEntityFormGrid.cs

@@ -11,7 +11,7 @@ using System.Windows.Media.Imaging;
 namespace InABox.DynamicGrid
 {
     public class DynamicEntityFormGrid<TForm, TEntity, TEntityLink> : DynamicDataGrid<TForm>
-        where TForm : EntityForm<TEntity, TEntityLink>, new()
+        where TForm : EntityForm<TEntity, TEntityLink, TForm>, new()
         where TEntity : Entity
         where TEntityLink : IEntityLink<TEntity>, new()
     {

+ 1 - 1
inabox.wpf/DynamicGrid/DynamicGridUtils.cs

@@ -506,7 +506,7 @@ namespace InABox.DynamicGrid
             Func<TEntity> loadEntity,
             bool editOnAdd = false
             )
-            where TEntityForm : EntityForm<TEntity, TEntityLink>, new()
+            where TEntityForm : EntityForm<TEntity, TEntityLink, TEntityForm>, new()
             where TEntity : Entity
             where TEntityLink : IEntityLink<TEntity>, new()
         {