///
/// Copyright (c) 2016-2017 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using MonitoringDB.Entities;
using MonitoringDB.Resources;
using NHibernate;
using NHibernate.Criterion;
namespace MonitoringDB
{
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;
}
/// 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) => string.IsNullOrEmpty(f) ? ((Workstep)x).Name : string.Format(f, ((Workstep)x).Name), ItemSpecCaps.ReferenceRecord));
AllItems.Add(new ItemSpec(ItemSpecID.Workplace, Strings.Workplace, (x, f, p, w) => string.IsNullOrEmpty(f) ? (string)x : string.Format(f, x), ItemSpecCaps.ReferenceRecord));
AllItems.Add(new ItemSpec(ItemSpecID.UserName, Strings.User_name, (x, f, p, w) => string.IsNullOrEmpty(f) ? (string)x : string.Format(f, x), ItemSpecCaps.ReferenceRecord));
AllItems.Add(new ItemSpec(ItemSpecID.Barcode, Strings.Barcode, (x, f, p, w) => string.IsNullOrEmpty(f) ? (string)x : string.Format(f, x), ItemSpecCaps.ReferenceRecord));
AllItems.Add(new ItemSpec(ItemSpecID.PartBarcode, Strings.Part_barcode, (x, f, p, w) => string.IsNullOrEmpty(f) ? (string)x : string.Format(f, x), 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) => string.IsNullOrEmpty(f) ? ((Workstep)x).Name : string.Format(f, ((Workstep)x).Name), ItemSpecCaps.ReferenceRecord));
AllItems.Add(new ItemSpec(ItemSpecID.Workplace2, Strings.Workplace+"2", (x, f, p, w) => string.IsNullOrEmpty(f) ? (string)x : string.Format(f, x), ItemSpecCaps.ReferenceRecord));
AllItems.Add(new ItemSpec(ItemSpecID.UserName2, Strings.User_name+"2", (x, f, p, w) => string.IsNullOrEmpty(f) ? (string)x : string.Format(f, x), ItemSpecCaps.ReferenceRecord));
AllItems.Add(new ItemSpec(ItemSpecID.Barcode2, Strings.Barcode+"2", (x, f, p, w) => string.IsNullOrEmpty(f) ? (string)x : string.Format(f, x), ItemSpecCaps.ReferenceRecord));
AllItems.Add(new ItemSpec(ItemSpecID.PartBarcode2, Strings.Part_barcode+"2",(x, f, p, w)=> string.IsNullOrEmpty(f) ? (string)x : string.Format(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.Max_pruefindex, (x, f, p, w) => x as string, ItemSpecCaps.ReferenceRecord | ItemSpecCaps.OracleQuery));
AllItems.Add(new ItemSpec(ItemSpecID.Process, Strings.Process, (x, f, p, w) => string.IsNullOrEmpty(f) ? ((Process)x).Name : string.Format(f, ((Process)x).Name), ItemSpecCaps.ReferenceRecord));
}
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.PartBarcode: return Projections.Property(() => record.Code);
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.Process: return Projections.Property(() => refRecord.Process);
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.Process: return Projections.Property(() => refRecord2.Process);
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