using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using DTS.Common.Interface.GroupTemplate;
using DTS.Common.Storage;
using DTS.Common.Utilities.Logging;
// ReSharper disable CheckNamespace
namespace DTS.Common.ISO
{
///
/// this class is a wrapper for the group template per the db, it's supposed to be a lighter weight version of concept of a test object template, with no
/// connection to UI, just serialization and structure
///
public class TestObjectTemplate : ITestObjectTemplate
{
#region Properties
///
/// name of the test object template, this could be a GUID in the case of embedded test object templates
///
public string TemplateName { get; set; }
///
/// a human readable name for the template, for an embedded template this is the original template name, for
/// a non embedded template, this is the template name (embedded templates have guids for names)
///
public string TemplateNameOrOriginalTemplateName => Embedded ? OriginalTemplateName : TemplateName;
///
/// the icon for the template
///
public string Icon { get; set; }
///
/// description for the template
///
public string Description { get; set; }
///
/// whether this template is intended to only be used locally or not
///
public bool LocalOnly { get; set; }
///
/// the version number of this template [not currently used?]
///
public int Version { get; set; }
///
/// last person to modify this template
///
public string LastModifiedBy { get; set; }
///
/// when this template was last modified
///
public DateTime LastModified { get; set; }
///
/// a CRC32 for the template, but not currently used
/// original idea was to allow us to not have to check changes in the template, just quickly calculate whether anything has changed
///
public int CRC32 { get; set; }
///
/// test object (iso meta field) for this template
///
public string TestObject { get; set; }
///
/// test object type (iso meta field) for this template, all channels are of this type ...
///
public string TestObjectType { get; set; }
///
/// unsure if this is still used, was originally used to build up templates from sub templates,
/// so an ATD could be composed of leg, arm, head, etc
///
public string TemplateParent { get; set; }
///
/// unsure, I think this is whether the group is dynamically added or an existing
///
public bool SysBuilt { get; set; }
///
/// all channels for the template
///
public TestObjectTemplateChannel[] Channels { get; set; }
///
/// the original template name [if we are embedded we got a new name that was a guid, but we store the old name here for readability purposes]
///
public string OriginalTemplateName { get; set; }
///
/// whether this group is embedded in a test setup, or is a user created and living on it's own template
///
public bool Embedded { get; set; }
#endregion
#region Constructors and initializers
public bool IsISOMode()
{
return !(TestObjectType.Contains(Classes.GroupTemplates.Constants.NON_ISO_TESTOBJECT_CHANNEL_TYPE) ||
TestObjectType.Contains(TestObjectTemplateChannel.NONISOCHANNELTYPE));
}
public TestObjectTemplate(string templateName, bool bLocalOnly)
{
Version = 1;
TemplateName = templateName;
LocalOnly = bLocalOnly;
Channels = new TestObjectTemplateChannel[0];
}
public TestObjectTemplate(TestObjectTemplate copy, ref ISO13499FileDb db)
{
if (copy?.TemplateName != null)
{
TemplateName = copy.TemplateName;
}
if (copy == null) return;
LocalOnly = copy.LocalOnly;
CRC32 = copy.CRC32;
Description = copy.Description;
Embedded = copy.Embedded;
OriginalTemplateName = copy.OriginalTemplateName;
Icon = copy.Icon;
LastModified = copy.LastModified;
LastModifiedBy = copy.LastModifiedBy;
LocalOnly = copy.LocalOnly;
SysBuilt = copy.SysBuilt;
TemplateParent = copy.TemplateParent;
TestObject = copy.TestObject;
TestObjectType = copy.TestObjectType;
Version = copy.Version;
var lookup = new Dictionary();
var channels = new List();
foreach (var c in copy.Channels)
{
var ch = new TestObjectTemplateChannel(c, this);
ch.SetTemplate(this);
channels.Add(ch);
lookup[$"{c.Channel.Id}x{c.Channel.MMEChannelType}"] = true;
}
var possibleChannels = db.GetPossibleChannelsForType(TestObjectType);
foreach (var pc in possibleChannels)
{
var key = $"{pc.Id}x{pc.MMEChannelType}";
if (lookup.ContainsKey(key)) continue;
lookup[key] = true;
var ch = new TestObjectTemplateChannel(pc);
ch.SetTemplate(this);
channels.Insert(0, ch);
}
AssignOrders(channels);
Channels = channels.ToArray();
}
private void AssignOrders(List channels)
{
if (!channels.Any()) { return; }
channels.Sort();
var maxDisplayOrder = (from ch in channels select ch.DisplayOrder).Max();
foreach (var ch in channels)
{
if (-1 == ch.DisplayOrder)
{
ch.DisplayOrder = ++maxDisplayOrder;
}
}
}
#endregion Constructors And Initializers
#region static functions
public static TestObjectTemplate GetTemplate(ref ISO13499FileDb db, string name)
{
return null;
}
#endregion
public override string ToString()
{
return TemplateNameOrOriginalTemplateName;
}
}
}