202 lines
8.6 KiB
C#
202 lines
8.6 KiB
C#
|
|
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Xml;
|
|||
|
|
using DataPROWin7.DataModel;
|
|||
|
|
using DTS.Common.Classes.TestSetups;
|
|||
|
|
using DTS.Common.Enums.DBExport;
|
|||
|
|
using DTS.Common.Interface.Groups.GroupList;
|
|||
|
|
using DTS.Common.Utils;
|
|||
|
|
using DTS.SensorDB;
|
|||
|
|
|
|||
|
|
namespace DTS.Common.Import.XML
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
public class XMLParseTestSetups : XMLParseBase
|
|||
|
|
{
|
|||
|
|
public XMLParseTestSetups(XmlElement root, double importedVersion, Func<bool> isCancelled = null, bool skipNormalizing= false) : base(root, importedVersion, isCancelled, skipNormalizing)
|
|||
|
|
{
|
|||
|
|
_channelIdMapping.Clear();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public IImportNotification ImportNotification { get; set; }
|
|||
|
|
|
|||
|
|
public override void Parse(ref ImportObject importObject)
|
|||
|
|
{
|
|||
|
|
ImportNotification?.SetStatus(new ImportStatus { PossibleStatus = Enums.PossibleStatus.Reading, ExtraStatus = Enums.ImportExtraStatus.ReadingTestSetups });
|
|||
|
|
var testTemplates = ParseTestTemplate(importObject, _root);
|
|||
|
|
if (!_skipNormalizing)
|
|||
|
|
{
|
|||
|
|
var newRoot = ConvertTestTemplates(testTemplates, importObject);
|
|||
|
|
testTemplates = ParseTestTemplate(importObject, newRoot);
|
|||
|
|
}
|
|||
|
|
importObject.TestSetupImportFileFormat = ImportObject.GetImportFileFormat(testTemplates.Count());
|
|||
|
|
importObject.AddTestSetups(testTemplates);
|
|||
|
|
}
|
|||
|
|
private XmlElement ConvertTestTemplates(IEnumerable<TestTemplate> testTemplates, ImportObject importObject)
|
|||
|
|
{
|
|||
|
|
_writer.WriteStartElement(TopLevelFields.TestSetups.ToString());
|
|||
|
|
foreach (var t in testTemplates)
|
|||
|
|
{
|
|||
|
|
var groupCount = -1;
|
|||
|
|
foreach (var group in t.Groups)
|
|||
|
|
{
|
|||
|
|
group.Id = groupCount;
|
|||
|
|
|
|||
|
|
if (group.StaticGroupId != null)
|
|||
|
|
{
|
|||
|
|
if (_groupIdMapping.ContainsKey(group.StaticGroupId.ToString()))
|
|||
|
|
{
|
|||
|
|
group.StaticGroupId = _groupIdMapping[group.StaticGroupId.ToString()];
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
//This can occur if an old (buggy) Test Setup export includes an embedded
|
|||
|
|
//Group which was created from a static Group which was later deleted.
|
|||
|
|
group.StaticGroupId = null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
groupCount--;
|
|||
|
|
}
|
|||
|
|
var channelCount = -1;
|
|||
|
|
foreach (var group in t.ChannelsForGroup)
|
|||
|
|
{
|
|||
|
|
foreach (var channel in group.Value)
|
|||
|
|
{
|
|||
|
|
if (_dasIdMapping.ContainsKey(channel.DASId))
|
|||
|
|
{
|
|||
|
|
channel.DASId = _dasIdMapping[channel.DASId];
|
|||
|
|
}
|
|||
|
|
if (_sensorIdMapping.ContainsKey(channel.SensorId))
|
|||
|
|
{
|
|||
|
|
var match = importObject.Sensors().FirstOrDefault(s => s.DatabaseId == _sensorIdMapping[channel.SensorId]);
|
|||
|
|
//setting the sensor just so IsUart, etc is populated
|
|||
|
|
if (null != match) { channel.SetSensorData(match, null, false); }
|
|||
|
|
channel.SensorId = _sensorIdMapping[channel.SensorId];
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
//FB 14308 - Don't fail Test Setup import if it was
|
|||
|
|
//exported when out-of-date due to a deleted sensor.
|
|||
|
|
channel.SensorId = 0;
|
|||
|
|
}
|
|||
|
|
_channelIdMapping[channel.Id] = channelCount;
|
|||
|
|
channel.Id = channelCount;
|
|||
|
|
channelCount--;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var addedHardwareList = new List<int>();
|
|||
|
|
foreach (var dasId in t.AddedHardware)
|
|||
|
|
{
|
|||
|
|
addedHardwareList.Add(_dasIdMapping[dasId]);
|
|||
|
|
}
|
|||
|
|
t.AddedHardware = addedHardwareList.ToArray();
|
|||
|
|
|
|||
|
|
var removedHardwareList = new List<int>();
|
|||
|
|
foreach (var dasId in t.RemovedHardware)
|
|||
|
|
{
|
|||
|
|
if (_dasIdMapping.ContainsKey(dasId))
|
|||
|
|
{
|
|||
|
|
removedHardwareList.Add(_dasIdMapping[dasId]);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
t.RemovedHardware = removedHardwareList.ToArray();
|
|||
|
|
|
|||
|
|
_writer.Flush();
|
|||
|
|
t.WriteXML(ref _writer);
|
|||
|
|
_writer.Flush();
|
|||
|
|
}
|
|||
|
|
_writer.WriteEndElement();
|
|||
|
|
return GetXmlElement();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public IEnumerable<TestTemplate> ParseTestTemplate(ImportObject importObject, XmlElement root)
|
|||
|
|
{
|
|||
|
|
List<TestTemplate> testTemplates = new List<TestTemplate>();
|
|||
|
|
var testobjectLookup = new Dictionary<string, TestObject>();
|
|||
|
|
var groupLookup = new Dictionary<string, IGroup>();
|
|||
|
|
|
|||
|
|
foreach (var to in importObject.Groups())
|
|||
|
|
{
|
|||
|
|
if (IsCancelled()) { return testTemplates; }
|
|||
|
|
testobjectLookup[to.SerialNumber] = to;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//Static Groups
|
|||
|
|
foreach (var g in importObject.StaticGroups())
|
|||
|
|
{
|
|||
|
|
if (IsCancelled()) { return testTemplates; }
|
|||
|
|
groupLookup[g.Name] = g;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var customerDetailsLookup = new Dictionary<string, ISO.CustomerDetails>();
|
|||
|
|
foreach (var c in importObject.CustomerDetails())
|
|||
|
|
{
|
|||
|
|
if (IsCancelled()) { return testTemplates; }
|
|||
|
|
customerDetailsLookup[c.Name] = c;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var testEngineerDetailsLookup = new Dictionary<string, ISO.TestEngineerDetails>();
|
|||
|
|
foreach (var t in importObject.TestEngineerDetails())
|
|||
|
|
{
|
|||
|
|
if (IsCancelled()) { return testTemplates; }
|
|||
|
|
testEngineerDetailsLookup[t.Name] = t;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var labDetailsLookup = new Dictionary<string, ISO.LabratoryDetails>();
|
|||
|
|
foreach (var l in importObject.LabDetails())
|
|||
|
|
{
|
|||
|
|
if (IsCancelled()) { return testTemplates; }
|
|||
|
|
labDetailsLookup[l.Name] = l;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var sensorLookup = new Dictionary<string, SensorData>();
|
|||
|
|
foreach (var s in importObject.Sensors())
|
|||
|
|
{
|
|||
|
|
if (IsCancelled()) { return testTemplates; }
|
|||
|
|
sensorLookup[s.SerialNumber] = s;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
foreach (var node in root.ChildNodes)
|
|||
|
|
{
|
|||
|
|
if (IsCancelled()) { return testTemplates; }
|
|||
|
|
if (node is XmlElement)
|
|||
|
|
{
|
|||
|
|
//18868 Pass the list of DAS from this import, instead of getting all of the DAS in the database.
|
|||
|
|
var simpleImportedHardwareList = new List<SimpleHardware>();
|
|||
|
|
foreach (var h in importObject.Hardware())
|
|||
|
|
{
|
|||
|
|
simpleImportedHardwareList.Add(new SimpleHardware(h.SerialNumber, h.ParentDAS, -1, Convert.ToInt32(h.DASTypeEnum)));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var d = new TestTemplate(null, true, simpleImportedHardwareList.ToArray());
|
|||
|
|
var hardware = importObject.Hardware()?.ToList();
|
|||
|
|
var calibration = importObject.Calibrations()?.ToList();
|
|||
|
|
d.ReadXML(node as XmlElement, testobjectLookup, groupLookup, labDetailsLookup, customerDetailsLookup, testEngineerDetailsLookup, sensorLookup,hardware,
|
|||
|
|
calibration);
|
|||
|
|
d.SetGroupsListOrder(); //If any were just imported and have a DisplayOrder of -1, fix them up
|
|||
|
|
testTemplates.Add(d);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
//FB 36879 If can't read the xml report the error
|
|||
|
|
importObject.AddError(new ImportError
|
|||
|
|
{
|
|||
|
|
Message = ex.Message,
|
|||
|
|
Severity = ImportSeverityError.Warning,
|
|||
|
|
ContinueImportOnError = true
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return testTemplates;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|