///
/// 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;
/// Public parameterless constructor
public ItemSpec()
{
}
/// Constructor to specify all items
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);
}
/// Copy constructor
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;
}
/// 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.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 AllItems;
///
/// Static constructor that initializes the list of all items
///
static ItemSpec()
{
AllItems = new List();
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 : ""), 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 : ""), 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 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