123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- using InABox.Core;
- using System;
- using System.Collections.Generic;
- using System.Collections.Immutable;
- using System.Linq.Expressions;
- namespace Comal.Classes
- {
- public abstract class Dimensions<TLink, TUnit> : EnclosedEntity, IDimensions
- where TLink : DimensionUnitLink<TUnit>, new()
- where TUnit : DimensionUnit, new()
- {
- [EditorSequence(1)]
- [RequiredColumn]
- [Caption("Sizing", IncludePath = false)]
- public abstract TLink Unit { get; set; }
- public IDimensionUnit GetUnit() => Unit;
- [DoubleEditor(Visible = Visible.Hidden)]
- [EditorSequence(2)]
- [Caption("Quantity", IncludePath = false)]
- [RequiredColumn]
- public abstract double Quantity { get; set; }
- [DoubleEditor(Visible = Visible.Hidden)]
- [EditorSequence(3)]
- [Caption("Length", IncludePath = false)]
- [RequiredColumn]
- public abstract double Length { get; set; }
- [DoubleEditor(Visible = Visible.Hidden)]
- [EditorSequence(4)]
- [Caption("Width", IncludePath = false)]
- [RequiredColumn]
- public abstract double Width { get; set; }
- [DoubleEditor(Visible = Visible.Hidden)]
- [EditorSequence(5)]
- [Caption("Height", IncludePath = false)]
- [RequiredColumn]
- public abstract double Height { get; set; }
- [DoubleEditor(Visible = Visible.Hidden)]
- [EditorSequence(6)]
- [Caption("Weight", IncludePath = false)]
- [RequiredColumn]
- public abstract double Weight { get; set; }
- [DoubleEditor(Visible = Visible.Optional, Editable = Editable.Hidden)]
- [Caption("Value", IncludePath = false)]
- [EditorSequence(7)]
- [RequiredColumn]
- public abstract double Value { get; set; }
- [TextBoxEditor(Visible = Visible.Default, Editable = Editable.Hidden)]
- [EditorSequence(8)]
- [Caption("Unit Size", IncludePath = false)]
- [RequiredColumn]
- public abstract String UnitSize { get; set; }
- IDimensionUnit IDimensions.Unit => Unit;
- protected override void Init()
- {
- base.Init();
- Unit.PropertyChanged += (s, e) =>
- {
- if (e.PropertyName == "ID")
- {
- DoPropertyChanged("Unit." + e.PropertyName, OriginalValues.GetValueOrDefault("Unit." + e.PropertyName), Unit.ID);
- }
- else if (e.PropertyName == "Formula")
- {
- DoPropertyChanged("Unit." + e.PropertyName, OriginalValues.GetValueOrDefault("Unit." + e.PropertyName), Unit.Formula);
- }
- else if (e.PropertyName == "Format")
- {
- DoPropertyChanged("Unit." + e.PropertyName, OriginalValues.GetValueOrDefault("Unit." + e.PropertyName), Unit.Format);
- }
- };
- }
- private bool bCalculating = false;
- private static Column<Dimensions<TLink, TUnit>> unitid = new Column<Dimensions<TLink, TUnit>>(x => x.Unit.ID);
- private static Column<Dimensions<TLink, TUnit>> quantity = new Column<Dimensions<TLink, TUnit>>(x => x.Quantity);
- private static Column<Dimensions<TLink, TUnit>> length = new Column<Dimensions<TLink, TUnit>>(x => x.Length);
- private static Column<Dimensions<TLink, TUnit>> width = new Column<Dimensions<TLink, TUnit>>(x => x.Width);
- private static Column<Dimensions<TLink, TUnit>> height = new Column<Dimensions<TLink, TUnit>>(x => x.Height);
- private static Column<Dimensions<TLink, TUnit>> weight = new Column<Dimensions<TLink, TUnit>>(x => x.Weight);
- private static Column<Dimensions<TLink, TUnit>> sizeformula = new Column<Dimensions<TLink, TUnit>>(x => x.Unit.Formula);
- private static Column<Dimensions<TLink, TUnit>> sizeformat = new Column<Dimensions<TLink, TUnit>>(x => x.Unit.Format);
- protected override void DoPropertyChanged(string name, object? before, object? after)
- {
- base.DoPropertyChanged(name, before, after);
- if (bCalculating)
- return;
- bCalculating = true;
- try
- {
- if (unitid.IsEqualTo(name))
- Calculate(Quantity, Length, Width, Height, Weight, Unit.Formula, Unit.Format);
- else if (quantity.IsEqualTo(name))
- Calculate(after, Length, Width, Height, Weight, Unit.Formula, Unit.Format);
- else if (length.IsEqualTo(name))
- Calculate(Quantity, after, Width, Height, Weight, Unit.Formula, Unit.Format);
- else if (width.IsEqualTo(name))
- Calculate(Quantity, Length, after, Height, Weight, Unit.Formula, Unit.Format);
- else if (height.IsEqualTo(name))
- Calculate(Quantity, Length, Width, after, Weight, Unit.Formula, Unit.Format);
- else if (weight.IsEqualTo(name))
- Calculate(Quantity, Length, Width, Height, after, Unit.Formula, Unit.Format);
- else if (sizeformula.IsEqualTo(name))
- Calculate(Quantity, Length, Width, Height, Weight, after as string, Unit.Format);
- else if (sizeformat.IsEqualTo(name))
- Calculate(Quantity, Length, Width, Height, Weight, Unit.Formula, after as string);
- }
- finally
- {
- bCalculating = false;
- }
- }
- public void Set(IDimensionUnit unit, double quantity, double length, double width, double height, double weight)
- {
- bCalculating = true;
- try
- {
- Unit.ID = unit.ID;
- Unit.HasQuantity = unit.HasQuantity;
- Unit.HasLength = unit.HasLength;
- Unit.HasWidth = unit.HasWidth;
- Unit.HasHeight = unit.HasHeight;
- Unit.HasWeight = unit.HasWeight;
- Unit.Code = unit.Code;
- Unit.Description = unit.Description;
- Unit.Formula = unit.Formula;
- Unit.Format = unit.Format;
- Quantity = quantity;
- Length = length;
- Width = width;
- Height = height;
- Weight = weight;
- Calculate(quantity, length, width, height, weight, unit.Formula, unit.Format);
- }
- finally
- {
- bCalculating = false;
- }
- }
- public void CalculateValueAndUnitSize()
- {
- Calculate(Quantity, Length, Width, Height, Weight, Unit.Formula, Unit.Format);
- }
- private void Calculate(object? quantity, object? length, object? width, object? height, object? weight, string? formula, string? format)
- {
- if (Evaluate<double>(formula, quantity, length, width, height, weight, out double value))
- Value = value;
- if (Evaluate<String>(format, quantity, length, width, height, weight, out string unitsize))
- UnitSize = unitsize;
- }
- private bool Evaluate<T>(string? formula, object? quantity, object? length, object? width, object? height, object? weight, out T result)
- {
- if (!String.IsNullOrWhiteSpace(formula))
- {
- var variables = new Dictionary<string, object?>()
- {
- { "Quantity", Convert.ToDouble(quantity) },
- { "Length", Convert.ToDouble(length) },
- { "Width", Convert.ToDouble(width) },
- { "Height", Convert.ToDouble(height) },
- { "Weight", Convert.ToDouble(weight) }
- };
- try
- {
- var expr = new CoreExpression(formula);
- var eval = expr.Evaluate(variables);
- result = (T)CoreUtils.ChangeType(eval, typeof(T));
- return true;
- }
- catch (Exception e)
- {
- Logger.Send(LogType.Information, "", String.Format("Error in Formula: [{0}] ({1})", formula, e.Message));
- result = default(T);
- return false;
- }
- }
- result = default(T);
- return true;
- }
- public void CopyFrom(IDimensions source, bool observing = false)
- {
- if (!observing)
- SetObserving(false);
- Unit.ID = source.GetUnit().ID;
- Unit.Synchronise(source.GetUnit());
- Quantity = source.Quantity;
- Length = source.Length;
- Width = source.Width;
- Height = source.Height;
- Weight = source.Weight;
- Value = source.Value;
- UnitSize = source.UnitSize;
- if (!observing)
- SetObserving(true);
- }
- public override string ToString()
- {
- var result = Value != 0 ? Math.Round(Value, 3).ToString() : "";
- result = !string.IsNullOrWhiteSpace(UnitSize) ? result + " " + UnitSize + ", " : "Empty unitsize";
- result = Quantity != 0 ? result + "Quantity: " + Quantity + ", " : result;
- result = Length != 0 ? result + "Length: " + Length + ", " : result;
- result = Width != 0 ? result + "Width: " + Width + ", " : result;
- result = Height != 0 ? result + "Height: " + Height + ", " : result;
- result = Weight != 0 ? result + "Weight: " + Weight : result;
- if (result.EndsWith(", "))
- result = result.Remove(result.Length - 2);
- return result;
- }
- public override bool Equals(object obj)
- {
- return obj is IDimensions dim
- && Unit.ID == dim.Unit.ID
- && Quantity.IsEffectivelyEqual(dim.Quantity)
- && Length.IsEffectivelyEqual(dim.Length)
- && Width.IsEffectivelyEqual(dim.Width)
- && Height.IsEffectivelyEqual(dim.Height)
- && Weight.IsEffectivelyEqual(dim.Weight);
- }
- public override int GetHashCode()
- {
- return HashCode.Combine(
- Unit.ID,
- Quantity,
- Length,
- Width,
- Height,
- Weight);
- }
- }
- public static class Dimensions
- {
- private static readonly Column<IDimensions> unitid = new Column<IDimensions>(x => x.Unit.ID);
- private static readonly Column<IDimensions> quantity = new Column<IDimensions>(x => x.Quantity);
- private static readonly Column<IDimensions> length = new Column<IDimensions>(x => x.Length);
- private static readonly Column<IDimensions> width = new Column<IDimensions>(x => x.Width);
- private static readonly Column<IDimensions> height = new Column<IDimensions>(x => x.Height);
- private static readonly Column<IDimensions> weight = new Column<IDimensions>(x => x.Weight);
- public static IEnumerable<Column<IDimensions>> GetFilterColumns()
- {
- yield return unitid;
- yield return quantity;
- yield return length;
- yield return width;
- yield return height;
- yield return weight;
- }
- public static Filter<T> DimensionEquals<T>(this Filter<T> filter, IDimensions dim)
- {
- if (!CoreUtils.TryFindMemberExpression(filter.Expression, out var mexp))
- {
- throw new ArgumentException("Filter expression is not a MemberExpression");
- }
- var prop = CoreUtils.GetFullPropertyName(mexp, ".") + ".";
- filter.Expression = CoreUtils.GetMemberExpression(typeof(T), prop + unitid.Property);
- filter.IsEqualTo(dim.Unit.ID);
- filter.And(prop + quantity.Property).IsEqualTo(dim.Quantity);
- filter.And(prop + length.Property).IsEqualTo(dim.Length);
- filter.And(prop + width.Property).IsEqualTo(dim.Width);
- filter.And(prop + height.Property).IsEqualTo(dim.Height);
- filter.And(prop + weight.Property).IsEqualTo(dim.Weight);
- return filter.Parent ?? filter;
- }
- public static IEnumerable<KeyValuePair<Expression<Func<T, object?>>, Expression<Func<U, object?>>>> GetLinks<T, U>()
- where T : IDimensioned
- where U : IDimensioned
- {
- return GetLinks<T, U>(x => x.Dimensions, x => x.Dimensions);
- }
- public static IEnumerable<KeyValuePair<Expression<Func<T, object?>>, Expression<Func<U, object?>>>> GetLinks<T, U>(
- Expression<Func<T, IDimensions>> tDimensions,
- Expression<Func<U, IDimensions>> uDimensions
- )
- {
- var tPrefix = CoreUtils.GetFullPropertyName(tDimensions, ".") + ".";
- var uPrefix = CoreUtils.GetFullPropertyName(uDimensions, ".") + ".";
- yield return new KeyValuePair<Expression<Func<T, object?>>, Expression<Func<U, object?>>>(
- CoreUtils.GetPropertyExpression<T>(tPrefix + unitid.Property), CoreUtils.GetPropertyExpression<U>(uPrefix + unitid.Property));
- yield return new KeyValuePair<Expression<Func<T, object?>>, Expression<Func<U, object?>>>(
- CoreUtils.GetPropertyExpression<T>(tPrefix + quantity.Property), CoreUtils.GetPropertyExpression<U>(uPrefix + quantity.Property));
- yield return new KeyValuePair<Expression<Func<T, object?>>, Expression<Func<U, object?>>>(
- CoreUtils.GetPropertyExpression<T>(tPrefix + length.Property), CoreUtils.GetPropertyExpression<U>(uPrefix + length.Property));
- yield return new KeyValuePair<Expression<Func<T, object?>>, Expression<Func<U, object?>>>(
- CoreUtils.GetPropertyExpression<T>(tPrefix + width.Property), CoreUtils.GetPropertyExpression<U>(uPrefix + width.Property));
- yield return new KeyValuePair<Expression<Func<T, object?>>, Expression<Func<U, object?>>>(
- CoreUtils.GetPropertyExpression<T>(tPrefix + height.Property), CoreUtils.GetPropertyExpression<U>(uPrefix + height.Property));
- yield return new KeyValuePair<Expression<Func<T, object?>>, Expression<Func<U, object?>>>(
- CoreUtils.GetPropertyExpression<T>(tPrefix + weight.Property), CoreUtils.GetPropertyExpression<U>(uPrefix + weight.Property));
- }
- public enum ColumnsType
- {
- Local,
- Data,
- All
- }
- public static int[] GetFilterColumnIndices<T>(Columns<T> columns, Expression<Func<T, IDimensions>> dimensions)
- {
- var dimCol = CoreUtils.GetFullPropertyName(dimensions, ".") + ".";
- return new int[]
- {
- columns.IndexOf(dimCol + unitid.Property),
- columns.IndexOf(dimCol + quantity.Property),
- columns.IndexOf(dimCol + length.Property),
- columns.IndexOf(dimCol + width.Property),
- columns.IndexOf(dimCol + height.Property),
- columns.IndexOf(dimCol + weight.Property)
- };
- }
- public static int[] GetFilterColumnIndices<T>(CoreTable table, Expression<Func<T, IDimensions>> dimensions)
- {
- var dimCol = CoreUtils.GetFullPropertyName(dimensions, ".") + ".";
- return new int[]
- {
- table.GetColumnIndex(dimCol + unitid.Property),
- table.GetColumnIndex(dimCol + quantity.Property),
- table.GetColumnIndex(dimCol + length.Property),
- table.GetColumnIndex(dimCol + width.Property),
- table.GetColumnIndex(dimCol + height.Property),
- table.GetColumnIndex(dimCol + weight.Property)
- };
- }
- public static TDim ToDimensions<TDim>(this CoreRow row, int[] columnIndices)
- where TDim : IDimensions, new()
- {
- var dimensions = new TDim();
- dimensions.Unit.ID = row.Get<Guid>(columnIndices[0]);
- dimensions.Quantity = row.Get<double>(columnIndices[1]);
- dimensions.Length = row.Get<double>(columnIndices[2]);
- dimensions.Width = row.Get<double>(columnIndices[3]);
- dimensions.Height = row.Get<double>(columnIndices[4]);
- dimensions.Weight = row.Get<double>(columnIndices[5]);
- return dimensions;
- }
- public static TDim ToDimensions<T, TDim>(this CoreRow row, Expression<Func<T, TDim>> dim)
- where TDim : IDimensions, new()
- {
- var dimensions = new TDim();
- var dimCol = CoreUtils.GetFullPropertyName(dim, ".") + ".";
- dimensions.Unit.ID = row.Get<Guid>(dimCol + unitid.Property);
- dimensions.Length = row.Get<double>(dimCol + length.Property);
- dimensions.Quantity = row.Get<double>(dimCol + quantity.Property);
- dimensions.Width = row.Get<double>(dimCol + width.Property);
- dimensions.Height = row.Get<double>(dimCol + height.Property);
- dimensions.Weight = row.Get<double>(dimCol + weight.Property);
- return dimensions;
- }
- public static Columns<T> LocalColumns<T>()
- where T : IDimensions
- {
- return Columns.None<T>().Add(x => x.Unit.ID)
- .Add(x => x.Quantity)
- .Add(x => x.Length)
- .Add(x => x.Width)
- .Add(x => x.Height)
- .Add(x => x.Weight)
- .Add(x => x.UnitSize)
- .Add(x => x.Value);
- }
- public static Columns<T> DataColumns<T>()
- where T : IDimensions
- {
- return Columns.None<T>().Add(x => x.Unit.ID)
- .Add(x => x.Unit.ID)
- .Add(x => x.Unit.Format)
- .Add(x => x.Unit.Formula)
- .Add(x => x.Unit.HasHeight)
- .Add(x => x.Unit.HasWeight)
- .Add(x => x.Unit.HasWidth)
- .Add(x => x.Unit.HasQuantity)
- .Add(x => x.Unit.HasLength)
- .Add(x => x.Quantity)
- .Add(x => x.Length)
- .Add(x => x.Width)
- .Add(x => x.Height)
- .Add(x => x.Weight)
- .Add(x => x.UnitSize)
- .Add(x => x.Value);
- }
- public static Columns<T> AllColumns<T>()
- where T : IDimensions
- {
- return Columns.None<T>().Add(x => x.Unit.ID)
- .Add(x => x.Unit.ID)
- .Add(x => x.Unit.Format)
- .Add(x => x.Unit.Formula)
- .Add(x => x.Unit.HasHeight)
- .Add(x => x.Unit.HasWeight)
- .Add(x => x.Unit.HasWidth)
- .Add(x => x.Unit.HasQuantity)
- .Add(x => x.Unit.HasLength)
- .Add(x => x.Unit.Code)
- .Add(x => x.Unit.Description)
- .Add(x => x.Quantity)
- .Add(x => x.Length)
- .Add(x => x.Width)
- .Add(x => x.Height)
- .Add(x => x.Weight)
- .Add(x => x.UnitSize)
- .Add(x => x.Value);
- }
- public static Columns<T> AddDimensionsColumns<T, TDim>(this Columns<T> columns, Expression<Func<T, TDim>> dim, ColumnsType type = ColumnsType.Local)
- where TDim : IDimensions
- {
- return columns.AddSubColumns(dim, type switch
- {
- ColumnsType.Data => DataColumns<TDim>(),
- ColumnsType.All => AllColumns<TDim>(),
- ColumnsType.Local => LocalColumns<TDim>(),
- _ => LocalColumns<TDim>()
- });
- }
- }
- }
|