init
This commit is contained in:
@@ -0,0 +1,340 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
|
||||
namespace DatabaseImport
|
||||
{
|
||||
public class TestObjectTemplate //: BasePropertyChanged, IComparable<TestObjectTemplate>
|
||||
{
|
||||
// #region enums and constants
|
||||
public enum Tags
|
||||
{
|
||||
ChannelCountText,
|
||||
PossibleChannelCountText,
|
||||
TemplateName,
|
||||
TemplateDescription,
|
||||
TestObject,
|
||||
TestObjectType,
|
||||
TemplateAllUIChannels,
|
||||
TemplateZones,
|
||||
AvailableTestObjectTypes,
|
||||
TestObjectIndex,
|
||||
TestObjectTypeIndex,
|
||||
CurrentZone,
|
||||
CurrentZoneIndex,
|
||||
AreZoneControlsEnabled,
|
||||
TemplateAllChannels,
|
||||
RequiredChannels
|
||||
}
|
||||
|
||||
public string LastModifiedBy { get; set; } = "N/A";
|
||||
|
||||
public DateTime LastModified { get; set; } = (DateTime)System.Data.SqlTypes.SqlDateTime.MinValue;
|
||||
|
||||
private int _currentZoneIndex = -1;
|
||||
public int CurrentZoneIndex
|
||||
{
|
||||
get => _currentZoneIndex;
|
||||
set
|
||||
{
|
||||
CurrentZone = value >= 0 ? TemplateZones[value] : null;
|
||||
_currentZoneIndex = value;
|
||||
}
|
||||
}
|
||||
|
||||
private Zone _currentZone;
|
||||
public Zone CurrentZone
|
||||
{
|
||||
get => _currentZone;
|
||||
set
|
||||
{
|
||||
//if (null != _currentZone && _currentZone != value) { _currentZone.PropertyChanged -= CurrentZone_PropertyChanged; }
|
||||
_currentZone = value;
|
||||
|
||||
//if (null != _currentZone) { _currentZone.PropertyChanged += CurrentZone_PropertyChanged; }
|
||||
AreZoneControlsEnabled = null != value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public bool AreZoneControlsEnabled { get; set; }
|
||||
|
||||
private ISO.TestObjectTemplate _template;
|
||||
public List<TestObjectTemplateChannel> RequiredChannels { get; set; } = new List<TestObjectTemplateChannel>();
|
||||
|
||||
public string TemplateParent { get; set; } = "";
|
||||
|
||||
public bool SysBuilt { get; set; }
|
||||
|
||||
private bool _embedded;
|
||||
public bool Embedded
|
||||
{
|
||||
get => _embedded;
|
||||
set
|
||||
{
|
||||
_embedded = value;
|
||||
if (null != _template) { _template.Embedded = value; }
|
||||
}
|
||||
}
|
||||
|
||||
private string _originalTemplateName = "";
|
||||
public string OriginalTemplateName
|
||||
{
|
||||
get => _originalTemplateName;
|
||||
set
|
||||
{
|
||||
_originalTemplateName = value;
|
||||
if (null != _template) { _template.OriginalTemplateName = value; }
|
||||
}
|
||||
}
|
||||
|
||||
public string TemplateName { get; set; }
|
||||
|
||||
public string TemplateDescription { get; set; }
|
||||
|
||||
public bool IsLocalOnly { get; set; }
|
||||
|
||||
private MMETestObjects _testObject;
|
||||
public MMETestObjects TestObject
|
||||
{
|
||||
get => _testObject;
|
||||
set
|
||||
{
|
||||
_testObject = value;
|
||||
if (null == _testObject) return;
|
||||
var app = Application.Current as App;
|
||||
if (app == null) return;
|
||||
AvailableTestObjectTypes = app.IsoDb.GetUniquePossibleChannelTypes(TestObjectTemplateChannel.NONISOCHANNELTYPE);
|
||||
if (AvailableTestObjectTypes.Length > 0) { TestObjectTypeIndex = 0; }
|
||||
else { TestObjectTypeIndex = -1; }
|
||||
}
|
||||
}
|
||||
|
||||
private string _testObjectType;
|
||||
public string TestObjectType
|
||||
{
|
||||
get => _testObjectType;
|
||||
set
|
||||
{
|
||||
_testObjectType = value;
|
||||
_channels = new List<MMEPossibleChannels>(((App)Application.Current).IsoDb.GetPossibleChannelsForType(_testObjectType));
|
||||
TemplateAllChannels = _channels.Select(pc => new TestObjectTemplateChannel(new TestObjectTemplateChannel(pc), /*ref db,*/ _template)).ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
private List<MMEPossibleChannels> _channels = new List<MMEPossibleChannels>();
|
||||
|
||||
private List<TestObjectTemplateChannel> _allChannels = new List<TestObjectTemplateChannel>();
|
||||
public TestObjectTemplateChannel[] TemplateAllChannels
|
||||
{
|
||||
get { _allChannels.Sort(CompareChannels); return _allChannels.ToArray(); }
|
||||
set
|
||||
{
|
||||
_allChannels = new List<TestObjectTemplateChannel>(value);
|
||||
|
||||
TemplateAllUIChannels = value.Select(c => new TemplateChannelUI(c)).ToArray();
|
||||
}
|
||||
}
|
||||
private List<TemplateChannelUI> _allUIChannels = new List<TemplateChannelUI>();
|
||||
public TemplateChannelUI[] TemplateAllUIChannels
|
||||
{
|
||||
get => _allUIChannels.ToArray();
|
||||
set => _allUIChannels = new List<TemplateChannelUI>(value);
|
||||
}
|
||||
|
||||
private List<Zone> _zones = new List<Zone>();
|
||||
public Zone[] TemplateZones
|
||||
{
|
||||
get => _zones.ToArray();
|
||||
set { _zones = new List<Zone>(value); var j = CurrentZoneIndex; }
|
||||
}
|
||||
private List<string> _availableTestObjectTypes = null;
|
||||
public string[] AvailableTestObjectTypes
|
||||
{
|
||||
get
|
||||
{
|
||||
if (null != _availableTestObjectTypes) return _availableTestObjectTypes.ToArray();
|
||||
if (null == TestObject) { return new string[0]; }
|
||||
_availableTestObjectTypes = new List<string>(((App)Application.Current).IsoDb.GetTestObjectTypeForTestObject(TestObject.Test_Object));
|
||||
return _availableTestObjectTypes.ToArray();
|
||||
}
|
||||
set => _availableTestObjectTypes = new List<string>(value);
|
||||
}
|
||||
private int _testObjectTypeIndex = -1;
|
||||
public int TestObjectTypeIndex
|
||||
{
|
||||
get
|
||||
{
|
||||
if (string.IsNullOrEmpty(TestObjectType)) { return -1; }
|
||||
return _availableTestObjectTypes.IndexOf(TestObjectType);
|
||||
}
|
||||
set
|
||||
{
|
||||
TestObjectType = value >= 0 ? _availableTestObjectTypes[value] : null;
|
||||
_testObjectTypeIndex = value;
|
||||
|
||||
TemplateAllChannels = _channels.Select(pc => new TestObjectTemplateChannel(new TestObjectTemplateChannel(pc), /*ref db,*/ _template)).ToArray();
|
||||
}
|
||||
}
|
||||
public static MMETestObjects GetNonISOTestObject()
|
||||
{
|
||||
var existingobjects = ((App)Application.Current).IsoDb.GetTestObjects(false);
|
||||
|
||||
var codes = new List<string>();
|
||||
|
||||
foreach (var existingobject in existingobjects)
|
||||
{
|
||||
if (existingobject.Text_L1 == Constants.NON_ISO_TESTOBJECT_NAME)
|
||||
{
|
||||
return existingobject;
|
||||
}
|
||||
codes.Add(existingobject.Test_Object);
|
||||
}
|
||||
|
||||
//first try to find a suitable alpha character that isn't in use
|
||||
for (var i = (int)'A'; i <= 'Z'; i++)
|
||||
{
|
||||
var s = new string(new[] { (char)i });
|
||||
if (!codes.Contains(s))
|
||||
{
|
||||
var to = new MMETestObjects(Guid.NewGuid().ToString(), s, Constants.NON_ISO_TESTOBJECT_NAME, Constants.NON_ISO_TESTOBJECT_NAME, 1,
|
||||
DateTime.Now, "SYSTEM", false, "", DateTime.Now, "created", "", MMEPossibleChannels.MMEChannelTypes.SQL);
|
||||
((App)Application.Current).IsoDb.Commit(to);
|
||||
return to;
|
||||
}
|
||||
}
|
||||
//didn't find one, now try for a suitable numeric character
|
||||
for (var i = (int)'0'; i <= '9'; i++)
|
||||
{
|
||||
var s = new string(new[] { (char)i });
|
||||
if (codes.Contains(s)) continue;
|
||||
var to = new MMETestObjects(Guid.NewGuid().ToString(), s, Constants.NON_ISO_TESTOBJECT_NAME, Constants.NON_ISO_TESTOBJECT_NAME, 1,
|
||||
DateTime.Now, "SYSTEM", false, "", DateTime.Now, "created", "", MMEPossibleChannels.MMEChannelTypes.SQL);
|
||||
((App)Application.Current).IsoDb.Commit(to);
|
||||
return to;
|
||||
}
|
||||
throw new NotSupportedException("TestObjectTemplate_CouldNotCreateNONISOTESTOBJECT");
|
||||
}
|
||||
public ISO.TestObjectTemplate ToISOTestObjectTemplate()
|
||||
{
|
||||
var template = new ISO.TestObjectTemplate(TemplateName, IsLocalOnly)
|
||||
{
|
||||
Description = TemplateDescription,
|
||||
OriginalTemplateName = OriginalTemplateName,
|
||||
Embedded = Embedded,
|
||||
Icon = "",
|
||||
LocalOnly = IsLocalOnly,
|
||||
TestObject = TestObject.Test_Object,
|
||||
TestObjectType = TestObjectType
|
||||
};
|
||||
var zones = new List<TemplateZone>();
|
||||
foreach (var zone in TemplateZones)
|
||||
{
|
||||
var isoZone = new TemplateZone(TemplateName, zone.Name, zone.GetPictureName(), zone.Description);
|
||||
|
||||
var i = 0;
|
||||
isoZone.TemplateRegions = zone.Regions.Select(region => region.ToISORegion(this, zone, i++)).ToArray();
|
||||
zones.Add(isoZone);
|
||||
}
|
||||
template.TemplateParent = TemplateParent;
|
||||
template.Channels = TemplateAllChannels;
|
||||
template.Zones = zones.ToArray();
|
||||
template.SysBuilt = SysBuilt;
|
||||
return template;
|
||||
}
|
||||
private int CompareChannels(TestObjectTemplateChannel left, TestObjectTemplateChannel right)
|
||||
{
|
||||
if (left == right) { return 0; }
|
||||
if (null == left) { return -1; }
|
||||
return null == right ? 1 : left.DisplayOrder.CompareTo(right.DisplayOrder);
|
||||
}
|
||||
public TestObjectTemplate(TestObjectTemplate copy, ref ISO13499FileDb db)
|
||||
{
|
||||
if (null != copy._template) { _template = new ISO.TestObjectTemplate(copy._template, ref db); }
|
||||
TemplateDescription = copy.TemplateDescription;
|
||||
TemplateParent = copy.TemplateParent;
|
||||
TemplateName = copy.TemplateName;
|
||||
LastModified = copy.LastModified;
|
||||
LastModifiedBy = copy.LastModifiedBy;
|
||||
TestObject = copy.TestObject;
|
||||
TestObjectType = copy.TestObjectType;
|
||||
foreach (var c in copy.RequiredChannels)
|
||||
{
|
||||
RequiredChannels.Add(new TestObjectTemplateChannel(c, /*ref db,*/ _template));
|
||||
}
|
||||
TemplateAllChannels = copy.TemplateAllChannels.Select(c => new TestObjectTemplateChannel(c, /*ref db,*/ _template)).ToArray();
|
||||
|
||||
TemplateZones = copy.TemplateZones.Select(z => new Zone(z, this)).ToArray();
|
||||
SysBuilt = copy.SysBuilt;
|
||||
Embedded = copy.Embedded;
|
||||
OriginalTemplateName = copy.OriginalTemplateName;
|
||||
}
|
||||
public TestObjectTemplate(ISO.TestObjectTemplate template, ref ISO13499FileDb db)
|
||||
{
|
||||
Initialize(template, ref db, null);
|
||||
}
|
||||
private void Initialize(ISO.TestObjectTemplate template, ref ISO13499FileDb db, List<MMETestObjects> testObjects)
|
||||
{
|
||||
_template = template;
|
||||
TemplateDescription = template.Description;
|
||||
TemplateName = template.TemplateName;
|
||||
LastModified = template.LastModified;
|
||||
LastModifiedBy = template.LastModifiedBy;
|
||||
|
||||
//if a list of test objects is provided, check if the test object we wish is in the list, if so use it
|
||||
var bFoundTestObject = false;
|
||||
if (null != testObjects)
|
||||
{
|
||||
var matches = from to in testObjects where to.Test_Object == template.TestObject select to;
|
||||
var mmeTestObjectses = matches as MMETestObjects[] ?? matches.ToArray();
|
||||
if (mmeTestObjectses.Any())
|
||||
{
|
||||
TestObject = mmeTestObjectses[0];
|
||||
bFoundTestObject = true;
|
||||
}
|
||||
}
|
||||
if (!bFoundTestObject)
|
||||
{
|
||||
TestObject = (Application.Current as App).IsoDb.GetTestObjectByIso(template.TestObject);
|
||||
}
|
||||
TestObjectType = template.TestObjectType;
|
||||
OriginalTemplateName = template.OriginalTemplateName;
|
||||
Embedded = template.Embedded;
|
||||
|
||||
TemplateAllChannels = template.Channels.Select(c => new TestObjectTemplateChannel(c, /*ref db,*/ _template)).ToArray();
|
||||
TemplateZones = template.Zones.Select(z => new Zone(z, this)).ToArray();
|
||||
TemplateParent = template.TemplateParent;
|
||||
SysBuilt = template.SysBuilt;
|
||||
}
|
||||
public TestObjectTemplate()
|
||||
{
|
||||
TemplateName = string.Empty;
|
||||
TemplateDescription = string.Empty;
|
||||
TemplateParent = string.Empty;
|
||||
|
||||
|
||||
switch (SerializedSettings.ISOSupportLevel)
|
||||
{
|
||||
case SerializedSettings.ISOSupportLevels.ISO_ONLY:
|
||||
TestObject = ((App)Application.Current).IsoDb.GetTestObjects(false).FirstOrDefault();
|
||||
break;
|
||||
case SerializedSettings.ISOSupportLevels.NO_ISO:
|
||||
//need to create a template just for this object!
|
||||
InitializeNonISO();
|
||||
break;
|
||||
case SerializedSettings.ISOSupportLevels.TRANSITORY:
|
||||
TestObject = ((App)Application.Current).IsoDb.GetTestObjects(false).FirstOrDefault();
|
||||
break;
|
||||
}
|
||||
}
|
||||
private void InitializeNonISO()
|
||||
{
|
||||
//create template, set template channel type to NONISOTYPE
|
||||
TemplateName = Guid.NewGuid().ToString();
|
||||
|
||||
TestObject = GetNonISOTestObject();
|
||||
TestObjectType = Constants.NON_ISO_TESTOBJECT_CHANNEL_TYPE;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user