tbf/TracingDB/ItemSpec.cs

351 lines
17 KiB
C#
Raw Normal View History

///
/// Copyright (c) 2016-2017 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using TracingDB.Entities;
using TracingDB.Resources;
using NHibernate;
using NHibernate.Criterion;
namespace TracingDB
{
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 ItemSpecCaps itemSpecCaps;
/// <summary> Public parameterless constructor </summary>
public ItemSpec()
{
}
/// <summary> Constructor to specify all items </summary>
public ItemSpec(ItemSpecID uid, string name, PrintDlgt printFn, ItemSpecCaps itemSpecCaps)
{
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);
}
/// <summary> Copy constructor </summary>
public ItemSpec Clone()
{
ItemSpec newItem = new ItemSpec(Uid, Name, PrintFn, itemSpecCaps);
newItem.Caption = Caption;
newItem.Format = Format;
newItem.Precision = Precision;
newItem.Width = Width;
return newItem;
}
/// <summary> Safe wrapper which replaces null-s by empty strings </summary>
public string Print(object x)
{
string str = PrintFn(x, Format, Precision, Width);
return (str == null) ? string.Empty : str;
}
/// <summary> Similar to Print(), in addition embeds text into "" </summary>
public string Export(object x)
{
string str = PrintFn(x, Format, Precision, Width);
if (str == null) str = string.Empty;
if (Uid == ItemSpecID.Barcode || Uid == ItemSpecID.PartBarcode || Uid == ItemSpecID.SerialNr || Uid == ItemSpecID.PurchaseOrder)
{
return string.Format("=\"{0}\"", str);
}
return str;
}
/// Static list of all available items
public static readonly IList<ItemSpec> AllItems;
///
/// Static constructor that initializes the list of all items
///
static ItemSpec()
{
AllItems = new List<ItemSpec>();
AllItems.Add(new ItemSpec(ItemSpecID.RefRecordID, Strings.RefRecordID, (x, f, p, w) => ((int)x).ToString(), ItemSpecCaps.ReferenceRecord));
AllItems.Add(new ItemSpec(ItemSpecID.TimeStamp, Strings.Time_stamp, (x, f, p, w) => string.IsNullOrEmpty(f) ? ((DateTime)x).ToString() : string.Format(f, (DateTime)x), ItemSpecCaps.ReferenceRecord));
AllItems.Add(new ItemSpec(ItemSpecID.Workstep, Strings.Workstep, (x, f, p, w) => FormatStr(f, x is Workstep ? (x as Workstep).Name : "<null>"), ItemSpecCaps.ReferenceRecord));
AllItems.Add(new ItemSpec(ItemSpecID.Workplace, Strings.Workplace, (x, f, p, w) => FormatStr(f, x), ItemSpecCaps.ReferenceRecord));
AllItems.Add(new ItemSpec(ItemSpecID.UserName, Strings.User_name, (x, f, p, w) => FormatStr(f, x), ItemSpecCaps.ReferenceRecord));
AllItems.Add(new ItemSpec(ItemSpecID.Barcode, Strings.Ref_part_barcode, (x, f, p, w) => FormatStr(f, x), ItemSpecCaps.ReferenceRecord));
AllItems.Add(new ItemSpec(ItemSpecID.RefPartName, Strings.Ref_part_name, (x, f, p, w) => FormatStr(f, ((Workstep)x).ReferencePart.Name), ItemSpecCaps.ReferenceRecord));
AllItems.Add(new ItemSpec(ItemSpecID.PartBarcode, Strings.Part_barcode, (x, f, p, w) => FormatStr(f, x), ItemSpecCaps.ReferenceRecord | ItemSpecCaps.AssociatedRecord));
AllItems.Add(new ItemSpec(ItemSpecID.PartName, Strings.Part_name, (x, f, p, w) => FormatStr(f, ((Part)x).Name), ItemSpecCaps.ReferenceRecord | ItemSpecCaps.AssociatedRecord));
AllItems.Add(new ItemSpec(ItemSpecID.TimeStamp2, Strings.Time_stamp+"2", (x, f, p, w) => string.IsNullOrEmpty(f) ? ((DateTime)x).ToString() : string.Format(f, (DateTime)x), ItemSpecCaps.ReferenceRecord));
AllItems.Add(new ItemSpec(ItemSpecID.Workstep2, Strings.Workstep + "2", (x, f, p, w) => FormatStr(f, ((Workstep)x).Name), ItemSpecCaps.ReferenceRecord));
AllItems.Add(new ItemSpec(ItemSpecID.Workplace2, Strings.Workplace + "2", (x, f, p, w) => FormatStr(f, x), ItemSpecCaps.ReferenceRecord));
AllItems.Add(new ItemSpec(ItemSpecID.UserName2, Strings.User_name + "2", (x, f, p, w) => FormatStr(f, x), ItemSpecCaps.ReferenceRecord));
AllItems.Add(new ItemSpec(ItemSpecID.Barcode2, Strings.Barcode + "2", (x, f, p, w) => FormatStr(f, x), ItemSpecCaps.ReferenceRecord));
AllItems.Add(new ItemSpec(ItemSpecID.PartBarcode2, Strings.Part_barcode+"2", (x, f, p, w) => FormatStr(f, x), ItemSpecCaps.ReferenceRecord | ItemSpecCaps.AssociatedRecord));
AllItems.Add(new ItemSpec(ItemSpecID.SerialNr, Strings.Serial_nr, (x, f, p, w) => x as string, ItemSpecCaps.ReferenceRecord | ItemSpecCaps.OracleQuery));
AllItems.Add(new ItemSpec(ItemSpecID.PurchaseOrder, Strings.Purchase_order, (x, f, p, w) => x as string, ItemSpecCaps.ReferenceRecord | ItemSpecCaps.OracleQuery));
AllItems.Add(new ItemSpec(ItemSpecID.TestResult, Strings.Test_result, (x, f, p, w) => x as string, ItemSpecCaps.ReferenceRecord | ItemSpecCaps.OracleQuery));
AllItems.Add(new ItemSpec(ItemSpecID.MaxPruefindex, Strings.Tests_count, (x, f, p, w) => x as string, ItemSpecCaps.ReferenceRecord | ItemSpecCaps.OracleQuery));
AllItems.Add(new ItemSpec(ItemSpecID.MaxTestindexEx,Strings.Tests_count_and_rslt, (x, f, p, w) => x as string, ItemSpecCaps.ReferenceRecord | ItemSpecCaps.OracleQuery));
AllItems.Add(new ItemSpec(ItemSpecID.TestBenchId, Strings.Test_bench_Id, (x, f, p, w) => x as string, ItemSpecCaps.ReferenceRecord | ItemSpecCaps.OracleQuery));
AllItems.Add(new ItemSpec(ItemSpecID.Process, Strings.Workflow, (x, f, p, w) => FormatStr(f, x is Process ? (x as Process).Name : "<null>"), ItemSpecCaps.ReferenceRecord));
AllItems.Add(new ItemSpec(ItemSpecID.Result, Strings.Result, (x, f, p, w) => FormatStr(f, ((int)x).ToString()), ItemSpecCaps.ReferenceRecord));
}
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, Record record)
{
switch (Uid)
{
case ItemSpecID.RefRecordID: return Projections.Property(() => refRecord.Id);
case ItemSpecID.TimeStamp: return Projections.Property(() => refRecord.TimeStamp);
case ItemSpecID.Workstep: return Projections.Property(() => refRecord.Workstep);
case ItemSpecID.Workplace: return Projections.Property(() => refRecord.Workplace);
case ItemSpecID.UserName: return Projections.Property(() => refRecord.UserName);
case ItemSpecID.Barcode: return Projections.Property(() => refRecord.Code);
case ItemSpecID.RefPartName: return Projections.Property(() => refRecord.Workstep);
case ItemSpecID.PartBarcode: return Projections.Property(() => record.Code);
case ItemSpecID.PartName: return Projections.Property(() => record.Part);
case ItemSpecID.TimeStamp2: return null;
case ItemSpecID.Workstep2: return null;
case ItemSpecID.Workplace2: return null;
case ItemSpecID.UserName2: return null;
case ItemSpecID.Barcode2: return null;
case ItemSpecID.PartBarcode2: return null;
case ItemSpecID.SerialNr: return null;
case ItemSpecID.PurchaseOrder: return null;
case ItemSpecID.TestResult: return null;
case ItemSpecID.MaxPruefindex: return null;
case ItemSpecID.MaxTestindexEx: return null;
case ItemSpecID.TestBenchId: return null;
case ItemSpecID.Process: return Projections.Property(() => refRecord.Process);
case ItemSpecID.Result: return Projections.Property(() => refRecord.Result);
default: return null;
}
}
public PropertyProjection GetProperty2(ReferenceRecord refRecord2, Record record2)
{
switch (Uid)
{
case ItemSpecID.RefRecordID: return null;
case ItemSpecID.TimeStamp: return null;
case ItemSpecID.Workstep: return null;
case ItemSpecID.Workplace: return null;
case ItemSpecID.UserName: return null;
case ItemSpecID.Barcode: return null;
case ItemSpecID.PartBarcode: return null;
case ItemSpecID.TimeStamp2: return Projections.Property(() => refRecord2.TimeStamp);
case ItemSpecID.Workstep2: return Projections.Property(() => refRecord2.Workstep);
case ItemSpecID.Workplace2: return Projections.Property(() => refRecord2.Workplace);
case ItemSpecID.UserName2: return Projections.Property(() => refRecord2.UserName);
case ItemSpecID.Barcode2: return Projections.Property(() => refRecord2.Code);
case ItemSpecID.PartBarcode2: return Projections.Property(() => record2.Code);
case ItemSpecID.SerialNr: return null;
case ItemSpecID.PurchaseOrder: return null;
case ItemSpecID.TestResult: return null;
case ItemSpecID.MaxPruefindex: return null;
case ItemSpecID.MaxTestindexEx: return null;
case ItemSpecID.TestBenchId: return null;
case ItemSpecID.Process: return Projections.Property(() => refRecord2.Process);
case ItemSpecID.Result: return Projections.Property(() => refRecord2.Result);
default: return null;
}
}
public ItemSpecCaps 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<ItemSpec> items, ReferenceRecord refRecord, Record record)
{
ProjectionList projections = Projections.ProjectionList();
foreach (var item in items)
{
PropertyProjection projection = item.GetProperty(refRecord, record);
if (projection != null)
projections.Add(projection);
}
return projections;
}
public static IList<object[]> Expand(IList<object[]> data, IList<ItemSpec> 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] = ((items[dstIx].GetCaps() & ItemSpecCaps.OracleQuery) == ItemSpecCaps.OracleQuery) ? string.Empty : inArr[srcIx++];
}
data[i] = outArr;
}
return data;
}
//static string GetPartCode(ReferenceRecord refRecord, string partName)
//{
// if (refRecord != null && refRecord.Records != null)
// {
// foreach (var record in refRecord.Records)
// {
// if (record.Part.Name.Equals(partName))
// {
// return record.Code;
// }
// }
// }
// return string.Empty;
//}
public static string[] ToStrArray(IList<ItemSpec> 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<ItemSpec> FromStrArray(string[] strArray)
{
IList<ItemSpec> result = new List<ItemSpec>();
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;
}
}
}