/* Test.cs Copyright © 2008 Diversified Technical Systems, Inc. All Rights Reserved */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Xml.Schema; using System.Xml.Serialization; using DTS.Utilities; using DTS.Utilities.DotNetProgrammingConstructs; using DTS.Utilities.Xml; namespace DTS.Serialization { /// /// Representation of a serializable test information. /// [XmlSerializationTag( "Test" )] public partial class Test : Exceptional, IXmlSerializable { private Property _software = new Property(typeof(Test).Namespace + ".Test.Software", "DataPRO", true); [XmlSerializationTag("Software")] public string Software { get { return _software.Value; } set { _software.Value = value; } } private Property _softwareVersion = new Property(typeof(Test).Namespace + ".Test.SoftwareVersion", "", false); [XmlSerializationTag("SoftwareVersion")] public string SoftwareVersion { get { return _softwareVersion.Value; } set { _softwareVersion.Value = value; } } /* /// /// The string ID of this test. /// [XmlSerializationTag("Id")] public string Id { get { return _Id.Value; } set { _Id.Value = value; } } private Property _Id = new Property(typeof(Test).Namespace + ".Test.Id", "", false);*/ // Initialize an instance of the Test class. public Test() { TryGetChannelOrder(); // // Note that the parameterless constructor for this object will leave // the Id and Description paramters } // public Test(string dtsfile) { } /// /// Initialize an instance of the Test class. /// /// /// /// The ID of this test. /// /// /// /// The description of this test. /// /// public Test(string id, string description) { TryGetChannelOrder(); try { Id = id; Description = description; } catch (System.Exception ex) { throw new Exception( "encountered problem constructing Test object (Id: " + ( !string.IsNullOrEmpty( id ) ? id : "<>" ) + "Description: " + ( !string.IsNullOrEmpty( description ) ? description : "<>" ) + ")", ex ); } } /// /// Get the date of this object's serialization's creation (if applicable). /// public DateTime InceptionDate { get { return _InceptionDate.Value; } set { _InceptionDate.Value = value; } } private Property _InceptionDate = new Property( typeof( Test ).Namespace + ".Test.InceptionDate", DateTime.Now, false ); private Dictionary _channelOrder = new Dictionary(); private static string GetID(Test.Module.Channel channel) { if (null != channel.SensorID && channel.SensorID.Length > 0) { return channel.SensorID; } else { return channel.ChannelDescriptionString; } } public class ChannelOrderComparor : IComparer { private IDictionary _dictionary; public ChannelOrderComparor(IDictionary dictionary) { _dictionary = dictionary; } public int Compare(Test.Module.Channel a, Test.Module.Channel b) { string keyA = GetID(a); string keyB = GetID(b); int iA = _dictionary.ContainsKey(keyA) ? _dictionary[keyA] : int.MaxValue; int iB = _dictionary.ContainsKey(keyB) ? _dictionary[keyB] : int.MaxValue; return iA.CompareTo(iB); } } public void TryGetChannelOrder() { try { _channelOrder.Clear(); if (System.IO.File.Exists("ChannelOrder.txt")) { using (System.IO.StreamReader sr = new System.IO.StreamReader("ChannelOrder.txt")) { string line; int i = 0; while ((line = sr.ReadLine()) != null) { _channelOrder.Add(line, i++); } } } } catch (System.Exception ex) { Utilities.Logging.APILogger.Log("Exception getting channel order", ex); } } /// /// Get a named-DAS/numbered-channel accessor to this Event's channels. /// public List Channels { get { try { if (_channelOrder.Count < 1) { TryGetChannelOrder(); } List allChannels = new List(); foreach (Test.Module testModule in this.Modules) { allChannels.AddRange(testModule.Channels); allChannels.AddRange(testModule.CalculatedChannels); } allChannels.Sort(new Comparison(CompareChannels)); if (_channelOrder.Count > 0) { allChannels.Sort(new ChannelOrderComparor(_channelOrder)); } return allChannels; } catch (System.Exception ex) { throw new Test.Exception("encountered problem getting all test channels", ex); } } } private int CompareChannels(Test.Module.Channel left, Test.Module.Channel right) { if (left == right) { return 0; } if (null == left) { return -1; } if (null == right) { return 1; } int ret = left.AbsoluteDisplayOrder.CompareTo(right.AbsoluteDisplayOrder); if (0 == ret) { ret = left.ParentModule.Number.CompareTo(right.ParentModule.Number); } if (0 == ret) { return left.Number.CompareTo(right.Number); } return ret; } /// /// The string ID of this test. /// [XmlSerializationTag( "Id" )] public string Id { get { return _Id.Value; } set { _Id.Value = value; } } private Property _Id = new Property( typeof( Test ).Namespace + ".Test.Id", "", false); /// /// The string description of this test. /// [XmlSerializationTag( "Description" )] public string Description { get { return _Description.Value; } set { _Description.Value = value; } } private Property _Description = new Property( typeof( Test ).Namespace + ".Test.Description", "", false ); /// /// The globally unique identification string for this test. /// [XmlSerializationTag( "Guid" )] public Guid Guid { get { return _Guid.Value; } set { _Guid.Value = value; } } private Property _Guid = new Property( typeof( Test ).Namespace + ".Test.Guid", new Guid( "00000000-0000-0000-0000-000000000000" ), false ); /// /// The globally unique identification string for this test. /// [XmlSerializationTag("FaultFlags")] public UInt16 FaultFlags { get { return _FaultFlags.Value; } set { _FaultFlags.Value = value; } } private Property _FaultFlags = new Property(typeof(Test).Namespace + ".Test.FaultFlags", 0, false); /// /// Get/set inline serialized data switch. /// [XmlSerializationTag( "InlineSerializedData" )] public bool InlineSerializedData { get { return _InlineSerializedData.Value; } set { try { _InlineSerializedData.Value = value; foreach ( Test.Module module in Modules ) module.InlineSerializedData = value; } catch ( System.Exception ex ) { throw new Exception( "encountered problem setting test InlineSerializedData state", ex ); } } } private Property _InlineSerializedData = new Property( typeof( Test ).Namespace + ".Test.InlineSerializedData", false, true ); /// /// The list of modules in this test. /// [XmlSerializationTag( "Modules" )] public List Modules { get { return _Modules.Value; } set { _Modules.Value = value; } } private Property> _Modules = new Property>( typeof( Test ).Namespace + ".Test.Modules", new List( ), true ); /// /// Write XML serialization for this object to the specified writer. /// /// /// /// The to which this object's XML serialization /// will be written. /// /// public void WriteXml( XmlWriter writer ) { try { AttributeExtractor attributeExtractor = new AttributeExtractor( ); writer.WriteAttributeString( attributeExtractor.ExtractAttachedAttributeFromProperty( this, "Id" ).Value, this.Id ); writer.WriteAttributeString( attributeExtractor.ExtractAttachedAttributeFromProperty( this, "Description" ).Value, this.Description ); writer.WriteAttributeString( attributeExtractor.ExtractAttachedAttributeFromProperty( this, "InlineSerializedData" ).Value, this.InlineSerializedData.ToString( ) ); writer.WriteAttributeString( attributeExtractor.ExtractAttachedAttributeFromProperty( this, "Guid" ).Value, this.Guid.ToString( ) ); writer.WriteAttributeString(attributeExtractor.ExtractAttachedAttributeFromProperty(this, "FaultFlags").Value, this.FaultFlags.ToString()); writer.WriteAttributeString(attributeExtractor.ExtractAttachedAttributeFromProperty(this, "Software").Value, this.Software.ToString()); writer.WriteAttributeString(attributeExtractor.ExtractAttachedAttributeFromProperty(this, "SoftwareVersion").Value, this.SoftwareVersion.ToString()); writer.WriteStartElement ( attributeExtractor.ExtractAttachedAttributeFromProperty( this, "Modules" ).Value ); foreach (var module in Modules) { module.WriteXml(writer); } writer.WriteEndElement(); } catch ( System.Exception ex ) { throw new Exception( "encountered problem converting DTS.Serialization.Test object to XML", ex ); } } /// /// Read XML serialization for this object from the specified reader. /// /// /// /// The from which this object's XML serialization /// will be read. /// /// public void ReadXml( XmlReader reader ) { try { AttributeExtractor attributeExtractor = new AttributeExtractor(); if (reader.IsStartElement("Test")) { this.Id = reader.GetAttribute(attributeExtractor.ExtractAttachedAttributeFromProperty(this, "Id").Value); this.Description = reader.GetAttribute(attributeExtractor.ExtractAttachedAttributeFromProperty(this, "Description").Value); this.InlineSerializedData = bool.Parse(reader.GetAttribute(attributeExtractor.ExtractAttachedAttributeFromProperty(this, "InlineSerializedData").Value)); try { this.Guid = new Guid(reader.GetAttribute(attributeExtractor.ExtractAttachedAttributeFromProperty(this, "Guid").Value)); } catch (System.Exception) { this.Guid = new Guid("00000000-0000-0000-0000-000000000000"); } try { this.FaultFlags = Convert.ToUInt16(reader.GetAttribute(attributeExtractor.ExtractAttachedAttributeFromProperty(this, "FaultFlags").Value)); } catch (System.Exception) { this.FaultFlags = 0; } try { this.Software = reader.GetAttribute(attributeExtractor.ExtractAttachedAttributeFromProperty(this, "Software").Value); } catch (System.Exception) { Software = "unknown"; } try { this.SoftwareVersion = reader.GetAttribute(attributeExtractor.ExtractAttachedAttributeFromProperty(this, "SoftwareVersion").Value); } catch (System.Exception) { SoftwareVersion = "unknown"; } this.Modules.Clear(); if (reader.ReadToDescendant("Modules")) { if (reader.ReadToDescendant("Module")) { do { Test.Module deserializedModule = new Test.Module(this); deserializedModule.InlineSerializedData = InlineSerializedData; //Give the deserializer a separate reader so that it can't go amuck and read past the current module. deserializedModule.ReadXml(reader.ReadSubtree()); this.Modules.Add(deserializedModule); } while (reader.ReadToNextSibling("Module")); } } } } catch (System.Exception ex) { throw new Exception("encountered problem converting XML to DTS.Serialization.Test object", ex); } } /// /// Should normally return a schema representing the form of the XML /// generated/consumed by WriteXml/ReadXml, but it never called during /// the serialization process so ours just returns null. /// /// /// /// Null reference, always. /// /// public XmlSchema GetSchema( ) { // This method is never invoked during XML object serialization. return null; } /// /// Test the specified object for equality with this object. /// /// /// /// The to be tested for equality. /// /// /// /// true if the specified object has memeberwise equality with /// this object; false otherwise. /// /// public override bool Equals( object obj ) { try { Test that = obj as Test; return null != that && this.Id.Equals( that.Id ) && this.Description.Equals( that.Description ) && this.ModulesEquals( that.Modules ); } catch ( System.Exception ex ) { throw new Exception( "encountered problem equality testing object " + ( null != obj ? "\"" + obj.ToString( ) + "\"" : "<>" ), ex ); } } /// /// Test the specified object's module list for equality with this object's /// module list. /// /// /// /// The of object to be /// compared for equality with this test's equivalent. /// /// /// /// true if the two lists contain equivalent-valued members; /// false otherwise. /// /// private bool ModulesEquals( List thoseModules ) { try { if ( this.Modules.Count != thoseModules.Count ) return false; else for ( int i=0; i < thoseModules.Count; i++ ) if ( !this.Modules[ i ].Equals( thoseModules[ i ] ) ) return false; return true; } catch ( System.Exception ex ) { throw new Exception( "encountered problem equality-testing module list", ex ); } } /// /// Return the hash code for this object. /// /// /// /// The hash code for this object. /// /// public override int GetHashCode( ) { return base.GetHashCode( ); } } }