using System; using System.Collections.Generic; namespace InABox.Core { public abstract class DimensionUnit : Entity, IRemotable, IPersistent, ISequenceable, IDimensionUnit { [UniqueCodeEditor(Visible = Visible.Default, Editable = Editable.Enabled)] [EditorSequence(1)] public virtual String Code { get; set; } [TextBoxEditor] [EditorSequence(2)] public virtual String Description { get; set; } [CheckBoxEditor] [EditorSequence(3)] public virtual bool HasQuantity { get; set; } [CheckBoxEditor] [EditorSequence(4)] public virtual bool HasLength { get; set; } [CheckBoxEditor] [EditorSequence(5)] public virtual bool HasWidth { get; set; } [CheckBoxEditor] [EditorSequence(6)] public virtual bool HasHeight { get; set; } [CheckBoxEditor] [EditorSequence(7)] public virtual bool HasWeight { get; set; } [TextBoxEditor] [EditorSequence(8)] public virtual String Formula { get; set; } [TextBoxEditor] [EditorSequence(9)] public virtual String Format { get; set; } [NullEditor] public long Sequence { get; set; } protected override void Init() { base.Init(); HasQuantity = false; HasLength = false; HasWidth = false; HasHeight = false; HasWeight = false; Formula = "1"; Format = "\"EACH\""; } public bool Validate(List errors) { bool result = true; var variables = new Dictionary() { { "Quantity", 1.00F }, { "Length", 1.00F }, { "Width", 1.00F }, { "Height", 1.00F }, { "Weight", 1.00F } }; try { var expr = new CoreExpression(Formula); expr.Evaluate(variables); result = true; } catch (Exception e) { errors.Add($"{Code}: Formula [{Formula}] => {e.Message}"); result = false; } try { var expr = new CoreExpression(Format); expr.Evaluate(variables); result = true; } catch (Exception e) { errors.Add($"{Code}: Format [{Format}] => {e.Message}"); result = false; } return result; } } }