/// /// Copyright (c) 2016-2021 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using NHibernate; using NHibernate.Criterion; using SharedDatabase.Entities; using ProductionTracing.Resources; namespace ProductionTracing { public enum ItemSpecID { RefRecordId, POrder, Workflow, Timestamp, Name1, Code1, Name2, Code2, Code3, Code4, PartName, PartCode, Workstep, Workplace, UserName, WstepTstamp, Result, } public enum ItemCaps { None = 0, RefRecord = 1, StepRecord = 2, Record = 4, } public class ItemSpec { /// /// Function delegate to pick a reference record and convert it to a string /// public delegate string PrintDlgt(object x, string format, string precision, int width); /// /// Public fields /// public readonly ItemSpecID Uid; /// Unique Uid used to distinguish items public readonly string Name; /// Name that can be localized to identify the item in a list public readonly PrintDlgt PrintFn; /// Function used to convert the result to a text public string Caption; /// User defined text printed in the column header public string Format; /// Specifies the text generation public string Precision; /// Specifies the text generation public int Width; /// Specifies the text generation readonly ItemCaps itemSpecCaps; /// Public parameterless constructor public ItemSpec() { } /// Constructor to specify all items public ItemSpec(ItemSpecID uid, string name, ItemCaps itemSpecCaps, PrintDlgt printFn) { Uid = uid; Name = name; PrintFn = printFn; this.itemSpecCaps = itemSpecCaps; /// Caption = name; Format = "{0}"; Precision = string.Empty; Width = 0; } public override string ToString() { return string.Format("uid={0}, name={1}, caption={2}", Uid, Name, Caption); } /// Copy constructor public ItemSpec Clone() { ItemSpec newItem = new ItemSpec(Uid, Name, itemSpecCaps, PrintFn); newItem.Caption = Caption; newItem.Format = Format; newItem.Precision = Precision; newItem.Width = Width; return newItem; } /// Safe wrapper which replaces null-s by empty strings public string Print(object x) { string str = PrintFn(x, Format, Precision, Width); return (str == null) ? string.Empty : str; } /// Similar to Print(), in addition embeds text into "" public string Export(object x) { string str = PrintFn(x, Format, Precision, Width); if (str == null) str = string.Empty; if (Uid == ItemSpecID.POrder || (Uid >= ItemSpecID.Name1 && Uid == ItemSpecID.PartCode)) { return string.Format("=\"{0}\"", str); } return str; } /// Static list of all available items public static readonly IList AllItems; /// /// Static constructor that initializes the list of all items /// static ItemSpec() { AllItems = new List(); AllItems.Add(new ItemSpec(ItemSpecID.RefRecordId, Strings.RefRecordId, ItemCaps.RefRecord, (x, f, p, w) => FormatStr(f, ((int)x).ToString()))); AllItems.Add(new ItemSpec(ItemSpecID.POrder, Strings.Order, ItemCaps.RefRecord, (x, f, p, w) => FormatStr(f, x))); AllItems.Add(new ItemSpec(ItemSpecID.Workflow, Strings.Workflow, ItemCaps.RefRecord, (x, f, p, w) => FormatStr(f, x))); AllItems.Add(new ItemSpec(ItemSpecID.Timestamp, Strings.Time_stamp, ItemCaps.RefRecord, (x, f, p, w) => string.IsNullOrEmpty(f) ? ((DateTime)x).ToString() : string.Format(f, (DateTime)x))); AllItems.Add(new ItemSpec(ItemSpecID.Name1, "Name1", ItemCaps.RefRecord, (x, f, p, w) => FormatStr(f, x))); AllItems.Add(new ItemSpec(ItemSpecID.Code1, "Code1", ItemCaps.RefRecord, (x, f, p, w) => FormatStr(f, x))); AllItems.Add(new ItemSpec(ItemSpecID.Name2, "Name2", ItemCaps.RefRecord, (x, f, p, w) => FormatStr(f, x))); AllItems.Add(new ItemSpec(ItemSpecID.Code2, "Code2", ItemCaps.RefRecord, (x, f, p, w) => FormatStr(f, x))); AllItems.Add(new ItemSpec(ItemSpecID.Code3, "Code3", ItemCaps.RefRecord, (x, f, p, w) => FormatStr(f, x))); AllItems.Add(new ItemSpec(ItemSpecID.Code4, "Code4", ItemCaps.RefRecord, (x, f, p, w) => FormatStr(f, x))); //AllItems.Add(new ItemSpec(ItemSpecID.PartName, Strings.Part_name, ItemCaps.RefRecord | ItemCaps.Record, (x, f, p, w) => FormatStr(f, ((Part)x).Name))); //AllItems.Add(new ItemSpec(ItemSpecID.PartCode, Strings.Part_barcode, ItemCaps.RefRecord | ItemCaps.Record, (x, f, p, w) => FormatStr(f, x))); //AllItems.Add(new ItemSpec(ItemSpecID.Workstep, Strings.Workstep, ItemCaps.RefRecord | ItemCaps.StepRecord, (x, f, p, w) => FormatStr(f, x is Workstep ? (x as Workstep).Name : ""))); //AllItems.Add(new ItemSpec(ItemSpecID.Workplace, Strings.Workplace, ItemCaps.RefRecord | ItemCaps.StepRecord, (x, f, p, w) => FormatStr(f, x))); //AllItems.Add(new ItemSpec(ItemSpecID.UserName, Strings.User_name, ItemCaps.RefRecord | ItemCaps.StepRecord, (x, f, p, w) => FormatStr(f, x))); //AllItems.Add(new ItemSpec(ItemSpecID.WstepTstamp, Strings.Workstep + " " + Strings.Time_stamp, // ItemCaps.RefRecord | ItemCaps.StepRecord, (x, f, p, w) => string.IsNullOrEmpty(f) ? ((DateTime)x).ToString() : string.Format(f, (DateTime)x))); //AllItems.Add(new ItemSpec(ItemSpecID.Result, Strings.Result, ItemCaps.RefRecord | ItemCaps.StepRecord, (x, f, p, w) => FormatStr(f, ((int)x).ToString()))); } public static string FormatStr(string format, object value) { int len; if (string.IsNullOrEmpty(format)) { return (string)value; } else if (int.TryParse(format, out len) && len > 0) { if (((string)value).Length >= len) { return ((string)value).Substring(0, len); } else { return (string)value; } } else { return string.Format(format, value); } } public PropertyProjection GetProperty(ReferenceRecord refRecord, StepRecord stepRecord, Record record) { switch (Uid) { default: case ItemSpecID.RefRecordId: return Projections.Property(() => refRecord.Id); case ItemSpecID.POrder: return Projections.Property(() => refRecord.POName); case ItemSpecID.Workflow: return Projections.Property(() => refRecord.Workflow); case ItemSpecID.Timestamp: return Projections.Property(() => refRecord.Timestamp); case ItemSpecID.Name1: return Projections.Property(() => refRecord.Name1); case ItemSpecID.Code1: return Projections.Property(() => refRecord.Code1); case ItemSpecID.Name2: return Projections.Property(() => refRecord.Name2); case ItemSpecID.Code2: return Projections.Property(() => refRecord.Code2); case ItemSpecID.Code3: return Projections.Property(() => refRecord.Code3); case ItemSpecID.Code4: return Projections.Property(() => refRecord.Code4); case ItemSpecID.PartName: return Projections.Property(() => record.Name); case ItemSpecID.PartCode: return Projections.Property(() => record.Code); case ItemSpecID.Workstep: return Projections.Property(() => stepRecord.Workstep); case ItemSpecID.Workplace: return Projections.Property(() => stepRecord.Workplace); case ItemSpecID.UserName: return Projections.Property(() => stepRecord.User); case ItemSpecID.Result: return Projections.Property(() => stepRecord.Result); } } public ItemCaps GetCaps() { return itemSpecCaps; } public static ItemSpec GetItem(ItemSpecID id) { foreach (var item in AllItems) if (item.Uid == id) return item.Clone(); return null; } public static ItemSpec GetItem(string uidName) { foreach (var item in AllItems) if (item.Uid.ToString() == uidName) return item.Clone(); return null; } public static ProjectionList GetProjections(IList items, ReferenceRecord refRecord, StepRecord stepRecord, Record record) { ProjectionList projections = Projections.ProjectionList(); foreach (var item in items) { PropertyProjection projection = item.GetProperty(refRecord, stepRecord, record); if (projection != null) projections.Add(projection); } return projections; } public static IList Expand(IList data, IList items) { if (data.Count == 0) return data; int iSize = data[0].Length; int oSize = items.Count; for (int i = 0; i < data.Count; i++) { object[] inArr = data[i]; object[] outArr = new object[oSize]; int srcIx = 0; for (int dstIx = 0; dstIx < oSize; dstIx++) { outArr[dstIx] = inArr[srcIx++]; } data[i] = outArr; } return data; } public static string[] ToStrArray(IList items) { int count = (items != null) ? items.Count : 0; string[] result = new string[count]; for (int i = 0; i < count; i++) { result[i] = string.Format("{0}~{1}~{2}~{3}~{4}", items[i].Uid, items[i].Caption, items[i].Format, items[i].Precision, items[i].Width); } return result; } public static IList FromStrArray(string[] strArray) { IList result = new List(); if (strArray != null) { for (int i = 0; i < strArray.Length; i++) { string[] field = strArray[i].Split(new char[] { '~' }); try { ItemSpec item = GetItem(field[0]); item.Caption = field[1]; item.Format = field[2]; item.Precision = field[3]; item.Width = int.Parse(field[4]); result.Add(item); } catch { ItemSpec item = GetItem(ItemSpecID.RefRecordId); item.Format = string.Format("Parse error: {0}", strArray[i]); result.Add(item); } } } return result; } } }