|
|
@@ -0,0 +1,258 @@
|
|
|
+using Comal.Classes;
|
|
|
+using Comal.Stores;
|
|
|
+using InABox.Core;
|
|
|
+using InABox.Database;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Data.OleDb;
|
|
|
+using System.Diagnostics.CodeAnalysis;
|
|
|
+using System.Linq;
|
|
|
+using System.Linq.Expressions;
|
|
|
+using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
+
|
|
|
+namespace PRSStores
|
|
|
+{
|
|
|
+ public class RenamedEntityStore<T, TNew> : BaseStore<T>
|
|
|
+ where T : Entity, new()
|
|
|
+ where TNew : Entity, new()
|
|
|
+ {
|
|
|
+ private Dictionary<string, string> PropertyMaps = new();
|
|
|
+
|
|
|
+ protected void AddPropertyMap(string oldProperty, string newProperty)
|
|
|
+ {
|
|
|
+ PropertyMaps.Add(oldProperty, newProperty);
|
|
|
+ }
|
|
|
+ protected void AddPropertyMap<TValue>(Expression<Func<T, TValue>> oldProperty, Expression<Func<TNew, TValue>> newProperty)
|
|
|
+ {
|
|
|
+ PropertyMaps.Add(CoreUtils.GetFullPropertyName(oldProperty, "."), CoreUtils.GetFullPropertyName(newProperty, "."));
|
|
|
+ }
|
|
|
+
|
|
|
+ private string? GetReplacementProperty(IProperty? property)
|
|
|
+ {
|
|
|
+ if(property is null)
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ if (PropertyMaps.TryGetValue(property.Name, out var replacement))
|
|
|
+ {
|
|
|
+ return replacement;
|
|
|
+ }
|
|
|
+ else if (property is StandardProperty stdProp
|
|
|
+ && stdProp.ObsoleteReplacementProperty is string replacementProperty)
|
|
|
+ {
|
|
|
+ return replacementProperty;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ [return: NotNullIfNotNull(nameof(filter))]
|
|
|
+ private Filter<TNew>? ConvertFilter(Filter<T>? filter)
|
|
|
+ {
|
|
|
+ if (filter is null) return null;
|
|
|
+
|
|
|
+ var newFilter = new Filter<TNew>();
|
|
|
+ newFilter.Property = GetReplacementProperty(filter.PropertyDefinition) ?? filter.Property;
|
|
|
+
|
|
|
+ newFilter.Operator = filter.Operator;
|
|
|
+ newFilter.Value = filter.Value;
|
|
|
+ newFilter.IsNot = filter.IsNot;
|
|
|
+ foreach(var and in filter.Ands)
|
|
|
+ {
|
|
|
+ newFilter.And(ConvertFilter(and));
|
|
|
+ }
|
|
|
+ foreach(var or in filter.Ors)
|
|
|
+ {
|
|
|
+ newFilter.Or(ConvertFilter(or));
|
|
|
+ }
|
|
|
+ return newFilter;
|
|
|
+ }
|
|
|
+
|
|
|
+ [return: NotNullIfNotNull(nameof(columns))]
|
|
|
+ private Columns<TNew>? ConvertColumns(Columns<T>? columns, Dictionary<string, string> columnMaps)
|
|
|
+ {
|
|
|
+ if (columns is null) return null;
|
|
|
+
|
|
|
+ var newColumns = Columns.None<TNew>();
|
|
|
+ foreach(var column in columns)
|
|
|
+ {
|
|
|
+ var property = column.Property;
|
|
|
+ if(GetReplacementProperty(column.PropertyDefinition) is string replacement)
|
|
|
+ {
|
|
|
+ columnMaps[replacement] = property;
|
|
|
+ property = replacement;
|
|
|
+ }
|
|
|
+ newColumns.Add(property);
|
|
|
+ }
|
|
|
+ return newColumns;
|
|
|
+ }
|
|
|
+
|
|
|
+ private SortOrder<TNew>? ConvertSortOrder(SortOrder<T>? sortOrder)
|
|
|
+ {
|
|
|
+ if (sortOrder is null) return null;
|
|
|
+
|
|
|
+ if (!CoreUtils.TryFindMemberExpression(sortOrder.Expression, out var mexp)) return null;
|
|
|
+
|
|
|
+ var prop = CoreUtils.GetFullPropertyName(mexp, "/");
|
|
|
+
|
|
|
+ var newSortOrder = new SortOrder<TNew>(prop, sortOrder.Direction);
|
|
|
+ foreach(var then in sortOrder.Thens)
|
|
|
+ {
|
|
|
+ var converted = ConvertSortOrder(then);
|
|
|
+ if(converted is not null)
|
|
|
+ {
|
|
|
+ newSortOrder.Thens.Add(converted);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return newSortOrder;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override CoreTable OnQuery(Filter<T>? filter, Columns<T>? columns, SortOrder<T>? sort, CoreRange? range)
|
|
|
+ {
|
|
|
+ var store = FindSubStore<TNew>();
|
|
|
+ var maps = new Dictionary<string, string>();
|
|
|
+ var table = store.Query(ConvertFilter(filter), ConvertColumns(columns, maps), ConvertSortOrder(sort), range);
|
|
|
+ foreach(var column in table.Columns)
|
|
|
+ {
|
|
|
+ if(maps.TryGetValue(column.ColumnName, out var oldColumn))
|
|
|
+ {
|
|
|
+ column.ColumnName = oldColumn;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return table;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override void OnSave(T entity, ref string auditnote)
|
|
|
+ {
|
|
|
+ var store = FindSubStore<TNew>();
|
|
|
+
|
|
|
+ var props = new Dictionary<string, (IProperty requisitionProperty, IProperty newProperty)>();
|
|
|
+ var newEntity = new TNew();
|
|
|
+ foreach(var (key, originalValue) in entity.OriginalValueList)
|
|
|
+ {
|
|
|
+ if (DatabaseSchema.Property(typeof(T), key) is IProperty requisitionProperty)
|
|
|
+ {
|
|
|
+ var property = requisitionProperty.Name;
|
|
|
+ if(GetReplacementProperty(requisitionProperty) is string replacement)
|
|
|
+ {
|
|
|
+ property = replacement;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(DatabaseSchema.Property(typeof(TNew), property) is IProperty newProperty)
|
|
|
+ {
|
|
|
+ newProperty.Setter()(newEntity, requisitionProperty.Getter()(entity));
|
|
|
+ props.Add(property, (requisitionProperty, newProperty));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ store.Save(newEntity, auditnote);
|
|
|
+
|
|
|
+ foreach (var (key, originalValue) in newEntity.OriginalValueList)
|
|
|
+ {
|
|
|
+ if (props.TryGetValue(key, out var item))
|
|
|
+ {
|
|
|
+ item.requisitionProperty.Setter()(entity, item.newProperty.Getter()(newEntity));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override void OnSave(T[] entities, ref string auditnote)
|
|
|
+ {
|
|
|
+ var store = FindSubStore<TNew>();
|
|
|
+
|
|
|
+ var props = new Dictionary<string, (IProperty requisitionProperty, IProperty newProperty)>();
|
|
|
+ var propMap = new Dictionary<string, string>();
|
|
|
+
|
|
|
+ foreach(var entity in entities)
|
|
|
+ {
|
|
|
+ foreach(var (key, originalValue) in entity.OriginalValueList)
|
|
|
+ {
|
|
|
+ if (!props.ContainsKey(key)
|
|
|
+ && DatabaseSchema.Property(typeof(T), key) is IProperty requisitionProperty)
|
|
|
+ {
|
|
|
+ var property = requisitionProperty.Name;
|
|
|
+ if(GetReplacementProperty(requisitionProperty) is string replacement)
|
|
|
+ {
|
|
|
+ property = replacement;
|
|
|
+ propMap.Add(replacement, key);
|
|
|
+ }
|
|
|
+ if(DatabaseSchema.Property(typeof(TNew), property) is IProperty newProperty)
|
|
|
+ {
|
|
|
+ props.Add(key, (requisitionProperty, newProperty));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var newEntities = entities.ToArray(x =>
|
|
|
+ {
|
|
|
+ var newEntity = new TNew();
|
|
|
+ foreach (var (key, originalValue) in x.OriginalValueList)
|
|
|
+ {
|
|
|
+ if (props.TryGetValue(key, out var item))
|
|
|
+ {
|
|
|
+ item.newProperty.Setter()(newEntity, item.requisitionProperty.Getter()(x));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return newEntity;
|
|
|
+ });
|
|
|
+
|
|
|
+ store.Save(newEntities, auditnote);
|
|
|
+
|
|
|
+ for(int i = 0; i < newEntities.Length; ++i)
|
|
|
+ {
|
|
|
+ var newEntity = newEntities[i];
|
|
|
+ var oldEntity = entities[i];
|
|
|
+ foreach (var (key, originalValue) in newEntity.OriginalValueList)
|
|
|
+ {
|
|
|
+ var originalKey = propMap.GetValueOrDefault(key) ?? key;
|
|
|
+ if (props.TryGetValue(originalKey, out var item))
|
|
|
+ {
|
|
|
+ item.requisitionProperty.Setter()(oldEntity, item.newProperty.Getter()(newEntity));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override void OnDelete(T entity)
|
|
|
+ {
|
|
|
+ var store = FindSubStore<TNew>();
|
|
|
+ store.Delete(new TNew
|
|
|
+ {
|
|
|
+ ID = entity.ID
|
|
|
+ }, "");
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override void OnDelete(IEnumerable<T> entities)
|
|
|
+ {
|
|
|
+ var store = FindSubStore<TNew>();
|
|
|
+ store.Delete(entities.Select(x => new TNew
|
|
|
+ {
|
|
|
+ ID = x.ID
|
|
|
+ }), "");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class RequisitionStore : RenamedEntityStore<Requisition, PickingList>
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ public class RequisitionItemStore : RenamedEntityStore<RequisitionItem, PickingListItem>
|
|
|
+ {
|
|
|
+ public RequisitionItemStore()
|
|
|
+ {
|
|
|
+ foreach(var property in DatabaseSchema.Properties<RequisitionLink>())
|
|
|
+ {
|
|
|
+ AddPropertyMap($"Requisition.{property.Name}", $"PickingList.{property.Name}");
|
|
|
+ AddPropertyMap($"RequisitionLink.{property.Name}", $"PickingList.{property.Name}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public class RequisitionDestinationStore : RenamedEntityStore<RequisitionDestination, PickingListDestination> { }
|
|
|
+ public class RequisitionDocumentStore : RenamedEntityStore<RequisitionDocument, PickingListDocument> { }
|
|
|
+}
|