12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using System.Text.RegularExpressions;
- using Comal.Classes;
- using InABox.Core;
- using InABox.Database;
- namespace PRS.Shared;
- /// <summary>
- /// Trigger the calculation of Digital Form Numbers
- /// </summary>
- /// <returns>true (always)</returns>
- public class Update_7_19 : DatabaseUpdateScript
- {
- public override VersionNumber Version => new VersionNumber(7, 19);
- public override bool Update()
- {
- // Find all classes that derive from EntityForm
- var formtypes = CoreUtils.TypeList(
- new[]
- {
- typeof(Setout).Assembly
- },
- myType =>
- myType is { IsClass: true, IsAbstract: false, IsGenericType: false }
- && myType.IsSubclassOf(typeof(Entity))
- && myType.GetInterfaces().Contains(typeof(IEntityForm))
- ).ToArray();
-
- foreach (var formtype in formtypes)
- {
- var columns = Columns.None(formtype).Add("ID").Add("Number");
- var forms = DbFactory.NewProvider(Logger.Main).Query(formtype, null, columns);
- var nullforms = forms.Rows
- .Where(r => String.IsNullOrWhiteSpace(r.Get<String>("Number")))
- .Select(r=>r.ToObject(formtype))
- .OfType<Entity>()
- .ToArray();
- if (!nullforms.Any())
- continue;
- var maxvalue = forms.Rows.Select(r => Regex.Match(r.Get<String>("Number") ?? "", @"(?<=-)\d+")?.Value ?? "0")
- .MaxBy(x => x) ?? "0";
- int.TryParse(maxvalue, out int iNumber);
- String prefix = null;
- String format = null;
- foreach (var form in nullforms)
- {
- prefix ??= (form as IStringAutoIncrement)?.AutoIncrementPrefix() ?? "";
- format ??= (form as IStringAutoIncrement)?.AutoIncrementFormat() ?? "{0:D8}";
- iNumber++;
- CoreUtils.SetPropertyValue(form, "Number", prefix+String.Format(format, iNumber));
- }
- DbFactory.NewProvider(Logger.Main).Save(formtype, nullforms);
- }
- return true;
- }
-
-
- }
|